forked from penguintutor/picow-railway-departure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display_utils.py
532 lines (420 loc) · 15.7 KB
/
display_utils.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
"""
Author: Adam Knowles
Version: 0.1
Name: display_utils.py
Description: Display utils for the Pico departure boards
GitHub Repository: https://github.com/Pharkie/picow-railway-departure
License: GNU General Public License (GPL)
"""
import asyncio
import utime
from micropython import const
import config
# from utils_logger import log_message
async def display_init_message_1screen(oled, screen_number, total_screens):
"""
Displays an initialization message on a single screen of the OLED display.
Parameters:
oled (OLED): The OLED display object.
screen_number (int): The current screen number.
total_screens (int): The total number of screens.
Side Effects:
Updates the OLED display with the initialization message.
"""
async with oled.oled_lock:
oled.fill(0)
oled.fd_oled.print_str("Loading", 0, config.LINEONE_Y)
oled.fd_oled.print_str("Pico departures", 0, config.THIN_LINETWO_Y)
oled.fd_oled.print_str(
f"Screen {screen_number} of {total_screens}",
0,
config.THIN_LINETHREE_Y,
)
oled.show()
async def display_init_message(oled1, oled2):
"""
Displays an initialization message on one or two OLED displays.
Parameters:
oled1 (OLED): The first OLED display object.
oled2 (OLED): The second OLED display object, if available.
Raises:
RuntimeError: If oled1 is not available.
Side Effects:
Updates the OLED displays with the initialization message.
"""
if not oled1:
raise RuntimeError("oled1 not available")
total_screens = 1 if not oled2 else 2
for screen_number, oled in enumerate((oled1, oled2), start=1):
if oled:
await display_init_message_1screen(
oled, screen_number, total_screens
)
async def display_departure_line(
oled, departure_number, destination, time_scheduled, y_pos
):
"""
Asynchronously displays a departure line on the OLED display.
Parameters:
oled (OLED): The OLED display object.
departure_number (str): The departure number to display.
destination (str): The destination to display.
time_scheduled (str): The scheduled time to display.
y_pos (int): The y position on the display to start at.
Side Effects:
Updates the OLED display with the departure line.
"""
# log_message(f"display_departure_line() showing train to {destination}", level="DEBUG")
_max_length = const(12)
# Split the destination into lines of up to max_length characters each, breaking
# at word boundaries
lines = wrap_text(destination, _max_length)
while True:
# Don't do exception handling here, let it bubble up to the main loop
for line in lines:
await clear_line(oled, y_pos)
async with oled.oled_lock:
oled.fd_oled.print_str(departure_number + line, 0, y_pos)
oled.fill_rect(
97, y_pos, config.DISPLAY_WIDTH, config.THIN_LINE_HEIGHT, 0
) # Make room for time
oled.fd_oled.print_str(time_scheduled, 99, y_pos)
oled.show()
await asyncio.sleep(3)
async def display_first_departure(
oled, first_departure, rail_data_instance, screen_number
):
"""
Asynchronously displays the first departure on the OLED display.
Parameters:
oled (OLED): The OLED display object.
rail_data_instance (RailDataInstance): The rail data instance.
screen_number (int): The screen number.
Side Effects:
Updates the OLED display with the first departure data.
"""
first_departure_task = None
if screen_number == 1:
first_departure_task = rail_data_instance.oled1_first_departure_task
elif screen_number == 2:
first_departure_task = rail_data_instance.oled2_first_departure_task
# If a display_first_departure_task is already running, cancel it
if first_departure_task:
first_departure_task.cancel()
try:
await first_departure_task
except asyncio.CancelledError:
pass
if first_departure:
new_task = asyncio.create_task(
display_departure_line(
oled,
"1 ",
first_departure["destination"],
first_departure["time_scheduled"],
config.LINEONE_Y,
)
)
if screen_number == 1:
rail_data_instance.oled1_first_departure_task = new_task
elif screen_number == 2:
rail_data_instance.oled2_first_departure_task = new_task
await asyncio.sleep(3)
time_estimated = first_departure["time_estimated"]
await clear_line(oled, config.THICK_LINETWO_Y)
# Second line: show "on time" or estimated time
if time_estimated == "On time":
await display_centred_text(
oled, "Due on time", config.THICK_LINETWO_Y
)
else:
await display_centred_text(
oled, f"Now due: {time_estimated}", config.THICK_LINETWO_Y
)
await asyncio.sleep(3)
await clear_line(oled, config.THIN_LINETWO_Y)
await asyncio.sleep(2)
# Second line: scroll the calling points
await scroll_text(
oled, format_calling_points(first_departure), config.THICK_LINETWO_Y
)
await asyncio.sleep(3)
if screen_number == 1 and rail_data_instance.oled1_first_departure_task:
rail_data_instance.oled1_first_departure_task.cancel()
elif (
screen_number == 2 and rail_data_instance.oled2_first_departure_task
):
rail_data_instance.oled2_first_departure_task.cancel()
async def display_second_departure(oled, second_departure):
"""
This coroutine displays the second departure on the OLED screen.
Parameters:
oled: The OLED display object.
departures: A list of departures to display.
"""
# print(f"Displaying second departure: {second_departure}")
await clear_line(oled, config.THIN_LINETWO_Y)
display_departure_task = asyncio.create_task(
display_departure_line(
oled,
"2 ",
second_departure["destination"],
second_departure["time_scheduled"],
config.THIN_LINETWO_Y,
)
)
await asyncio.sleep(6)
if display_departure_task:
display_departure_task.cancel()
async def display_no_departures(oled):
"""
This coroutine displays a "No departures" message on the OLED screen.
Parameters:
oled: The OLED display object.
"""
await clear_line(oled, config.LINEONE_Y)
await clear_line(oled, config.THIN_LINETWO_Y)
line1_message = "No departures"
async with oled.oled_lock:
oled.fd_oled.print_str(
line1_message,
centre_x(line1_message, config.THIN_CHAR_WIDTH),
config.LINEONE_Y,
)
line2_message = "in next 2 hours"
oled.fd_oled.print_str(
line2_message,
centre_x(line2_message, config.THIN_CHAR_WIDTH),
config.THIN_LINETWO_Y,
)
oled.show()
await asyncio.sleep(12)
async def both_screen_text(
oled1,
oled2,
text1,
y1,
text2=None,
y2=None,
text3=None,
y3=None,
):
"""
Displays up to three lines of text on both OLED displays.
Parameters:
oled1, oled2 (OLED): The OLED display objects.
text1, text2, text3 (str): The text lines to display. If None, the line is not displayed.
y1, y2, y3 (int): The y positions on the display to start each line at.
Effect:
Updates both OLED displays with the provided text lines.
"""
for oled in (oled1, oled2):
if oled is not None:
async with oled.oled_lock:
oled.fill(0)
oled.fd_oled.print_str(text1, 0, y1)
if text2 is not None and y2 is not None:
oled.fd_oled.print_str(text2, 0, y2)
if text3 is not None and y3 is not None:
oled.fd_oled.print_str(text3, 0, y3)
oled.show()
def format_calling_points(departure):
"""
Formats the calling points of a departure into a string.
Parameters:
departure (dict): The departure data, including 'subsequentCallingPoints' and 'operator'.
Returns:
str: A string describing the calling points and the operator of the departure.
"""
calling_points = departure["subsequentCallingPoints"]
if not calling_points:
return f"Calling at destination only. Operator: {departure['operator']}"
# Format all but the last calling point
calling_points_text = "Calling at: " + ", ".join(
f"{calling_point[0]} {calling_point[1]}"
for calling_point in calling_points[:-1]
)
# Add 'and' before the last calling point
last_calling_point = f"{calling_points[-1][0]} {calling_points[-1][1]}"
if calling_points_text and len(calling_points) > 1:
calling_points_text += f" and {last_calling_point}"
else:
calling_points_text = f"Calling at: {last_calling_point}"
# Add operator at the end
calling_points_text += f" ({departure['operator']})"
return calling_points_text
async def display_centred_text(oled, text, y):
"""
This function displays a given text centered on the OLED display at a given y-coordinate.
Parameters:
oled: The OLED display object.
text: The text string to display.
y: The y-coordinate at which to display the text.
"""
text_width = len(text) * config.THICK_CHAR_WIDTH # 8 pixels per character
x = max(
0, (config.DISPLAY_WIDTH - text_width) // 2
) # Calculate x-coordinate to center text
async with oled.oled_lock:
oled.fill_rect(0, y, config.DISPLAY_WIDTH, 8, 0) # Clear the line
oled.text(text, x, y) # Display the text
oled.show()
def centre_x(text, char_width):
"""
Calculates the x-coordinate to centre a text message on the display.
Parameters:
text (str): The text message to be displayed.
char_width (int): The width of a character on the display.
Returns:
int: The x-coordinate to start the text at to centre it on the display.
"""
message_width = len(text) * char_width
x = (config.DISPLAY_WIDTH - message_width) // 2
# print(f"centre_x() called with text: {text}, char_width: {char_width}" +
# f" message_width: {message_width}, x: {x}")
return x
def wrap_text(text, max_length):
"""
This function wraps a given text to a specified maximum length.
Parameters:
text: The text string to wrap.
max_length: The maximum length of a line of text.
Returns:
lines: A list of lines of text, each line being no longer than max_length.
"""
words = text.split(" ")
lines = []
current_line = ""
for word in words:
if len(current_line) + len(word) <= max_length or not current_line:
current_line += " " + word
else:
lines.append(current_line.strip())
current_line = word
lines.append(current_line.strip())
return lines
async def clear_display(oled):
"""
This function clears the OLED display.
Parameters:
oled: The OLED display object.
"""
async with oled.oled_lock:
oled.fill(0)
oled.show()
async def clear_line(oled, y):
"""
This function clears a specific line on the OLED display.
Parameters:
oled: The OLED display object.
y: The y-coordinate of the line to clear.
"""
async with oled.oled_lock:
oled.fill_rect(0, y, config.DISPLAY_WIDTH, config.THICK_LINE_HEIGHT, 0)
async def display_clock(oled):
"""
This coroutine displays the current time on an OLED screen.
Parameters:
oled: The OLED display object.
"""
time_format = "{:02d}:{:02d}:{:02d}"
offline_string = "[offline]"
# Don't do Exception handling here, let it bubble up to the main loop
while True:
current_time = utime.localtime()
current_seconds = utime.time()
# async with oled.oled_lock:
# Clear where the time is displayed without clearing whole line to save time (?)
oled.fill_rect(
40, config.THIN_LINETHREE_Y, 80, config.THIN_LINE_HEIGHT, 0
)
# offline_string_turn is true for 2 seconds every 15 seconds
offline_string_turn = config.OFFLINE_MODE and current_seconds % 15 < 2
if offline_string_turn:
oled.fd_oled.print_str(offline_string, 40, config.THIN_LINETHREE_Y)
else:
clock_string = time_format.format(
current_time[3], current_time[4], current_time[5]
)
# log_message(f"display_clock() updating {oled}", level="DEBUG")
oled.fd_oled.print_str(clock_string, 46, config.THIN_LINETHREE_Y)
oled.show()
await asyncio.sleep(0.9)
async def display_travel_alert(oled, alert_message):
"""
This coroutine displays a travel alert message on the OLED screen.
Parameters:
oled: The OLED display object.
nrcc_message: A travel alert message to display.
"""
max_lines_per_screen = 2 # Maximum number of lines per screen
max_chars_per_line = 18 # Maximum number of characters per line
preroll_text = "Travel Alert"
preroll_centre_x = (
config.DISPLAY_WIDTH - len(preroll_text) * config.THIN_CHAR_WIDTH
) // 2 # Center the text
await clear_line(oled, config.LINEONE_Y)
await clear_line(oled, config.THIN_LINETWO_Y)
# Flash the alert text before displaying the message
for _ in range(2):
async with oled.oled_lock:
oled.fd_oled.print_str(
preroll_text, preroll_centre_x, config.LINEONE_Y
)
oled.show()
await asyncio.sleep(0.5)
await clear_line(oled, config.LINEONE_Y)
async with oled.oled_lock:
oled.show()
await asyncio.sleep(0.5)
words = alert_message.split()
screens = []
screen = []
line = ""
for word in words:
if len(line) + len(word) + 1 > max_chars_per_line: # +1 for the space
screen.append(line)
line = ""
if len(screen) == max_lines_per_screen:
screens.append(screen)
screen = []
line += (
" " + word if line else word
) # Add space only if line is not empty
# Add the last line and screen if they're not empty
if line:
screen.append(line)
if screen:
screens.append(screen)
for screen in screens:
await clear_line(oled, config.LINEONE_Y)
await clear_line(oled, config.THIN_LINETWO_Y)
for i, line in enumerate(screen):
async with oled.oled_lock:
oled.fd_oled.print_str(line, 0, i * config.THIN_LINE_HEIGHT)
async with oled.oled_lock:
oled.show() # Update the display
await asyncio.sleep(
3
) # Wait 3 seconds without yielding to the event loop
oled.fill(0)
async def scroll_text(oled, text, y):
"""
Asynchronously scrolls a line of text across the OLED display.
Parameters:
oled (OLED): The OLED display object.
text (str): The text to scroll.
y (int): The y position on the display to start at.
Side Effects:
Updates the OLED display with the scrolling text.
"""
# print(f"scroll_text() called with text: {text}")
text_width = len(text) * config.THICK_CHAR_WIDTH
frame_delay = 0.1 # Delay between frames
step_size = 6 # Smoother scrolling takes too much CPU?
for x in range(config.DISPLAY_WIDTH, -(text_width + step_size), -step_size):
await clear_line(oled, y)
async with oled.oled_lock:
oled.text(text, x, y)
oled.show()
await asyncio.sleep(frame_delay) # Delay between frames