forked from nebhead/PiFire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display_pygame_64x128.py
295 lines (243 loc) · 8.63 KB
/
display_pygame_64x128.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
#!/usr/bin/env python3
'''
*****************************************
PiFire Display Interface Library
*****************************************
Description: This library supports using pygame
on your Linux development PC for debug and development
purposes. Only works in an graphical desktop
environment. Tested on Ubuntu 20.04.
*****************************************
'''
'''
Imported Libraries
'''
import time
import socket
import qrcode
import threading
import pygame
from PIL import Image, ImageDraw, ImageFont
'''
Display class definition
'''
class Display:
def __init__(self, buttonslevel='HIGH', rotation=0, units='F'):
# Init Global Variables and Constants
self.units = units
self.displayactive = False
self.in_data = None
self.status_data = None
self.displaytimeout = None
self.displaycommand = 'splash'
# Init Display Device, Input Device, Assets
self._init_globals()
self._init_assets()
self._init_display_device()
def _init_globals(self):
# Init constants and variables
self.WIDTH = 128
self.HEIGHT = 64
def _init_display_device(self):
# Setup & Start Display Loop Thread
display_thread = threading.Thread(target=self._display_loop)
display_thread.start()
def _display_loop(self):
# Init Device
pygame.init()
# set the pygame window name
pygame.display.set_caption('PiFire Device Display')
# Create Display Surface
self.display_surface = pygame.display.set_mode(size=(self.WIDTH, self.HEIGHT), flags=pygame.SHOWN)
self.displaycommand = 'splash'
'''
Main display loop
'''
while True:
pygame.time.delay(100)
events = pygame.event.get() # Gets events (required for keypresses to be registered)
''' Normal display loop'''
if self.displaytimeout:
if time.time() > self.displaytimeout:
self.displaycommand = 'clear'
if self.displaycommand == 'clear':
self.displayactive = False
self.displaytimeout = None
self.displaycommand = None
self._display_clear()
if self.displaycommand == 'splash':
self.displayactive = True
self._display_splash()
self.displaytimeout = time.time() + 3
self.displaycommand = None
time.sleep(3) # Hold splash screen for 3 seconds
if self.displaycommand == 'text':
self.displayactive = True
self._display_text()
self.displaycommand = None
self.displaytimeout = time.time() + 10
if self.displaycommand == 'network':
self.displayactive = True
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
networkip = s.getsockname()[0]
if (networkip != ''):
self._display_network(networkip)
self.displaytimeout = time.time() + 30
self.displaycommand = None
else:
self.display_text("No IP Found")
if self.displayactive:
if not self.displaytimeout:
if (self.in_data is not None) and (self.status_data is not None):
self._display_current(self.in_data, self.status_data)
'''
============== Graphics / Display / Draw Methods =============
'''
def _init_assets(self):
self._init_splash()
def _init_splash(self):
self.splash = Image.open('color-boot-splash.png') \
.transform((self.WIDTH, self.HEIGHT), Image.AFFINE, (1, 0, 0, 0, 1, 0), Image.BILINEAR) \
.convert("L") \
.convert("1")
self.splashSize = self.splash.size
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 _display_clear(self):
self.display_surface.fill((0,0,0))
pygame.display.update()
def _display_splash(self):
# Create canvas
screen = Image.new('RGB', (self.WIDTH, self.HEIGHT), color=0)
screen.paste(self.splash, (32, 0, self.splashSize[0]+32, self.splashSize[1]))
# Convert to PyGame and Display
strFormat = screen.mode
size = screen.size
raw_str = screen.tobytes("raw", strFormat)
self.display_image = pygame.image.fromstring(raw_str, size, strFormat)
self.display_surface.fill((255,255,255))
self.display_surface.blit(self.display_image, (0, 0))
pygame.display.update()
def _display_text(self):
# 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(self.displaydata)
draw.text((self.WIDTH // 2 - font_width // 2, self.HEIGHT // 2 - font_height // 2), self.displaydata, font=font, fill=255)
# Convert to PyGame and Display
strFormat = img.mode
size = img.size
raw_str = img.tobytes("raw", strFormat)
self.display_image = pygame.image.fromstring(raw_str, size, strFormat)
self.display_surface.fill((255,255,255))
self.display_surface.blit(self.display_image, (0, 0))
pygame.display.update()
def _display_network(self, networkip):
# Create canvas
img = Image.new('RGB', (self.WIDTH, self.HEIGHT), color=(255, 255, 255))
img_qr = qrcode.make('http://' + networkip)
img_qr_width, img_qr_height = img_qr.size
img_qr_width *= 2
img_qr_height *= 2
w = min(self.WIDTH, self.HEIGHT)
new_image = img_qr.resize((w, w))
position = (int((self.WIDTH/2)-(w/2)), 0)
img.paste(new_image, position)
# Convert to PyGame and Display
strFormat = img.mode
size = img.size
raw_str = img.tobytes("raw", strFormat)
self.display_image = pygame.image.fromstring(raw_str, size, strFormat)
self.display_surface.fill((255,255,255))
self.display_surface.blit(self.display_image, (0, 0))
pygame.display.update()
def _display_current(self, in_data, status_data):
self.units = status_data['units']
# Create canvas
img = Image.new('RGB', (self.WIDTH, self.HEIGHT), color=(0, 0, 0))
# Create drawing object
draw = ImageDraw.Draw(img)
# 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((self.WIDTH//2 - font_width//2,0), text, font=font, fill=(255,255,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,255,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,255,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,255,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,255,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((self.WIDTH - font_width, 0), text, font=font, fill=(255,255,255))
# Convert to PyGame and Display
strFormat = img.mode
size = img.size
raw_str = img.tobytes("raw", strFormat)
self.display_image = pygame.image.fromstring(raw_str, size, strFormat)
self.display_surface.fill((255,255,255))
self.display_surface.blit(self.display_image, (0, 0))
pygame.display.update()
'''
================ Externally Available Methods ================
'''
def display_status(self, in_data, status_data):
'''
- Updates the current data for the display loop, if in a work mode
'''
self.units = status_data['units']
self.displayactive = True
self.in_data = in_data
self.status_data = status_data
def display_splash(self):
'''
- Calls Splash Screen
'''
self.displaycommand = 'splash'
def clear_display(self):
'''
- Clear display and turn off backlight
'''
self.displaycommand = 'clear'
def display_text(self, text):
'''
- Display some text
'''
self.displaycommand = 'text'
self.displaydata = text
def display_network(self):
'''
- Display Network IP QR Code
'''
self.displaycommand = 'network'