【发布时间】:2016-04-13 23:18:59
【问题描述】:
我的 Python 代码当前正在循环,在我希望它重新循环之前,我真的希望能够检测到两个按钮之一。根据按下的按钮,应该执行代码的不同部分。
对于一个按钮,如下所示:
while True:
time.sleep(0.2)
GPIO.wait_for_edge(button_1, GPIO.FALLING)
run_button_1_code()
我怎样才能为两个按钮做到这一点?我在想一些很长的事情:
while True:
time.sleep(0.2)
GPIO.wait_for_edge(button_1, button_2, GPIO.FALLING)
if button_1 is pressed:
run_button_1_code()
elif button_2 is pressed:
run_button_2_code()
或者也许:
def button_1():
GPIO.remove_event_detect(button1)
print "doing my code here"
GPIO.add_event_detect(button1, GPIO.BOTH, callback=button_1, bouncetime=800)
def button_2():
GPIO.remove_event_detect(button2)
print "doing my code here"
GPIO.add_event_detect(button2, GPIO.BOTH, callback=button_2, bouncetime=800)
While True:
time.sleep(0.05)
print "waiting for button"
我想不出任何其他选择.. 请指教!
【问题讨论】:
标签: python