Skip to content

Add compatibility with ESP32 GPIO RGB LEDs #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions adafruit_rgbled.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class RGBLED:
:type ~microcontroller.Pin: Microcontroller's blue_pin.
:type pulseio.PWMOut: PWMOut object associated with blue_pin.
:type PWMChannel: PCA9685 PWM channel associated with blue_pin.
:param ESP_SPIcontrol esp: The ESP object connected to a RGB LED. Defaults to None.
:param bool invert_pwm: False if the RGB LED is common cathode,
true if the RGB LED is common anode.

Expand Down Expand Up @@ -110,17 +111,37 @@ class RGBLED:
with adafruit_rgbled.RGBLED(board.D5, board.D6, board.D7, invert_pwm=True) as rgb_led:
rgb_led.color = (0, 255, 0)

Example for setting a RGB LED connected to an ESP32 module, using adafruit_esp32spi:

.. code-block:: python

import board
import adafruit_rgbled
from adafruit_esp32spi import adafruit_esp32spi
# Create ESP Object
esp32_cs = DigitalInOut(board.D13)
esp32_ready = DigitalInOut(board.D11)
esp32_reset = DigitalInOut(board.D12)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
with adafruit_rgbled.RGBLED(25, 26, 27, esp) as rgb_led:
rgb_led.color = (0, 255, 0)

"""
def __init__(self, red_pin, green_pin, blue_pin, invert_pwm=False):
# pylint: disable=too-many-arguments
def __init__(self, red_pin, green_pin, blue_pin, esp=None, invert_pwm=False):
self._rgb_led_pins = [red_pin, green_pin, blue_pin]
for i in range(len(self._rgb_led_pins)):
if hasattr(self._rgb_led_pins[i], 'frequency'):
self._rgb_led_pins[i].duty_cycle = 0
elif str(type(self._rgb_led_pins[i])) == "<class 'Pin'>":
self._rgb_led_pins[i] = PWMOut(self._rgb_led_pins[i])
self._rgb_led_pins[i].duty_cycle = 0
elif esp is not None:
esp.set_pin_mode(self._rgb_led_pins[i], 1)
else:
raise TypeError('Must provide a pin, PWMOut, or PWMChannel.')
raise TypeError('Must provide a pin, PWMOut, ESP Object, or PWMChannel.')
self._esp = esp
self._invert_pwm = invert_pwm
self._current_color = (0, 0, 0)
self.color = self._current_color
Expand All @@ -134,7 +155,7 @@ def __exit__(self, exception_type, exception_value, traceback):
def deinit(self):
"""Turn the LEDs off, deinit pwmout and release hardware resources."""
for pin in self._rgb_led_pins:
pin.deinit() #pylint: no-member
pin.deinit()
self._current_color = (0, 0, 0)

@property
Expand All @@ -153,7 +174,7 @@ def color(self, value):
color = int(map_range(value[i], 0, 255, 0, 65535))
if self._invert_pwm:
color -= 65535
self._rgb_led_pins[i].duty_cycle = abs(color)
self._write_color(color, self._rgb_led_pins[i])
elif isinstance(value, int):
if value>>24:
raise ValueError("Only bits 0->23 valid for integer input")
Expand All @@ -165,6 +186,17 @@ def color(self, value):
rgb[color] = int(map_range(rgb[color], 0, 255, 0, 65535))
if self._invert_pwm:
rgb[color] -= 65535
self._rgb_led_pins[color].duty_cycle = abs(rgb[color])
self._write_color(color, self._rgb_led_pins[color])
else:
raise ValueError('Color must be a tuple or 24-bit integer value.')

def _write_color(self, color, rgb_led_pin):
"""Sets duty-cycle for user-defined color to a RGB LED.
:param int color: Color, from color method.
:param int rgb_led_pin: GPIO pin to write to.
"""
if self._esp is not None:
color = map_range(abs(color), 0, 65535, 0, 1.0)
self._esp.set_analog_write(rgb_led_pin, abs(color))
else:
rgb_led_pin.duty_cycle = abs(color)
67 changes: 67 additions & 0 deletions examples/rgbled_esp32spi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import time
import board
import busio
from digitalio import DigitalInOut
import adafruit_rgbled
from adafruit_esp32spi import adafruit_esp32spi

# If you have an externally connected ESP32:
esp32_cs = DigitalInOut(board.D13)
esp32_ready = DigitalInOut(board.D11)
esp32_reset = DigitalInOut(board.D12)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

RED_LED = 26
GREEN_LED = 25
BLUE_LED = 27

# Create the RGB LED Object
led = adafruit_rgbled.RGBLED(RED_LED, GREEN_LED, BLUE_LED, esp)

def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if pos < 0 or pos > 255:
return 0, 0, 0
if pos < 85:
return int(255 - pos * 3), int(pos * 3), 0
if pos < 170:
pos -= 85
return 0, int(255 - pos * 3), int(pos * 3)
pos -= 170
return int(pos * 3), 0, int(255 - (pos * 3))

def rainbow_cycle(wait):
for i in range(255):
i = (i + 1) % 256
led.color = wheel(i)
time.sleep(wait)

while True:
# setting RGB LED color to RGB Tuples (R, G, B)
print('setting color 1')
led.color = (255, 0, 0)
time.sleep(1)

print('setting color 2')
led.color = (0, 255, 0)
time.sleep(1)

print('setting color 3')
led.color = (0, 0, 255)
time.sleep(1)

# setting RGB LED color to 24-bit integer values
led.color = 0xFF0000
time.sleep(1)

led.color = 0x00FF00
time.sleep(1)

led.color = 0x0000FF
time.sleep(1)

# rainbow cycle the RGB LED
rainbow_cycle(0.1)