|
| 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