-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonHandler.py
More file actions
68 lines (56 loc) · 2.39 KB
/
ButtonHandler.py
File metadata and controls
68 lines (56 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
# Classes to handle button presses
import time
import RPi.GPIO as GPIO
import config
class ButtonHandler(object):
def __init__(self):
pass
# wait_for_buttons()
# Argument 'buttons' can be one or more of these characters:
# 'l' - left button
# 'r' - right button
# 's' - select button
# If arg 'turn_off_after' is True, then all button LEDs will be switched off after button press
def wait_for_buttons(self, buttons, turn_off_after=True):
# Turn on the button LEDs
self.light_button_leds(buttons, True)
time.sleep(0.2) # Debounce
# Keep track of how long we have been waiting for a button press
self.start_time = time.time()
while True:
# Check the button states
if 's' in buttons and self.button_is_down(config.button_pin_select):
if turn_off_after:
self.light_button_leds(buttons, False)
return 's'
elif 'l' in buttons and self.button_is_down(config.button_pin_left):
if turn_off_after:
self.light_button_leds(buttons, False)
return 'l'
elif 'r' in buttons and self.button_is_down(config.button_pin_right):
if turn_off_after:
self.light_button_leds(buttons, False)
return 'r'
elif self.button_is_down(config.button_pin_exit):
return 'exit'
# If we've been waiting for a button press for longer than screen_saver_seconds secs
# then go into screen_saver mode.
if time.time() - self.start_time > config.screen_saver_seconds:
return 'screensaver'
def button_is_down(self, button_pin):
is_up = GPIO.input(button_pin)
return not is_up
def light_button_leds(self, buttons, turn_on):
if 's' in buttons:
GPIO.output(config.led_pin_select, turn_on)
if 'l' in buttons:
GPIO.output(config.led_pin_left, turn_on)
if 'r' in buttons:
GPIO.output(config.led_pin_right, turn_on)
def flash_button_leds(self, buttons, interval_secs, stop_event):
while not stop_event.is_set():
self.light_button_leds(buttons, False)
time.sleep(interval_secs)
self.light_button_leds(buttons, True)
time.sleep(interval_secs)