|
| 1 | +import random |
| 2 | +import board |
| 3 | +import neopixel |
| 4 | +import adafruit_fancyled.adafruit_fancyled as fancy |
| 5 | +from adafruit_bluefruit_connect.packet import Packet |
| 6 | +from adafruit_bluefruit_connect.button_packet import ButtonPacket |
| 7 | +from adafruit_ble import BLERadio |
| 8 | +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement |
| 9 | +from adafruit_ble.services.nordic import UARTService |
| 10 | + |
| 11 | +# setting up # of neopixels |
| 12 | +TREE_LEDS = 12 |
| 13 | +CPX_LEDS = 10 |
| 14 | +# setting up pins for neopixels |
| 15 | +TREE_PIN = board.A1 |
| 16 | +CPX_PIN = board.D8 |
| 17 | + |
| 18 | +# neopixel setup |
| 19 | +tree = neopixel.NeoPixel(TREE_PIN, TREE_LEDS, brightness=0.5, auto_write=False) |
| 20 | +cpx = neopixel.NeoPixel(CPX_PIN, CPX_LEDS, brightness=0.1, auto_write=False) |
| 21 | + |
| 22 | +# BLE setup |
| 23 | +ble = BLERadio() |
| 24 | +uart = UARTService() |
| 25 | +advertisement = ProvideServicesAdvertisement(uart) |
| 26 | +advertising = False |
| 27 | + |
| 28 | +# to turn neopixels off |
| 29 | +OFF = (0, 0, 0) |
| 30 | + |
| 31 | +# fancyLED color palettes |
| 32 | + |
| 33 | +fairy_palette = [fancy.CRGB(1.0, 0.0, 0.0), |
| 34 | + fancy.CRGB(1.0, 0.5, 0.0), |
| 35 | + fancy.CRGB(0.0, 0.5, 0.0), |
| 36 | + fancy.CRGB(0.0, 1.0, 1.0), |
| 37 | + fancy.CRGB(0.0, 0.0, 1.0), |
| 38 | + fancy.CRGB(0.75, 0.0, 1.0)] |
| 39 | + |
| 40 | +merry_palette = [fancy.CRGB(1.0, 0.0, 0.0), |
| 41 | + fancy.CRGB(0.0, 1.0, 0.0)] |
| 42 | + |
| 43 | +winter_palette = [fancy.CRGB(0.0, 0.75, 0.0), |
| 44 | + fancy.CRGB(0.0, 1.0, 1.0), |
| 45 | + fancy.CRGB(0.75, 0.0, 1.0), |
| 46 | + fancy.CRGB(1.0, 1.0, 1.0), |
| 47 | + fancy.CRGB(0.0, 0.75, 0.0), |
| 48 | + fancy.CRGB(0.75, 0.0, 1.0), |
| 49 | + fancy.CRGB(0.0, 0.0, 1.0), |
| 50 | + fancy.CRGB(0.0, 1.0, 1.0), |
| 51 | + fancy.CRGB(1.0, 0.0, 1.0)] |
| 52 | + |
| 53 | +star_palette = [fancy.CRGB(1.0, 0.75, 0.0), |
| 54 | + fancy.CRGB(1.0, 1.0, 1.0), |
| 55 | + fancy.CRGB(1.0, 0.75, 0.0), |
| 56 | + fancy.CRGB(0.75, 0.75, 0.75), |
| 57 | + fancy.CRGB(1.0, 0.75, 0.0)] |
| 58 | + |
| 59 | +hanukkah_palette = [fancy.CRGB(0.0, 1.0, 1.0), |
| 60 | + fancy.CRGB(0.0, 0.0, 1.0), |
| 61 | + fancy.CRGB(1.0, 0.75, 0.0), |
| 62 | + fancy.CRGB(0.0, 0.0, 1.0), |
| 63 | + fancy.CRGB(1.0, 1.0, 1.0)] |
| 64 | + |
| 65 | +# default offset value |
| 66 | +offset = 0 |
| 67 | + |
| 68 | +def gimel(): |
| 69 | + for i in range(TREE_LEDS): |
| 70 | + color = fancy.palette_lookup(hanukkah_palette, (offset - i) / 5) |
| 71 | + color = fancy.gamma_adjust(color, brightness=0.3) |
| 72 | + tree[i] = color.pack() |
| 73 | + tree.show() |
| 74 | + |
| 75 | + for i in range(CPX_LEDS): |
| 76 | + color = fancy.palette_lookup(hanukkah_palette, (offset - i) / 3) |
| 77 | + color = fancy.gamma_adjust(color, brightness=0.3) |
| 78 | + cpx[i] = color.pack() |
| 79 | + cpx.show() |
| 80 | + |
| 81 | +# neopixel animations |
| 82 | + |
| 83 | +def jazzy(): |
| 84 | + for i in range(TREE_LEDS): |
| 85 | + color = fancy.palette_lookup(fairy_palette, (offset - i) / 4.8) |
| 86 | + color = fancy.gamma_adjust(color, brightness=0.3) |
| 87 | + tree[i] = color.pack() |
| 88 | + tree.show() |
| 89 | + |
| 90 | + for i in range(CPX_LEDS): |
| 91 | + color = fancy.palette_lookup(fairy_palette, (offset + i) / 4) |
| 92 | + color = fancy.gamma_adjust(color, brightness=0.3) |
| 93 | + cpx[i] = color.pack() |
| 94 | + cpx.show() |
| 95 | + |
| 96 | +def latkes(): |
| 97 | + for i in range(TREE_LEDS): |
| 98 | + color = fancy.palette_lookup(hanukkah_palette, (offset - 24) / TREE_LEDS) |
| 99 | + color = fancy.gamma_adjust(color, brightness=0.3) |
| 100 | + tree[i] = color.pack() |
| 101 | + tree.show() |
| 102 | + |
| 103 | + for i in range(CPX_LEDS): |
| 104 | + color = fancy.palette_lookup(hanukkah_palette, (offset - 20) / CPX_LEDS) |
| 105 | + color = fancy.gamma_adjust(color, brightness=0.3) |
| 106 | + cpx[i] = color.pack() |
| 107 | + cpx.show() |
| 108 | + |
| 109 | +def twinkle(): |
| 110 | + for i in range(60): |
| 111 | + color = fancy.palette_lookup(fairy_palette, offset + i / CPX_LEDS) |
| 112 | + color = fancy.gamma_adjust(color, brightness=0.25) |
| 113 | + p = random.randint(0, (CPX_LEDS - 1)) |
| 114 | + cpx[p] = color.pack() |
| 115 | + cpx.show() |
| 116 | + |
| 117 | + for i in range(60): |
| 118 | + color = fancy.palette_lookup(fairy_palette, offset + i / TREE_LEDS) |
| 119 | + color = fancy.gamma_adjust(color, brightness=0.25) |
| 120 | + p = random.randint(0, (TREE_LEDS - 1)) |
| 121 | + tree[p] = color.pack() |
| 122 | + tree.show() |
| 123 | + |
| 124 | +def merry(): |
| 125 | + for i in range(TREE_LEDS): |
| 126 | + color = fancy.palette_lookup(merry_palette, (offset + i) / (TREE_LEDS / 2)) |
| 127 | + color = fancy.gamma_adjust(color, brightness=0.25) |
| 128 | + tree[i] = color.pack() |
| 129 | + tree.show() |
| 130 | + |
| 131 | + for i in range(60): |
| 132 | + color = fancy.palette_lookup(star_palette, (offset + i) / CPX_LEDS) |
| 133 | + color = fancy.gamma_adjust(color, brightness=0.25) |
| 134 | + p = random.randint(0, (CPX_LEDS - 1)) |
| 135 | + cpx[p] = color.pack() |
| 136 | + cpx.show() |
| 137 | + |
| 138 | +def festive(): |
| 139 | + for i in range(TREE_LEDS): |
| 140 | + color = fancy.palette_lookup(merry_palette, (offset - i) / 2) |
| 141 | + color = fancy.gamma_adjust(color, brightness=0.25) |
| 142 | + tree[i] = color.pack() |
| 143 | + tree.show() |
| 144 | + |
| 145 | + for i in range(CPX_LEDS): |
| 146 | + color = fancy.palette_lookup(star_palette, (offset + i) / CPX_LEDS) |
| 147 | + color = fancy.gamma_adjust(color, brightness=0.25) |
| 148 | + cpx[i] = color.pack() |
| 149 | + cpx.show() |
| 150 | + |
| 151 | +def fancy_swirl(): |
| 152 | + for i in range(TREE_LEDS): |
| 153 | + color = fancy.palette_lookup(winter_palette, (offset + i) / TREE_LEDS) |
| 154 | + color = fancy.gamma_adjust(color, brightness=0.25) |
| 155 | + tree[i] = color.pack() |
| 156 | + tree.show() |
| 157 | + |
| 158 | + for i in range(CPX_LEDS): |
| 159 | + color = fancy.palette_lookup(star_palette, (offset - i) / CPX_LEDS) |
| 160 | + color = fancy.gamma_adjust(color, brightness=0.25) |
| 161 | + cpx[i] = color.pack() |
| 162 | + cpx.show() |
| 163 | + |
| 164 | +# states for different neopixel displays |
| 165 | +fairies = False |
| 166 | +feeling_fancy = False |
| 167 | +feeling_festive = False |
| 168 | +feeling_jazzy = False |
| 169 | +feeling_merry = False |
| 170 | +frying_latkes = False |
| 171 | +rolling_gimel = False |
| 172 | + |
| 173 | +while True: |
| 174 | + # states to trigger the different neopixel modes |
| 175 | + if fairies: |
| 176 | + twinkle() |
| 177 | + offset += 0.5 |
| 178 | + if feeling_fancy: |
| 179 | + fancy_swirl() |
| 180 | + offset += 0.05 |
| 181 | + if feeling_festive: |
| 182 | + festive() |
| 183 | + offset += 0.05 |
| 184 | + if feeling_jazzy: |
| 185 | + jazzy() |
| 186 | + offset += 0.08 |
| 187 | + if feeling_merry: |
| 188 | + merry() |
| 189 | + offset += 0.12 |
| 190 | + if frying_latkes: |
| 191 | + latkes() |
| 192 | + offset += 0.05 |
| 193 | + if rolling_gimel: |
| 194 | + gimel() |
| 195 | + offset += 0.05 |
| 196 | + |
| 197 | + if not ble.connected and not advertising: |
| 198 | + # not connected in the app yet |
| 199 | + ble.start_advertising(advertisement) |
| 200 | + advertising = True |
| 201 | + |
| 202 | + if ble.connected: |
| 203 | + # after connected via app |
| 204 | + advertising = False |
| 205 | + if uart.in_waiting: |
| 206 | + # waiting for input from app |
| 207 | + packet = Packet.from_stream(uart) |
| 208 | + if isinstance(packet, ButtonPacket): |
| 209 | + # if buttons in the app are pressed |
| 210 | + if packet.pressed: |
| 211 | + # fairies |
| 212 | + if packet.button == ButtonPacket.UP: |
| 213 | + fairies = True |
| 214 | + feeling_fancy = False |
| 215 | + feeling_festive = False |
| 216 | + feeling_jazzy = False |
| 217 | + feeling_merry = False |
| 218 | + frying_latkes = False |
| 219 | + rolling_gimel = False |
| 220 | + # fancy |
| 221 | + if packet.button == ButtonPacket.LEFT: |
| 222 | + fairies = False |
| 223 | + feeling_fancy = True |
| 224 | + feeling_festive = False |
| 225 | + feeling_jazzy = False |
| 226 | + feeling_merry = False |
| 227 | + frying_latkes = False |
| 228 | + rolling_gimel = False |
| 229 | + # festive |
| 230 | + if packet.button == ButtonPacket.RIGHT: |
| 231 | + fairies = False |
| 232 | + feeling_fancy = False |
| 233 | + feeling_festive = True |
| 234 | + feeling_jazzy = False |
| 235 | + feeling_merry = False |
| 236 | + frying_latkes = False |
| 237 | + rolling_gimel = False |
| 238 | + # jazzy |
| 239 | + if packet.button == ButtonPacket.DOWN: |
| 240 | + fairies = False |
| 241 | + feeling_fancy = False |
| 242 | + feeling_festive = False |
| 243 | + feeling_jazzy = True |
| 244 | + feeling_merry = False |
| 245 | + frying_latkes = False |
| 246 | + rolling_gimel = False |
| 247 | + # merry |
| 248 | + if packet.button == ButtonPacket.BUTTON_1: |
| 249 | + fairies = False |
| 250 | + feeling_fancy = False |
| 251 | + feeling_festive = False |
| 252 | + feeling_jazzy = False |
| 253 | + feeling_merry = True |
| 254 | + frying_latkes = False |
| 255 | + rolling_gimel = False |
| 256 | + # latkes |
| 257 | + if packet.button == ButtonPacket.BUTTON_2: |
| 258 | + fairies = False |
| 259 | + feeling_fancy = False |
| 260 | + feeling_festive = False |
| 261 | + feeling_jazzy = False |
| 262 | + feeling_merry = False |
| 263 | + frying_latkes = True |
| 264 | + rolling_gimel = False |
| 265 | + # gimel |
| 266 | + if packet.button == ButtonPacket.BUTTON_3: |
| 267 | + fairies = False |
| 268 | + feeling_fancy = False |
| 269 | + feeling_festive = False |
| 270 | + feeling_jazzy = False |
| 271 | + feeling_merry = False |
| 272 | + frying_latkes = False |
| 273 | + rolling_gimel = True |
| 274 | + # off |
| 275 | + if packet.button == ButtonPacket.BUTTON_4: |
| 276 | + fairies = False |
| 277 | + feeling_fancy = False |
| 278 | + feeling_festive = False |
| 279 | + feeling_jazzy = False |
| 280 | + feeling_merry = False |
| 281 | + frying_latkes = False |
| 282 | + rolling_gimel = False |
| 283 | + cpx.fill(OFF) |
| 284 | + tree.fill(OFF) |
| 285 | + tree.show() |
| 286 | + cpx.show() |
0 commit comments