-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay_st7789p.py
executable file
·157 lines (129 loc) · 4.85 KB
/
display_st7789p.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
#!/usr/bin/env python3
# *****************************************
# PiFire Display Interface Library
# *****************************************
#
# Description: This library supports using the
# ST7789 SPI display with the __Pimoroni__ libraries.
#
# Dependancies: (Pimoroni ST7789 Library, Pillow, Numpy)
# sudo apt-get update
# sudo apt-get install python3-rpi.gpio python3-spidev python3-pip python3-pil python3-numpy
# sudo pip3 install st7789
#
# *****************************************
# *****************************************
# Imported Libraries
# *****************************************
import ST7789 as ST7789
from PIL import Image, ImageDraw, ImageFont
import datetime
import time
class Display:
def __init__(self, units='F'):
self.device = ST7789.ST7789(
port=0,
cs=0,
dc=24,
backlight=5,
rst=25,
rotation=0,
spi_speed_hz=80 * 1000 * 1000
)
self.units = units
self.WIDTH = self.device.width
self.HEIGHT = self.device.height
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']
# Turn on Backlight (just in case it was off)
self.device.set_backlight(1)
# 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))
try:
self.device.display(img)
except:
now = str(datetime.datetime.now())
now = now[0:19] # Truncate the microseconds
print(str(now) + ' ERROR displaying status.')
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)
self.device.display(img)
def ClearDisplay(self):
try:
# Create blank canvas
img = Image.new('RGB', (self.WIDTH, self.HEIGHT), color=(0, 0, 0))
self.device.display(img)
# Kill the backlight to the display
self.device.set_backlight(0)
except:
now = str(datetime.datetime.now())
now = now[0:19] # Truncate the microseconds
print(str(now) + ' Error clearing display.')
def DisplayText(self, text):
# Turn on Backlight (just in case it was off)
self.device.set_backlight(1)
# 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)
try:
self.device.display(img)
except:
now = str(datetime.datetime.now())
now = now[0:19] # Truncate the microseconds
print(str(now) + ' Error displaying text.')
def EventDetect(self):
return()