-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display_pygame.py
executable file
·169 lines (130 loc) · 5.16 KB
/
display_pygame.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
#!/usr/bin/env python3
# *****************************************
# PiFire Display Interface Library
# *****************************************
#
# Description: This library supports using pygame
# on your development PC for debug and development
# purposes. Likely only works in an desktop
# environment. Tested on Ubuntu 20.04.
#
# Edit the WIDTH / HEIGHT constants below to
# simulate your screen size.
#
# Dependancies:
# sudo pip3 install pygame Pillow
# sudo apt install ttf-mscorefonts-installer
#
# *****************************************
# *****************************************
# Imported Libraries
# *****************************************
import pygame
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 = 240
self.HEIGHT = 240
self.units = units
# Activate PyGame
pygame.init()
# Create Display Surface
self.display_surface = pygame.display.set_mode((self.WIDTH, self.HEIGHT ))
# set the pygame window name
pygame.display.set_caption('PiFire Device Display')
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']
# 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("trebuc.ttf", 128)
else:
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,0), text, font=font, fill=(255,255,255))
# Active Outputs F = Fan (Left), I = Igniter(Center Left), A = Auger (Center Right)
font = ImageFont.truetype("FA-Free-Solid.otf", 48)
if(status_data['outpins']['fan']==0):
text = '\uf863'
(font_width, font_height) = font.getsize(text)
draw.text(( ((self.WIDTH//8)*1) - font_width//2, self.HEIGHT - 96), text, font=font, fill=(0,0,255))
if(status_data['outpins']['igniter']==0):
text = '\uf46a'
(font_width, font_height) = font.getsize(text)
draw.text(( ((self.WIDTH//8)*3) - font_width//2, self.HEIGHT - 96), text, font=font, fill=(255,200,0))
if(status_data['outpins']['auger']==0):
text = '\uf101'
(font_width, font_height) = font.getsize(text)
draw.text(( ((self.WIDTH//8)*5) - font_width//2, self.HEIGHT - 96), text, font=font, fill=(0,255,0))
# Notification Indicator (Right)
font = ImageFont.truetype("FA-Free-Solid.otf", 48)
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//8)*7) - font_width//2, self.HEIGHT - 96), text, font=font, fill=(255,255,0))
# Current Mode (Bottom Center)
font = ImageFont.truetype("trebuc.ttf", 36)
text = status_data['mode'] #+ ' Mode'
(font_width, font_height) = font.getsize(text)
draw.text((self.WIDTH//2 - font_width//2, self.HEIGHT - font_height - 4), 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()
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)
# 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 ClearDisplay(self):
# Fill with black
self.display_surface.fill((0,0,0))
pygame.display.update()
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)
# 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 EventDetect(self):
return()