这是我使用的代码 | https://makersportal.com/blog/ws2812-ring-light-with-raspberry-pi-pico |首先,这工作得很好,但是在没有问题地停止并使用 thonny 多次运行代码后,我收到了错误:
“操作系统错误:[Errno 12] ENOMEM”
我使用 nuke.uf2 文件清除了 pi pico W 上的所有存储,但是在运行该文件太多次后,该错误很快再次出现。我非常困惑,希望得到一些帮助。
谢谢!
我尝试使用 nuke.uf2 这是一个临时修复
###############################################################
# WS2812 RGB LED Ring Light Breathing
# with the Raspberry Pi Pico Microcontroller
#
# by Joshua Hrisko, Maker Portal LLC (c) 2021
#
# Based on the Example neopixel_ring at:
# https://github.com/raspberrypi/pico-micropython-examples
###############################################################
#
import array, time
from machine import Pin
import rp2
#
############################################
# RP2040 PIO and Pin Configurations
############################################
#
# WS2812 LED Ring Configuration
led_count = 12 # number of LEDs in ring light
PIN_NUM = 1 # pin connected to ring light
brightness = 0.2 # 0.1 = darker, 1.0 = brightest
@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT,
autopull=True, pull_thresh=24) # PIO configuration
# define WS2812 parameters
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()
# Create the StateMachine with the ws2812 program, outputting on pre-defined pin
# at the 8MHz frequency
sm = rp2.StateMachine(0, ws2812, freq=8_000_000, sideset_base=Pin(PIN_NUM))
# Activate the state machine
sm.active(1)
# Range of LEDs stored in an array
ar = array.array("I", [0 for _ in range(led_count)])
#
############################################
# Functions for RGB Coloring
############################################
#
def pixels_show(brightness_input=brightness):
dimmer_ar = array.array("I", [0 for _ in range(led_count)])
for ii,cc in enumerate(ar):
r = int(((cc >> 8) & 0xFF) * brightness_input) # 8-bit red dimmed to brightness
g = int(((cc >> 16) & 0xFF) * brightness_input) # 8-bit green dimmed to brightness
b = int((cc & 0xFF) * brightness_input) # 8-bit blue dimmed to brightness
dimmer_ar[ii] = (g<<16) + (r<<8) + b # 24-bit color dimmed to brightness
sm.put(dimmer_ar, 8) # update the state machine with new colors
time.sleep_ms(10)
def pixels_set(i, color):
ar[i] = (color[1]<<16) + (color[0]<<8) + color[2] # set 24-bit color
def breathing_led(color):
step = 5
breath_amps = [ii for ii in range(0,255,step)]
breath_amps.extend([ii for ii in range(255,-1,-step)])
for ii in breath_amps:
for jj in range(len(ar)):
pixels_set(jj, color) # show all colors
pixels_show(ii/255)
time.sleep(0.02)
#
############################################
# Main Calls and Loops
############################################
#
# color specifications
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
yellow = (255,255,0)
cyan = (0,255,255)
white = (255,255,255)
blank = (0,0,0)
colors = [blue,yellow,cyan,red,green,white]
while True: # loop indefinitely
for color in colors: # emulate breathing LED (similar to Amazon's Alexa)
breathing_led(color)
time.sleep(0.1) # wait between colors
我自己就被这个绊倒了。
快速解决方法是在 Thonny 控制台中输入
machine.reset()
(您可能需要先输入 import machine
)。
问题在于初始化状态机的行。您可以运行它而不会出现错误的次数取决于PIO代码(
ws2812()
函数)的长度,所以我假设由于某种原因它没有重置只能容纳32条指令的PIO内存。
您的代码有 4 个 PIO 指令,在我的测试中,它在第 9 次运行时失败,所以加起来。
这似乎是 Pico W 特有的,因为同一 MicroPython nightly 构建上的完全相同的代码在非 W Pico 上运行良好。
编辑:问题在这里 - MicroPython 不会重置 Pico W 上的 PIO 内存,因为它会干扰无线驱动程序。
编辑:另一种解决方案是将
rp2.PIO(0).remove_program()
放在rp2.StateMachine
行之前,以清除PIO内存。
使用
gc
模块:
import gc
gc.collect()