Has anyone experience controlling an Adafruit WS2801 led strip over SPI using the micro python on the pyboard?
Thanks
Floris
pboard controlling WS2801 ledstrip over SPI
-
- Posts: 16
- Joined: Tue Feb 02, 2016 11:20 am
Re: pboard controlling WS2801 ledstrip over SPI
Works pretty much out of the box. I wrote this code for mine:
Good luck.
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()
Re: pboard controlling WS2801 ledstrip over SPI
Also, you'll probably have to cheat to get to the 5V logic level: https://hackaday.com/2017/01/20/cheatin ... -data-line
Re: pboard controlling WS2801 ledstrip over SPI
Thanks for the code, razanur! I hope you don't mind me using and sharing your code (with credits) on a small project. If anyone is interested, you can see the PC and ESP32 code on Github:
Monitor Ring Light - A custom ring light around a monitor, providing uniform lighting and custom color toning for video recordings and video meetings. Made with RGB LED Pixels (WS2801), ESP32, MicroPython and Dear PyGUI.
Monitor Ring Light - A custom ring light around a monitor, providing uniform lighting and custom color toning for video recordings and video meetings. Made with RGB LED Pixels (WS2801), ESP32, MicroPython and Dear PyGUI.