Estou tentando detectar quando um botão está inativo e por quanto tempo. Eu quero usar o botão para um passo "Próximo" iniciado pelo usuário, e se o botão for pressionado por três segundos, terminarei o programa. No entanto, o wait_for_edge fornece o erro de tempo de execução: Já existem eventos de detecção de borda conflitantes para este canal GPIO.
Existe uma solução melhor? TIA, Steve
Aqui está o código.
#!/usr/bin/env python2.7
import sys
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def myInterrupt(channel):
global buttonStatus
start_time = time.time()
GPIO.wait_for_edge(channel, GPIO.RISING)
buttonStatus = time.time() - start_time
# Setup the interrupt call when pin 26 goes low
GPIO.add_event_detect(26, GPIO.FALLING, callback=myInterrupt, bouncetime=300)
try:
quit = False
buttonStatus = 0
while not quit:
# Do stuff here, waiting for an interrupt.
sys.stdout.write("\r{}".format(time.ctime()))
sys.stdout.flush()
time.sleep(1)
# Did the button get pressed?
if buttonStatus != 0:
# Button was pressed.
buttonStatus = 0
print
print "Button pressed for {} seconds".format(buttonStatus)
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup() # clean up GPIO on normal exit
print
Tags python