当按下按钮时,Raspberry Pi Python暂停循环序列 [英] Raspberry Pi Python pause a loop sequence, when button pushed

查看:126
本文介绍了当按下按钮时,Raspberry Pi Python暂停循环序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个树莓派2.有了中继板,我可以用来进行开关序列(如交通信号灯). 我使用一个名为"webiopi"的工具在网站上创建按钮.单击该按钮后,以下python脚本的功能即会启动.

I have a raspberry PI 2. With a relay board, what i use to for a switch sequence (like a traffic light). I use a tool, called "webiopi" what create buttons on a website. When the button is clicked the function of the python script below is started.

我想要的是单击另一个按钮时跳出循环(或暂停它).但是,只要此循环正在运行,该工具就不会查看网页

What i want is to break out of the loop (or pause it) when another button is clicked. However, as long this loop is running, the tool don't look at the webpage

此处提出了类似的问题在python通过webiopi 进行设置,但这仅适用于单个事件,因此该解决方案在我的情况下不起作用.

A kind of similar question is asked here Exiting a continuous loop in python via webiopi but this is for a single event and the solution doesn't work in my case.

问题是.在循环运行时,如何使该脚本查看按钮上单击的按钮(也可以是gpio开关)

Question is. How can I make this script look at a button what is clicked (can be a gpio switch as well) while the loop is running

GPIO_nek=11 
GPIO_schouder=12
GPIO_rug1=8
GPIO_ONOFF=18
interval1 = 2
interval2 = 4
for x in range(0, 20):
    GPIO.digitalWrite(GPIO_nek, GPIO.LOW)
    time.sleep(interval1)
    GPIO.digitalWrite(GPIO_schouder, GPIO.LOW)
    time.sleep(interval1)
    GPIO.digitalWrite(GPIO_nek, GPIO.HIGH)
    time.sleep(interval1)
    GPIO.digitalWrite(GPIO_rug1, GPIO.LOW)
    time.sleep(interval2)
    GPIO.digitalWrite(GPIO_schouder, GPIO.HIGH)
    if (GPIO.digitalRead(GPIO_ONOFF) == GPIO.LOW):
        GPIO.digitalWrite(GPIO_ONOFF, GPIO.HIGH)
        break

推荐答案

如果按钮是问题末尾提到的GPIO开关,而不是网页按钮,则可以利用内置的GPIO中断该功能可为您的计算机节省持续轮询的资源:

If the button is the GPIO switch as you mentioned at the end of the question, instead of the webpage button, then you can make use of an inbuilt GPIO interrupt function that saves your computer the resouces of constant polling:

import RPi.GPIO as GPIO
from threading import Event  # We'll use it like time.sleep, but we can interrupt it.
GPIO_nek=11 
GPIO_schouder=12
GPIO_rug1=8
GPIO_ONOFF=18
interval1 = 2
interval2 = 4
GPIO.setup(GPIO_ONOFF, GPIO.IN, pull_up_down=GPIO.PUD_UP)

done = False  # loop control
timer = Event()
def quit_loop():  # Called by inbuilt threaded interrupt
    global done
    done = True
    timer.set()  # Interrupt the waiting

GPIO.add_event_detect(GPIO_ONOFF, GPIO.FALLING, callback=quit_loop, bouncetime=300) # Setup interrupt to call quit_loop

由于要使用它来打破循环,因此您希望将该循环缩短为一个进程:

Because you're using this to break out of a loop, you want to shorten that loop to a single process:

tasks = [
(GPIO_nek, GPIO.LOW, interval1),
(GPIO_schouder, GPIO.LOW, interval1),
(GPIO_nek, GPIO.HIGH, interval1),
(GPIO_rug1, GPIO.LOW, interval2),
(GPIO_schouder, GPIO.HIGH, 0) ]

for pin, level, interval in tasks * 20:  # Above you ran it 20 times, this notation keeps it in a single loop to break our o
    if not done:
        GPIO.digitalWrite(pin, level)
        timer.wait(interval)
    else:
        timer.clear()
        break

通过使用线程Event().wait()和.set()而不是标准time.sleep(),您甚至不必等待睡眠间隔结束.

By using the threading Event().wait() and .set() instead of the standard time.sleep() you won't even have to wait for the sleep interval to finish.

这篇关于当按下按钮时,Raspberry Pi Python暂停循环序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆