背景
在电子森林上买了一片自制的RP2040板卡,上面的WS2812B电路引起了我的兴趣。具体电路如下
乍一看这个电路,很奇怪,三色灯串联是什么鬼,关键还是一个普通的GPIO去控制,这怎么保证写数据的过程中前面的灯不变,直到写完一起变?带着这个疑问,我跑去查看了WS2812B的datasheet。
WS2812B datasheet内容
查看datasheet时,发现了这么一段内容
看到这,我只能说,好家伙,这不就是类似于遥控器编码里的NEC编码的传输规则嘛(WS2812B把这个协议定义成NZR协议),逻辑是低电平0.4us高 + 0.85us低代表逻辑0,0.85us高 + 0.4us低代表逻辑1,超过50us的低电平表示数据传输完毕,新写入的数据可以使用。问题是,0.4us正负150ns,0.85us正负150ns,这要求也太过严格了吧,只有特定接口才能实现这种功能,普通的GPIO大概率是搞不定这种逻辑了。
RP2040 PIO管脚
上面已经看出,基本上普通IO口无法实现这么精确的时序调用了。因此查看RP2040此接口的定义,发现RP2040使用一叫PIO的模块实现这种精确的波形控制。
PIO是2040特定的模块,他的功能类似于FPGA,可以很好的控制IO口的时序,达到精确输出有严格时序要求波形的目的。这部分知识的具体链接如下:微雪树莓派PICO笔记——8-PIO(可编程输入输出接口)_pio接口-CSDN博客。看到这,基本上明白了这个控制逻辑的实现思路。
RP2040编程
WS2812B,其实github上已经有对应的额树莓派驱动了,因此不再从0开始实现,此处仅粘贴驱动部分的实现代码。
import array, time from machine import Pin import rp2 @rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=24) def ws2812(): T1 = 2 T2 = 5 T3 = 3 wrap_target() label("bitloop") out(x, 1) .side(0) [T3 - 1] jmp(not_x, "do_zero") .side(1) [T1 - 1] jmp("bitloop") .side(1) [T2 - 1] label("do_zero") nop() .side(0) [T2 - 1] wrap() #delay here is the reset time. You need a pause to reset the LED strip back to the initial LED #however, if you have quite a bit of processing to do before the next time you update the strip #you could put in delay=0 (or a lower delay) class ws2812b: def __init__(self, num_leds, state_machine, pin, delay=0.001): self.pixels = array.array("I", [0 for _ in range(num_leds)]) self.sm = rp2.StateMachine(state_machine, ws2812, freq=8000000, sideset_base=Pin(pin)) self.sm.active(1) self.num_leds = num_leds self.delay = delay self.brightnessvalue = 255 # Set the overal value to adjust brightness when updating leds def brightness(self, brightness = None): if brightness == None: return self.brightnessvalue else: if (brightness < 1): brightness = 1 if (brightness > 255): brightness = 255 self.brightnessvalue = brightness # Create a gradient with two RGB colors between "pixel1" and "pixel2" (inclusive) def set_pixel_line_gradient(self, pixel1, pixel2, left_red, left_green, left_blue, right_red, right_green, right_blue): if pixel2 - pixel1 == 0: return right_pixel = max(pixel1, pixel2) left_pixel = min(pixel1, pixel2) for i in range(right_pixel - left_pixel + 1): fraction = i / (right_pixel - left_pixel) red = round((right_red - left_red) * fraction + left_red) green = round((right_green - left_green) * fraction + left_green) blue = round((right_blue - left_blue) * fraction + left_blue) self.set_pixel(left_pixel + i, red, green, blue) # Set an array of pixels starting from "pixel1" to "pixel2" to the desired color. def set_pixel_line(self, pixel1, pixel2, red, green, blue): for i in range(pixel1, pixel2+1): self.set_pixel(i, red, green, blue) def set_pixel(self, pixel_num, red, green, blue): # Adjust color values with brightnesslevel blue = round(blue * (self.brightness() / 255)) red = round(red * (self.brightness() / 255)) green = round(green * (self.brightness() / 255)) self.pixels[pixel_num] = blue | red << 8 | green << 16 # rotate x pixels to the left def rotate_left(self, num_of_pixels): if num_of_pixels == None: num_of_pixels = 1 self.pixels = self.pixels[num_of_pixels:] + self.pixels[:num_of_pixels] # rotate x pixels to the right def rotate_right(self, num_of_pixels): if num_of_pixels == None: num_of_pixels = 1 num_of_pixels = -1 * num_of_pixels self.pixels = self.pixels[num_of_pixels:] + self.pixels[:num_of_pixels] def show(self): for i in range(self.num_leds): self.sm.put(self.pixels[i],8) time.sleep(self.delay) def fill(self, red, green, blue): for i in range(self.num_leds): self.set_pixel(i, red, green, blue) time.sleep(self.delay)
对于这段代码,有一份比较好的解释博文,虽然实现方法不一样,但其中讲解了此份代码所使用的rp2.StateMachine接口对应的功能:【雕爷学编程】MicroPython手册之 RP2040 特定端口库rp2.StateMachine-CSDN博客
亮灯程序编写
以下部分写入测试代码main.py中,上一节的驱动代码,写入的文件为ws2812b.py。
import time from ws2812b import ws2812b num_leds = 4 pixels = ws2812b(num_leds, 0, 23, delay=0) pixels.fill(10,10,10) pixels.show() pixels.brightness(200) while True: for i in range(num_leds): for j in range(num_leds): pixels.set_pixel(j,abs(i+j)%10,abs(i-(j+3))%10,abs(i-(j+6))%10) pixels.show() time.sleep(0.1)
验证效果
最终得到的现象为,电子森林自制的RP2040 pico板卡上的四颗灯珠交替变换颜色,四颗灯珠已经实现点亮。