/
ThexBerryClock.py
executable file
·387 lines (331 loc) · 12.1 KB
/
ThexBerryClock.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
#!/usr/bin/python
## Configuration -- Note there is more config down lower in the startUp function, such as brightness
basePath = '/opt/ThexBerryClock'
logFile = '/tmp/TBC.log'
blockHeightFile = '/tmp/latestBlockheight.txt'
interruptFile = '/tmp/TBC-INTERRUPT.txt'
# App will stay in foreground & verbose logging
debugMode = False
# Main script
import thread
import time
import datetime
from datetime import date
import math
from rgbmatrix import RGBMatrix
import Image
import ImageDraw
import ImageFont
import locale
import signal
import json
import os
import sys
import logging
import subprocess
from exchanges.bitstamp import Bitstamp
locale.setlocale( locale.LC_ALL, 'en_GB.utf8' )
def startUp():
log("Starting up ThexBerryClock (test)...")
global TBC, iterations, itime
iterations = 0
itime = int(time.time())
TBC = {}
TBC['epoch']=datetime.datetime.utcfromtimestamp(0)
TBC['bhdisplay'] = 0
TBC['sleep'] = 0.01
TBC['clockmode'] = effect_startupSplash
TBC['bitcoin'] = 0
TBC['btcopen'] = 0
TBC['rainbowBorderMode'] = 0
TBC['timers'] = {'bitcoin':0,'bhflipper':0}
TBC['timerFreqList'] = {'bitcoin':60,'bhflipper':10}
TBC['timerFuncList'] = {'bitcoin':getBitcoinPrice,'bhflipper':blockheight_flipDisplay}
TBC['matrix'] = RGBMatrix(32, 2)
TBC['matrix'].pwmBits = 11
TBC['matrix'].brightness = 50
TBC['canvas'] = TBC['matrix'].CreateFrameCanvas()
TBC['font'] = ImageFont.load(basePath + "/pilfonts/timR08.pil")
TBC['blockheight'] = 0
thread.start_new_thread( startup_blockheight, () )
signal.signal(signal.SIGUSR1, interruptHandler)
def startup_blockheight():
liveUpdate(blockHeightFile, 0)
def shutDown():
log("Main loop exited after " + str(iterations) + " iterations. Shutting down.")
exit
def mainLoop():
global TBC
global image, draw, itime
# Keep track of the current iteration's time
itime = int(time.time())
# Set up the image canvas
image = Image.new("RGB", (64,32))
draw = ImageDraw.Draw(image)
# Determine the mode to be in
h = time.strftime("%I")
m = time.strftime("%M")
s = time.strftime("%S")
if (h == "04" and (m == "19" or m == "20")) or (h == "07" and (m == "09" or m == "10")):
TBC['clockmode'] = clock420
rainbowBorder()
if m == "00" and (s == "00" or s == "01"):
effect_borderPulse()
# Execute the current clock line
TBC['clockmode']()
if TBC['clockmode'] == mainClock:
bitcoinDisplay()
blockheightDisplay()
# Perform frequency-based timers
for timer in TBC['timerFreqList']:
timeSince = itime - TBC['timers'][timer]
if timeSince >= TBC['timerFreqList'][timer]:
TBC['timers'][timer] = itime
thread.start_new_thread( TBC['timerFuncList'][timer], () )
# Output the image canvas to display
renderDisplay()
# Pause for garbage collection
time.sleep(TBC['sleep'])
def renderDisplay():
setimage(image, TBC['canvas'])
TBC['matrix'].SwapOnVSync(TBC['canvas'])
def interruptHandler(a, b):
thread.start_new_thread( liveUpdate, (interruptFile,1) )
def liveUpdate(confFile, removeConf):
conf = getInterruptConfig(confFile, removeConf)
if not conf:
return
if conf['mode'] == 'heightupdate':
try:
if TBC['blockheight'] != conf['data']['height']:
TBC['blockheight'] = conf['data']['height']
TBC['blockheight_date'] = conf['data']['time']
TBC['blockheight_tx'] = conf['data']['n_tx']
TBC['blockheight_bytes'] = conf['data']['size']
TBC['blockheight_time'] = itime
except:
TBC['blockheight'] = 0
log("Blockheight update: " + str(conf['data']['height']))
elif conf['mode'] == 'toke':
log("Toke called: " + str(conf))
def getInterruptConfig(interruptFile,removeInterrupt):
if not os.path.isfile(interruptFile):
return
with open(interruptFile,'r') as f:
data = json.load(f)
if removeInterrupt == 1:
os.remove(interruptFile)
return data
def getBitcoinPrice():
global TBC
(TBC['bitcoin'],btco) = Bitstamp.get_current_price()
TBC['btcopen'] = float(TBC['bitcoin']-btco)
def effect_borderPulse():
(w,h) = image.size
pix = image.load()
(r,g,b) = makeColorGradient(1.666, 2.666, 3.666, 0, 2, 4, 128, 127, 8, iterations)
for x in range(0,w):
for y in range(0,h):
if (x == 0 or x == 63 or y == 0 or y == 31):
pix[x, y] = (r, g, b)
def effect_startupSplash():
rainbowBorder()
(r,g,b) = makeColorGradient(.1, .1, .1, 0, 2, 4, 128, 127, 255, iterations)
(r2,g2,b2) = makeColorGradient(.1, .1, .1, 0, 2, 4, 128, 127, 255, iterations+5)
(r3,g3,b3) = makeColorGradient(.1, .1, .1, 0, 2, 4, 128, 127, 255, iterations+10)
(r4,g4,b4) = makeColorGradient(.1, .1, .1, 0, 2, 4, 128, 127, 255, iterations+15)
draw.text((5, -1), time.strftime("%I:%M:%S ")+time.strftime("%p").upper(), font=TBC['font'], fill=rgb_to_hex((r,g,b)))
draw.text((1, 7), "THEXBERRY", font=TBC['font'], fill=rgb_to_hex((r2,g2,b2)))
draw.text((1, 14), "THEXBERRY", font=TBC['font'], fill=rgb_to_hex((r3,g3,b3)))
draw.text((1, 21), "THEXBERRY", font=TBC['font'], fill=rgb_to_hex((r4,g4,b4)))
if iterations > 10:
TBC['clockmode'] = mainClock
def mainClock():
h = time.strftime("%I")
m = time.strftime("%M")
s = time.strftime("%S")
ampm = time.strftime("%p").upper()
(r,g,b) = makeColorGradient(.1, .1, .1, 0, 2, 4, 128, 127, 255, int(time.time())/60)
# Colon Blink Color
if (itime % 2 == 0):
(r1,g1,b1) = makeColorGradient(.1, .1, .1, 0, 2, 4, 128, 127, 255, iterations)
else:
(r1,g1,b1) = (r,g,b)
draw.text((5, -1), str(h), font=TBC['font'], fill=rgb_to_hex((r,g,b)))
draw.text((15, -1), ":", font=TBC['font'], fill=rgb_to_hex((r1,g1,b1)))
draw.text((18, -1), str(m), font=TBC['font'], fill=rgb_to_hex((r,g,b)))
draw.text((28, -1), ":", font=TBC['font'], fill=rgb_to_hex((r1,g1,b1)))
draw.text((31, -1), str(s), font=TBC['font'], fill=rgb_to_hex((r,g,b)))
draw.text((43, -1), str(ampm), font=TBC['font'], fill=rgb_to_hex((r,g,b)))
def blockheight_flipDisplay():
if TBC['bhdisplay'] == 1:
TBC['bhdisplay'] = 0
else:
TBC['bhdisplay'] = 1
def blockheightDisplay():
(r,g,b) = makeColorGradient(.1, .1, .1, 0, 2, 4, 128, 127, 255, int(time.time())/60)
if (TBC['blockheight'] == 0):
return
# (r,g,b) = makeColorGradient(.1, .1, .1, 0.2, 0.5, 0.8, 128, 127, 255, iterations)
bhdate = datetime.datetime.strptime(TBC['blockheight_date'], "%Y-%m-%dT%H:%M:%SZ")
secsSinceUpdate = itime - (bhdate-TBC['epoch']).total_seconds()
secsSince = itime - TBC['blockheight_time']
# secsSinceUpdate = itime - TBC['blockheight_time']
percent = secsSince * 5
if percent < 100:
if percent <= 0:
percent = 1
gr=linear_gradient("#FFFFFF",rgb_to_hex((r, g, b)),percent,100)
r=gr['r'][0]
g=gr['g'][0]
b=gr['b'][0]
# txt = str(TBC['blockheight']) + " " + str(TBC['blockheight_tx']) + "tx"
txt = " " + str(TBC['blockheight'])
ago = int(secsSinceUpdate/60)
if TBC['bhdisplay'] == 0:
txt = str(ago) + " min " + str(TBC['blockheight_bytes']/1024) + "KB"
draw.text((1, 14), str(txt), font=TBC['font'], fill=rgb_to_hex((r,g,b)))
def bitcoinDisplay():
(r,g,b) = makeColorGradient(.1, .1, .1, 0, 2, 4, 128, 127, 255, int(time.time())/60)
btcs = locale.currency(TBC['btcopen'], grouping=False, symbol=False)
# TODO: Pixel Up/Down arrows
if btcs.startswith("-"):
btcstr = btcs[1:]
(r1,g1,b1) = (255,0,0)
else:
btcstr = btcs
(r1,g1,b1) = (0,255,0)
draw.text((3, 7), str(TBC['bitcoin']), font=TBC['font'], fill=rgb_to_hex((r,g,b)))
# draw.text((32, 7), "$", font=TBC['font'], fill=rgb_to_hex((r,g,b)))
draw.text((34, 7), "$" + str(btcstr), font=TBC['font'], fill=rgb_to_hex((r1,g1,b1)))
# draw.text((60, 7), ")", font=TBC['font'], fill=rgb_to_hex((r,g,b)))
def clock420():
global TBC
h = time.strftime("%I")
m = time.strftime("%M")
s = time.strftime("%S")
ampm = time.strftime("%p").upper()
if (h == "04" and m == "21") or (h == "07" and m == "11"):
TBC['clockmode'] = mainClock
(r1,g1,b1) = makeColorGradient(.1, .1, .1, 0, 2, 4, 128, 127, 255, iterations+10)
if m == "20" or m == "10":
TBC['rainbowBorderMode'] = 1
if itime % 2 == 0:
(r,g,b) = makeColorGradient(.1, .1, .1, 0, 2, 4, 128, 127, 255, iterations)
draw.text((5, -1), str(h), font=TBC['font'], fill=rgb_to_hex((r,g,b)))
draw.text((15, -1), ":", font=TBC['font'], fill=rgb_to_hex((r1,g1,b1)))
draw.text((18, -1), str(m), font=TBC['font'], fill=rgb_to_hex((r,g,b)))
draw.text((28, -1), ":", font=TBC['font'], fill=rgb_to_hex((r1,g1,b1)))
draw.text((31, -1), str(s), font=TBC['font'], fill=rgb_to_hex((r,g,b)))
draw.text((43, -1), str(ampm), font=TBC['font'], fill=rgb_to_hex((r,g,b)))
else:
(r,g,b) = makeColorGradient(1.666, 2.666, 3.666, 0, 2, 4, 128, 127, 8, iterations)
draw.text((5, -1), "CHEERS!!!!!", font=TBC['font'], fill=rgb_to_hex((r,g,b)))
else:
TBC['rainbowBorderMode'] = 0
(r,g,b) = makeColorGradient(.1, .1, .1, 0, 2, 4, 128, 127, 255, iterations)
draw.text((5, -1), str(h), font=TBC['font'], fill=rgb_to_hex((r,g,b)))
draw.text((15, -1), ":", font=TBC['font'], fill=rgb_to_hex((r1,g1,b1)))
draw.text((18, -1), str(m), font=TBC['font'], fill=rgb_to_hex((r,g,b)))
draw.text((28, -1), ":", font=TBC['font'], fill=rgb_to_hex((r1,g1,b1)))
draw.text((31, -1), str(s), font=TBC['font'], fill=rgb_to_hex((r,g,b)))
draw.text((43, -1), str(ampm), font=TBC['font'], fill=rgb_to_hex((r,g,b)))
def rainbowBorder():
(w,h) = image.size
pix = image.load()
if (TBC['rainbowBorderMode'] == 1):
(r,g,b) = makeColorGradient(1.666, 2.666, 3.666, 0, 2, 4, 128, 127, 8, iterations)
else:
(r,g,b) = makeColorGradient(.1, .1, .1, 0, 2, 4, 128, 127, 255, iterations)
for x in range(0,w):
for y in range(0,h):
if (x == 0 or x == 63 or y == 0 or y == 31):
pix[x, y] = (r, g, b)
def makeColorGradient(f1, f2, f3, p1, p2, p3, center, width, len, i):
r = int(math.floor(math.sin(f1*i + p1) * width + center))
g = int(math.floor(math.sin(f2*i + p2) * width + center))
b = int(math.floor(math.sin(f3*i + p3) * width + center))
return (r,g,b)
def setimage(im,mx):
(w,h) = im.size
pix = im.load()
for x in range(w):
for y in range(h):
(r, g, b) = pix[x, y]
mx.SetPixel(x, y, r, g, b)
def rgb_to_hex(rgb):
return '#%02x%02x%02x' % rgb
def RGB_to_hex(RGB):
# Components need to be integers for hex to make sense
RGB = [int(x) for x in RGB]
return "#"+"".join(["0{0:x}".format(v) if v < 16 else
"{0:x}".format(v) for v in RGB])
def hex_to_RGB(hex):
# Pass 16 to the integer function for change of base
return [int(hex[i:i+2], 16) for i in range(1,6,2)]
def linear_gradient(start_hex, finish_hex, i, n):
s = hex_to_RGB(start_hex)
f = hex_to_RGB(finish_hex)
vector = [
int(s[j] + (float(i)/(n-1))*(f[j]-s[j]))
for j in range(3)
]
return color_dict([vector])
def color_dict(gradient):
return {"hex":[RGB_to_hex(RGB) for RGB in gradient],
"r":[RGB[0] for RGB in gradient],
"g":[RGB[1] for RGB in gradient],
"b":[RGB[2] for RGB in gradient]}
def log(msg):
logger.debug(str(msg))
def usage():
print("-----------------------------")
print("ThexBerryClock")
print("-----------------------------")
print("ThexBerryClock.py daemon - Launch clock daemon")
print("ThexBerryClock.py toke - Perform a toke effect")
def command_toke():
try:
a2 = sys.argv[2]
except:
a2 = 60
command = {}
command['mode'] = 'toke'
command['duration'] = a2
sendCommand(command)
def sendInterrupt():
subprocess.call('/usr/bin/killall -USR1 ThexBerryClock.py', shell=True)
def sendCommand(myDict):
f = open(interruptFile, 'w')
f.write(json.dumps(myDict))
f.close()
sendInterrupt()
print("Sent command to clock: " + str(myDict))
def commandLine():
try:
a1 = sys.argv[1]
except:
a1 = ''
if a1 == "daemon":
return True
elif a1 == "interrupt":
sendInterrupt()
elif a1 == "toke":
command_toke()
else:
usage()
return False
def TBCLoop():
global iterations
startUp()
while 1:
mainLoop()
iterations = iterations + 1
shutDown()
if commandLine() == True:
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler(logFile, "w")
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
TBCLoop()