1+ """ FancyLED Palette and Color Picker Control with BlueFruit App
2+ Code by Phil Burgess, Dan Halbert and Erin St Blaine for Adafruit Industries
3+ """
4+ import time
15import board
26import neopixel
7+ import adafruit_fancyled .adafruit_fancyled as fancy
38from adafruit_ble .uart import UARTServer
9+ # for >= CPy 5.0.0
10+ #from adafruit_ble.uart_server import UARTServer
411from adafruit_bluefruit_connect .packet import Packet
12+ from adafruit_bluefruit_connect .button_packet import ButtonPacket
513from adafruit_bluefruit_connect .color_packet import ColorPacket
614
15+ NUM_LEDS = 60 # change to reflect your LED strip
16+ NEOPIXEL_PIN = board .D13 # change to reflect your wiring
17+
18+ # Palettes can have any number of elements in various formats
19+ # check https://learn.adafruit.com/fancyled-library-for-circuitpython/colors for more info
20+
21+ # Declare a 6-element RGB rainbow palette
22+ PALETTE_RAINBOW = [fancy .CRGB (1.0 , 0.0 , 0.0 ), # Red
23+ fancy .CRGB (0.5 , 0.5 , 0.0 ), # Yellow
24+ fancy .CRGB (0.0 , 1.0 , 0.0 ), # Green
25+ fancy .CRGB (0.0 , 0.5 , 0.5 ), # Cyan
26+ fancy .CRGB (0.0 , 0.0 , 1.0 ), # Blue
27+ fancy .CRGB (0.5 , 0.0 , 0.5 )] # Magenta
28+
29+ # Declare a Purple Gradient palette
30+ PALETTE_GRADIENT = [fancy .CRGB (160 , 0 , 141 ), # Purples
31+ fancy .CRGB (77 , 0 , 160 ),
32+ fancy .CRGB (124 , 0 , 255 ),
33+ fancy .CRGB (0 , 68 , 214 )]
34+
35+ # Declare a FIRE palette
36+ PALETTE_FIRE = [fancy .CRGB (0 , 0 , 0 ), # Black
37+ fancy .CHSV (1.0 ), # Red
38+ fancy .CRGB (1.0 , 1.0 , 0.0 ), # Yellow
39+ 0xFFFFFF ] # White
40+
41+ # Declare a Water Colors palette
42+ PALETTE_WATER = [fancy .CRGB (0 , 214 , 214 ), # blues and cyans
43+ fancy .CRGB (0 , 92 , 160 ),
44+ fancy .CRGB (0 , 123 , 255 ),
45+ fancy .CRGB (0 , 68 , 214 )]
46+
47+ # Declare a NeoPixel object on NEOPIXEL_PIN with NUM_LEDS pixels, no auto-write.
48+ # Set brightness to max because we'll be using FancyLED's brightness control.
49+ pixels = neopixel .NeoPixel (NEOPIXEL_PIN , NUM_LEDS , brightness = 1.0 , auto_write = False )
50+
51+ offset = 0 # Positional offset into color palette to get it to 'spin'
52+ offset_increment = 1
53+ OFFSET_MAX = 1000000
54+
755uart_server = UARTServer ()
856
9- pixel = neopixel .NeoPixel (board .D13 , 60 )
57+ def set_palette (palette ):
58+ for i in range (NUM_LEDS ):
59+ # Load each pixel's color from the palette using an offset, run it
60+ # through the gamma function, pack RGB value and assign to pixel.
61+ color = fancy .palette_lookup (palette , (offset + i ) / NUM_LEDS )
62+ color = fancy .gamma_adjust (color , brightness = 0.25 )
63+ pixels [i ] = color .pack ()
64+ pixels .show ()
1065
66+ # set initial palette to run on startup
67+ palette_choice = PALETTE_RAINBOW
1168
69+ # True if cycling a palette
70+ cycling = True
1271
1372while True :
14-
1573 uart_server .start_advertising ()
1674 while not uart_server .connected :
17- pass
75+ if cycling :
76+ set_palette (palette_choice )
77+ offset = (offset + offset_increment ) % OFFSET_MAX
1878
1979 # Now we're connected
2080
2181 while uart_server .connected :
2282 if uart_server .in_waiting :
2383 packet = Packet .from_stream (uart_server )
2484 if isinstance (packet , ColorPacket ):
25- # Change the NeoPixel color.
26- pixel .fill (packet .color )
27-
28- # If we got here, we lost the connection. Go up to the top and start
29- # advertising again and waiting for a connection.
85+ cycling = False
86+ # Set all the pixels to one color and stay there.
87+ pixels .fill (packet .color )
88+ pixels .show ()
89+ elif isinstance (packet , ButtonPacket ):
90+ cycling = True
91+ if packet .pressed :
92+ if packet .button == ButtonPacket .BUTTON_1 :
93+ palette_choice = PALETTE_RAINBOW
94+ elif packet .button == ButtonPacket .BUTTON_2 :
95+ palette_choice = PALETTE_GRADIENT
96+ elif packet .button == ButtonPacket .BUTTON_3 :
97+ palette_choice = PALETTE_FIRE
98+ elif packet .button == ButtonPacket .BUTTON_4 :
99+ palette_choice = PALETTE_WATER
100+ # change the speed of the animation by incrementing offset
101+ elif packet .button == ButtonPacket .UP :
102+ offset_increment += 1
103+ elif packet .button == ButtonPacket .DOWN :
104+ offset_increment -= 1
30105
106+ if cycling :
107+ offset = (offset + offset_increment ) % OFFSET_MAX
108+ set_palette (palette_choice )
0 commit comments