-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio.py
316 lines (284 loc) · 13.5 KB
/
gpio.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import RPi.GPIO as GPIO
import time
from threading import Thread
import config.main as config
class Outputs:
LOCK_CHANNEL = config.lock['lock_gpio']
LOCK_OPEN_STATE = GPIO.HIGH if config.lock['lock_reversed'] else GPIO.LOW
LOCK_CLOSE_STATE = GPIO.LOW if config.lock['lock_reversed'] else GPIO.HIGH
GREEN_LED_CHANNEL = config.led['green_gpio']
GREEN_LED_ON_STATE = GPIO.LOW if config.led['green_reversed'] else GPIO.HIGH
GREEN_LED_OFF_STATE = GPIO.HIGH if config.led['green_reversed'] else GPIO.LOW
RED_LED_CHANNEL = config.led['red_gpio']
RED_LED_ON_STATE = GPIO.LOW if config.led['red_reversed'] else GPIO.HIGH
RED_LED_OFF_STATE = GPIO.HIGH if config.led['red_reversed'] else GPIO.LOW
singleton = None
leds_thread = None
class _Outputs:
def open_door(self):
GPIO.output(Outputs.LOCK_CHANNEL, Outputs.LOCK_OPEN_STATE)
time.sleep(config.lock['opening_time'])
GPIO.output(Outputs.LOCK_CHANNEL, Outputs.LOCK_CLOSE_STATE)
def control_led(self, type, frequency=0.1, number_of_blinks=-1, duration=-1):
if Outputs.leds_thread is not None:
Outputs.leds_thread.stop()
Outputs.leds_thread.join()
Outputs.leds_thread = LEDs()
Outputs.leds_thread.set_params(type, frequency, number_of_blinks, duration)
Outputs.leds_thread.start()
def __enter__(self):
if Outputs.singleton is None:
GPIO.setmode(GPIO.BOARD)
GPIO.setup(Outputs.LOCK_CHANNEL, GPIO.OUT)
GPIO.output(Outputs.LOCK_CHANNEL, Outputs.LOCK_CLOSE_STATE)
GPIO.setup(Outputs.GREEN_LED_CHANNEL, GPIO.OUT)
GPIO.output(Outputs.GREEN_LED_CHANNEL, GPIO.LOW)
GPIO.setup(Outputs.RED_LED_CHANNEL, GPIO.OUT)
GPIO.output(Outputs.RED_LED_CHANNEL, GPIO.LOW)
Outputs.singleton = Outputs._Outputs()
return Outputs.singleton
def __exit__(self, exc_type, exc_val, exc_tb):
GPIO.cleanup()
Outputs.singleton = None
class LEDs(Thread):
TURN_OFF = 0
RED_GREEN_STATIC = 1
RED_GREEN_BLINKING_ALTERNATELY = 2
RED_GREEN_BLINKING_TOGETHER = 3
RED_STATIC = 4
GREEN_STATIC = 5
RED_BLINKING = 6
GREEN_BLINKING = 7
RED_SOFT_DIMM_IN_OUT = 8
GREEN_SOFT_DIMM_IN_OUT = 9
RED_GREEN_DIMM_IN_OUT_ALTERNATELY = 10
RED_GREEN_DIMM_IN_OUT_TOGETHER = 11
RED_PWM_FREQUENCY = 50
RED_PWM_STEP = 10
GREEN_PWM_FREQUENCY = 50
GREEN_PWM_STEP = 10
def set_params(self, type, frequency=0.1, number_of_blinks=-1, duration=-1):
self.type = type
self.frequency = frequency
self.number_of_blinks = number_of_blinks
self.duration = duration
self.stopThread = False
self.red_pwm = None
self.green_pwm = None
def stop(self):
self.stopThread = True
def easing_function(self, x):
return x*x*0.01
def start_pwm_red(self):
self.red_pwm = GPIO.PWM(Outputs.RED_LED_CHANNEL, LEDs.RED_PWM_FREQUENCY)
self.red_pwm.start(0)
def stop_pwm_red(self):
self.red_pwm.stop()
self.red_pwm = None
def start_pwm_green(self):
self.green_pwm = GPIO.PWM(Outputs.GREEN_LED_CHANNEL, LEDs.GREEN_PWM_FREQUENCY)
self.green_pwm.start(0)
def stop_pwm_green(self):
self.green_pwm.stop()
self.green_pwm = None
def run(self):
if self.type == LEDs.TURN_OFF:
GPIO.output(Outputs.RED_LED_CHANNEL, Outputs.RED_LED_OFF_STATE)
GPIO.output(Outputs.GREEN_LED_CHANNEL, Outputs.GREEN_LED_OFF_STATE)
return
if self.type == LEDs.RED_GREEN_STATIC:
GPIO.output(Outputs.RED_LED_CHANNEL, Outputs.RED_LED_ON_STATE)
GPIO.output(Outputs.GREEN_LED_CHANNEL, Outputs.GREEN_LED_ON_STATE)
if self.duration > -1:
n = round(float(self.duration) / 0.1)
while n > 0:
if self.stopThread:
return
time.sleep(0.1)
n -= 1
GPIO.output(Outputs.RED_LED_CHANNEL, Outputs.RED_LED_OFF_STATE)
GPIO.output(Outputs.GREEN_LED_CHANNEL, Outputs.GREEN_LED_OFF_STATE)
return
if self.type == LEDs.RED_GREEN_BLINKING_ALTERNATELY:
GPIO.output(Outputs.RED_LED_CHANNEL, Outputs.RED_LED_OFF_STATE)
while self.number_of_blinks == -1 or self.number_of_blinks > 0:
if self.stopThread:
return
GPIO.output(Outputs.GREEN_LED_CHANNEL, Outputs.GREEN_LED_ON_STATE)
time.sleep(self.frequency)
GPIO.output(Outputs.GREEN_LED_CHANNEL, Outputs.GREEN_LED_OFF_STATE)
if self.stopThread:
return
GPIO.output(Outputs.RED_LED_CHANNEL, Outputs.RED_LED_ON_STATE)
time.sleep(self.frequency)
GPIO.output(Outputs.RED_LED_CHANNEL, Outputs.RED_LED_OFF_STATE)
if self.stopThread:
return
if self.number_of_blinks != -1:
self.number_of_blinks -= 1
if self.type == LEDs.RED_GREEN_BLINKING_TOGETHER:
while self.number_of_blinks == -1 or self.number_of_blinks > 0:
if self.stopThread:
return
GPIO.output(Outputs.GREEN_LED_CHANNEL, Outputs.GREEN_LED_ON_STATE)
GPIO.output(Outputs.RED_LED_CHANNEL, Outputs.RED_LED_ON_STATE)
time.sleep(self.frequency)
if self.stopThread:
return
GPIO.output(Outputs.RED_LED_CHANNEL, Outputs.RED_LED_OFF_STATE)
GPIO.output(Outputs.GREEN_LED_CHANNEL, Outputs.GREEN_LED_OFF_STATE)
time.sleep(self.frequency)
if self.stopThread:
return
if self.number_of_blinks != -1:
self.number_of_blinks -= 1
if self.type == LEDs.RED_STATIC:
GPIO.output(Outputs.RED_LED_CHANNEL, Outputs.RED_LED_ON_STATE)
GPIO.output(Outputs.GREEN_LED_CHANNEL, Outputs.GREEN_LED_OFF_STATE)
if self.duration > -1:
n = round(float(self.duration) / 0.1)
while n > 0:
if self.stopThread:
return
time.sleep(0.1)
n -= 1
GPIO.output(Outputs.RED_LED_CHANNEL, Outputs.RED_LED_OFF_STATE)
return
if self.type == LEDs.GREEN_STATIC:
GPIO.output(Outputs.RED_LED_CHANNEL, Outputs.RED_LED_OFF_STATE)
GPIO.output(Outputs.GREEN_LED_CHANNEL, Outputs.GREEN_LED_ON_STATE)
if self.duration > -1:
n = round(float(self.duration) / 0.1)
while n > 0:
if self.stopThread:
return
time.sleep(0.1)
n -= 1
GPIO.output(Outputs.GREEN_LED_CHANNEL, Outputs.GREEN_LED_OFF_STATE)
return
if self.type == LEDs.RED_BLINKING:
GPIO.output(Outputs.GREEN_LED_CHANNEL, Outputs.GREEN_LED_OFF_STATE)
while self.number_of_blinks == -1 or self.number_of_blinks > 0:
GPIO.output(Outputs.RED_LED_CHANNEL, Outputs.RED_LED_ON_STATE)
time.sleep(self.frequency)
if self.stopThread:
return
GPIO.output(Outputs.RED_LED_CHANNEL, Outputs.RED_LED_OFF_STATE)
time.sleep(self.frequency)
if self.stopThread:
return
if self.number_of_blinks != -1:
self.number_of_blinks -= 1
if self.type == LEDs.GREEN_BLINKING:
GPIO.output(Outputs.RED_LED_CHANNEL, Outputs.RED_LED_OFF_STATE)
while self.number_of_blinks == -1 or self.number_of_blinks > 0:
GPIO.output(Outputs.GREEN_LED_CHANNEL, Outputs.GREEN_LED_ON_STATE)
time.sleep(self.frequency)
if self.stopThread:
return
GPIO.output(Outputs.GREEN_LED_CHANNEL, Outputs.GREEN_LED_OFF_STATE)
time.sleep(self.frequency)
if self.stopThread:
return
if self.number_of_blinks != -1:
self.number_of_blinks -= 1
if self.type == LEDs.RED_SOFT_DIMM_IN_OUT:
self.start_pwm_red()
while self.number_of_blinks == -1 or self.number_of_blinks > 0:
for d in range(0, 101, LEDs.RED_PWM_STEP):
self.red_pwm.ChangeDutyCycle(self.easing_function(d))
time.sleep(self.frequency/(100.0/float(LEDs.RED_PWM_STEP)))
if self.stopThread:
self.stop_pwm_red()
return
for d in range(100, -1, -LEDs.RED_PWM_STEP):
self.red_pwm.ChangeDutyCycle(self.easing_function(d))
time.sleep(self.frequency/(100.0/float(LEDs.RED_PWM_STEP)))
if self.stopThread:
self.stop_pwm_red()
return
if self.number_of_blinks != -1:
self.number_of_blinks -= 1
self.stop_pwm_red()
if self.type == LEDs.GREEN_SOFT_DIMM_IN_OUT:
self.start_pwm_green()
while self.number_of_blinks == -1 or self.number_of_blinks > 0:
for d in range(0, 101, LEDs.GREEN_PWM_STEP):
self.green_pwm.ChangeDutyCycle(self.easing_function(d))
time.sleep(self.frequency/(100.0/float(LEDs.GREEN_PWM_STEP)))
if self.stopThread:
self.stop_pwm_green()
return
for d in range(100, -1, -LEDs.GREEN_PWM_STEP):
self.green_pwm.ChangeDutyCycle(self.easing_function(d))
time.sleep(self.frequency/(100.0/float(LEDs.GREEN_PWM_STEP)))
if self.stopThread:
self.stop_pwm_green()
return
if self.number_of_blinks != -1:
self.number_of_blinks -= 1
self.stop_pwm_green()
if self.type == LEDs.RED_GREEN_DIMM_IN_OUT_ALTERNATELY:
self.green_pwm = GPIO.PWM(Outputs.GREEN_LED_CHANNEL, LEDs.GREEN_PWM_FREQUENCY)
self.green_pwm.start(0)
self.red_pwm = GPIO.PWM(Outputs.RED_LED_CHANNEL, LEDs.RED_PWM_FREQUENCY)
self.red_pwm.start(0)
while self.number_of_blinks == -1 or self.number_of_blinks > 0:
for d in range(0, 101, LEDs.RED_PWM_STEP):
self.red_pwm.ChangeDutyCycle(self.easing_function(d))
self.green_pwm.ChangeDutyCycle(100.0 - self.easing_function(d))
time.sleep(self.frequency/(100.0/float(LEDs.RED_PWM_STEP)))
if self.stopThread:
self.red_pwm.stop()
self.red_pwm = None
self.green_pwm.stop()
self.green_pwm = None
return
for d in range(100, -1, -LEDs.GREEN_PWM_STEP):
self.red_pwm.ChangeDutyCycle(self.easing_function(d))
self.green_pwm.ChangeDutyCycle(100.0 - self.easing_function(d))
time.sleep(self.frequency/(100.0/float(LEDs.GREEN_PWM_STEP)))
if self.stopThread:
self.red_pwm.stop()
self.red_pwm = None
self.green_pwm.stop()
self.green_pwm = None
return
if self.number_of_blinks != -1:
self.number_of_blinks -= 1
self.red_pwm.stop()
self.red_pwm = None
self.green_pwm.stop()
self.green_pwm = None
if self.type == LEDs.RED_GREEN_DIMM_IN_OUT_TOGETHER:
self.green_pwm = GPIO.PWM(Outputs.GREEN_LED_CHANNEL, LEDs.GREEN_PWM_FREQUENCY)
self.green_pwm.start(0)
self.red_pwm = GPIO.PWM(Outputs.RED_LED_CHANNEL, LEDs.RED_PWM_FREQUENCY)
self.red_pwm.start(0)
while self.number_of_blinks == -1 or self.number_of_blinks > 0:
for d in range(0, 101, LEDs.RED_PWM_STEP):
self.red_pwm.ChangeDutyCycle(self.easing_function(d))
self.green_pwm.ChangeDutyCycle(self.easing_function(d))
time.sleep(self.frequency/(100.0/float(LEDs.RED_PWM_STEP)))
if self.stopThread:
self.red_pwm.stop()
self.red_pwm = None
self.green_pwm.stop()
self.green_pwm = None
return
for d in range(100, -1, -LEDs.GREEN_PWM_STEP):
self.red_pwm.ChangeDutyCycle(self.easing_function(d))
self.green_pwm.ChangeDutyCycle(self.easing_function(d))
time.sleep(self.frequency/(100.0/float(LEDs.GREEN_PWM_STEP)))
if self.stopThread:
self.red_pwm.stop()
self.red_pwm = None
self.green_pwm.stop()
self.green_pwm = None
return
if self.number_of_blinks != -1:
self.number_of_blinks -= 1
self.red_pwm.stop()
self.red_pwm = None
self.green_pwm.stop()
self.green_pwm = None