如何中断while循环,运行一个函数并返回循环

时间:2016-11-13 12:32:27

标签: python loops while-loop raspberry-pi3

我从Python和Raspberry Pi开始,并试图用它们进行一些物理计算。

我从那开始:https://www.raspberrypi.org/learning/physical-computing-with-python/一切顺利。

我试图玩交通灯(成功!): https://www.raspberrypi.org/learning/physical-computing-with-python/trafficlights/

这是我的代码:

from gpiozero import Button, TrafficLights
from time import sleep

lights = TrafficLights(25, 8, 7)

while True:
    lights.green.on()
    print("GREEN")
    sleep(12)
    lights.green.off()
    lights.amber.on()
    print("AMBER")
    sleep(4)
    lights.red.on()
    lights.amber.off()
    print("RED")
    sleep(12)
    lights.amber.on()
    print("RED & AMBER")
    sleep(4)
    lights.red.off()
    lights.amber.off()

我试图为行人过路处添加一个按钮。但在这里我遇到了问题。

这里是代码:

from gpiozero import Button, TrafficLights
from time import sleep

button = Button(21)
lights = TrafficLights(25, 8, 7)

def pedestrian_crossing():
    sleep(4)
    lights.off()
    lights.amber.on()
    print("Pedestrian crossing: AMBER")
    sleep(4)
    lights.red.on()
    lights.amber.off()
    print("Pedestrian crossing: RED")
    sleep(12)
    lights.amber.on()
    print("Pedestrian crossing: RED & AMBER")
    sleep(4)
    lights.red.off()
    lights.amber.off()

button.when_pressed = pedestrian_crossing

while True:
    lights.green.on()
    print("GREEN")
    sleep(12)
    lights.green.off()
    lights.amber.on()
    print("AMBER")
    sleep(4)
    lights.red.on()
    lights.amber.off()
    print("RED")
    sleep(12)
    lights.amber.on()
    print("RED & AMBER")
    sleep(4)
    lights.red.off()
    lights.amber.off()

我尝试使用button.wait_for_pressbutton.wait_for_release&但是,button.is_pressed是给我最好结果的那个。

问题是当我按下按钮时,会调用该函数,但循环继续。那么我怎么能重写代码呢?当我按下按钮时循环停止,函数被调用,函数内部的所有内容都完成后再返回循环?

还有其他解决方案,还有其他按钮属性?

提前致谢!

1 个答案:

答案 0 :(得分:0)

如下所示,按下按钮时不直接调用行人过路功能,设置变量指示行人过路按下触发器,然后在内部循环调用pedestrian_crossing功能,如果按下则返回到循环

在my_sleep函数中检查administrative_press var,这样每次my_sleep调用时都会检查while循环,这使得它几乎是1s延迟中断

pedestrian_press = False
def pedestrian_crossing():
    global pedestrian_press
    pedestrian_press = False # reset press
    sleep(4)
    lights.off()
    lights.amber.on()
    print("Pedestrian crossing: AMBER")
    sleep(4)
    lights.red.on()
    lights.amber.off()
    print("Pedestrian crossing: RED")
    sleep(12)
    lights.amber.on()
    print("Pedestrian crossing: RED & AMBER")
    sleep(4)
    lights.red.off()
    lights.amber.off()

def set_pedestrian_press():
    global pedestrian_press
    pedestrian_press = True

button.when_pressed = set_pedestrian_press


def my_sleep(n):
    # checking for pedestrian press done at 1 sec interval
    # to make it check at smaller interval
    # change to range(n*10) then sleep(0.1), for 0.1s interval
    for i in range(n):
        sleep(1)
        if pedestrian_press:  # check if pedestrian press
            pedestrian_crossing()

while True:
    lights.green.on()
    print("GREEN")
    my_sleep(12) # every second will check for pedestrian_press
    lights.green.off()
    lights.amber.on()
    print("AMBER")
    my_sleep(4) # every second will check for pedestrian_press
    lights.red.on()
    lights.amber.off()
    print("RED")
    my_sleep(12) # every second will check for pedestrian_press
    lights.amber.on()
    print("RED & AMBER")
    my_sleep(4) # every second will check for pedestrian_press
    lights.red.off()
    lights.amber.off()