继续 运行 循环,直到使用 gpiozero 按下按钮 - 按钮按下未注册
Continue running a loop until a button is pressed using gpiozero - button press not registered
我在 raspberry pi 上有一些 python 代码,我想 运行 执行循环直到按下按钮。
button.wait_for_press() 不合适,因为它会暂停程序直到 运行s,但我已经尝试过看硬件是否正常工作,确实如此。
def shutterPressed():
global shutterHasBeenPressed
shutterHasBeenPressed = True
def main():
"""
Main program loop
"""
#start camera preview
camera.start_preview(resolution=(SCREEN_W, SCREEN_H))
#Display Intro screens
intro_image_1 = REAL_PATH + '/assets/intro_1.png'
intro_image_2 = REAL_PATH + '/assets/intro_2.png'
overlay_1 = overlay_image(intro_image_1, 0, 3)
overlay_2 = overlay_image(intro_image_2, 0, 4)
#Wait for button press
i = 0
blink_speed = 10
button.when_pressed = shutterPressed
while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")
当我运行这段代码时,根本没有注册按钮按下。
我错了什么?
编辑:所以我在 shutterPressed() 函数中添加了一个打印语句,并确认按下按钮时它是 运行ning。
In 还在 if 语句之前添加了一条语句来打印 shutterHasBeenPressed 的值。这从未改变过。
但是,如果我删除了在循环开始时将变量更改为 false 的行,那么代码就可以工作,所以当各个位得到 运行 时,这显然与此有关。也许 while 循环在 shutterPressed() 函数 运行s?
之后再次开始
无论哪种方式,我都通过将变量的重新分配移动到 if 语句之后来修复它。
欢迎来到并发世界(有点)!
想象一下您的程序 运行:指令按照它们编写的顺序一个接一个地执行,根据它们定义的执行流程,例外情况 的 shutterPressed
是异步执行的 (maybe).
因此,假设我们进入循环并在第一行,<here>
:
while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False # <here>
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")
现在,shutterHasBeenPressed
已经设置为False
,后面的条件也得到验证,所以我们输入if
.
程序一直保持 运行,直到意外地按下按钮。比如说,它达到了 <here>
:
while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255 # <here>
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")
此时,shutterPressed
运行,将 shutterHasBeenPressed
设置为 True
。然后,回到我们的循环中,迭代结束,我们 continue
在循环的开始......那里有什么?!
shutterHasBeenPressed = False
按钮的按下完全没有引起注意!
我相信这可以回答您提出问题的问题。
我在 raspberry pi 上有一些 python 代码,我想 运行 执行循环直到按下按钮。
button.wait_for_press() 不合适,因为它会暂停程序直到 运行s,但我已经尝试过看硬件是否正常工作,确实如此。
def shutterPressed():
global shutterHasBeenPressed
shutterHasBeenPressed = True
def main():
"""
Main program loop
"""
#start camera preview
camera.start_preview(resolution=(SCREEN_W, SCREEN_H))
#Display Intro screens
intro_image_1 = REAL_PATH + '/assets/intro_1.png'
intro_image_2 = REAL_PATH + '/assets/intro_2.png'
overlay_1 = overlay_image(intro_image_1, 0, 3)
overlay_2 = overlay_image(intro_image_2, 0, 4)
#Wait for button press
i = 0
blink_speed = 10
button.when_pressed = shutterPressed
while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")
当我运行这段代码时,根本没有注册按钮按下。
我错了什么?
编辑:所以我在 shutterPressed() 函数中添加了一个打印语句,并确认按下按钮时它是 运行ning。
In 还在 if 语句之前添加了一条语句来打印 shutterHasBeenPressed 的值。这从未改变过。
但是,如果我删除了在循环开始时将变量更改为 false 的行,那么代码就可以工作,所以当各个位得到 运行 时,这显然与此有关。也许 while 循环在 shutterPressed() 函数 运行s?
之后再次开始无论哪种方式,我都通过将变量的重新分配移动到 if 语句之后来修复它。
欢迎来到并发世界(有点)!
想象一下您的程序 运行:指令按照它们编写的顺序一个接一个地执行,根据它们定义的执行流程,例外情况 的 shutterPressed
是异步执行的 (maybe).
因此,假设我们进入循环并在第一行,<here>
:
while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False # <here>
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")
现在,shutterHasBeenPressed
已经设置为False
,后面的条件也得到验证,所以我们输入if
.
程序一直保持 运行,直到意外地按下按钮。比如说,它达到了 <here>
:
while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255 # <here>
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")
此时,shutterPressed
运行,将 shutterHasBeenPressed
设置为 True
。然后,回到我们的循环中,迭代结束,我们 continue
在循环的开始......那里有什么?!
shutterHasBeenPressed = False
按钮的按下完全没有引起注意!
我相信这可以回答您提出问题的问题。