我正在研究一个基于RPi Pico W的项目,我需要使用TLC5947 led驱动器。连接是SPI,我被告知是非常简单的,但我试图实现它自己,不能。Adafruit有一个circuitpython模块,但它似乎不能(至少直接)转换成micropython。
我需要继续研究自己制作还是已经制作了一个模块?
我的尝试:(我假设write
或pwmbuffer
是问题流。注释直接从Adafruit的c++版本的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
编辑:明白了。这个repo指的是这样一个文件夹结构:
project/
├── modules/
│ └──tlc5947-rgb-micropython/
│ ├──...
│ └──micropython.mk
└── micropython/
├──ports/
... ├──stm32/
...
但是我没有那样的东西。我的是:
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
通过Awesome MicroPython,我找到了https://gitlab.com/peterzuger/tlc5947-rgb-micropython -它看起来相当最新。