在micropython中显示事件之前的图像
Display image before event in micropython
我正在尝试使用 BBC:Microbit 在按下按钮 a 时在其 LED 上显示 1 秒的闪光。这有效,但我希望它在等待按下按钮时显示动画(待机)。下面的代码仅显示待机图像,而不会 运行 按下按钮 a 时的其余代码。我做错了什么?谢谢
from microbit import *
standby1 = Image("00000:"
"00000:"
"90000:"
"00000:"
"00000")
standby2 = Image("00000:"
"00000:"
"09000:"
"00000:"
"00000")
standby3 = Image("00000:"
"00000:"
"00900:"
"00000:"
"00000")
standby4 = Image("00000:"
"00000:"
"00090:"
"00000:"
"00000")
standby5 = Image("00000:"
"00000:"
"00009:"
"00000:"
"00000")
all_leds_on = Image("99999:"
"99999:"
"99999:"
"99999:"
"99999")
standby = [standby1, standby2, standby3, standby4, standby5, standby4, standby3, standby2]
display.show(standby, loop=True, delay=100)#Show standby LEDS on a loop
#Wait for button a to be pressed
while True:
if button_a.was_pressed():
sleep(1000)#pause program for 1 second
display.show(all_leds_on) #Turn on LEDS for 1 second
sleep(1000)#pause program for 1 second
display.clear()
microbit.display.show
的 documentation 说:
If loop
is True
, the animation will repeat forever.
因此,您需要编写自己的 Python for
或 while
循环来显示动画中的一帧,而不是使用 loop=True
,检查按钮是否被按下,如果是则退出循环。
您需要自己在此循环中添加时间延迟,并且您还需要弄清楚如何在显示最后一帧后返回第一帧 - 有不止一种方法可以做到。
正如 nekomatic 所说,替换 loop=True 是一个解决方案。请在下面找到一些示例代码。
事件处理程序将是处理按钮按下的更简洁的方法。 microbit 上的 micropython 实现缺少事件处理程序,例如 micropython 的完整实现。 pyboards有。 microbit 可用的 C 编译器中提供了事件处理程序。
from microbit import *
standby1 = Image("00000:"
"00000:"
"90000:"
"00000:"
"00000")
standby2 = Image("00000:"
"00000:"
"09000:"
"00000:"
"00000")
standby3 = Image("00000:"
"00000:"
"00900:"
"00000:"
"00000")
standby4 = Image("00000:"
"00000:"
"00090:"
"00000:"
"00000")
standby5 = Image("00000:"
"00000:"
"00009:"
"00000:"
"00000")
all_leds_on = Image("99999:"
"99999:"
"99999:"
"99999:"
"99999")
def flash_all():
''' Flash all LEDs on the display. '''
display.show(all_leds_on)
sleep(1000)
display.clear()
standby = [standby1, standby2, standby3, standby4, standby5,
standby4, standby3, standby2]
while True:
for image in standby:
if button_a.was_pressed():
flash_all()
display.show(image)
sleep(100)
我正在尝试使用 BBC:Microbit 在按下按钮 a 时在其 LED 上显示 1 秒的闪光。这有效,但我希望它在等待按下按钮时显示动画(待机)。下面的代码仅显示待机图像,而不会 运行 按下按钮 a 时的其余代码。我做错了什么?谢谢
from microbit import *
standby1 = Image("00000:"
"00000:"
"90000:"
"00000:"
"00000")
standby2 = Image("00000:"
"00000:"
"09000:"
"00000:"
"00000")
standby3 = Image("00000:"
"00000:"
"00900:"
"00000:"
"00000")
standby4 = Image("00000:"
"00000:"
"00090:"
"00000:"
"00000")
standby5 = Image("00000:"
"00000:"
"00009:"
"00000:"
"00000")
all_leds_on = Image("99999:"
"99999:"
"99999:"
"99999:"
"99999")
standby = [standby1, standby2, standby3, standby4, standby5, standby4, standby3, standby2]
display.show(standby, loop=True, delay=100)#Show standby LEDS on a loop
#Wait for button a to be pressed
while True:
if button_a.was_pressed():
sleep(1000)#pause program for 1 second
display.show(all_leds_on) #Turn on LEDS for 1 second
sleep(1000)#pause program for 1 second
display.clear()
microbit.display.show
的 documentation 说:
If
loop
isTrue
, the animation will repeat forever.
因此,您需要编写自己的 Python for
或 while
循环来显示动画中的一帧,而不是使用 loop=True
,检查按钮是否被按下,如果是则退出循环。
您需要自己在此循环中添加时间延迟,并且您还需要弄清楚如何在显示最后一帧后返回第一帧 - 有不止一种方法可以做到。
正如 nekomatic 所说,替换 loop=True 是一个解决方案。请在下面找到一些示例代码。
事件处理程序将是处理按钮按下的更简洁的方法。 microbit 上的 micropython 实现缺少事件处理程序,例如 micropython 的完整实现。 pyboards有。 microbit 可用的 C 编译器中提供了事件处理程序。
from microbit import *
standby1 = Image("00000:"
"00000:"
"90000:"
"00000:"
"00000")
standby2 = Image("00000:"
"00000:"
"09000:"
"00000:"
"00000")
standby3 = Image("00000:"
"00000:"
"00900:"
"00000:"
"00000")
standby4 = Image("00000:"
"00000:"
"00090:"
"00000:"
"00000")
standby5 = Image("00000:"
"00000:"
"00009:"
"00000:"
"00000")
all_leds_on = Image("99999:"
"99999:"
"99999:"
"99999:"
"99999")
def flash_all():
''' Flash all LEDs on the display. '''
display.show(all_leds_on)
sleep(1000)
display.clear()
standby = [standby1, standby2, standby3, standby4, standby5,
standby4, standby3, standby2]
while True:
for image in standby:
if button_a.was_pressed():
flash_all()
display.show(image)
sleep(100)