-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpiohandler.py
267 lines (219 loc) · 9.31 KB
/
gpiohandler.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# This file is party of quPy, a program that allows to remotely control
# digital mixing consoles of Allen&Heath's Qu series from a RaspberryPi.
# Copyright (C) 2014-2015 Niels Ott
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# handler for communication with the GPIO interface of the Raspberry Pi
#
# TODO: this class uses fixedly named constants from config.py - which leads to shitloads of copy/paste code which is very ugly
#
from quevent import quEvent
import time
from mythreading import synchronized
import config
import RPi.GPIO as GPIO
from threading import Thread
from qumessagehandler import quMessageHandler
# class for letting a LED twinkle (blink once)
class ledTwinkler(object):
def __init__(self, ledPin):
self.ledPin = ledPin
@synchronized
def ledOn(self):
GPIO.output(self.ledPin, GPIO.HIGH)
@synchronized
def ledOff(self):
GPIO.output(self.ledPin, GPIO.LOW)
def run(self):
self.ledOn()
time.sleep(0.050)
self.ledOff()
def twinkle(self):
t = Thread(target=self.run)
t.start()
# class for letting an LED flash in a certain time interval
class ledFlasher(object):
def __init__(self, ledPin, time):
self.ledPin = ledPin
self.time = time
self.aborted = False
def stop(self):
self.aborted = True
def start(self):
tw = ledTwinkler(self.ledPin)
tw.twinkle()
t = Thread(target=self.run)
t.start()
def run(self):
while ( True ):
time.sleep(self.time)
if (self.aborted):
break
tw = ledTwinkler(self.ledPin)
tw.twinkle()
class gpioHandler(quMessageHandler):
shutdownState = False
onlineState = False
btn_held = {
config.BTN1_PIN : False,
config.BTN2_PIN : False,
config.BTN3_PIN : False,
config.BTN4_PIN : False }
btn_toggle = {
config.BTN1_ON_ACTION : False,
config.BTN2_ON_ACTION : False,
config.BTN3_ON_ACTION : False,
config.BTN4_ON_ACTION : False }
led_flashers = {
quEvent.DELAY_TIME_FX1 : None,
quEvent.DELAY_TIME_FX2 : None,
quEvent.DELAY_TIME_FX3 : None,
quEvent.DELAY_TIME_FX4 : None
}
tap_time_map = {
quEvent.DELAY_TIME_FX1 : quEvent.TAP_FX1,
quEvent.DELAY_TIME_FX2 : quEvent.TAP_FX2,
quEvent.DELAY_TIME_FX3 : quEvent.TAP_FX3,
quEvent.DELAY_TIME_FX4 : quEvent.TAP_FX4
}
def __init__(self, controller):
super(gpioHandler, self).__init__(controller)
# init GPIO interface
GPIO.setwarnings(False)
GPIO.cleanup()
# prepare IO modes
GPIO.setmode(GPIO.BOARD)
GPIO.setup(config.BTN1_PIN, GPIO.IN)
GPIO.setup(config.BTN2_PIN, GPIO.IN)
GPIO.setup(config.BTN3_PIN, GPIO.IN)
GPIO.setup(config.BTN4_PIN, GPIO.IN)
GPIO.setup(config.LED1_PIN, GPIO.OUT)
GPIO.setup(config.LED2_PIN, GPIO.OUT)
GPIO.setup(config.LED3_PIN, GPIO.OUT)
GPIO.setup(config.LED4_PIN, GPIO.OUT)
GPIO.setup(config.CONN_LED_PIN, GPIO.OUT)
# default LEDs to OFF
GPIO.output(config.LED1_PIN, GPIO.LOW)
GPIO.output(config.LED2_PIN, GPIO.LOW)
GPIO.output(config.LED3_PIN, GPIO.LOW)
GPIO.output(config.LED4_PIN, GPIO.LOW)
GPIO.output(config.CONN_LED_PIN, GPIO.LOW)
# generic methods for checking events that are related
# to buttons
def checkLedEventGeneric(self, event, ledPin, isToggle, onAction, offAction):
if (isToggle and event.getType() == onAction):
self.btn_toggle[onAction] = True
GPIO.output(ledPin, GPIO.HIGH)
if (isToggle and event.getType() == offAction):
self.btn_toggle[onAction] = False
GPIO.output(ledPin, GPIO.LOW)
# check the delay time events and set LED flashers up accordingly of necessary
def checkDelayTimeEvent(self, event, led_pin, action_set):
if ( action_set and event.getType() in self.led_flashers.keys() ):
flasher = self.led_flashers[event.getType()]
if (flasher != None):
flasher.stop()
flasher = ledFlasher(led_pin, event.getArguments()[0])
flasher.start()
self.led_flashers[event.getType()] = flasher
# switch on/off LEDs depending on event
def checkLedEvent(self,event):
# check events that relate to LED status
self.checkLedEventGeneric(event, config.LED1_PIN, (config.BTN1_MODE=="toggle"), config.BTN1_ON_ACTION, config.BTN1_OFF_ACTION)
self.checkLedEventGeneric(event, config.LED2_PIN, (config.BTN2_MODE=="toggle"), config.BTN2_ON_ACTION, config.BTN2_OFF_ACTION)
self.checkLedEventGeneric(event, config.LED3_PIN, (config.BTN3_MODE=="toggle"), config.BTN3_ON_ACTION, config.BTN3_OFF_ACTION)
self.checkLedEventGeneric(event, config.LED4_PIN, (config.BTN4_MODE=="toggle"), config.BTN4_ON_ACTION, config.BTN4_OFF_ACTION)
# check events that involve tap LEDs flashing
self.checkDelayTimeEvent(event, config.LED1_PIN, (config.BTN1_MODE=="oneshot" and event.getType() in self.tap_time_map.keys() and config.BTN1_ON_ACTION == self.tap_time_map[event.getType()]))
self.checkDelayTimeEvent(event, config.LED2_PIN, (config.BTN2_MODE=="oneshot" and event.getType() in self.tap_time_map.keys() and config.BTN2_ON_ACTION == self.tap_time_map[event.getType()]))
self.checkDelayTimeEvent(event, config.LED3_PIN, (config.BTN3_MODE=="oneshot" and event.getType() in self.tap_time_map.keys() and config.BTN3_ON_ACTION == self.tap_time_map[event.getType()]))
self.checkDelayTimeEvent(event, config.LED4_PIN, (config.BTN4_MODE=="oneshot" and event.getType() in self.tap_time_map.keys() and config.BTN4_ON_ACTION == self.tap_time_map[event.getType()]))
# handle network online/offline LED status
if (event.getType() == quEvent.NET_ONLINE):
GPIO.output(config.CONN_LED_PIN, GPIO.HIGH)
if (event.getType() == quEvent.NET_OFFLINE):
GPIO.output(config.CONN_LED_PIN, GPIO.LOW)
# handle incoming events and update display
@synchronized
def handleEvent(self, sender, event):
# always ignore events from myself to avoid loops,
# those of my own I need to react to come from ECO handler
if (self == sender):
return
# handle system events
if (event.getType() == quEvent.SYS_SHUTDOWN):
# stop LED flashers
for f in self.led_flashers.values():
if (f != None):
f.stop()
self.shutdownState = True
elif (event.getType() == quEvent.NET_ONLINE):
self.onlineState = True
elif (event.getType() == quEvent.NET_OFFLINE):
self.onlineState = False
# update LED status
self.checkLedEvent(event)
def getName(self):
return('GIO')
# generic method to poll the state of a button from the GPIO interface and take required action
def pollButton(self, buttonPin, isToggle, invert, onAction, offAction):
# button is pushed down and held
if ( (not self.btn_held[buttonPin]) and (( not GPIO.input(buttonPin) == GPIO.HIGH) and invert) ):
self.btn_held[buttonPin] = True
# if this is a not a toggle (so oneshot), or a toggle and ON is the required action,
# send event
if ((not isToggle) or (not self.btn_toggle[onAction])):
self.sendEvent(quEvent(onAction, []))
# otherwise, (toggle and OFF is required) send event
else:
self.sendEvent(quEvent(offAction, []))
#button is released
if ( (self.btn_held[buttonPin]) and (( not GPIO.input(buttonPin) == GPIO.LOW) and invert) ):
self.btn_held[buttonPin] = False
# switch on shutdown status LED
@synchronized
def setShutdownLEDs(self):
# switch off all button LEDs
if (config.BTN1_MODE != "off"):
GPIO.output(config.LED1_PIN, GPIO.LOW)
if (config.BTN2_MODE != "off"):
GPIO.output(config.LED2_PIN, GPIO.LOW)
if (config.BTN3_MODE != "off"):
GPIO.output(config.LED3_PIN, GPIO.LOW)
if (config.BTN4_MODE != "off"):
GPIO.output(config.LED4_PIN, GPIO.LOW)
# switch on shutdown LED
if ( config.SHUTDOWN_LED_PIN >= 0 ):
GPIO.output(config.SHUTDOWN_LED_PIN, GPIO.HIGH)
# --> this method should be run in a thread
def run(self):
while (True):
# quit on system shutdown
if (self.shutdownState):
self.setShutdownLEDs()
break
if (not self.onlineState):
continue
# do IO
if (config.BTN1_MODE != "off"):
self.pollButton(config.BTN1_PIN, (config.BTN1_MODE=="toggle"), config.INV1, config.BTN1_ON_ACTION, config.BTN1_OFF_ACTION)
if (config.BTN2_MODE != "off"):
self.pollButton(config.BTN2_PIN, (config.BTN2_MODE=="toggle"), config.INV2, config.BTN2_ON_ACTION, config.BTN2_OFF_ACTION)
if (config.BTN3_MODE != "off"):
self.pollButton(config.BTN3_PIN, (config.BTN3_MODE=="toggle"), config.INV3, config.BTN3_ON_ACTION, config.BTN3_OFF_ACTION)
if (config.BTN4_MODE != "off"):
self.pollButton(config.BTN4_PIN, (config.BTN4_MODE=="toggle"), config.INV4, config.BTN4_ON_ACTION, config.BTN4_OFF_ACTION)
# wait a bit
time.sleep(0.002)