Skip to content

Commit 41219f7

Browse files
authored
Add rgb led example (#67)
* Adding helper methods (toRed, toGreen, toBlue) for hex-encoded color feeds * Adding example for AIO Basics: Color
1 parent 6c89c4e commit 41219f7

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

Adafruit_IO/client.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,21 @@ def delete(self, feed, data_id):
211211
"""
212212
path = "feeds/{0}/data/{1}".format(feed, data_id)
213213
self._delete(path)
214+
215+
def toRed(self, data):
216+
"""Hex color feed to red channel.
217+
"""
218+
return ((int(data[1], 16))*16) + int(data[2], 16)
219+
220+
def toGreen(self, data):
221+
"""Hex color feed to green channel.
222+
"""
223+
return (int(data[3], 16) * 16) + int(data[4], 16)
224+
225+
def toBlue(self, data):
226+
"""Hex color feed to blue channel.
227+
"""
228+
return (int(data[5], 16) * 16) + int(data[6], 16)
214229

215230
# Feed functionality.
216231
def feeds(self, feed=None):

Adafruit_IO/mqtt_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def loop_background(self, stop=None):
147147
Params:
148148
- stop: boolean, stops the execution of the background loop.
149149
"""
150-
is stop:
150+
if stop:
151151
self._client.loop_stop()
152152
self._client.loop_start()
153153

examples/basics/rgb_led.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""
2+
`rgb_led.py`
3+
------------------------
4+
Control a RGB LED using
5+
Adafruit IO and Python
6+
7+
Requires:
8+
- Adafruit-Blinka
9+
- Adafruit-CircuitPython-PCA9685
10+
"""
11+
# import system libraries
12+
import time
13+
14+
# import Adafruit Blinka
15+
from board import SCL, SDA
16+
from busio import I2C
17+
18+
# import the PCA9685 module.
19+
from adafruit_pca9685 import PCA9685
20+
21+
# import Adafruit IO REST client
22+
from Adafruit_IO import Client, Feed, RequestError
23+
24+
# PWM Pins
25+
RED_PIN = 6
26+
GREEN_PIN = 5
27+
BLUE_PIN = 4
28+
29+
# Set to your Adafruit IO key.
30+
# Remember, your key is a secret,
31+
# so make sure not to publish it when you publish this code!
32+
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
33+
34+
# Set to your Adafruit IO username.
35+
# (go to https://accounts.adafruit.com to find your username)
36+
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
37+
38+
# Create an instance of the REST client.
39+
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
40+
41+
try: # if we have a 'color' feed
42+
color = aio.feeds('color')
43+
except RequestError: # create an `color` feed
44+
feed = Feed(name='color')
45+
color = aio.create_feed(feed)
46+
47+
# Create the I2C bus interface.
48+
i2c_bus = I2C(SCL, SDA)
49+
50+
# Create a simple PCA9685 class instance.
51+
pca = PCA9685(i2c_bus)
52+
pca.frequency = 60
53+
prev_color = '#000000'
54+
55+
def map_range(x, in_min, in_max, out_min, out_max):
56+
"""re-maps a number from one range to another."""
57+
mapped = (x-in_min) * (out_max - out_min) / (in_max-in_min) + out_min
58+
if out_min <= out_max:
59+
return max(min(mapped, out_max), out_min)
60+
return min(max(mapped, out_max), out_min)
61+
62+
while True:
63+
# grab the `color` feed
64+
color_val = aio.receive(color.key)
65+
if color_val != prev_color:
66+
# print rgb values and hex value
67+
print('Received Color: ')
68+
red = aio.toRed(color_val.value)
69+
print('\t - R: ', red)
70+
green = aio.toGreen(color_val.value)
71+
print('\t - G: ', green)
72+
blue = aio.toBlue(color_val.value)
73+
print('\t - B: ', blue)
74+
print('\t - HEX: ', color_val.value)
75+
# map color values (0-255) to 16-bit values for the pca
76+
red = map_range(int(red), 0, 255, 0, 65535)
77+
green = map_range(int(green), 0, 255, 0, 65535)
78+
blue = map_range(int(blue), 0, 255, 0, 65535)
79+
# invert RGB values for common anode LEDs.
80+
pca.channels[RED_PIN].duty_cycle = 65535 - int(red)
81+
pca.channels[GREEN_PIN].duty_cycle = 65535 - int(green)
82+
pca.channels[BLUE_PIN].duty_cycle = 65535 - int(blue)
83+
prev_color = color_val
84+
# let's wait a bit so we don't flood adafruit io's servers...
85+
time.sleep(1)

0 commit comments

Comments
 (0)