-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPracticeDisplayLCD.py
executable file
·353 lines (246 loc) · 10.2 KB
/
PracticeDisplayLCD.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
# PracticeDisplay object for MIDIbit project
# robcranfill
# Based on Adafruit code by ladyada, but repainting only when needed
import time
import digitalio
import board
from PIL import Image, ImageDraw, ImageFont
from adafruit_rgb_display import st7789
import RPi.GPIO as GPIO
# Display:
#
# CURRENT SESSION TIME in big font
# RUNNING TOTAL TIME in big font
# SESSION NUMBER in small font
# NOTES THIS SESSION in small font
#
#
# Device name small
FONT_SIZE_BIG = 48
FONT_SIZE_SMALL = 24
ELAPSED_TIME_Y = 50
SESSION_TIME_Y = 00
REGULAR_COLOR = "#FFFFFF"
HIGHLIGHT_COLOR = "#FF0000"
WIDGET_AREA_WIDTH = 30
WIDGET_EXECUTE_Y = 50
WIDGET_MOVE_Y = 150
HEADER_AREA_HEIGHT = 24
MAX_MENU_ITEMS = 9
def format_seconds(n_seconds):
return f"00:{(n_seconds // 60):02}:{(n_seconds % 60):02}"
# TODO: FIXME: Should the deinit method tidy up the GPIO pins? It does call deinit() is that enough?
class PracticeDisplay:
def __init__(self):
# Configuration for CS and DC pins
cs_pin = digitalio.DigitalInOut(board.CE0)
dc_pin = digitalio.DigitalInOut(board.D25)
reset_pin = None
# Display baudrate (default max is 24mhz)
BAUDRATE = 64000000
# Setup SPI bus using hardware SPI
spi = board.SPI()
# Create the ST7789 display
disp = st7789.ST7789(
spi,
cs=cs_pin,
dc=dc_pin,
rst=reset_pin,
baudrate=BAUDRATE,
width=240,
height=240,
x_offset=0,
y_offset=80,
)
# hang onto this...
self.disp_ = disp
# Create blank image for drawing.
# Make sure to create image with mode 'RGB' for full color.
height = disp.width # we swap height/width to rotate it to landscape (huh?)
width = disp.height
image = Image.new("RGB", (width, height))
rotation = 0
# ...and these
self.image_ = image
self.rotation_ = rotation
self.height_ = height
self.width_ = width
# Get drawing object to draw on image
draw = ImageDraw.Draw(image)
self.draw_ = draw
# Draw a black filled box to clear the image
draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0))
disp.image(image, rotation)
# Fonts to use.
# (Some other nice fonts to try: http://www.dafont.com/bitmap.php)
# small_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", FONT_SIZE_SMALL)
small_font = ImageFont.load_default(size=FONT_SIZE_SMALL)
self.small_font_ = small_font
# big_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", FONT_SIZE_BIG)
big_font = ImageFont.load_default(size=FONT_SIZE_BIG)
self.big_font_ = big_font
# Connect to the backlight and turn it on.
self.backlight_ = digitalio.DigitalInOut(board.D22)
self.backlight_.switch_to_output()
self.set_backlight_on(True)
# end __init__
def show_image(self, image_path):
self.draw_.rectangle((0, 0, self.width_, self.height_), outline=0, fill=(0, 0, 0))
self.disp_.image(self.image_)
new_image = Image.open(image_path)
# Scale the image to the smaller screen dimension
image_ratio = new_image.width / new_image.height
screen_ratio = self.width_ / self.height_
if screen_ratio < image_ratio:
scaled_width = new_image.width * self.height_ // new_image.height
scaled_height = self.height_
else:
scaled_width = self.width_
scaled_height = new_image.height * self.width_ // new_image.width
new_image = new_image.resize((scaled_width, scaled_height), Image.BICUBIC)
# Crop and center the image
x = scaled_width // 2 - self.width_ // 2
y = scaled_height // 2 - self.height_ // 2
new_image = new_image.crop((x, y, x + self.width_, y + self.height_))
# Display image.
self.disp_.image(new_image)
def set_backlight_on(self, on_state):
print(f"* Setting backlight {'on' if on_state else 'off'}")
self.backlight_.value = on_state
def __del__(self):
print("* Garbage-collecting display object")
self.clear_display()
self.set_backlight_on(False)
## not needed?
self.disp_ = None
self.image_ = None
self.rotation_ = None
self.height_ = None
self.width_ = None
self.big_font_ = None
self.small_font_ = None
# this may matter
self.backlight_.deinit()
self.backlight_ = None
# This, like update_display(), takes some time. Do not use willy-nilly!
def clear_display(self):
self.draw_.rectangle((0, 0, self.width_, self.height_), outline=0, fill=(0, 0, 0))
self.disp_.image(self.image_, self.rotation_)
# paint all the changes that have been made
def update_display(self):
# FIXME: This takes 0.6 seconds!
# start = time.time()
self.disp_.image(self.image_, self.rotation_)
# print(f"update_display: {(time.time() - start):0.2f} s")
def draw_text_in_color(self, line_number, string, color):
start = time.time()
x = 0
y = line_number * FONT_SIZE_SMALL
w = self.width_
h = FONT_SIZE_SMALL
# black out old text
# print(f"clearing {x}, {y}, {w}, {y+h}")
self.draw_.rectangle((0, y, w, y+h), outline=0, fill="#000000")
self.draw_.text((x, y), string, font=self.small_font_, fill=color)
# self.disp_.image(self.image_, self.rotation_)
# print(f"draw_text_in_color: {(time.time() - start):0.2f} s")
def draw_text_in_white(self, line_number, string):
self.draw_text_in_color(line_number, string, "#FFFFFF")
def show_elapsed_time(self, n_seconds):
x = 10
y = ELAPSED_TIME_Y
w = self.width_
h = FONT_SIZE_BIG
# black out old text
self.draw_.rectangle((0, y, w, y+h), outline=0, fill="#000000")
self.draw_.text((x, y), format_seconds(n_seconds), font=self.big_font_, fill="#00FFFF")
# self.disp_.image(self.image_, self.rotation_)
def show_session_time(self, n_seconds):
x = 10
y = SESSION_TIME_Y
w = self.width_
h = FONT_SIZE_BIG
# black out old text
self.draw_.rectangle((0, y, w, y+h), outline=0, fill="#000000")
self.draw_.text((x, y), format_seconds(n_seconds), font=self.big_font_, fill="#00FF00")
# self.disp_.image(self.image_, self.rotation_)
# def set_time_total(self, time_str):
# self.show_elapsed_time(time_str)
# def set_time_session(self, session_str):
# self.show_session_time(session_str)
def set_time_session_fg(self, fg_color_str):
print(f"set_time_session_fg {fg_color_str}")
def set_session_label(self, session_str):
self.draw_text_in_white(5, session_str)
def set_notes_label(self, notes_str):
self.draw_text_in_white(6, notes_str)
def show_timeout(self, n_timeout):
self.draw_text_in_color(6, f"Timeout: {n_timeout} sec", "#00FF00")
def set_status_blob(self, color):
self.draw_.rectangle((5, 200, 25, 225), outline=0, fill=color)
# self.disp_.image(self.image_, self.rotation_)
def set_device_name(self, device_str):
self.draw_text_in_color(9, device_str, "#00FF00")
# ************************************************** NEW MENU STUFF
def start_menu_mode(self, menu_data):
self.menu_data = menu_data
self.menu_item_selected = 0
self.clear_display()
self.update_menu_display()
def select_next_item(self):
self.menu_item_selected = (self.menu_item_selected + 1) % len(self.menu_data)
self.update_menu_display()
# def select_prev_item(self):
# self.menu_item_selected = (self.menu_item_selected -1) % len(self.menu_data)
# self.update_menu_display()
# draw according to menu_data and menu_selected_item
def update_menu_display(self):
i = 0
for m in self.menu_data:
c = "#00FF00" if i == self.menu_item_selected else "#FFFFFF"
self.draw_text_in_color(i, m["text"], c)
i += 1
self.update_display()
def test_simple():
print("\nRunning simple test code for PracticeDisplay....")
pd = PracticeDisplay()
pd.clear_display()
pd.draw_text_in_color(1, "Hey Cran!", "#FF0000")
pd.draw_text_in_color(2, " what's", "#00FF00")
pd.draw_text_in_color(3, " happening?", "#00FFFF")
pd.draw_text_in_white(4, " okeedokee??")
pd.update_display()
print("Displaying! Press ^C to break")
try:
while True:
pass
except KeyboardInterrupt:
print("\nCleaning up...")
pd = None
def test_timing():
print("\nRunning timing test code for PracticeDisplay....")
pd = PracticeDisplay()
pd.clear_display()
pd.draw_text_in_color(1, "Timing test", "#FF0000")
print("Displaying! Press ^C to break")
try:
n = 0
while True:
for n in [1, 2, 3, 4, 5]:
# no, don't clear the whole thing, that's slow
# pd.clear_display()
for i in range(n):
pd.draw_text_in_color(i+1, f"Painting iter {n}, item {i}", "#00FF00")
start = time.time()
pd.update_display()
print(f"update_display for {n}: {(time.time() - start):0.2f} s")
time.sleep(1)
start = time.time()
pd.clear_display()
print(f"clear display took: {(time.time() - start):0.2f} s")
except KeyboardInterrupt:
print("\nCleaning up...")
pd = None
if __name__ == "__main__":
# test_simple()
test_timing()