I'm working on a RPi Pico W based project, and I need to use a TLC5947 led driver. The connection is SPI, which I'm told is pretty simple, but I tried to implement it myself and couldn't. Adafruit has a circuitpython module, but it dosen't seem to translate (directly at least) into micropython.
Do I need to keep researching making it myself or is there a module already made?
My attempt: (I assume write
or pwmbuffer
is the problem fwiw. Comments are copied directly from Adafruit's C++ version of the library for arduino.)
from machine import Pin
import machine
class TLC5947:
def __init__(self, clock: int = 2, data: int = 3, latch: int = 5):
self.numdrivers = 1
self.data = Pin(data, Pin.OUT)
self.clock = Pin(clock, Pin.OUT)
self.latch = Pin(latch, Pin.OUT)
self.latch.low()
self._spi = machine.SPI(0)
# self.OE = OE
self.pwmbuffer = [0] * (24 * 2 * self.numdrivers) # memset(pwmbuffer, 0, 2 * 24 * n);
# self.spi = machine.SPI(0)
def write(self):
self.latch.low() # digitalWrite(_lat, LOW);
# // 24 channels per TLC5974
for c in range(24 * self.numdrivers - 1, -1, -1): # for (int16_t c = 24 * numdrivers - 1; c >= 0; c--) {
# // 12 bits per channel, send MSB first
for b in range(11, -1, -1): # for (int8_t b = 11; b >= 0; b--) {
self.clock.low() # digitalWrite(_clk, LOW);
if self.pwmbuffer[c] & (1 << b): # if (pwmbuffer[c] & (1 << b))
self.data.high() # digitalWrite(_dat, HIGH);
else: # else
self.data.low() # digitalWrite(_dat, LOW);
#
self.clock.high() # digitalWrite(_clk, HIGH);
# }
# }
self.clock.low() # digitalWrite(_clk, LOW);
self.latch.high() # digitalWrite(_lat, HIGH);
self.latch.low() # digitalWrite(_lat, LOW);
def setLed(self, lednum, r,g,b):
self.setPWM(lednum * 3, r)
self.setPWM(lednum * 3 + 1, g)
self.setPWM(lednum * 3 + 2, b)
def setPWM(self, chan: int, pwm: int):
if (pwm > 4095):
pwm = 4095
try:
self.pwmbuffer[chan] = pwm
except:
pass
Edit: Got it. That repo refers to a folder structure like this:
project/
├── modules/
│ └──tlc5947-rgb-micropython/
│ ├──...
│ └──micropython.mk
└── micropython/
├──ports/
... ├──stm32/
...
But I don't have anything like that. Mine is:
project/
|_ .vscode/
| |_ ...
|_ lib/
|_ code.py
|_ i2c_display.py
|_ tlc5947_ME.py
|_ .picowgo # Used by Pico-W-Go vscode extention to allow Pico programming in vscode