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 as board_info.D[4]

    • led_num: How many lamp beads are in the strip

    • i2s_num: Which I2S device the object uses to drive, the default is I2S_DEVICE_2, the value range is 0-2

    • i2s_chn: Which I2S channel the object uses, the default is I2S_CHANNEL_3, and the value range is 0-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: the N lamp beads, starting from 0

    • color: the assigned color of the lamp bead, which is of tuple 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