-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display_ssd1306b.py
executable file
·423 lines (389 loc) · 15.1 KB
/
display_ssd1306b.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
#!/usr/bin/env python3
# *****************************************
# PiFire Display Interface Library
# *****************************************
#
# Description: This library supports using the SSD1306 as a display.
#
# *****************************************
# *****************************************
# Imported Libraries
# *****************************************
from luma.core.interface.serial import i2c
from luma.core.render import canvas
from luma.oled.device import ssd1306
from PIL import Image, ImageDraw, ImageFont
import datetime
import time
from RPi import GPIO
from common import ReadControl, WriteControl # Common Library for WebUI and Control Program
class Display:
def __init__(self, buttonslevel='HIGH', units='F'):
self.serial = i2c(port=1, address=0x3C)
self.device = ssd1306(self.serial)
self.menuactive = False
self.menutime = 0
self.menuitem = ''
self.units = units
# Init GPIO for button input, setup callbacks
self.up = 16 # UP - GPIO16
self.down = 20 # DOWN - GPIO20
self.enter = 21 # ENTER - GPIO21
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.up, GPIO.IN)
GPIO.setup(self.down, GPIO.IN)
GPIO.setup(self.enter, GPIO.IN)
#GPIO.add_event_detect(self.up, GPIO.FALLING, callback=self.UpCallback, bouncetime=300)
#GPIO.add_event_detect(self.down, GPIO.FALLING, callback=self.DownCallback, bouncetime=300)
#GPIO.add_event_detect(self.enter, GPIO.FALLING, callback=self.EnterCallback, bouncetime=300)
# ==== Buttons Setup =====
if buttonslevel == 'HIGH':
# Defines for input buttons level HIGH
self.BUTTON_INPUT = 0
else:
# Defines for input buttons level LOW
self.BUTTON_INPUT = 1
self.menu = {}
self.menu['inactive'] = { # List of options for the 'inactive' menu. This is the initial menu when smoker is not running.
'Startup' : {
'displaytext' : 'Startup',
'icon' : '\uf04b' # FontAwesome Play Icon
},
'Monitor' : {
'displaytext' : 'Monitor',
'icon' : '\uf530' # FontAwesome Glasses Icon
},
'Stop' : {
'displaytext' : 'Stop',
'icon' : '\uf04d' # FontAwesome Stop Icon
}
}
self.menu['active'] = { # List of options for the 'active' menu. This is the second level menu of options while running.
'Shutdown' : {
'displaytext' : 'Shutdown',
'icon' : '\uf11e' # FontAwesome Finish Icon
},
'Hold' : {
'displaytext' : 'Hold',
'icon' : '\uf76b' # FontAwesome Temperature Low Icon
},
'Smoke' : {
'displaytext' : 'Smoke',
'icon' : '\uf0c2' # FontAwesome Cloud Icon
},
'Stop' : {
'displaytext' : 'Stop',
'icon' : '\uf04d' # FontAwesome Stop Icon
},
'SmokePlus' : {
'displaytext' : 'Toggle Smoke+',
'icon' : '\uf0c2' # FontAwesome Cloud Icon
}
}
self.menu['current'] = {}
self.menu['current']['mode'] = 'none' # Current Menu Mode (inactive, active)
self.menu['current']['option'] = 0 # Current option in current mode
self.DisplaySplash()
time.sleep(3) # Keep the splash up for three seconds on boot-up - you can certainly disable this if you want
def DisplayStatus(self, in_data, status_data):
self.units = status_data['units']
if (self.menuactive == True) and (time.time() - self.menutime > 5):
self.menuactive = False
self.menu['current']['mode'] = 'none'
self.menu['current']['option'] = 0
print('Menu Inactive')
self.ClearDisplay()
if (self.menuactive == False):
with canvas(self.device) as draw:
try:
# Grill Temperature (Large Centered)
if(self.units == 'F'):
font = ImageFont.truetype("impact.ttf", 42)
else:
font = ImageFont.truetype("impact.ttf", 38)
text = str(in_data['GrillTemp'])[:5]
(font_width, font_height) = font.getsize(text)
draw.text((128//2 - font_width//2,0), text, font=font, fill=255)
# Active Outputs F = Fan, I = Igniter, A = Auger (Upper Left)
font = ImageFont.truetype("FA-Free-Solid.otf", 24)
if(status_data['outpins']['fan']==0):
text = '\uf863'
draw.text((0, 0), text, font=font, fill=255)
if(status_data['outpins']['igniter']==0):
text = '\uf46a'
(font_width, font_height) = font.getsize(text)
draw.text((0, 5 + (64//2 - font_height//2)), text, font=font, fill=255)
if(status_data['outpins']['auger']==0):
text = '\uf101'
(font_width, font_height) = font.getsize(text)
draw.text((128 - font_width, 5 + (64//2 - font_height//2)), text, font=font, fill=255)
# Current Mode (Bottom Left)
font = ImageFont.truetype("trebuc.ttf", 18)
text = status_data['mode'] + ' Mode'
(font_width, font_height) = font.getsize(text)
draw.text((128//2 - font_width//2, 64 - font_height), text, font=font, fill=255)
# Notification Indicator (Upper Right)
font = ImageFont.truetype("FA-Free-Solid.otf", 24)
text = ' '
for item in status_data['notify_req']:
if status_data['notify_req'][item] == True:
text = '\uf0f3'
(font_width, font_height) = font.getsize(text)
draw.text((128 - font_width, 0), text, font=font, fill=255)
except:
now = str(datetime.datetime.now())
now = now[0:19] # Truncate the microseconds
print(str(now) + ' Error displaying status.')
def DisplaySplash(self):
frameSize = (128, 64)
screen = Image.new('1', (frameSize), color=0)
splash = Image.open('color-boot-splash.png') \
.transform(self.device.size, Image.AFFINE, (1, 0, 0, 0, 1, 0), Image.BILINEAR) \
.convert("L") \
.convert(self.device.mode)
splashSize = splash.size
screen.paste(splash, (32, 0, splashSize[0]+32, splashSize[1]))
try:
self.device.display(screen)
except:
now = str(datetime.datetime.now())
now = now[0:19] # Truncate the microseconds
print(str(now) + ' Error displaying splash.')
def ClearDisplay(self):
try:
self.device.clear()
except:
now = str(datetime.datetime.now())
now = now[0:19] # Truncate the microseconds
print(str(now) + ' Error clearing display.')
def DisplayText(self, text):
with canvas(self.device) as draw:
font = ImageFont.truetype("impact.ttf", 42)
(font_width, font_height) = font.getsize(text)
try:
draw.text((128//2 - font_width//2, 64//2 - font_height//2), text, font=font, fill=255)
except:
now = str(datetime.datetime.now())
now = now[0:19] # Truncate the microseconds
print(str(now) + ' Error displaying text.')
# OnScreen Menu Controls
def EventDetect(self):
if(GPIO.input(self.up) == self.BUTTON_INPUT):
self.UpCallback(self.up)
if(GPIO.input(self.down) == self.BUTTON_INPUT):
self.DownCallback(self.down)
if(GPIO.input(self.enter) == self.BUTTON_INPUT):
self.EnterCallback(self.enter)
def UpCallback(self, pin):
self.menuactive = True
self.menutime = time.time()
self.MenuDisplay('up')
def DownCallback(self, pin):
self.menuactive = True
self.menutime = time.time()
self.MenuDisplay('down')
def EnterCallback(self, pin):
self.menuactive = True
self.menutime = time.time()
self.MenuDisplay('enter')
def MenuDisplay(self, action):
# If menu is not currently being displayed, check mode and draw menu
if(self.menu['current']['mode'] == 'none'):
control = ReadControl()
if (control['mode'] == 'Stop' or control['mode'] == 'Error' or control['mode'] == 'Monitor'): # If in an inactive mode
self.menu['current']['mode'] = 'inactive'
else: # Use the active menu
self.menu['current']['mode'] = 'active'
self.menu['current']['option'] = 0 # Set the menu option to the very first item in the list
print('Menu Active')
# If selecting the 'grill_hold_value', take action based on button press was
elif(self.menu['current']['mode'] == 'grill_hold_value'):
if(self.units == 'F'):
stepValue = 5 # change in temp each time button pressed
minTemp = 120 # minimum temperature set for hold
maxTemp = 500 # maximum temperature set for hold
else:
stepValue = 2 # change in temp each time button pressed
minTemp = 50 # minimum temperature set for hold
maxTemp = 260 # maximum temperature set for hold
if(action == 'down'):
self.menu['current']['option'] -= stepValue # Step down by stepValue degrees
if(self.menu['current']['option'] <= minTemp):
self.menu['current']['option'] = maxTemp # Roll over to maxTemp if you go less than 120.
elif(action == 'up'):
self.menu['current']['option'] += stepValue # Step up by stepValue degrees
if(self.menu['current']['option'] > maxTemp):
self.menu['current']['option'] = minTemp # Roll over to minTemp if you go greater than 500.
elif(action == 'enter'):
control = ReadControl()
control['setpoints']['grill'] = self.menu['current']['option']
control['updated'] = True
control['mode'] = 'Hold'
WriteControl(control)
self.menu['current']['mode'] = 'none'
self.menu['current']['option'] = 0
self.menuactive = False
self.menutime = 0
self.ClearDisplay()
# If selecting either active menu items or inactive menu items, take action based on what the button press was
else:
if(action == 'down'):
self.menu['current']['option'] -= 1
if(self.menu['current']['option'] < 0): # Check to make sure we haven't gone past 0
self.menu['current']['option'] = len(self.menu[self.menu['current']['mode']])-1
tempvalue = self.menu['current']['option']
tempmode = self.menu['current']['mode']
index = 0
selected = 'undefined'
for item in self.menu[tempmode]:
if(index == tempvalue):
selected = item
break
index += 1
print(f"Down pressed. Mode = {self.menu['current']['mode']} Value = {self.menu['current']['option']} Selected = {selected}")
elif(action == 'up'):
self.menu['current']['option'] += 1
if(self.menu['current']['option'] == len(self.menu[self.menu['current']['mode']])): # Check to make sure we haven't gone past the end of the menu
self.menu['current']['option'] = 0
tempvalue = self.menu['current']['option']
tempmode = self.menu['current']['mode']
index = 0
selected = 'undefined'
for item in self.menu[tempmode]:
if(index == tempvalue):
selected = item
break
index += 1
print(f"Up pressed. Mode = {self.menu['current']['mode']} Value = {self.menu['current']['option']} Selected = {selected}")
elif(action == 'enter'):
print(f"Enter pressed. Value = {self.menu['current']['option']}")
index = 0
selected = 'undefined'
for item in self.menu[self.menu['current']['mode']]:
if(index == self.menu['current']['option']):
selected = item
break
index += 1
# Inactive Mode Items
if(selected == 'Startup'):
print('Startup Selected')
self.menu['current']['mode'] = 'none'
self.menu['current']['option'] = 0
self.menuactive = False
self.menutime = 0
control = ReadControl()
control['updated'] = True
control['mode'] = 'Startup'
WriteControl(control)
elif(selected == 'Monitor'):
print('Monitor Selected')
self.menu['current']['mode'] = 'none'
self.menu['current']['option'] = 0
self.menuactive = False
self.menutime = 0
control = ReadControl()
control['updated'] = True
control['mode'] = 'Monitor'
WriteControl(control)
elif(selected == 'Stop'):
print('Stop Selected')
self.menu['current']['mode'] = 'none'
self.menu['current']['option'] = 0
self.menuactive = False
self.menutime = 0
self.ClearDisplay()
control = ReadControl()
control['updated'] = True
control['mode'] = 'Stop'
WriteControl(control)
# Active Mode
elif(selected == 'Shutdown'):
print('Shutdown Selected')
self.menu['current']['mode'] = 'none'
self.menu['current']['option'] = 0
self.menuactive = False
self.menutime = 0
self.ClearDisplay()
control = ReadControl()
control['updated'] = True
control['mode'] = 'Shutdown'
WriteControl(control)
elif(selected == 'Hold'):
print('Hold Selected')
self.menu['current']['mode'] = 'grill_hold_value'
if(self.units == 'F'):
self.menu['current']['option'] = 225 # start at 225 for F
else:
self.menu['current']['option'] = 100 # start at 100 for C
elif(selected == 'Smoke'):
print('Smoke Selected')
self.menu['current']['mode'] = 'none'
self.menu['current']['option'] = 0
self.menuactive = False
self.menutime = 0
self.ClearDisplay()
control = ReadControl()
control['updated'] = True
control['mode'] = 'Smoke'
WriteControl(control)
elif(selected == 'SmokePlus'):
print('Smoke Plus Selected')
self.menu['current']['mode'] = 'none'
self.menu['current']['option'] = 0
self.menuactive = False
self.menutime = 0
self.ClearDisplay()
control = ReadControl()
if(control['s_plus'] == True):
control['s_plus'] = False
else:
control['s_plus'] = True
WriteControl(control)
if(self.menu['current']['mode'] == 'grill_hold_value'):
print(f"Grill Set Point = {self.menu['current']['option']}")
with canvas(self.device) as draw:
# Grill Temperature (Large Centered)
font = ImageFont.truetype("impact.ttf", 42)
text = str(self.menu['current']['option'])
(font_width, font_height) = font.getsize(text)
draw.text((128//2 - font_width//2,0), text, font=font, fill=255)
# Current Mode (Bottom Center)
font = ImageFont.truetype("trebuc.ttf", 18)
text = "Grill Set Point"
(font_width, font_height) = font.getsize(text)
draw.text((128//2 - font_width//2, 64 - font_height), text, font=font, fill=255)
# Up / Down Arrows (Middle Right)
font = ImageFont.truetype("FA-Free-Solid.otf", 30)
text = '\uf0dc' # FontAwesome Icon Sort (Up/Down Arrows)
(font_width, font_height) = font.getsize(text)
draw.text(((128 - font_width), (64//2 - font_height//2)), text, font=font, fill=255)
elif(self.menu['current']['mode'] != 'none'):
with canvas(self.device) as draw:
# Menu Option (Large Top Center)
index = 0
selected = 'undefined'
for item in self.menu[self.menu['current']['mode']]:
if(index == self.menu['current']['option']):
selected = item
break
index += 1
font = ImageFont.truetype("FA-Free-Solid.otf", 42)
text = self.menu[self.menu['current']['mode']][selected]['icon']
(font_width, font_height) = font.getsize(text)
draw.text((128//2 - font_width//2,0), text, font=font, fill=255)
# Draw a Plus Icon over the top of the Smoke Icon
if(selected == 'SmokePlus'):
font = ImageFont.truetype("FA-Free-Solid.otf", 32)
text = '\uf067' # FontAwesome Icon for PLUS
(font_width, font_height) = font.getsize(text)
draw.text((128//2 - font_width//2,4), text, font=font, fill=0)
# Current Mode (Bottom Center)
font = ImageFont.truetype("trebuc.ttf", 18)
text = self.menu[self.menu['current']['mode']][selected]['displaytext']
(font_width, font_height) = font.getsize(text)
draw.text((128//2 - font_width//2, 64 - font_height), text, font=font, fill=255)
# Up / Down Arrows (Middle Right)
font = ImageFont.truetype("FA-Free-Solid.otf", 30)
text = '\uf0dc' # FontAwesome Icon Sort (Up/Down Arrows)
(font_width, font_height) = font.getsize(text)
draw.text(((128 - font_width), (64//2 - font_height//2)), text, font=font, fill=255)