MicroPython ESP8266基于NeoPixel 驱动WS2812
- 相关篇《【MicroPython esp8266】固件烧写教程》
- ✨本案例基于Thonny平台开发。✨
- 固件版本信息:MicroPython
v1.19.1
on 2022-06-18; ESP module with ESP8266
- ESP8266可用管脚有:0、1、2、3、4、5、12、13、14、15、16,对应ESP8266芯片实际的GPIO管脚编号。
- NeoPixel库默认是已经打包到ESP8266 Python固件当中了,在使用时只需引入该模块名即可调用。
效果示例一
import machine
import neopixel
import time
red = (128, 0, 0)
green = (0, 128, 0)
# Turn off all the LEDs
def off(np):
for i in range(np.n):
np[i] = (0, 0, 0)
np.write()
def evens_green(np):
for i in range(np.n):
if i % 2 == 0:
np[i] = green
else:
np[i] = red
np.write()
def evens_red(np):
for i in range(np.n):
if i % 2 == 0:
np[i] = red
else:
np[i] = green
np.write()
def alternate_colors(np):
if np[0] == green:
evens_red(np)
else:
evens_green(np)
if __name__ == '__main__':
# Data line is on pin 13
# and there are 12 LEDs on our NeoPixel ring
np = neopixel.NeoPixel(machine.Pin(13), 8)
for i in range(20):
alternate_colors(np)
time.sleep(1)
# 关闭显示
off(np)
# do_connect()
# at this point we should start a cycle of colors
# and also start listening for any webrepl or api commands
效果示例二
import machine
import neopixel
import time
WIFI_SSID = "#######"
WIFI_PASS = "*******"
def do_connect():
import network
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect(secrets.WIFI_SSID, secrets.WIFI_PASS)
while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())
# cycle one LED around the circle
def cycle(np):
n = np.n
for i in range(4 * n):
for j in range(n):
np[j] = (0, 0, 0)
np[i % n] = (68, 125, 67)
np.write()
time.sleep_ms(25)
def bounce_blue(np):
n = np.n
for i in range(4 * n):
for j in range(n):
np[j] = (0, 0, 128)
if (i
np[i % n] = (0, 0, 0)
else:
np[n - 1 - (i % n)] = (0, 0, 0)
np.write()
time.sleep_ms(60)
# all the LEDs make a brighter/dimmer red with fad effect
def fade_red(np):
n = np.n
for i in range(0, 4 * 256, 8):
for j in range(n):
if (i
val = i & 0xff
else:
val = 255 - (i & 0xff)
np[j] = (val, 0, 0)
np.write()
# Turn off all the LEDs
def off(np):
n = np.n
for i in range(n):
np[i] = (0, 0, 0)
np.write()
# Make a nice red-orange glow
def fire(np, red=(240, 0, 0), orange=(240, 120, 0)):
for i in range(np.n):
if i % 2 == 0:
np[i] = red
else:
np[i] = orange
np.write()
# Calculate an orange based on the max and current brightness
def calc_orange(max_bright, current):
return (max_bright - current, (max_bright - current)
def flame(np):
max_brightness = 120
for f in range(4, max_brightness):
# Start with bright orange and dim red
red = (f, 0, 0)
orange = calc_orange(max_brightness, f)
fire(np, red, orange)
for f in range(max_brightness, 4, -1):
# Now orange LEDs are dim and the red ones are bright, so switch:
red = (f, 0, 0)
orange = calc_orange(max_brightness, f)
fire(np, red, orange)
if __name__ == '__main__':
# Data line is on pin 13
# and there are 8 LEDs on our NeoPixel ring
np = neopixel.NeoPixel(machine.Pin(13), 8)
do_connect()
# at this point we should start a cycle of colors
# and also start listening for any webrepl or api commands
for i in range(3):
flame(np)
time.sleep(1)
for i in range(3):
fade_red(np)
time.sleep(1)
# Blue
for i in range(3):
bounce_blue(np)
time.sleep(1)
for i in range(3):
cycle(np)
time.sleep(1)