在ESP8266/ESP32上,使用MicroPython可以非常容易的控制WS2812彩灯。通过neopixel模块,只要简单的几行代码就可以控制WS2812显示各种颜色。
- import neopixel
- import machine
- np = neopixel.NeoPixel(machine.Pin(2), 256)
- for i in range(256):
- np[i] = (i//16, 0, 0)
- np.write()
WS2812可以做成灯带,也可以做成彩屏。以16x16彩屏为例,它有16 x 16 = 256颗WS2812,这些LED是串联方式连接的,可以看成是一种特殊的灯带。它们的排列方式如下:
知道了排列方式,就可以用它来显示各种内容。16x16的彩屏虽然分辨率不高,但是也可以显示汉字:
- from machine import Pin
- import time
- from neo16x16 import neo16x16
- np = neo16x16(Pin(5))
- npdat=[
- 0x8040,0x0201,0x4042,0x4204,0x1042,0x4230,0x0342,0x7F00,
- 0x0342,0x4230,0x1042,0x4204,0x4042,0x0201,0x8040,0x0000,
- 0x4080,0x0202,0x4230,0x7842,0x4210,0x0842,0x4210,0xFFFE,
- 0x4210,0x0842,0x4210,0x0842,0x4210,0x0802,0x4000,0x0000,
- 0x0020,0x2410,0x4824,0x2421,0x7FFE,0xC440,0x4122,0x0402,
- 0x2020,0xFFC8,0x0C20,0x4428,0x222C,0x0582,0xF820,0x0000,
- 0x0810,0x0820,0x0310,0x0B00,0xFFFF,0x0900,0x0310,0x0000,
- 0x0810,0x0820,0x4310,0x0B01,0x7FFF,0x0800,0x0010,0x0000,
- 0x0800,0x0061,0x41C0,0x0004,0x3FE0,0x4012,0x4404,0x1842,
- 0x4100,0x0302,0x4030,0x301E,0x0040,0x0180,0x0E00,0x0000,
- 0x0404,0x2040,0x0104,0x2100,0xFFE4,0x3C90,0x0927,0x2490,
- 0x0924,0x2492,0x8924,0x27FE,0x0004,0x2000,0x0004,0x0000,
- 0x8000,0x0006,0x1FFE,0x4440,0x0222,0x4440,0x0222,0x7FFE,
- 0x0222,0x4440,0x4222,0x4441,0x7FFE,]
- n = 0
- while True:
- np.show(npdat, n)
- n = (n+16)%112
- time.sleep(500)
也可以显示小图案:
- from machine import Pin
- import time
- from neo16x16_img import neo16x16_img
- np = neo16x16_img(Pin(2))
- npdat=[
- 0x2AF3AF, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x6CF000, 0x39F19F,
- 0x06F000, 0x5CF29F, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x5CF000, 0x07F2AF, 0x00014E,
- 0x000000, 0x06F16E, 0x6CF2AF, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x6DF000, 0x06F29F, 0x00004E, 0x000000,
- 0x000000, 0x36E000, 0x07F05F, 0x5DF2AF,
- 0x0005EF, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x5CF000,
- 0x08F29F, 0x14A06F, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x26E000, 0x08F05F,
- 0x6CF19F, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x5B76CA, 0x000000, 0x08F4BC,
- 0x25D05F, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x26F000,
- 0x4B839F, 0x5A45A3, 0x0007C8, 0x000000,
- 0x8A7000, 0x5C85A6, 0x7B75B5, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x6A76B8, 0x6A75B7, 0x8A6695,
- 0x000000, 0x5A76C9, 0x8B8594, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x6DA8CA, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x121145, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x169156,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x234000,
- 0x15818B, 0x000217, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x129000, 0x0AE17B,
- 0x000169, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x19C301,
- 0x24709C, 0x00013A, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x116000, 0x169237,
- 0x24718B, 0x245169, 0x000000, 0x000000,
- 0x000000, 0x235000, 0x0CF09D, 0x1590AE,
- 0x159159, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x17C149,
- 0x09D18C, 0x0BF0BE, 0x23519C, 0x000234,
- 0x000000, 0x17B000, 0x16B15C, 0x14817C,
- 0x000024, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x002013,
- 0x012000, 0x11A126, 0x000116, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x048012,
- 0x16B149, 0x12716A, 0x000000, 0x000000,
- 0x000000, 0x12811B, 0x147247, 0x09E16A,
- 0x15B09D, 0x00010A, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x16C127, 0x0BE08D,
- 0x17A0BF, 0x18B09C, 0x13A17A, 0x000227,
- 0x214000, 0x0AE17A, 0x1680AE, 0x0AD235,
- 0x0BE0BF, 0x00009C, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x236235, 0x158246,
- 0x000245, 0x246312, 0x18B168, 0x200145,
- 0x122FFF, 0x000123, 0x000000, 0x000000,
- 0x000000, 0x235000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x9FF9FF,
- 0x9FF9FF, 0x0009FF, 0x000000, 0x000000,
- 0x000000, 0x9FF000, 0x0CF9FF, 0x0CF0CF,
- 0x0CF0CF, 0x9FF9FF, 0x000000, 0x000000,
- 0x000000, 0x9FF000, 0x0CF0CF, 0x0CF0CF,
- 0x0CF0CF, 0x0CF0CF, 0x9FF0CF, 0x000000,
- 0x9FF000, 0x0CF0CF, 0x0CF0CF, 0x0CF0CF,
- 0x0CF0CF, 0x0CF0CF, 0x9FF0CF, 0x000000,
- 0x000000, 0x0CF9FF, 0x0CF0CF, 0x0CF0CF,
- 0x0CF0CF, 0x0CF0CF, 0x0CF0CF, 0x0009FF,
- 0x0CF9FF, 0x0CF0CF, 0x0CF0CF, 0x0CF0CF,
- 0x0CF0CF, 0x0CF0CF, 0x0CF0CF, 0x0009FF,
- 0x9FF000, 0x0CF0CF, 0x0CF0CF, 0x0CF0CF,
- 0x0CF0CF, 0x0CF0CF, 0x0CF0CF, 0x9FF0CF,
- 0x0CF9FF, 0x0CF0CF, 0x0CF0CF, 0x0000FF,
- 0x0CF0FF, 0x0CF0CF, 0x0CF0CF, 0x0009FF,
- 0x9FF000, 0x0CF0CF, 0x0CF0CF, 0x0FF0CF,
- 0x0FF000, 0x0CF0CF, 0x0CF0CF, 0x9FF0CF,
- 0x0CF9FF, 0x0CF0CF, 0x0FF0CF, 0x000000,
- 0x0FF000, 0x0CF0CF, 0x0CF0CF, 0x0009FF,
- 0x000000, 0x0CF9FF, 0x0CF0CF, 0x0000FF,
- 0x000000, 0x0CF0FF, 0x0CF000, 0x0009FF,
- 0x9FF000, 0x0CF0CF, 0x0FF0CF, 0x000000,
- 0x0FF000, 0x0CF0CF, 0x9FF0CF, 0x000000,
- 0x000000, 0x9FF000, 0x0CF0CF, 0x0000FF,
- 0x000000, 0x0CF0FF, 0x9FF0CF, 0x000000,
- 0x000000, 0x9FF000, 0x0009FF, 0x000000,
- 0x000000, 0x9FF9FF, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0xEEE000, 0xFC9FC9,
- 0xFC9FC9, 0x000000, 0x000000, 0x000000,
- 0x000000, 0x000000, 0xFC9000, 0xFC9FC9,
- 0xFC9FC9, 0xEEEFC9, 0x000000, 0x000000,
- 0x000000, 0xAAA000, 0x555FC9, 0x000000,
- 0x333000, 0xEEEFC9, 0x000000, 0x000000,
- 0x000000, 0x000000, 0xFC9FC9, 0x000000,
- 0x000F90, 0xFC9000, 0x000FC9, 0x000000,
- 0x000000, 0xFC9000, 0x000FC9, 0xF99000,
- 0x000000, 0xFC9FC9, 0x000000, 0x000000,
- 0x000000, 0x000000, 0xFC9FC9, 0x000000,
- 0x000000, 0xFC9000, 0x000FC9, 0x000000,
- 0x000000, 0xFC9000, 0x000FC9, 0x000000,
- 0x000000, 0xFC9FC9, 0x000000, 0x000000,
- 0x000000, 0x000000, 0xFC9FC9, 0x000000,
- 0x000000, 0xFC9000, 0x000FC9, 0x000000,
- 0x000000, 0xFC9000, 0x000FC9, 0x000000,
- 0x000000, 0xFC9FC9, 0x000000, 0x000000,
- 0x000000, 0x000000, 0xFC9FC9, 0x000000,
- 0x000000, 0xFC9000, 0x000FC9, 0x000000,
- 0x000000, 0xFC9000, 0x000FC9, 0xF99000,
- 0x000000, 0xFC9FC9, 0x000000, 0x000000,
- 0x000000, 0x000000, 0xFC9FC9, 0x000000,
- 0x000F90, 0xFC9000, 0x000FC9, 0x000000,
- 0x000000, 0xBBB000, 0x333FC9, 0x000000,
- 0x111000, 0xEEEFC9, 0x000000, 0x000000,
- 0x000000, 0x000000, 0xFC9000, 0xFC9FC9,
- 0xFC9FC9, 0xFC9FC9, 0x000000, 0x000000,
- 0x000000, 0x000000, 0xFC9000, 0xFC9FC9,
- 0xFC9FC9, 0x000000, 0x000000, 0x000000,
- ]
- n = 0
- while 1:
- np.show(npdat, n)
- n = (n+16)%64
- time.sleep_ms(1000)
为了方便产生显示需要的数据,还编写了一个小软件,只要输入文字或者选择图片,就可以自动产生显示数据。然后用新的数据替换程序中的,就可以显示新的内容了。
https://github.com/shaoziyang/mi ... ls/microbit_toolbox
此内容由EEWORLD论坛网友dcexpert原创,如需转载或用于商业用途需征得作者同意并注明出处