Skip to content

Commit 8e8d0ee

Browse files
authored
Merge branch 'master' into master
2 parents 5d63771 + 8fced95 commit 8e8d0ee

File tree

2 files changed

+197
-37
lines changed

2 files changed

+197
-37
lines changed

Bluetooth_Luminaries/code.py

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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 touchio
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 = 24 # change to reflect your total number of ring LEDs
16+
RING_PIN = board.A1 # change to reflect your wiring
17+
CPX_PIN = board.D8 # CPX Neopixels live on pin D8
18+
19+
touch_A2 = touchio.TouchIn(board.A2)
20+
touch_A3 = touchio.TouchIn(board.A3)
21+
touch_A4 = touchio.TouchIn(board.A4)
22+
touch_A5 = touchio.TouchIn(board.A5)
23+
touch_A6 = touchio.TouchIn(board.A6)
24+
touch_TX = touchio.TouchIn(board.TX)
25+
26+
# Palettes can have any number of elements in various formats
27+
# check https://learn.adafruit.com/fancyled-library-for-circuitpython/colors
28+
# for more info
29+
# Declare a 6-element RGB rainbow palette
30+
PALETTE_RAINBOW = [fancy.CRGB(1.0, 0.0, 0.0), # Red
31+
fancy.CRGB(0.5, 0.3, 0.0), # Orange
32+
fancy.CRGB(0.5, 0.5, 0.0), # Yellow
33+
fancy.CRGB(0.3, 0.7, 0.0), # Yellow Green
34+
fancy.CRGB(0.0, 1.0, 0.0), # Green
35+
fancy.CRGB(0.0, 0.7, 0.3), # Teal
36+
fancy.CRGB(0.0, 0.5, 0.5), # Cyan
37+
fancy.CRGB(0.0, 0.3, 0.7), # Blue
38+
fancy.CRGB(0.0, 0.0, 1.0), # Blue
39+
fancy.CRGB(0.5, 0.0, 0.5), # Magenta
40+
fancy.CRGB(0.7, 0.0, 0.3)] # Purple
41+
42+
# Reading Lamp mode - Warm Yellow
43+
PALETTE_BRIGHT = [fancy.CRGB(255, 183, 55)]
44+
45+
# Black Only palette for "off" mode
46+
PALETTE_DARK = [fancy.CRGB(0, 0, 0)]
47+
48+
# Declare a FIRE palette
49+
PALETTE_FIRE = [fancy.CRGB(160, 30, 0), # Reds and Yellows
50+
fancy.CRGB(27, 65, 0),
51+
fancy.CRGB(0, 0, 0),
52+
fancy.CRGB(224, 122, 0),
53+
fancy.CRGB(0, 0, 0),
54+
fancy.CRGB(250, 80, 0),
55+
fancy.CRGB(0, 0, 0),
56+
fancy.CRGB(0, 0, 0),
57+
fancy.CRGB(200, 40, 0)]
58+
59+
# Declare a NeoPixel object on NEOPIXEL_PIN with NUM_LEDS pixels,
60+
# no auto-write.
61+
# Set brightness to max because we'll be using FancyLED's brightness control.
62+
ring = neopixel.NeoPixel(RING_PIN, NUM_LEDS, brightness=1.0, auto_write=False)
63+
cpx = neopixel.NeoPixel(CPX_PIN, NUM_LEDS, brightness=1.0, auto_write=False)
64+
65+
offset = 0 # Positional offset into color palette to get it to 'spin'
66+
offset_increment = 6
67+
OFFSET_MAX = 1000000
68+
69+
uart_server = UARTServer()
70+
71+
def set_palette(palette):
72+
for i in range(NUM_LEDS):
73+
# Load each pixel's color from the palette using an offset, run it
74+
# through the gamma function, pack RGB value and assign to pixel.
75+
color = fancy.palette_lookup(palette, (offset + i) / NUM_LEDS)
76+
color = fancy.gamma_adjust(color, brightness=1.0)
77+
ring[i] = color.pack()
78+
ring.show()
79+
80+
for i in range(NUM_LEDS):
81+
# Load each pixel's color from the palette using an offset, run it
82+
# through the gamma function, pack RGB value and assign to pixel.
83+
color = fancy.palette_lookup(palette, (offset + i) / NUM_LEDS)
84+
color = fancy.gamma_adjust(color, brightness=1.0)
85+
cpx[i] = color.pack()
86+
cpx.show()
87+
88+
# set initial palette to run on startup
89+
palette_choice = PALETTE_FIRE
90+
91+
# True if cycling a palette
92+
cycling = True
93+
94+
# Are we already advertising?
95+
advertising = False
96+
97+
98+
while True:
99+
100+
if cycling:
101+
set_palette(palette_choice)
102+
offset = (offset + offset_increment) % OFFSET_MAX
103+
104+
if not uart_server.connected and not advertising:
105+
uart_server.start_advertising()
106+
advertising = True
107+
108+
# Are we connected via Bluetooth now?
109+
if uart_server.connected:
110+
# Once we're connected, we're not advertising any more.
111+
advertising = False
112+
# Have we started to receive a packet?
113+
if uart_server.in_waiting:
114+
packet = Packet.from_stream(uart_server)
115+
if isinstance(packet, ColorPacket):
116+
cycling = False
117+
# Set all the pixels to one color and stay there.
118+
ring.fill(packet.color)
119+
cpx.fill(packet.color)
120+
ring.show()
121+
cpx.show()
122+
elif isinstance(packet, ButtonPacket):
123+
cycling = True
124+
if packet.pressed:
125+
if packet.button == ButtonPacket.BUTTON_1:
126+
palette_choice = PALETTE_DARK
127+
elif packet.button == ButtonPacket.BUTTON_2:
128+
palette_choice = PALETTE_BRIGHT
129+
elif packet.button == ButtonPacket.BUTTON_3:
130+
palette_choice = PALETTE_FIRE
131+
offset_increment = 6
132+
elif packet.button == ButtonPacket.BUTTON_4:
133+
palette_choice = PALETTE_RAINBOW
134+
offset_increment = 1
135+
136+
# change the speed of the animation by incrementing offset
137+
elif packet.button == ButtonPacket.UP:
138+
offset_increment += 1
139+
elif packet.button == ButtonPacket.DOWN:
140+
offset_increment -= 1
141+
142+
# Whether or not we're connected via Bluetooth,
143+
# we also want to handle touch inputs.
144+
if touch_A2.value:
145+
cycling = True
146+
palette_choice = PALETTE_DARK
147+
elif touch_A3.value:
148+
cycling = True
149+
palette_choice = PALETTE_BRIGHT
150+
elif touch_A4.value:
151+
cycling = True
152+
palette_choice = PALETTE_FIRE
153+
offset_increment = 6
154+
elif touch_A5.value:
155+
cycling = True
156+
palette_choice = PALETTE_RAINBOW
157+
offset_increment = 1
158+
# Also check for touch speed control
159+
# if touch_A6.value:
160+
# offset_increment += 1
161+
# if touch_TX.value:
162+
# offset_increment -= 1

CircusPython_BLE/circus.py

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# CircusPython!
22
# For use with the Adafruit BlueFruit LE Connect app.
3-
# Works with CircuitPython 4.0.0-beta.1 and later running on an nRF52840 board.
3+
# Works with CircuitPython 5.0.0-beta.0 and later running on an nRF52840 board.
44

55
import random
66
import time
77

88
from adafruit_crickit import crickit
9-
from adafruit_ble.uart import UARTServer
9+
from adafruit_ble import BLERadio
10+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
11+
from adafruit_ble.services.nordic import UARTService
1012

1113
from adafruit_bluefruit_connect.packet import Packet
1214
# Only the packet classes that are imported will be known to Packet.
@@ -24,7 +26,9 @@ def sparkle():
2426
crickit.neopixel[random.randrange(24)] = color
2527
crickit.neopixel[random.randrange(24)] = color
2628

27-
uart_server = UARTServer()
29+
ble = BLERadio()
30+
uart_service = UARTService()
31+
advertisement = ProvideServicesAdvertisement(uart_service)
2832

2933
# Increase this to slow down movement of the servo arm.
3034
DELAY = 0.0
@@ -36,41 +40,35 @@ def sparkle():
3640
# slightly as necessary so you don't bump into the ring.
3741
DOWN_ANGLE = 2
3842

39-
advertising_now = False
4043
crickit.servo_1.angle = UP_ANGLE
4144
angle = UP_ANGLE
4245

4346
while True:
44-
sparkle()
45-
if not uart_server.connected:
46-
if not advertising_now:
47-
uart_server.start_advertising()
48-
advertising_now = True
49-
continue
50-
51-
# Connected, so no longer advertising.
52-
advertising_now = False
53-
54-
if uart_server.in_waiting:
55-
packet = Packet.from_stream(uart_server)
56-
if isinstance(packet, ColorPacket):
57-
# Change the fire color.
58-
color = packet.color
59-
elif isinstance(packet, ButtonPacket):
60-
if packet.pressed:
61-
if packet.button == '5' and angle != UP_ANGLE:
62-
# The Up button was pressed.
63-
for a in range(angle, UP_ANGLE+1, 1):
64-
crickit.servo_1.angle = a
65-
# Sparkle while moving.
66-
sparkle()
67-
time.sleep(DELAY)
68-
angle = UP_ANGLE
69-
elif packet.button == '6' and angle != DOWN_ANGLE:
70-
# The Down button was pressed.
71-
for a in range(angle, DOWN_ANGLE-1, -1):
72-
crickit.servo_1.angle = a
73-
# Sparkle while moving.
74-
sparkle()
75-
time.sleep(DELAY)
76-
angle = DOWN_ANGLE
47+
ble.start_advertising(advertisement)
48+
while not ble.connected:
49+
sparkle()
50+
while ble.connected:
51+
sparkle()
52+
if uart_service.in_waiting:
53+
packet = Packet.from_stream(uart_service)
54+
if isinstance(packet, ColorPacket):
55+
# Change the fire color.
56+
color = packet.color
57+
elif isinstance(packet, ButtonPacket):
58+
if packet.pressed:
59+
if packet.button == '5' and angle != UP_ANGLE:
60+
# The Up button was pressed.
61+
for a in range(angle, UP_ANGLE+1, 1):
62+
crickit.servo_1.angle = a
63+
# Sparkle while moving.
64+
sparkle()
65+
time.sleep(DELAY)
66+
angle = UP_ANGLE
67+
elif packet.button == '6' and angle != DOWN_ANGLE:
68+
# The Down button was pressed.
69+
for a in range(angle, DOWN_ANGLE-1, -1):
70+
crickit.servo_1.angle = a
71+
# Sparkle while moving.
72+
sparkle()
73+
time.sleep(DELAY)
74+
angle = DOWN_ANGLE

0 commit comments

Comments
 (0)