/
garage.py
463 lines (374 loc) · 13.4 KB
/
garage.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import threading
import time
from datetime import datetime
from math import *
from random import randint
import holidays
import opc
from hurricane import Weather
GRG_LEN = 150
PERIOD = 1024
SLEEP = 0.01
HIBERNATE = 1
AUTO_ON_ENABLED_DAILY = False
DAILY_START_PROGRAM = 4
AUTO_ON_ENABLED_SPECIAL = True
SPECIAL_DAY_START = lambda: datetime.now().replace(hour=7, minute=00, second=0)
START_TIME = lambda: datetime.now().replace(hour=17, minute=30, second=0)
BEDTIME = lambda: datetime.now().replace(hour=23, minute=30, second=0)
now = lambda: datetime.now()
j = lambda: int(round(time.time() * 10)) % PERIOD
theTime = lambda: int(round(time.time() * 10))
class GarageController(threading.Thread):
def __init__(self, ip='127.0.0.1:7890', debug=False):
threading.Thread.__init__(self)
self.fc = opc.Client(ip)
self.debug = debug
self.state = 0
self.direction = True
self.position = 0
self.initialized = False
self.timetillon = datetime.now().time()
self.timetilloff = datetime.now().time()
self.specialdays = {}
self.is_special_day = False
self.special_day_colors = None
self.last_initialized_date = datetime.min.date()
self.initspecialdays()
self.check_special_day()
self.offsets = [randint(0,1023) for x in range(150)]
self.halloween_states = [0] * GRG_LEN
#One-off events
self.manual_show_test = False
self.g_open = False
self.g_close = False
self.w = Weather()
self.w.daemon = True
self.w.start()
if self.fc.can_connect():
print('Connected to Fadecandy')
else:
print('WARNING: could not connect to fadecandy')
def initspecialdays(self): #Updated for 2018
self.specialdays = holidays.get_holidays()
def setstate(self, state):
self.state = state
print('State set to %f' % state)
def run(self):
while True:
# Kinetics
if self.state == 1:
self.fade()
elif self.state == 2:
self.wipe()
elif self.state == 3:
self.twinkle()
elif self.state == 4:
self.red_green_twinkle()
elif self.state == 5:
self.cylon()
elif self.state == 13:
self.halloween()
elif self.state == 14:
self.scare()
elif self.state == 99:
self.alert()
elif self.state == 901:
self.hurricane()
# Statics
elif self.state == 100:
self.red_green_static()
elif self.state == 101:
self.white_static()
elif self.state == 999:
self.special_day_static()
# Default to off
else:
self.all_off()
time.sleep(SLEEP)
self.timetillon = START_TIME() - now()
self.timetilloff = BEDTIME() - now()
if self.last_initialized_date < now().date(): #Check if today is a special day, once per day
self.check_special_day()
self.last_initialized_date = now().date()
if self.is_special_day and SPECIAL_DAY_START() <= now() <= BEDTIME() and AUTO_ON_ENABLED_SPECIAL \
and not self.initialized:
self.state = 999
self.initialized = True
if self.manual_show_test: #Test show manually, if needed
self.init_show()
self.manual_show_test = False
self.state = 0
if self.g_open:
self.garage_open()
if self.g_close:
self.garage_close()
if AUTO_ON_ENABLED_DAILY and START_TIME() <= now() <= BEDTIME() and not self.initialized\
and not self.is_special_day: #Daily Init
if self.state == 0:
self.init_show()
self.state = DAILY_START_PROGRAM
self.initialized = True
if now() >= BEDTIME(): #Daily shutoff and un-init, every day
self.state = 0
self.initialized = False
if self.state == 0 and not self.debug:
self.all_off()
time.sleep(HIBERNATE)
def test_show(self):
self.manual_show_test = True
def check_special_day(self):
if any(now().month == tup[0] and now().day == tup[1] for tup in self.specialdays.keys()):
self.is_special_day = True
self.special_day_colors = self.specialdays.get((now().month, now().day), (0, 0, 0))
else:
self.is_special_day = False
self.special_day_colors = None
if self.debug:
print ("Special day?: " + str(self.is_special_day))
print ("Special colors: " + str(self.special_day_colors))
def init_show(self):
RAND_DENSITY = 15
MAX_LOOPS = 10
SLEEP = 0.75
self.all_off()
time.sleep(SLEEP)
self.fc.put_pixels([])
time.sleep(SLEEP)
show_colors = []
for i in range(int(ceil(GRG_LEN / 6))):
show_colors.append((255, 0, 0))
show_colors.append((255, 255, 0))
show_colors.append((0, 255, 0))
show_colors.append((0, 255, 255))
show_colors.append((0, 0, 255))
show_colors.append((255, 0, 255))
is_colored = [False] * GRG_LEN
pixels = [(0, 0, 0)] * GRG_LEN
i = 0
while any(element is False for element in is_colored):
for p in range(GRG_LEN):
if randint(0, 100) < RAND_DENSITY or i is MAX_LOOPS:
is_colored[p] = True
if is_colored[p]:
pixels[p] = show_colors[p]
else:
pixels[p] = (0, 0, 0)
self.fc.put_pixels(pixels)
time.sleep(SLEEP)
if i >= MAX_LOOPS:
break
i += 1
i = 0
time.sleep(SLEEP)
while any(element is True for element in is_colored):
for p in range(GRG_LEN):
if randint(0, 100) < RAND_DENSITY or i is MAX_LOOPS:
is_colored[p] = False
if not is_colored[p]:
pixels[p] = (128, 140, 65)
else:
pixels[p] = show_colors[p]
self.fc.put_pixels(pixels)
time.sleep(SLEEP)
if i >= MAX_LOOPS:
break
i += 1
time.sleep(SLEEP * 2)
pixels = [(255, 255, 175)] * GRG_LEN
self.fc.put_pixels(pixels)
time.sleep(SLEEP * 3)
i = 100
while i > 0:
for p in range(GRG_LEN):
if randint(0, 100) < i:
pixels[p] = (255, 255, 175)
else:
pixels[p] = (0, 0, 0)
self.fc.put_pixels(pixels)
i -= 1
time.sleep(0.04)
self.all_off()
def all_off(self):
pixels = []
for i in range(GRG_LEN):
pixels.append((0, 0, 0))
self.fc.put_pixels(pixels)
def fade(self):
pixels = []
for i in range(int(ceil(GRG_LEN / 6))):
sinval = 255 * (0.5 * sin(j() * 180.0 / PERIOD))
sinval2 = 255 * (0.5 * sin(pi + (j() * 180.0 / PERIOD)))
pixels.append((sinval, sinval2, 0))
pixels.append((sinval, sinval2, 0))
pixels.append((0, 0, 0))
pixels.append((sinval2, sinval, 0))
pixels.append((sinval2, sinval, 0))
pixels.append((0, 0, 0))
self.fc.put_pixels(pixels)
def wipe(self):
pixels = []
k = (theTime() % (GRG_LEN * 2))
for i in range(GRG_LEN):
if k < GRG_LEN:
if i < k:
pixels.append((255, 0, 0))
else:
pixels.append((0, 255, 0))
else:
if (GRG_LEN + i) < k:
pixels.append((0, 255, 0))
else:
pixels.append((255, 0, 0))
self.fc.put_pixels(pixels)
def twinkle(self):
pixels = []
for i in range(GRG_LEN):
if randint(0, 10000) < 20:
pixels.append((255, 255, 175))
else:
pixels.append((128, 140, 65))
self.fc.put_pixels(pixels)
time.sleep(0.04)
def red_green_twinkle(self):
pixels = []
for i in range(int(ceil(GRG_LEN / 6))):
pixels.append((255, 0, 0))
# pixels.append((255, 0, 0))
if randint(0, 10000) < 20:
pixels.append((255, 255, 175))
else:
pixels.append((128, 140, 65))
if randint(0, 10000) < 20:
pixels.append((255, 255, 175))
else:
pixels.append((128, 140, 65))
pixels.append((0, 255, 0))
# pixels.append((0, 255, 0))
if randint(0, 10000) < 20:
pixels.append((255, 255, 175))
else:
pixels.append((128, 140, 65))
if randint(0, 10000) < 20:
pixels.append((255, 255, 175))
else:
pixels.append((128, 140, 65))
self.fc.put_pixels(pixels)
def cylon(self):
pixels = []
for i in range(GRG_LEN):
pixels.append((0, 0, 0))
try:
pixels[self.position] = (128, 0, 0)
pixels[self.position + 1] = (0, 255, 0)
pixels[self.position + 2] = (128, 0, 0)
except IndexError:
self.direction = not self.direction
self.fc.put_pixels(pixels)
if self.position <= 1:
self.direction = True
if self.direction:
self.position += 1
else:
self.position -= 1
time.sleep(0.01)
def halloween(self):
pixels = [(0, 0, 0)] * GRG_LEN
for i in range(0, GRG_LEN):
self.halloween_states[i] = max(self.halloween_states[i] - 0.01, 0)
if(randint(0, 1000) < 10):
self.halloween_states[i] = 1
if i % 2:
pixels[i] = (165*self.halloween_states[i], 255*self.halloween_states[i], 0)
else:
pixels[i] = (0, 150*self.halloween_states[i], 128*self.halloween_states[i])
self.fc.put_pixels(pixels)
time.sleep(0.02)
def scare(self):
for i in range(25):
self.fc.put_pixels([(255, 255, 175)] * GRG_LEN)
self.fc.put_pixels([(255, 255, 175)] * GRG_LEN)
time.sleep(0.02)
self.fc.put_pixels([(0, 0, 0)] * GRG_LEN)
self.fc.put_pixels([(0, 0, 0)] * GRG_LEN)
time.sleep(0.03)
self.fc.put_pixels([(0, 0, 0)] * GRG_LEN)
self.setstate(999)
def alert(self):
self.fc.put_pixels([(0, 255, 0)] * GRG_LEN)
time.sleep(0.5)
self.fc.put_pixels([(0, 0, 0)] * GRG_LEN)
time.sleep(0.5)
def garage_open(self):
HALF_LEN = GRG_LEN / 2
for i in range(HALF_LEN):
pixels = []
for a in range(HALF_LEN - i):
pixels.append((155,255,0))
for b in range(i * 2):
pixels.append((0,0,0))
for c in range(HALF_LEN - i):
pixels.append((155,255,0))
self.fc.put_pixels(pixels)
time.sleep(0.04)
self.g_open = False
time.sleep(1)
def garage_close(self):
HALF_LEN = GRG_LEN / 2
for i in range(HALF_LEN):
pixels = []
for a in range(i):
pixels.append((155,255,0))
for b in range(GRG_LEN - (i * 2)):
pixels.append((0,0,0))
for c in range(i):
pixels.append((155,255,0))
self.fc.put_pixels(pixels)
time.sleep(0.04)
self.g_close = False
time.sleep(1)
def hurricane(self):
speed = self.w.get_gusts()
#speed = randint(5, 145)
if speed < 38:
color = (255, 0, 0)
elif speed < 73:
color = (255, 255, 0)
elif speed < 95:
color = (165, 255, 0)
elif speed < 110:
color = (0, 255, 0)
elif speed < 130:
color = (0, 255, 255)
else:
color = (255, 255, 175)
pixels = [(0, 0, 0) for x in range(GRG_LEN)]
for x in range(speed):
pixels[x] = color
pixels[1] = color
self.fc.put_pixels(pixels)
time.sleep(5)
# Statics
def special_day_static(self):
try:
num_colors = len(self.special_day_colors)
except TypeError:
return
pixels = []
for i in range(GRG_LEN):
pixels.append(((self.special_day_colors[i % num_colors][1]), (self.special_day_colors[i % num_colors][0]),
(self.special_day_colors[i % num_colors][2]))) # Because GRB colors are nasty
self.fc.put_pixels(pixels)
def red_green_static(self):
pixels = []
for i in range(int(ceil(GRG_LEN / 4))):
pixels.append((255, 0, 0))
pixels.append((255, 0, 0))
pixels.append((0, 255, 0))
pixels.append((0, 255, 0))
self.fc.put_pixels(pixels)
def white_static(self):
pixels = []
for i in range(GRG_LEN):
pixels.append((255, 255, 175))
self.fc.put_pixels(pixels)