Skip to content

Commit e91ef4e

Browse files
authored
Create code.py
1 parent 2ff4c77 commit e91ef4e

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

NeoPixel_Badge_Lanyard/code.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 adafruit_fancyled.adafruit_fancyled as fancy
7+
from adafruit_ble.uart import UARTServer
8+
# for >= CPy 5.0.0
9+
# from adafruit_ble.uart_server import UARTServer
10+
from adafruit_bluefruit_connect.packet import Packet
11+
from adafruit_bluefruit_connect.button_packet import ButtonPacket
12+
from adafruit_bluefruit_connect.color_packet import ColorPacket
13+
14+
NUM_LEDS = 60 # change to reflect your LED strip
15+
NEOPIXEL_PIN = board.D13 # change to reflect your wiring
16+
17+
# Palettes can have any number of elements in various formats
18+
# check https://learn.adafruit.com/fancyled-library-for-circuitpython/colors
19+
# 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,
48+
# no auto-write.
49+
# Set brightness to max because we'll be using FancyLED's brightness control.
50+
pixels = neopixel.NeoPixel(NEOPIXEL_PIN, NUM_LEDS, brightness=1.0,
51+
auto_write=False)
52+
53+
offset = 0 # Positional offset into color palette to get it to 'spin'
54+
offset_increment = 1
55+
OFFSET_MAX = 1000000
56+
57+
uart_server = UARTServer()
58+
59+
def set_palette(palette):
60+
for i in range(NUM_LEDS):
61+
# Load each pixel's color from the palette using an offset, run it
62+
# through the gamma function, pack RGB value and assign to pixel.
63+
color = fancy.palette_lookup(palette, (offset + i) / NUM_LEDS)
64+
color = fancy.gamma_adjust(color, brightness=0.25)
65+
pixels[i] = color.pack()
66+
pixels.show()
67+
68+
# set initial palette to run on startup
69+
palette_choice = PALETTE_RAINBOW
70+
71+
# True if cycling a palette
72+
cycling = True
73+
74+
while True:
75+
uart_server.start_advertising()
76+
while not uart_server.connected:
77+
if cycling:
78+
set_palette(palette_choice)
79+
offset = (offset + offset_increment) % OFFSET_MAX
80+
81+
# Now we're connected
82+
83+
while uart_server.connected:
84+
if uart_server.in_waiting:
85+
packet = Packet.from_stream(uart_server)
86+
if isinstance(packet, ColorPacket):
87+
cycling = False
88+
# Set all the pixels to one color and stay there.
89+
pixels.fill(packet.color)
90+
pixels.show()
91+
elif isinstance(packet, ButtonPacket):
92+
cycling = True
93+
if packet.pressed:
94+
if packet.button == ButtonPacket.BUTTON_1:
95+
palette_choice = PALETTE_RAINBOW
96+
elif packet.button == ButtonPacket.BUTTON_2:
97+
palette_choice = PALETTE_GRADIENT
98+
elif packet.button == ButtonPacket.BUTTON_3:
99+
palette_choice = PALETTE_FIRE
100+
elif packet.button == ButtonPacket.BUTTON_4:
101+
palette_choice = PALETTE_WATER
102+
# change the speed of the animation by incrementing offset
103+
elif packet.button == ButtonPacket.UP:
104+
offset_increment += 1
105+
elif packet.button == ButtonPacket.DOWN:
106+
offset_increment -= 1
107+
108+
if cycling:
109+
offset = (offset + offset_increment) % OFFSET_MAX
110+
set_palette(palette_choice)

0 commit comments

Comments
 (0)