-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display_ili9341.py
323 lines (279 loc) · 14.3 KB
/
display_ili9341.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
#!/usr/bin/env python3
# *****************************************
# PiFire Display Interface Library
# *****************************************
#
# Description: This library supports using
# the ILI9341 display with 240Wx320H resolution.
# This module utilizes Luma.LCD to interface
# this display.
#
# Dependancies:
# sudo pip3 install Pillow
# sudo apt install ttf-mscorefonts-installer
# sudo pip3 install luma.lcd
#
# *****************************************
# *****************************************
# Imported Libraries
# *****************************************
from luma.core.interface.serial import spi
from luma.core.render import canvas
from luma.lcd.device import ili9341
from PIL import Image, ImageDraw, ImageFont
import time
class Display:
def __init__(self, units='F'):
# Set Display Width and Height. Modify for your needs.
self.WIDTH = 320
self.HEIGHT = 240
# Init Device
self.serial = spi(port=0, device=0, gpio_DC=24, gpio_RST=25, bus_speed_hz=32000000, reset_hold_time=0.2, reset_release_time=0.2)
self.device = ili9341(self.serial, active_low=False, width=self.WIDTH, height=self.HEIGHT, gpio_LIGHT=5)
self.units = units
self.DisplaySplash()
time.sleep(0.5) # Keep the splash up for three seconds on boot-up - you can certainly disable this if you want
self.ClearDisplay()
def DisplayStatus(self, in_data, status_data):
self.units = status_data['units'] # Update units in case there was a change since instantiation
# Create canvas
img = Image.new('RGB', (self.WIDTH, self.HEIGHT), color=(0, 0, 0))
background = Image.open('background.jpg')
# Resize the boot-splash
background = background.resize((self.WIDTH, self.HEIGHT))
# Set the position
position = (0,0)
# Paste the splash screen onto the canvas
img.paste(background, position)
# Create drawing object
draw = ImageDraw.Draw(img)
# Grill Temp Circle
draw.ellipse((80, 10, 240, 170), fill=(50, 50, 50)) # Grey Background Circle
if in_data['GrillTemp'] < 0:
endpoint = 0
elif self.units == 'F':
endpoint = ((360*in_data['GrillTemp']) // 600) + 90
else:
endpoint = ((360*in_data['GrillTemp']) // 300) + 90
draw.pieslice((80, 10, 240, 170), start=90, end=endpoint, fill=(200, 0, 0)) # Red Arc for Temperature
if (in_data['GrillSetPoint'] > 0):
if self.units == 'F':
setpoint = ((360*in_data['GrillSetPoint']) // 600) + 90
else:
setpoint = ((360*in_data['GrillSetPoint']) // 300) + 90
draw.pieslice((80, 10, 240, 170), start=setpoint-2, end=setpoint+2, fill=(255, 255, 0)) # Yellow Arc for SetPoint
draw.ellipse((90, 20, 230, 160), fill=(0, 0, 0)) # Black Circle for Center
# Grill Temp Label
font = ImageFont.truetype("trebuc.ttf", 16)
text = "Grill"
(font_width, font_height) = font.getsize(text)
draw.text((self.WIDTH//2 - font_width//2,20), text, font=font, fill=(255,255,255))
# Grill Set Point (Small Centered Top)
if (in_data['GrillSetPoint'] > 0):
font = ImageFont.truetype("trebuc.ttf", 16)
text = ">" + str(in_data['GrillSetPoint'])[:5] + "<"
(font_width, font_height) = font.getsize(text)
draw.text((self.WIDTH//2 - font_width//2, 45 - font_height//2), text, font=font, fill=(0,200,255))
# Grill Temperature (Large Centered)
if(self.units == 'F'):
font = ImageFont.truetype("trebuc.ttf", 80)
text = str(in_data['GrillTemp'])[:5]
(font_width, font_height) = font.getsize(text)
draw.text((self.WIDTH//2 - font_width//2,40), text, font=font, fill=(255,255,255))
else:
font = ImageFont.truetype("trebuc.ttf", 55)
text = str(in_data['GrillTemp'])[:5]
(font_width, font_height) = font.getsize(text)
draw.text((self.WIDTH//2 - font_width//2,56), text, font=font, fill=(255,255,255))
# Draw Grill Temp Scale Label
text = "°" + self.units
font = ImageFont.truetype("trebuc.ttf", 24)
(font_width, font_height) = font.getsize(text)
draw.text((self.WIDTH//2 - font_width//2, self.HEIGHT//2 - font_height//2 + 10), text, font=font, fill=(255, 255, 255))
# PROBE1 Temp Circle
draw.ellipse((10, self.HEIGHT//2 + 10, 110, self.HEIGHT//2 + 110), fill=(50, 50, 50))
if in_data['Probe1Temp'] < 0:
endpoint = 0
elif self.units == 'F':
endpoint = ((360*in_data['Probe1Temp']) // 300) + 90
else:
endpoint = ((360*in_data['Probe1Temp']) // 150) + 90
draw.pieslice((10, self.HEIGHT//2 + 10, 110, self.HEIGHT//2 + 110), start=90, end=endpoint, fill=(3, 161, 252))
if (in_data['Probe1SetPoint'] > 0):
if self.units == 'F':
setpoint = ((360*in_data['Probe1SetPoint']) // 300) + 90
else:
setpoint = ((360*in_data['Probe1SetPoint']) // 150) + 90
draw.pieslice((10, self.HEIGHT//2 + 10, 110, self.HEIGHT//2 + 110), start=setpoint-2, end=setpoint+2, fill=(255, 255, 0)) # Yellow Arc for SetPoint
draw.ellipse((20, self.HEIGHT//2 + 20, 100, self.HEIGHT//2 + 100), fill=(0, 0, 0))
# PROBE1 Temp Label
font = ImageFont.truetype("trebuc.ttf", 16)
text = "Probe-1"
(font_width, font_height) = font.getsize(text)
draw.text((60 - font_width//2, self.HEIGHT//2 + 40 - font_height//2), text, font=font, fill=(255,255,255))
# PROBE1 Temperature (Large Centered)
if(self.units == 'F'):
font = ImageFont.truetype("trebuc.ttf", 36)
else:
font = ImageFont.truetype("trebuc.ttf", 30)
text = str(in_data['Probe1Temp'])[:5]
(font_width, font_height) = font.getsize(text)
draw.text((60 - font_width//2, self.HEIGHT//2 + 60 - font_height//2), text, font=font, fill=(255,255,255))
# PROBE1 Set Point (Small Centered Bottom)
if (in_data['Probe1SetPoint'] > 0):
font = ImageFont.truetype("trebuc.ttf", 16)
text = ">" + str(in_data['Probe1SetPoint'])[:5] + "<"
(font_width, font_height) = font.getsize(text)
draw.text((60 - font_width//2, self.HEIGHT//2 + 85 - font_height//2), text, font=font, fill=(0,200,255))
# PROBE2 Temp Circle
draw.ellipse((self.WIDTH - 110, self.HEIGHT//2 + 10, self.WIDTH - 10, self.HEIGHT//2 + 110), fill=(50, 50, 50))
if in_data['Probe2Temp'] < 0:
endpoint = 0
elif self.units == 'F':
endpoint = ((360*in_data['Probe2Temp']) // 300) + 90
else:
endpoint = ((360*in_data['Probe2Temp']) // 150) + 90
draw.pieslice((self.WIDTH - 110, self.HEIGHT//2 + 10, self.WIDTH - 10, self.HEIGHT//2 + 110), start=90, end=endpoint, fill=(3, 161, 252))
if (in_data['Probe2SetPoint'] > 0):
if self.units == 'F':
setpoint = ((360*in_data['Probe2SetPoint']) // 300) + 90
else:
setpoint = ((360*in_data['Probe2SetPoint']) // 150) + 90
draw.pieslice((self.WIDTH - 110, self.HEIGHT//2 + 10, self.WIDTH - 10, self.HEIGHT//2 + 110), start=setpoint-2, end=setpoint+2, fill=(255, 255, 0)) # Yellow Arc for SetPoint
draw.ellipse((self.WIDTH - 100, self.HEIGHT//2 + 20, self.WIDTH - 20, self.HEIGHT//2 + 100), fill=(0, 0, 0))
# PROBE2 Temp Label
font = ImageFont.truetype("trebuc.ttf", 16)
text = "Probe-2"
(font_width, font_height) = font.getsize(text)
draw.text((self.WIDTH - 60 - font_width//2, self.HEIGHT//2 + 40 - font_height//2), text, font=font, fill=(255,255,255))
# PROBE2 Temperature (Large Centered)
if(self.units == 'F'):
font = ImageFont.truetype("trebuc.ttf", 36)
else:
font = ImageFont.truetype("trebuc.ttf", 30)
text = str(in_data['Probe2Temp'])[:5]
(font_width, font_height) = font.getsize(text)
draw.text((self.WIDTH - 60 - font_width//2, self.HEIGHT//2 + 60 - font_height//2), text, font=font, fill=(255,255,255))
# PROBE2 Set Point (Small Centered Bottom)
if (in_data['Probe2SetPoint'] > 0):
font = ImageFont.truetype("trebuc.ttf", 16)
text = ">" + str(in_data['Probe2SetPoint'])[:5] + "<"
(font_width, font_height) = font.getsize(text)
draw.text((self.WIDTH - 60 - font_width//2, self.HEIGHT//2 + 85 - font_height//2), text, font=font, fill=(0,200,255))
# Active Outputs
font = ImageFont.truetype("FA-Free-Solid.otf", 36)
if(status_data['outpins']['fan']==0):
#F = Fan (Upper Left), 40x40, origin 10,10
text = '\uf863'
(font_width, font_height) = font.getsize(text)
draw = self.rounded_rectangle(draw, (self.WIDTH//8 - 22, self.HEIGHT//6 - 22, self.WIDTH//8 + 22, self.HEIGHT//6 + 22), 5, (0, 100, 255))
draw = self.rounded_rectangle(draw, (self.WIDTH//8 - 20, self.HEIGHT//6 - 20, self.WIDTH//8 + 20, self.HEIGHT//6 + 20), 5, (0, 0, 0))
draw.text((self.WIDTH//8 - font_width//2 + 1, self.HEIGHT//6 - font_height//2), text, font=font, fill=(0,100,255))
if(status_data['outpins']['igniter']==0):
# I = Igniter(Center Right)
text = '\uf46a'
(font_width, font_height) = font.getsize(text)
draw = self.rounded_rectangle(draw, (7*(self.WIDTH//8) - 22, self.HEIGHT//2.5 - 22, 7*(self.WIDTH//8) + 22, self.HEIGHT//2.5 + 22), 5, (255, 200, 0))
draw = self.rounded_rectangle(draw, (7*(self.WIDTH//8) - 20, self.HEIGHT//2.5 - 20, 7*(self.WIDTH//8) + 20, self.HEIGHT//2.5 + 20), 5, (0, 0, 0))
draw.text((7*(self.WIDTH//8) - font_width//2, self.HEIGHT//2.5 - font_height//2), text, font=font, fill=(255,200,0))
if(status_data['outpins']['auger']==0):
# A = Auger (Center Left)
text = '\uf101'
(font_width, font_height) = font.getsize(text)
draw = self.rounded_rectangle(draw, (self.WIDTH//8 - 22, self.HEIGHT//2.5 - 22, self.WIDTH//8 + 22, self.HEIGHT//2.5 + 22), 5, (0, 255, 0))
draw = self.rounded_rectangle(draw, (self.WIDTH//8 - 20, self.HEIGHT//2.5 - 20, self.WIDTH//8 + 20, self.HEIGHT//2.5 + 20), 5, (0, 0, 0))
draw.text((self.WIDTH//8 - font_width//2 + 1, self.HEIGHT//2.5 - font_height//2 - 2), text, font=font, fill=(0,255,0))
# Notification Indicator (Right)
show_notify_indicator = False
for item in status_data['notify_req']:
if status_data['notify_req'][item] == True:
show_notify_indicator = True
if(show_notify_indicator == True):
font = ImageFont.truetype("FA-Free-Solid.otf", 36)
text = '\uf0f3'
(font_width, font_height) = font.getsize(text)
draw = self.rounded_rectangle(draw, (7*(self.WIDTH//8) - 22, self.HEIGHT//6 - 22, 7*(self.WIDTH//8) + 22, self.HEIGHT//6 + 22), 5, (255,255,0))
draw = self.rounded_rectangle(draw, (7*(self.WIDTH//8) - 20, self.HEIGHT//6 - 20, 7*(self.WIDTH//8) + 20, self.HEIGHT//6 + 20), 5, (0, 0, 0))
draw.text((7*(self.WIDTH//8) - font_width//2 + 1, self.HEIGHT//6 - font_height//2), text, font=font, fill=(255,255,0))
# Smoke Plus Inidicator
if(status_data['s_plus'] == True) and ((status_data['mode']=='Smoke') or (status_data['mode']=='Hold')):
draw = self.rounded_rectangle(draw, (7*(self.WIDTH//8) - 22, self.HEIGHT//2.5 - 22, 7*(self.WIDTH//8) + 22, self.HEIGHT//2.5 + 22), 5, (150, 0, 255))
draw = self.rounded_rectangle(draw, (7*(self.WIDTH//8) - 20, self.HEIGHT//2.5 - 20, 7*(self.WIDTH//8) + 20, self.HEIGHT//2.5 + 20), 5, (0, 0, 0))
font = ImageFont.truetype("FA-Free-Solid.otf", 32)
text = '\uf0c2' # FontAwesome Icon for Cloud (Smoke)
(font_width, font_height) = font.getsize(text)
draw.text((7*(self.WIDTH//8) - font_width//2, self.HEIGHT//2.5 - font_height//2), text, font=font, fill=(100,0,255))
font = ImageFont.truetype("FA-Free-Solid.otf", 24)
text = '\uf067' # FontAwesome Icon for PLUS
(font_width, font_height) = font.getsize(text)
draw.text((7*(self.WIDTH//8) - font_width//2, self.HEIGHT//2.5 - font_height//2 + 3), text, font=font, fill=(0,0,0))
# Grill Hopper Level (Lower Center)
font = ImageFont.truetype("trebuc.ttf", 16)
text = "Hopper:" + str(status_data['hopper_level']) + "%"
(font_width, font_height) = font.getsize(text)
if(status_data['hopper_level'] > 70):
hopper_color = (0,255,0)
elif(status_data['hopper_level'] > 30):
hopper_color = (255,150,0)
else:
hopper_color = (255,0,0)
draw = self.rounded_rectangle(draw, (self.WIDTH//2 - font_width//2 - 7, 156 - font_height//2, self.WIDTH//2 + font_width//2 + 7, 166 + font_height//2), 5, hopper_color)
draw = self.rounded_rectangle(draw, (self.WIDTH//2 - font_width//2 - 5, 158 - font_height//2, self.WIDTH//2 + font_width//2 + 5, 164 + font_height//2), 5, (0,0,0))
draw.text((self.WIDTH//2 - font_width//2, 160 - font_height//2), text, font=font, fill=hopper_color)
# Current Mode (Bottom Center)
font = ImageFont.truetype("trebuc.ttf", 36)
text = status_data['mode'] #+ ' Mode'
(font_width, font_height) = font.getsize(text)
draw = self.rounded_rectangle(draw, (self.WIDTH//2 - font_width//2 - 7, self.HEIGHT - font_height - 2, self.WIDTH//2 + font_width//2 + 7, self.HEIGHT-2), 5, (3, 161, 252))
draw = self.rounded_rectangle(draw, (self.WIDTH//2 - font_width//2 - 5, self.HEIGHT - font_height, self.WIDTH//2 + font_width//2 + 5, self.HEIGHT-4), 5, (255,255,255))
draw.text((self.WIDTH//2 - font_width//2, self.HEIGHT - font_height - 6), text, font=font, fill=(0,0,0))
# Display Image
self.device.backlight(True)
self.device.show()
self.device.display(img)
def DisplaySplash(self):
# Create canvas
img = Image.new('RGB', (self.WIDTH, self.HEIGHT), color=(0, 0, 0))
splash = Image.open('color-boot-splash.png')
(splash_width, splash_height) = splash.size
splash_width *= 2
splash_height *= 2
# Resize the boot-splash
splash = splash.resize((splash_width, splash_height))
# Set the position
position = ((self.WIDTH - splash_width)//2, (self.HEIGHT - splash_height)//2)
# Paste the splash screen onto the canvas
img.paste(splash, position)
# Display Image
self.device.backlight(True)
self.device.show()
self.device.display(img)
def ClearDisplay(self):
img = Image.new('RGB', (self.WIDTH, self.HEIGHT), color=(0, 0, 0))
self.device.display(img)
self.device.hide()
# Kill the backlight to the display
self.device.backlight(False)
def DisplayText(self, text):
# Create canvas
img = Image.new('RGB', (self.WIDTH, self.HEIGHT), color=(0, 0, 0))
# Create drawing object
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("impact.ttf", 42)
(font_width, font_height) = font.getsize(text)
draw.text((self.WIDTH//2 - font_width//2, self.HEIGHT//2 - font_height//2), text, font=font, fill=255)
# Display Image
self.device.backlight(True)
self.device.show()
self.device.display(img)
def rounded_rectangle(self, draw, xy, rad, fill=None):
x0, y0, x1, y1 = xy
draw.rectangle([ (x0, y0 + rad), (x1, y1 - rad) ], fill=fill)
draw.rectangle([ (x0 + rad, y0), (x1 - rad, y1) ], fill=fill)
draw.pieslice([ (x0, y0), (x0 + rad * 2, y0 + rad * 2) ], 180, 270, fill=fill)
draw.pieslice([ (x1 - rad * 2, y1 - rad * 2), (x1, y1) ], 0, 90, fill=fill)
draw.pieslice([ (x0, y1 - rad * 2), (x0 + rad * 2, y1) ], 90, 180, fill=fill)
draw.pieslice([ (x1 - rad * 2, y0), (x1, y0 + rad * 2) ], 270, 360, fill=fill)
return(draw)
def EventDetect(self):
return()