Skip to content

Commit 0dd7ea3

Browse files
authored
Merge branch 'master' into swap_hid_api
2 parents 0039034 + 1fe1e15 commit 0dd7ea3

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Magic Light Bulb remote color mixer
2+
# Sends RGB color values, read from three faders on CPB to the bulb
3+
# https://www.magiclightbulbs.com/collections/bluetooth-bulbs
4+
5+
import adafruit_ble
6+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
7+
from adafruit_ble_magic_light import MagicLightService
8+
import _bleio
9+
import board
10+
from analogio import AnalogIn
11+
from adafruit_circuitplayground import cp
12+
13+
14+
def find_connection():
15+
for connection in radio.connections:
16+
if MagicLightService not in connection: # Filter services
17+
continue
18+
return connection, connection[MagicLightService]
19+
return None, None
20+
21+
radio = adafruit_ble.BLERadio()
22+
23+
24+
def scale(value):
25+
# Scale a value from 0-65535 (AnalogIn range) to 0-255 (RGB range)
26+
return int(value / 65535 * 255)
27+
a4 = AnalogIn(board.A4) # red slider
28+
a5 = AnalogIn(board.A5) # green slider
29+
a6 = AnalogIn(board.A6) # blue slider
30+
31+
cp.pixels.brightness = 0.1
32+
dimmer = 1.0
33+
34+
active_connection, bulb = find_connection() # In case already connected
35+
36+
while True:
37+
if not active_connection: # There's no connection, so let's scan for one
38+
cp.pixels[0] = (60, 40, 0) # set CPB NeoPixel 0 to yellow while searching
39+
print("Scanning for Magic Light...")
40+
# Scan and filter for advertisements with ProvideServicesAdvertiesment type
41+
for advertisement in radio.start_scan(ProvideServicesAdvertisement):
42+
# Filter further for advertisements with MagicLightService
43+
if MagicLightService in advertisement.services:
44+
active_connection = radio.connect(advertisement)
45+
print("Connected to Magic Light")
46+
cp.pixels[0] = (0, 0, 255) # Set NeoPixel 0 to blue when connected
47+
# Play a happy tone
48+
cp.play_tone(440, 0.1)
49+
cp.play_tone(880, 0.1)
50+
print("Adjust slide potentiometers to mix RGB colors")
51+
try:
52+
bulb = active_connection[MagicLightService]
53+
except _bleio.ConnectionError:
54+
print("disconnected")
55+
continue
56+
break
57+
radio.stop_scan() # Now that we're connected, stop scanning
58+
59+
while active_connection.connected: # Connected, now we can set attrs to change colors
60+
# Toggle slide switch to go to half or full brightness
61+
if cp.switch:
62+
cp.red_led = True
63+
dimmer = 0.5
64+
else:
65+
cp.red_led = False
66+
dimmer = 1.0
67+
68+
# Press the 'A' button to momentarily black the bulb
69+
if cp.button_a:
70+
dimmer = 0.0
71+
72+
r = scale(a4.value * dimmer)
73+
g = scale(a5.value * dimmer)
74+
b = scale(a6.value * dimmer)
75+
76+
# Press the 'B' button to momentarily white the bulb
77+
if cp.button_b:
78+
r, g, b = (255, 255, 255)
79+
80+
color = (r, g, b)
81+
82+
try:
83+
bulb[0] = color # Send color to bulb's color characteristic
84+
except _bleio.ConnectionError:
85+
print("disconnected")
86+
continue
87+
cp.pixels[2] = (r, 0, 0)
88+
cp.pixels[3] = (0, g, 0)
89+
cp.pixels[4] = (0, 0, b)
90+
cp.pixels[7] = (color)
91+
92+
active_connection = None # Not connected, start scanning again
93+
cp.pixels[0] = (60, 40, 0)

0 commit comments

Comments
 (0)