I don’t know if this will work via the GPIO expander setup, but:
import time
import colorsys
import gpiozero
class Apa102(gpiozero.SPIDevice):
def __init__(self, *args, **kwargs):
gpiozero.SPIDevice.__init__(self, *args, **kwargs)
self._led_count = kwargs.get('led_count', 8)
self._led_values = [[0, 0, 0, 0] for _ in range(self._led_count)]
def set_pixel(self, x, r, g, b, brightness=1.0):
self._led_values[x] = [r, g, b, brightness]
def set_all(self, r, g, b, brightness=1.0):
for x in range(self._led_count):
self.set_pixel(x, r, g, b, brightness)
def show(self):
data = []
# Start Of Frame
for _ in range(4):
data.append(0)
for led in self._led_values:
r, g, b, brightness = led
brightness = max(0, min(0b11111, int(brightness * 0b11111)))
data.append(0b11100000 | brightness)
data.append(b & 0xff)
data.append(g & 0xff)
data.append(r & 0xff)
# End Of Frame
for _ in range(5):
data.append(0)
self._spi.transfer(data)
blinkt = Apa102(mosi_pin=23, clock_pin=24)
blinkt.set_pixel(0, 0, 0, 255)
blinkt.set_pixel(1, 0, 255, 0)
blinkt.set_pixel(2, 255, 0, 0)
blinkt.show()
time.sleep(1.0)
while True:
r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(time.time() / 10, 1.0, 1.0)]
blinkt.set_all(r, g, b, brightness=0.5)
blinkt.show()
time.sleep(1.0 / 60)