Switched from poling to interrupts
This commit is contained in:
parent
ad49985a12
commit
0ac94b5b31
1 changed files with 27 additions and 14 deletions
33
main.py
33
main.py
|
@ -3,19 +3,32 @@
|
||||||
import RPi.GPIO as GPIO
|
import RPi.GPIO as GPIO
|
||||||
import time
|
import time
|
||||||
|
|
||||||
GPIO.setmode(GPIO.BCM)
|
|
||||||
|
|
||||||
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
def buttonEventHandler(value):
|
||||||
GPIO.setup(24, GPIO.OUT)
|
print "buton event!"
|
||||||
|
if value is 0:
|
||||||
try:
|
|
||||||
while True:
|
|
||||||
button_state = GPIO.input(23)
|
|
||||||
if button_state == False:
|
|
||||||
GPIO.output(24, True)
|
GPIO.output(24, True)
|
||||||
print('Button Pressed...')
|
print('Button Pressed...')
|
||||||
time.sleep(0.2)
|
|
||||||
else:
|
else:
|
||||||
GPIO.output(24, False)
|
GPIO.output(24, False)
|
||||||
except:
|
print('Button Released...')
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print "Let's do this!"
|
||||||
|
|
||||||
|
GPIO.setmode(GPIO.BCM)
|
||||||
|
|
||||||
|
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||||
|
GPIO.setup(24, GPIO.OUT)
|
||||||
|
#GPIO.add_event_detect(23, GPIO.FALLING)
|
||||||
|
#GPIO.add_event_callback(23, buttonEventHandler)
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
GPIO.wait_for_edge(23, GPIO.BOTH)
|
||||||
|
buttonEventHandler(GPIO.input(23))
|
||||||
|
except KeyboardInterrupt as e:
|
||||||
GPIO.cleanup()
|
GPIO.cleanup()
|
||||||
|
raise
|
||||||
|
|
||||||
|
if __name__== "__main__":
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in a new issue