modules.ws2812 (WS2812 light strip)
This module uses the I2S
of K210
to drive the module, so you need to pay attention to whether there is conflict during use
Currently supports up to 12 street light strips
Constructor
from modules import ws2812
class ws2812(led_pin=-1,led_num=-1,i2s_num=I2S_DEVICE_2,i2s_chn=I2S_CHANNEL_3,i2s_dma_chn=DMAC_CHANNEL1)
Create a new ws2812
object by specifying parameters
Parameters
led_pin
: The pin connected to the light strip data line, such asboard_info.D[4]
led_num
: How many lamp beads are in the stripi2s_num
: WhichI2S
device the object uses to drive, the default isI2S_DEVICE_2
, the value range is0-2
i2s_chn
: WhichI2S
channel the object uses, the default isI2S_CHANNEL_3
, and the value range is0-3
i2s_dma_chn
: DMA channel used by the object, users generally do not consider
Method
set_led
Set the color of a certain led light
class_ws2812.set_led(num, color)
Parameters
num
: theN
lamp beads, starting from0
color
: the assigned color of the lamp bead, which is oftuple
type, (R,G,B)
return value
no
display
Start working, call after setting
class_ws2812.display()
Parameters
no
return value
no
Routine 0
All 30 LED lights are red
from modules import ws2812
class_ws2812 = ws2812(board_info.D[4],30)
for i in range(30):
class_ws2812.set_led(i,(0xff,0,0))
class_ws2812.display()
Routine 1
Light with red gradient
from modules import ws2812
class_ws2812 = ws2812(board_info.D[4],30)
r=0
dir = True
while True:
if dir:
r += 1
else:
r -= 1
if r>=255:
r = 255
dir = False
elif r<0:
r = 0
dir = True
for i in range(30):
a = class_ws2812.set_led(i,(r,0,0))
a=class_ws2812.display()
For the above example, see
MaixPy_scripts