[英]How to make a button press have different outputs depending on time pressed in Python?
I am attempting to learn how to program a little. 我正在尝试学习一些编程方法。 I am making this script for my Raspberry Pi, but the concept doesn't have to be specifically for it. 我正在为我的Raspberry Pi编写此脚本,但是该概念不必专门针对它。 I want to have a button press that will execute one of two commands: 我想按下一个按钮,它将执行以下两个命令之一:
If held for one second, then run command A If held for five seconds, then run command B It is okay for command A to run multiple times while waiting for B to register. 如果按住一秒钟,然后运行命令A如果按住五秒钟,然后运行命令B在等待B注册之前,命令A可以多次运行是可以的。
Here is my script, and afterwards I will explain its purpose: 这是我的脚本,然后我将解释其目的:
import RPi.GPIO as GPIO
import uinput
inport time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
def main():
events = (
uinput.KEY_ESC,
)
device = uinput.Device(events)
device.emit(uinput.KEY_ESC, 1) #Press
device.emit(uinput.KEY_ESC, 0) #Release
Then, here are the two things that I want to have added: 然后,这是我要添加的两件事:
while 1:
if time.sleep(1)
if GPIO.input(17)==True:
main()
break
while 1:
if time.sleep(10)
if GPIO.input(17)==True:
os.system("sudo reboot")
break
Essentially, this script will cause the button to have two purposes. 本质上,此脚本将导致按钮具有两个目的。 Pressing it for one second will emulate a keystroke of the key ESC. 按住一秒钟将模拟ESC键的击键。 Pressing it for ten seconds will cause it to reboot the system. 按下它十秒钟将导致它重新引导系统。 How can I have the two happen at the same time? 我怎样才能使两者同时发生? Learning Python is pretty challenging for me, but I've had no programming experience at all until now. 学习Python对我来说是一个很大的挑战,但是到目前为止,我还没有编程经验。
Though, I don't have experience with with Raspberry Pi, I have decided to answer you, since it look like you have been missing attention, from guru. 虽然,我没有使用Raspberry Pi的经验,但我还是决定回答您,因为您似乎已经失去了专家的关注。 With corresponding with this article Buttons and Switches , I think follwed code should work properly: 与本文的“ 按钮和开关”相对应,我认为下面的代码应该可以正常工作:
import os
import time
while True:
if GPIO.input(17):
#start counting pressed time
pressed_time=time.monotonic()
while GPIO.input(17): #call: is button still pressed
# just to force process wait
# may be possible use in this time.sleep(1) but I don't have confidence
pass
pressed_time=time.monotonic()-pressed_time
if pressed_time<5:
#send corresponding signal, you mentioned to call "main()", ok then:
main()
elif pressed_time>=5:
os.system("sudo reboot")
I can not speak for your special case but in general when you want a button to be pressed and measure the time. 我不能代表您的特殊情况,但总的来说,当您想要按下按钮并测量时间时。 Maybe it is of help. 也许有帮助。
The algorithm is in the comments and I sketch the Python equivalent: 该算法在注释中,我绘制了Python等效代码:
import time
while 1:
time.sleep(0.001) # do not use all the cpu power
# make a loop to test for the button being pressed
if button == pressed:
when_pressed = time.time()
while button == pressed:
# wait until the button is not pressed any more
time.sleep(0.001) # do not use all the cpu power
# measure the time
time_pressed = time.time() - when_pressed
if time_pressed < too_short:
continue # pressed too short do not use the other cases
if 1 < time_pressed < 10:
pass # pressed more than 1 second but less then 10
if time_pressed > 10:
pass # pressed more then 10 seconds
# a computer usually uses 6 seconds to wait for the shutdown
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.