|
| 1 | +""" FancyLED Palette and Color Picker Control with BlueFruit App |
| 2 | + Code by Phil Burgess, Dan Halbert & Erin St Blaine for Adafruit Industries |
| 3 | +""" |
| 4 | +import board |
| 5 | +import neopixel |
| 6 | +import touchio |
| 7 | +import adafruit_fancyled.adafruit_fancyled as fancy |
| 8 | +# from adafruit_ble.uart import UARTServer |
| 9 | +# for >= CPy 5.0.0 |
| 10 | +from adafruit_ble.uart_server import UARTServer |
| 11 | +from adafruit_bluefruit_connect.packet import Packet |
| 12 | +from adafruit_bluefruit_connect.button_packet import ButtonPacket |
| 13 | +from adafruit_bluefruit_connect.color_packet import ColorPacket |
| 14 | + |
| 15 | +NUM_LEDS = 24 # change to reflect your total number of ring LEDs |
| 16 | +RING_PIN = board.A1 # change to reflect your wiring |
| 17 | +CPX_PIN = board.D8 # CPX Neopixels live on pin D8 |
| 18 | + |
| 19 | +touch_A2 = touchio.TouchIn(board.A2) |
| 20 | +touch_A3 = touchio.TouchIn(board.A3) |
| 21 | +touch_A4 = touchio.TouchIn(board.A4) |
| 22 | +touch_A5 = touchio.TouchIn(board.A5) |
| 23 | +touch_A6 = touchio.TouchIn(board.A6) |
| 24 | +touch_TX = touchio.TouchIn(board.TX) |
| 25 | + |
| 26 | +# Palettes can have any number of elements in various formats |
| 27 | +# check https://learn.adafruit.com/fancyled-library-for-circuitpython/colors |
| 28 | +# for more info |
| 29 | +# Declare a 6-element RGB rainbow palette |
| 30 | +PALETTE_RAINBOW = [fancy.CRGB(1.0, 0.0, 0.0), # Red |
| 31 | + fancy.CRGB(0.5, 0.3, 0.0), # Orange |
| 32 | + fancy.CRGB(0.5, 0.5, 0.0), # Yellow |
| 33 | + fancy.CRGB(0.3, 0.7, 0.0), # Yellow Green |
| 34 | + fancy.CRGB(0.0, 1.0, 0.0), # Green |
| 35 | + fancy.CRGB(0.0, 0.7, 0.3), # Teal |
| 36 | + fancy.CRGB(0.0, 0.5, 0.5), # Cyan |
| 37 | + fancy.CRGB(0.0, 0.3, 0.7), # Blue |
| 38 | + fancy.CRGB(0.0, 0.0, 1.0), # Blue |
| 39 | + fancy.CRGB(0.5, 0.0, 0.5), # Magenta |
| 40 | + fancy.CRGB(0.7, 0.0, 0.3)] # Purple |
| 41 | + |
| 42 | +# Reading Lamp mode - Warm Yellow |
| 43 | +PALETTE_BRIGHT = [fancy.CRGB(255, 183, 55)] |
| 44 | + |
| 45 | +# Black Only palette for "off" mode |
| 46 | +PALETTE_DARK = [fancy.CRGB(0, 0, 0)] |
| 47 | + |
| 48 | +# Declare a FIRE palette |
| 49 | +PALETTE_FIRE = [fancy.CRGB(160, 30, 0), # Reds and Yellows |
| 50 | + fancy.CRGB(27, 65, 0), |
| 51 | + fancy.CRGB(0, 0, 0), |
| 52 | + fancy.CRGB(224, 122, 0), |
| 53 | + fancy.CRGB(0, 0, 0), |
| 54 | + fancy.CRGB(250, 80, 0), |
| 55 | + fancy.CRGB(0, 0, 0), |
| 56 | + fancy.CRGB(0, 0, 0), |
| 57 | + fancy.CRGB(200, 40, 0)] |
| 58 | + |
| 59 | +# Declare a NeoPixel object on NEOPIXEL_PIN with NUM_LEDS pixels, |
| 60 | +# no auto-write. |
| 61 | +# Set brightness to max because we'll be using FancyLED's brightness control. |
| 62 | +ring = neopixel.NeoPixel(RING_PIN, NUM_LEDS, brightness=1.0, auto_write=False) |
| 63 | +cpx = neopixel.NeoPixel(CPX_PIN, NUM_LEDS, brightness=1.0, auto_write=False) |
| 64 | + |
| 65 | +offset = 0 # Positional offset into color palette to get it to 'spin' |
| 66 | +offset_increment = 6 |
| 67 | +OFFSET_MAX = 1000000 |
| 68 | + |
| 69 | +uart_server = UARTServer() |
| 70 | + |
| 71 | +def set_palette(palette): |
| 72 | + for i in range(NUM_LEDS): |
| 73 | + # Load each pixel's color from the palette using an offset, run it |
| 74 | + # through the gamma function, pack RGB value and assign to pixel. |
| 75 | + color = fancy.palette_lookup(palette, (offset + i) / NUM_LEDS) |
| 76 | + color = fancy.gamma_adjust(color, brightness=1.0) |
| 77 | + ring[i] = color.pack() |
| 78 | + ring.show() |
| 79 | + |
| 80 | + for i in range(NUM_LEDS): |
| 81 | + # Load each pixel's color from the palette using an offset, run it |
| 82 | + # through the gamma function, pack RGB value and assign to pixel. |
| 83 | + color = fancy.palette_lookup(palette, (offset + i) / NUM_LEDS) |
| 84 | + color = fancy.gamma_adjust(color, brightness=1.0) |
| 85 | + cpx[i] = color.pack() |
| 86 | + cpx.show() |
| 87 | + |
| 88 | +# set initial palette to run on startup |
| 89 | +palette_choice = PALETTE_FIRE |
| 90 | + |
| 91 | +# True if cycling a palette |
| 92 | +cycling = True |
| 93 | + |
| 94 | +# Are we already advertising? |
| 95 | +advertising = False |
| 96 | + |
| 97 | + |
| 98 | +while True: |
| 99 | + |
| 100 | + if cycling: |
| 101 | + set_palette(palette_choice) |
| 102 | + offset = (offset + offset_increment) % OFFSET_MAX |
| 103 | + |
| 104 | + if not uart_server.connected and not advertising: |
| 105 | + uart_server.start_advertising() |
| 106 | + advertising = True |
| 107 | + |
| 108 | + # Are we connected via Bluetooth now? |
| 109 | + if uart_server.connected: |
| 110 | + # Once we're connected, we're not advertising any more. |
| 111 | + advertising = False |
| 112 | + # Have we started to receive a packet? |
| 113 | + if uart_server.in_waiting: |
| 114 | + packet = Packet.from_stream(uart_server) |
| 115 | + if isinstance(packet, ColorPacket): |
| 116 | + cycling = False |
| 117 | + # Set all the pixels to one color and stay there. |
| 118 | + ring.fill(packet.color) |
| 119 | + cpx.fill(packet.color) |
| 120 | + ring.show() |
| 121 | + cpx.show() |
| 122 | + elif isinstance(packet, ButtonPacket): |
| 123 | + cycling = True |
| 124 | + if packet.pressed: |
| 125 | + if packet.button == ButtonPacket.BUTTON_1: |
| 126 | + palette_choice = PALETTE_DARK |
| 127 | + elif packet.button == ButtonPacket.BUTTON_2: |
| 128 | + palette_choice = PALETTE_BRIGHT |
| 129 | + elif packet.button == ButtonPacket.BUTTON_3: |
| 130 | + palette_choice = PALETTE_FIRE |
| 131 | + offset_increment = 6 |
| 132 | + elif packet.button == ButtonPacket.BUTTON_4: |
| 133 | + palette_choice = PALETTE_RAINBOW |
| 134 | + offset_increment = 1 |
| 135 | + |
| 136 | + # change the speed of the animation by incrementing offset |
| 137 | + elif packet.button == ButtonPacket.UP: |
| 138 | + offset_increment += 1 |
| 139 | + elif packet.button == ButtonPacket.DOWN: |
| 140 | + offset_increment -= 1 |
| 141 | + |
| 142 | + # Whether or not we're connected via Bluetooth, |
| 143 | + # we also want to handle touch inputs. |
| 144 | + if touch_A2.value: |
| 145 | + cycling = True |
| 146 | + palette_choice = PALETTE_DARK |
| 147 | + elif touch_A3.value: |
| 148 | + cycling = True |
| 149 | + palette_choice = PALETTE_BRIGHT |
| 150 | + elif touch_A4.value: |
| 151 | + cycling = True |
| 152 | + palette_choice = PALETTE_FIRE |
| 153 | + offset_increment = 6 |
| 154 | + elif touch_A5.value: |
| 155 | + cycling = True |
| 156 | + palette_choice = PALETTE_RAINBOW |
| 157 | + offset_increment = 1 |
| 158 | + # Also check for touch speed control |
| 159 | +# if touch_A6.value: |
| 160 | +# offset_increment += 1 |
| 161 | +# if touch_TX.value: |
| 162 | +# offset_increment -= 1 |
0 commit comments