Raspberry PI按钮推送监听器
是否有可能在Python中为Raspberry Pi使用按钮推送监听器。我有一个非锁定按钮进入GPIO。我想在第一次按下按钮时运行一些python代码。然后,我希望代码停止在第二个按钮上,而不管它在第一行代码中的位置。Raspberry PI按钮推送监听器
我使用了一个名为“旗帜”触发位变量注册按钮推动,但显然没有侦听器来确定第二推时作出。
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
Button = 16 # pin16
def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(Button, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Button Input
def motorcontrol():
flag=0
while True:
j=GPIO.input(16)
if j==1: #Robot is activated when button is pressed
flag=1
print "Robot Activated",j
while flag==1:
time.sleep(5)
print "Robot Activated for 5 seconds"
time.sleep(5)
print "Robot Activated for 10 seconds"
time.sleep(5)
print "Robot Activated for 15 seconds"
j=GPIO.input(16)
if j==1: #De activate robot on pushing the button
flag=0
print "Robot DeActivated",j
destroy()
def destroy():
GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
motorcontrol()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()
不能使用sleep()
这种方式,因为你的while
循环无法确认您的钮。你必须循环所有时间,并检查是否应该显示文本。
您可以使用小sleep()
使用酒糟CPU。
import time
current_time = time.time()
text_1 = current_time + 5
text_2 = current_time + 10
text_3 = current_time + 15
flag = True
while flag:
# TODO: check your button and change `flag`
current_time = time.time()
if text_1 and current_time >= text_1:
print("5 seconds")
text_1 = None # to stop displaying
# or show again after 5 seconds
#text_1 = current_time + 5
if text_2 and current_time >= text_2:
print("10 seconds")
text_2 = None # to stop displaying
if text_3 and current_time >= text_3:
print("15 seconds")
text_3 = None # to stop displaying
flag = False
#time.sleep(0.1)
还是更喜欢在大多数GUI
import time
# --- functions ---
def callback_1():
print("5 seconds")
# add new task to list
tasks.append((current_time + 5, callback_1))
def callback_2():
print("10 seconds")
def callback_3():
print("15 seconds")
def callback_4():
global flag
flag = False
# --- main ---
current_time = time.time()
tasks = []
tasks.append((current_time + 5, callback_1))
tasks.append((current_time + 10, callback_2))
tasks.append((current_time + 15, callback_3))
tasks.append((current_time + 17, callback_4))
flag = True
while flag:
# TODO: check your button
current_time = time.time()
# this way I execute task and remove from list
new_tasks = []
for t, c in tasks:
if current_time >= t:
c()
else:
new_tasks.append((t,c))
tasks = new_tasks
#time.sleep(0.1)
编辑:我没有RPI所以我尽量用自己的类GPIO
模拟 - 但也许它会在工作你的电脑。它显示你应该把代码放在哪里。
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
'''
#
# for test only - instead of `import RPi.GPIO as GPIO`
#
# simulate button press
current_time = time.time()
button_1 = current_time + 2
button_2 = current_time + 10
class GPIO:
BOARD = None
IN = None
PUD_DOWN = None
@staticmethod
def setmode(a):
pass
@staticmethod
def setup(a, b, pull_up_down=None):
pass
@staticmethod
def input(a):
global button_1, button_2
current_time = time.time()
if button_1 and current_time >= button_1:
button_1 = None
return 1
if button_2 and current_time >= button_2:
button_2 = None
return 1
return 0
@staticmethod
def cleanup():
pass
'''
Button = 16 # pin16
def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(Button, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Button Input
def motorcontrol():
flag = False
while True:
j = GPIO.input(16)
if j == 1:
flag = True
print "Robot Activated", j
current_time = time.time()
text_1 = current_time + 5
text_2 = current_time + 10
text_3 = current_time + 15
while flag:
j = GPIO.input(16)
if j == 1:
flag = False
print "Robot DeActivated", j
destroy()
current_time = time.time()
if text_1 and current_time >= text_1:
print "5 seconds"
text_1 = None # to stop displaying
# or show again after 5 seconds
#text_1 = current_time + 5
if text_2 and current_time >= text_2:
print "10 seconds"
text_2 = None # to stop displaying
if text_3 and current_time >= text_3:
print "15 seconds"
text_3 = None # to stop displaying
flag = False
time.sleep(0.1)
def destroy():
GPIO.cleanup()
if __name__ == '__main__':
setup()
try:
motorcontrol()
except KeyboardInterrupt:
destroy()
这是好东西。谢谢。然而,运行时的第一个代码同时生成所有文本行,而不等待5秒,因为while循环在我第一次单击该按钮(启动代码)之前不会启动,因此current_time总是大于text_3 。当我尝试更新当前时间和第一次按下按钮后,文本1,我得到以下错误“文本1 = CURRENT_TIME + 5类型错误:builtin_function_or_method和INT:为+不支持的操作类型” – Airfix
我的计划是先从程序按钮并让我的机器人重复行进一个方形路径,直到我再次按下按钮结束程序。我打算使用time.sleep来控制每个电机持续多长时间,因此上述测试代码片段。 – Airfix
错误可能意味着您忘记了函数中的()。 – furas
http://razzpisampler.oreilly.com/ch07.html – Natecat
您不能使用'sleep()',因为它会阻止您的代码。你可以这样做:开始设置'first_text = current_time + 5秒',然后在循环中检查'if current_time> = first_text:print“Robot Activated 5 seconds”' – furas