-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountdownwidget.py
executable file
·141 lines (121 loc) · 4.41 KB
/
countdownwidget.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
#!/usr/bin/env python3
from rgbmatrix import RGBMatrix, RGBMatrixOptions
from widget import Widget
from PIL import Image, ImageDraw, ImageFont
import time
import re
class CountdownWidget(Widget):
def __init__(self,x=0,y=0,color=None,size=14,width=64,height=64,font=None,bigat=-1):
if color is None:
self.dynamiccolor = True
else:
self.dynamiccolor = False
super(CountdownWidget,self).__init__(x,y,color,size,width,height)
if font:
self.font = ImageFont.load("fonts/"+font)
else:
self.font = ImageFont.load("fonts/"+self.size2font(size))
self.bigat = bigat
self.bigfont = ImageFont.truetype(font="RobotoCondensed-Light.ttf",size=height)
self.starttime = 0
self.endtime = 0
self.running = False
self.started = False
self.ended = False
self.cancelled = False
self.lasttime= 0
def start(self,minutes=0,seconds=0):
if (minutes > 0 or seconds > 0):
self.starttime = int(time.time())
self.endtime = self.starttime + (60*minutes) + seconds
self.running = True
self.started = True
def cancel(self):
if (self.running):
self.running = False
self.started = False
self.ended = False
self.cancelled = True
def mqttstart(self,topic=None,value=None):
v = value.decode()
m1 = re.match("(\d+)\s+(\d+)",v)
m2 = re.match("^\d+$",v)
at = re.match("(\d+):(\d+)",v)
if (v == "cancel"):
self.cancel()
elif m1:
minutes = int(m1.group(1))
seconds = int(m1.group(2))
self.start(minutes,seconds)
elif m2:
seconds = int(m2.group(0))
self.start(seconds = seconds)
elif at:
lthen = list(time.localtime())
lthen[3]=int(at.group(1))
lthen[4]=int(at.group(2)) # minutes
lthen[5]=0
then = time.mktime(tuple(lthen))
self.start(seconds=(then-int(time.time())))
def big(self):
now = int(time.time())
seconds_left = int(self.endtime - now)
if seconds_left < 0:
self.running = False
self.ended = True
self.changed = True
elif (self.running and self.lasttime != now):
self.image = Image.new("RGBA",(self.width,self.height))
draw = ImageDraw.Draw(self.image)
if self.dynamiccolor:
if seconds_left < 60:
self.color = (255,0,0)
elif seconds_left < 5*60:
self.color = (255,255,0)
else:
self.color = (64,255,64)
draw.text((0,0),"{:^3}".format(seconds_left),font=self.bigfont,fill=self.color)
self.lasttime = now
self.changed = True
else:
self.changed = False
def update(self):
now = int(time.time())
seconds_left = self.endtime - now
if seconds_left <= self.bigat:
self.big()
elif seconds_left < 0:
self.running = False
self.ended = True
self.changed = True
elif (self.running and self.lasttime != now):
current_time = time.gmtime(seconds_left)
time_string = time.strftime("%H:%M:%S",current_time)
self.image = Image.new("RGBA",(self.width,self.height))
draw = ImageDraw.Draw(self.image)
if self.dynamiccolor:
if seconds_left < 60:
self.color = (255,0,0)
elif seconds_left < 5*60:
self.color = (255,255,0)
else:
self.color = (64,255,64)
draw.text((self.x,self.y),time_string,font=self.font,fill=self.color)
self.changed = True
self.lasttime = now
else:
self.changed = False
if __name__ == "__main__":
options = RGBMatrixOptions()
options.rows = 64
options.cols = 64
matrix = RGBMatrix(options = options)
currenttime = CountdownWidget()
currenttime.mqttstart("countdown",b"16:00")
while True:
currenttime.update()
if (currenttime.changed):
im = Image.new("RGBA",(64,64))
im.alpha_composite(currenttime.image)
matrix.SetImage(im.convert("RGB"))
time.sleep(0.5)