Skip to content

Commit 7440f0e

Browse files
authored
Merge pull request #780 from firepixie/master
NeoPixel Lanyard
2 parents d116716 + fec94ae commit 7440f0e

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

Neopixel_Badge_Lanyard/code.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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
5+
import board
6+
import neopixel
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 = 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+
55+
uart_server = UARTServer()
56+
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()
65+
66+
# set initial palette to run on startup
67+
palette_choice = PALETTE_RAINBOW
68+
69+
# True if cycling a palette
70+
cycling = True
71+
72+
while True:
73+
uart_server.start_advertising()
74+
while not uart_server.connected:
75+
if cycling:
76+
set_palette(palette_choice)
77+
offset = (offset + offset_increment) % OFFSET_MAX
78+
79+
# Now we're connected
80+
81+
while uart_server.connected:
82+
if uart_server.in_waiting:
83+
packet = Packet.from_stream(uart_server)
84+
if isinstance(packet, ColorPacket):
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
105+
106+
if cycling:
107+
offset = (offset + offset_increment) % OFFSET_MAX
108+
set_palette(palette_choice)

0 commit comments

Comments
 (0)