-
Notifications
You must be signed in to change notification settings - Fork 0
/
temperaturewidget.py
executable file
·205 lines (178 loc) · 6.72 KB
/
temperaturewidget.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
#!/usr/bin/env python3
from rgbmatrix import RGBMatrix, RGBMatrixOptions
from widget import Widget
from PIL import Image, ImageDraw, ImageFont
import time
class NumberWidget(Widget):
def __init__(self,x=0,y=0,color=(255,255,255),size=15,width=64,height=64,font=None):
super(NumberWidget,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.lastnumber = None
self.off = True
self.lastupdate = 0
def update(self,number=None,off=False):
if number == None:
return
if off == True:
self.image = Image.new("RGBA",(self.width,self.height))
self.lastupdate = time.time()
self.changed = True
return
q = number
self.lastupdate = time.time()
if (self.lastnumber != q):
numStr = "{:2.2f}".format(q)
self.image = Image.new("RGBA",(self.width,self.height))
draw = ImageDraw.Draw(self.image)
draw.text((self.x,self.y),numStr,font=self.font,fill=self.color)
self.lastnumber = q
self.changed = True
else:
self.changed = False
class AirQualityWidget(Widget):
def __init__(self,x=0,y=0,color=(255,255,255),size=15,width=64,height=64,font=None):
super(AirQualityWidget,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.lastqual = None
self.lastupdate = 0
self.sensors = {}
def update(self,sensor=None, airquality=None):
if airquality == None:
if (time.time() - self.lastupdate > 60*5):
self.image = Image.new("RGBA",(self.width,self.height))
self.lastupdate = time.time()
self.changed = True
else:
self.changed = False
return
q = int(float(airquality.decode()))
self.lastupdate = time.time()
if (self.lastqual != q):
qualStr = "{:4d}".format(q) + "ppm"
self.image = Image.new("RGBA",(self.width,self.height))
draw = ImageDraw.Draw(self.image)
if q <= 1000:
self.color = (0,255,0)
elif q <= 2000:
self.color = (255,255,0)
else:
self.color = (255,0,0)
draw.text((self.x,self.y),qualStr,font=self.font,fill=self.color)
self.lastqual = q
self.changed = True
else:
self.changed = False
class HumidityWidget(Widget):
def __init__(self,x=0,y=0,color=(255,255,255),size=15,width=64,height=64,font=None):
super(HumidityWidget,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.lasthum = None
self.lastupdate = 0
self.sensors = {}
def update(self,sensor=None, humidity=None):
if humidity == None:
if (time.time() - self.lastupdate > 60*5):
self.image = Image.new("RGBA",(self.width,self.height))
self.lastupdate = time.time()
self.changed = True
else:
self.changed = False
return
h = int(float(humidity.decode()))
self.lastupdate = time.time()
if (self.lasthum != h):
humStr = "{:2d}".format(h) + "%"
self.image = Image.new("RGBA",(self.width,self.height))
self.color=(0,200-h*2,255)
draw = ImageDraw.Draw(self.image)
draw.text((self.x,self.y),humStr,font=self.font,fill=self.color)
self.lasthum = h
self.changed = True
else:
self.changed = False
class TemperatureWidget(Widget):
class Sensor:
def __init__(self,name,value):
self.name = name
self.value= value
self.update=time.time()
def __init__(self,x=0,y=0,color=(255,255,255),size=15,width=64,height=64,font=None):
super(TemperatureWidget,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.lasttemp = None
self.lastupdate = 0
self.sensors = {}
def update(self,sensor=None, temperature=None):
try:
t = float(temperature)
except:
temperature = None
if temperature == None:
if (time.time() - self.lastupdate > 60*5):
self.image = Image.new("RGBA",(self.width,self.height))
self.lastupdate = time.time()
self.changed = True
else:
self.changed = False
return
self.lastupdate = time.time()
if (self.lasttemp != t):
tempStr = "{:2.1f}".format(t)
self.image = Image.new("RGBA",(self.width,self.height))
if (t< 0):
self.color=(0,0,255)
elif (t< 10):
self.color=(0,128,255)
elif (t< 15):
self.color=(255,255,0)
elif (t< 20):
self.color=(255,192,0)
elif (t< 25):
self.color=(255,128,0)
elif (t< 30):
self.color=(255,64,0)
else:
self.color=(255,0,0)
if (t > -20):
draw = ImageDraw.Draw(self.image)
draw.text((self.x,self.y),tempStr,font=self.font,fill=self.color)
self.lasttemp = t
self.changed = True
else:
self.changed = False
else:
self.changed = False
def minimum(self,sensor=None,temperature=None):
t = float(temperature)
s = Sensor(sensor,t)
self.sensors[sensor] = s
# remove all too old readings and record the minimum
minimum = s
for x in iter(self.sensors):
if (x.update < time.time() - 5*60):
del self.sensors[x]
elif x.value < minimum.value:
minimum = x
self.update(minimum.name,minimum.value)
if __name__ == "__main__":
options = RGBMatrixOptions()
options.rows = 64
options.cols = 64
matrix = RGBMatrix(options = options)
currenttemp = TemperatureWidget(size=12)
currenttemp.update(16.9)
while True:
if (currenttemp.changed):
matrix.SetImage(currenttemp.image.convert("RGB"))