pboard controlling WS2801 ledstrip over SPI
Posted: Tue Feb 02, 2016 11:22 am
Has anyone experience controlling an Adafruit WS2801 led strip over SPI using the micro python on the pyboard?
Thanks
Floris
Thanks
Floris
Please see the new forum at
https://forum.micropython.org/
Code: Select all
N_LED = const(100)
DEFAULT_COLOR = {'r': 0xff, 'g': 0xA0, 'b': 0xA0}
class WS2801:
def __init__(self, num_led, spi):
self.NUM_LED = num_led
self._databuffer = bytearray(3*num_led)
self._spi = spi
# Check if baud frequency adequate.
def set_led(self, num, r=0x00, g=0x00, b=0x00):
if num < self.NUM_LED:
self._databuffer[num*3] = r
self._databuffer[num*3+1] = b
self._databuffer[num*3+2] = g
else:
print("Command ignored. LED out of index!")
def write(self, clear=False):
self._spi.write(self._databuffer)
if clear:
self.clear()
def clear(self, default=0x00):
for i in range(len(self._databuffer)):
self._databuffer[i] = default
def set_all(self, r=0xff, g=0xff, b=0xff):
for i in range(self.NUM_LED):
self.set_led(num=i, r=r, g=g, b=b)
hspi = SPI(1, baudrate=int(250e3), sck=Pin(14), mosi=Pin(13), miso=Pin(12))
print("Initialize WS2801 handler")
ws_handler = WS2801.WS2801(spi=hspi, num_led=N_LED)
print("Test LEDs")
ws_handler.clear()
ws_handler.write()
for i in range(N_LED):
ws_handler.clear()
ws_handler.set_led(num=i, **DEFAULT_COLOR)
ws_handler.write()
time.sleep_ms(100)
ws_handler.clear()
ws_handler.write()