-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbutton_main.py
executable file
·143 lines (112 loc) · 4.39 KB
/
button_main.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import logging.config
import yaml
import sys
logging.config.dictConfig(yaml.load(open('logging.yml', 'r')))
import argparse
from power import EdimaxPowerPlug, FixPowerSupply
from leds import LEDThread
from slider import SliderThread, UP, DOWN
try:
from evdev import InputDevice, categorize, ecodes, event
except ImportError:
# probably not running on a pi ...
pass
logger = logging.getLogger(__name__)
button_names = dict()
trigger_buttons = dict()
slider_buttons = dict();
def register_slider_buttons(downButton, upButton, sliderId):
slider_buttons[downButton] = (sliderId, DOWN);
slider_buttons[upButton] = (sliderId, UP);
def register_trigger_button(button, function):
trigger_buttons[button] = function;
def on_off():
if on_off_switch.is_on():
on_off_switch.off()
else:
on_off_switch.on()
def next_mode():
if on_off_switch.is_on():
led_thread.next_mode()
def light_up(step):
if on_off_switch.is_on():
led_thread.light_up(step)
def light_down(step):
if on_off_switch.is_on():
led_thread.light_down(step)
def next_color(step):
if on_off_switch.is_on():
led_thread.next_color(step)
def prev_color(step):
if on_off_switch.is_on():
led_thread.prev_color(step)
def log_button_event(event):
if not logger.isEnabledFor(logging.INFO):
return
if event.code in button_names:
button_name = button_names.get(event.code)
else:
button_name = "Unknown Button"
logger.info("Recognized " + button_name + ": " + str(categorize(event)))
def find_joystick(name):
all_found = []
try:
for i in range(10):
device_file = '/dev/input/event' + str(i)
device = InputDevice(device_file)
found = str(device)
if name in found:
logger.info("Found joystick: " + found)
return device
else:
all_found.append(found)
except:
logger.error("Did not find a joystick named '" + name + "'. Found the following devices:")
for found in all_found:
logger.error("\t" + found)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Starts button-based WS2801 controller')
parser.add_argument("-joy", required=True, metavar='joystick_name', help="Name of the joystick to use")
parser.add_argument("-edimax", required=False, nargs=2, metavar=('host', 'pass'), help="Use edimax power supply")
args = parser.parse_args()
joystick = find_joystick(args.joy)
if joystick is None:
sys.exit()
led_thread = LEDThread()
if args.edimax == None:
on_off_switch = FixPowerSupply(led_thread)
else:
on_off_switch = EdimaxPowerPlug(led_thread, args.edimax[0], 'admin', args.edimax[1])
button_names[288] = "On/Off Button"
button_names[290] = "Left Light Button"
button_names[294] = "Right Light Button"
button_names[295] = "Left Color Button"
button_names[298] = "Right Color Button"
button_names[299] = "Next Effect Button"
register_trigger_button(288, on_off) # button 1
register_slider_buttons(290, 294, 'light') # button 2, 3
register_slider_buttons(295, 298, 'color') # buttons 4, 5
register_trigger_button(299, next_mode) # button 6
slider_thread = SliderThread()
slider_thread.registerSlider('light', light_up, light_down)
slider_thread.registerSlider('color', next_color, prev_color)
led_thread.start()
slider_thread.start()
try:
for event in joystick.read_loop():
if event.type == ecodes.EV_KEY:
log_button_event(event)
if event.code in trigger_buttons and event.value == 1: # button pressed
trigger_buttons[event.code]();
elif event.code in slider_buttons:
if(event.value == 1): # button pressed
slider_thread.start_movement(*slider_buttons[event.code])
if(event.value == 0): # button released
slider_thread.stop_movement(*slider_buttons[event.code])
except KeyboardInterrupt:
logger.info('Shutting all down ...')
slider_thread.shutdown()
slider_thread.join()
led_thread.set_mode(1)
led_thread.shutdown()
led_thread.join()