refined button handler
This commit is contained in:
parent
34e18d2748
commit
0e8bc288b8
1 changed files with 16 additions and 17 deletions
|
@ -10,11 +10,19 @@ greenLed = 18
|
||||||
|
|
||||||
buttonPin = 27
|
buttonPin = 27
|
||||||
|
|
||||||
sleepTime = 0.25
|
sleepTimes = [1, 0.5, 0.25, 0.125]
|
||||||
|
sleepTimeIndex = 0
|
||||||
|
|
||||||
def buttonHandler(channel):
|
def buttonHandler(channel):
|
||||||
|
global sleepTimeIndex
|
||||||
print 'button pushed'
|
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:
|
try:
|
||||||
GPIO.setmode(GPIO.BCM)
|
GPIO.setmode(GPIO.BCM)
|
||||||
GPIO.setup(yellowLed, GPIO.OUT)
|
GPIO.setup(yellowLed, GPIO.OUT)
|
||||||
|
@ -26,21 +34,12 @@ try:
|
||||||
GPIO.add_event_detect(buttonPin, GPIO.FALLING, callback=buttonHandler, bouncetime=1000)
|
GPIO.add_event_detect(buttonPin, GPIO.FALLING, callback=buttonHandler, bouncetime=1000)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
GPIO.output(yellowLed, 1)
|
setLeds(1, 0, 0)
|
||||||
GPIO.output(redLed, 0)
|
sleep(sleepTimes[sleepTimeIndex])
|
||||||
GPIO.output(greenLed, 0)
|
setLeds(0, 1, 0)
|
||||||
sleep(sleepTime)
|
sleep(sleepTimes[sleepTimeIndex])
|
||||||
GPIO.output(yellowLed, 0)
|
setLeds(0, 0, 1)
|
||||||
GPIO.output(redLed, 1)
|
sleep(sleepTimes[sleepTimeIndex])
|
||||||
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)
|
|
||||||
finally:
|
finally:
|
||||||
|
setLeds(0, 0, 0)
|
||||||
GPIO.cleanup()
|
GPIO.cleanup()
|
||||||
|
|
Loading…
Reference in a new issue