Skip to content

Commit fa6d5b4

Browse files
authored
Merge pull request #2285 from adafruit/clue_neopixels
Adding code for the PyLeap CLUE NeoPixels
2 parents 8488b31 + 268fb69 commit fa6d5b4

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed

PyLeap_CLUE_BLE_NeoPixels/code.py

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
# SPDX-FileCopyrightText: 2020 Kattni Rembor for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2020 Erin St Blaine for Adafruit Industries
3+
#
4+
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries (Adapted for CLUE)
5+
#
6+
# SPDX-License-Identifier: MIT
7+
8+
# pylint: disable=attribute-defined-outside-init
9+
# pylint: disable=too-few-public-methods
10+
11+
import board
12+
import neopixel
13+
import displayio
14+
import terminalio
15+
from adafruit_display_text import label
16+
from rainbowio import colorwheel
17+
from adafruit_display_shapes.rect import Rect
18+
from adafruit_led_animation.animation.solid import Solid
19+
from adafruit_led_animation.animation.comet import Comet
20+
from adafruit_led_animation.animation.rainbow import Rainbow
21+
from adafruit_led_animation.animation.rainbowcomet import RainbowComet
22+
from adafruit_led_animation.animation.sparkle import Sparkle
23+
from adafruit_led_animation.animation.sparklepulse import SparklePulse
24+
from adafruit_led_animation.sequence import AnimationSequence
25+
from adafruit_led_animation.group import AnimationGroup
26+
from adafruit_led_animation.animation import Animation
27+
from adafruit_led_animation.sequence import AnimateOnce
28+
from adafruit_led_animation.color import (
29+
AMBER,
30+
ORANGE,
31+
WHITE,
32+
RED,
33+
BLACK
34+
)
35+
36+
from adafruit_ble import BLERadio
37+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
38+
from adafruit_ble.services.nordic import UARTService
39+
40+
from adafruit_bluefruit_connect.packet import Packet
41+
from adafruit_bluefruit_connect.button_packet import ButtonPacket
42+
from adafruit_bluefruit_connect.color_packet import ColorPacket
43+
44+
NUM_LEDS = 60 # change to reflect your LED strip
45+
NEOPIXEL_PIN = board.D0 # change to reflect your wiring
46+
47+
# Declare a NeoPixel object on NEOPIXEL_PIN with NUM_LEDS pixels,
48+
# no auto-write.
49+
# Set brightness to max, we'll control it later in the code
50+
pixels = neopixel.NeoPixel(NEOPIXEL_PIN, NUM_LEDS, brightness=1.0,
51+
auto_write=False,
52+
)
53+
54+
ble = BLERadio()
55+
uart_service = UARTService()
56+
advertisement = ProvideServicesAdvertisement(uart_service)
57+
58+
display = board.DISPLAY
59+
clue_group = displayio.Group()
60+
61+
rect = Rect(0, 0, 240, 240, fill=0x0)
62+
clue_group.append(rect)
63+
64+
text_area = label.Label(terminalio.FONT, text="CONNECT TO BLE", color=WHITE)
65+
text_area.anchor_point = (0.5, 0.5)
66+
text_area.anchored_position = (240 / 2, 240 / 2)
67+
text_area.scale = 2
68+
clue_group.append(text_area)
69+
70+
display.show(clue_group)
71+
72+
class RainbowFade(Animation):
73+
''' fades the entire strip through the whole spectrum '''
74+
_color_index = 150 # choose start color (0-255)
75+
def __init__(self, pixel_object, speed, name): # define animation
76+
super().__init__(pixel_object, speed=speed, color=WHITE, name=name)
77+
78+
def draw(self): # draw the animation
79+
''' fades the entire strip through the whole spectrum '''
80+
self.color = colorwheel(self._color_index + 1)
81+
self._color_index = (self._color_index + 1) % 256
82+
self.fill(self.color)
83+
84+
# ANIMATION DEFINITIONS --
85+
# create as many animations as you'd like and define their attributes here.
86+
# They can be a single line or a group of animations - the groups will play
87+
# at the same time, overlaid on top of each other.
88+
89+
90+
readingLight = Solid(pixels, color=0xFF7D13, name="reading light") #warm white color HEX code
91+
brightWhite = Solid(pixels, color=(150, 150, 150), name="bright white")
92+
rainbow = Rainbow(pixels, speed=0.1, period=10, step=0.5, name="rainbow")
93+
rainbowfade = RainbowFade(pixels, speed=0.4, name="rainbowfade")
94+
powerup = RainbowComet(pixels, speed=0, tail_length=50, bounce=False, name="rainbow comet")
95+
off = Solid(pixels, color=BLACK, name="off")
96+
97+
#startup animation will play just once
98+
startup = AnimateOnce(powerup)
99+
100+
#starrynight and fire are animation groups with layered effects.
101+
starrynight = AnimationGroup(
102+
SparklePulse(pixels, speed=0.01, color=(0, 0, 150), period=1),
103+
Comet(pixels, speed=0, tail_length=8, color=(150, 150, 150), bounce=False),
104+
name = "starrynight")
105+
106+
fire = AnimationGroup(
107+
Comet(pixels, speed=0, tail_length=1, color=BLACK),
108+
Sparkle(pixels, speed=0.1, num_sparkles=10, color=AMBER),
109+
Sparkle(pixels, speed=0.1, num_sparkles=10, color=RED),
110+
Sparkle(pixels, speed=0.1, num_sparkles=20, color=ORANGE),
111+
Sparkle(pixels, speed=0.1, num_sparkles=5, color=0xFF7D13),
112+
Sparkle(pixels, speed=0.1, num_sparkles=10, color=BLACK),
113+
name = "fire"
114+
)
115+
116+
# Here is the animation playlist where you set the order of modes
117+
118+
animations = AnimationSequence(
119+
readingLight,
120+
fire,
121+
rainbow,
122+
starrynight,
123+
rainbowfade,
124+
brightWhite,
125+
auto_clear=True,
126+
)
127+
128+
129+
current_color = BLACK
130+
MODE = 0
131+
132+
while True:
133+
if MODE == 0: # If currently off...
134+
startup.animate()
135+
while startup.animate():
136+
pass
137+
MODE = 3
138+
# Advertise when not connected
139+
140+
elif MODE >= 1: # If not OFF MODE...
141+
ble.start_advertising(advertisement)
142+
while not ble.connected:
143+
text_area.text = "CONNECT TO BLE"
144+
if MODE == 2:
145+
pass
146+
elif MODE == 1:
147+
animations.animate()
148+
# Now we're connected
149+
150+
while ble.connected:
151+
if uart_service.in_waiting:
152+
packet = Packet.from_stream(uart_service)
153+
# Color Picker Functionality
154+
if isinstance(packet, ColorPacket):
155+
MODE = 2
156+
# Set all the pixels to one color and stay there.
157+
pixels.fill(packet.color)
158+
pixels.show()
159+
text_area.text = str(packet.color)
160+
# Control Pad Functionality
161+
elif isinstance(packet, ButtonPacket):
162+
if packet.pressed:
163+
if packet.button == ButtonPacket.BUTTON_1:
164+
MODE = 1
165+
animations.activate(1)
166+
elif packet.button == ButtonPacket.BUTTON_2:
167+
MODE = 1
168+
animations.activate(2)
169+
elif packet.button == ButtonPacket.BUTTON_3:
170+
MODE = 1
171+
animations.activate(3)
172+
elif packet.button == ButtonPacket.BUTTON_4:
173+
MODE = 1
174+
animations.activate(4)
175+
# change the mode with right arrow
176+
elif packet.button == ButtonPacket.RIGHT:
177+
MODE = 1
178+
animations.next()
179+
elif packet.button == ButtonPacket.LEFT:
180+
MODE = 4
181+
off.animate()
182+
#change the brightness with up and down arrows
183+
elif packet.button == ButtonPacket.UP:
184+
pixels.brightness = pixels.brightness + 0.1
185+
pixels.show()
186+
if pixels.brightness > 1:
187+
pixels.brightness = 1
188+
elif packet.button == ButtonPacket.DOWN:
189+
pixels.brightness = pixels.brightness - 0.1
190+
pixels.show()
191+
if pixels.brightness < 0.1:
192+
pixels.brightness = 0.1
193+
if MODE == 1:
194+
text_area.text = str(animations.current_animation.name)
195+
if MODE == 4:
196+
text_area.text = str(off.name)
197+
if MODE == 1:
198+
animations.animate()
199+
if MODE == 3:
200+
text_area.text = "CONNECTED"
201+
if MODE == 4:
202+
animations.freeze()

0 commit comments

Comments
 (0)