From 0e8bc288b8018c5a68df54b6989f8f70fc89f56f Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Thu, 7 Jan 2016 15:59:21 +0100 Subject: [PATCH] refined button handler --- Moped/moped.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/Moped/moped.py b/Moped/moped.py index d6ed352..4c71ce6 100755 --- a/Moped/moped.py +++ b/Moped/moped.py @@ -10,11 +10,19 @@ greenLed = 18 buttonPin = 27 -sleepTime = 0.25 +sleepTimes = [1, 0.5, 0.25, 0.125] +sleepTimeIndex = 0 def buttonHandler(channel): + global sleepTimeIndex print 'button pushed' + sleepTimeIndex = (sleepTimeIndex + 1) % len(sleepTimes) +def setLeds(yellow, red, green): + GPIO.output(yellowLed, yellow) + GPIO.output(redLed, red) + GPIO.output(greenLed, green) + try: GPIO.setmode(GPIO.BCM) GPIO.setup(yellowLed, GPIO.OUT) @@ -26,21 +34,12 @@ try: GPIO.add_event_detect(buttonPin, GPIO.FALLING, callback=buttonHandler, bouncetime=1000) while True: - GPIO.output(yellowLed, 1) - GPIO.output(redLed, 0) - GPIO.output(greenLed, 0) - sleep(sleepTime) - GPIO.output(yellowLed, 0) - GPIO.output(redLed, 1) - GPIO.output(greenLed, 0) - sleep(sleepTime) - GPIO.output(yellowLed, 0) - GPIO.output(redLed, 0) - GPIO.output(greenLed, 1) - sleep(sleepTime) -except KeyboardInterrupt: - GPIO.output(yellowLed, 0) - GPIO.output(redLed, 0) - GPIO.output(greenLed, 0) + setLeds(1, 0, 0) + sleep(sleepTimes[sleepTimeIndex]) + setLeds(0, 1, 0) + sleep(sleepTimes[sleepTimeIndex]) + setLeds(0, 0, 1) + sleep(sleepTimes[sleepTimeIndex]) finally: + setLeds(0, 0, 0) GPIO.cleanup()