-
Notifications
You must be signed in to change notification settings - Fork 5
/
screen.py
1185 lines (1056 loc) · 38 KB
/
screen.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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Michael duPont - [email protected]
screen.py - Display ICAO METAR weather data with a Raspberry Pi and touchscreen
"""
# pylint: disable=E1101
# stdlib
import asyncio as aio
import math
import sys
import time
from copy import copy
from datetime import datetime
from os import system
from typing import Callable, List, Tuple
# library
import avwx
import pygame
from dateutil.tz import tzlocal
# module
import common
import config as cfg
from common import IDENT_CHARS, logger
class SpChar:
"""
Special Characters
"""
CANCEL = "\u2715"
CHECKMARK = "\u2713"
DEGREES = "\u00B0"
DOWN_TRIANGLE = "\u25bc"
INFO = "\u2139"
MOON = "\u263E"
RELOAD = "\u21ba"
SETTINGS = "\u2699"
SUN = "\u2600"
UP_TRIANGLE = "\u25b2"
Coord = Tuple[int, int]
class Color:
"""
RGB color values
"""
WHITE = 255, 255, 255
BLACK = 0, 0, 0
RED = 255, 0, 0
GREEN = 0, 255, 0
BLUE = 0, 0, 255
PURPLE = 150, 0, 255
GRAY = 60, 60, 60
def __getitem__(self, key: str) -> Tuple[int, int, int]:
try:
return getattr(self, key)
except AttributeError:
raise KeyError(f"{key} is not a set color: {self.__slots__}")
# Init pygame and fonts
pygame.init()
ICON_PATH = cfg.LOC / "icons"
FONT_PATH = str(ICON_PATH / "DejaVuSans.ttf")
FONT_S1 = pygame.font.Font(FONT_PATH, cfg.layout["fonts"]["s1"])
FONT_S2 = pygame.font.Font(FONT_PATH, cfg.layout["fonts"]["s2"])
FONT_S3 = pygame.font.Font(FONT_PATH, cfg.layout["fonts"]["s3"])
FONT_M1 = pygame.font.Font(FONT_PATH, cfg.layout["fonts"]["m1"])
FONT_M2 = pygame.font.Font(FONT_PATH, cfg.layout["fonts"]["m2"])
FONT_L1 = pygame.font.Font(FONT_PATH, cfg.layout["fonts"]["l1"])
try:
FONT_L2 = pygame.font.Font(FONT_PATH, cfg.layout["fonts"]["l2"])
except KeyError:
pass
def midpoint(p1: Coord, p2: Coord) -> Coord:
"""
Returns the midpoint between two points
"""
return (p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2
def centered(rendered_text, around: Coord) -> Coord:
"""
Returns the top left point for rendered text at a center point
"""
width, height = rendered_text.get_size()
return around[0] - width / 2 + 1, around[1] - height / 2 + 1
def radius_point(degree: int, center: Coord, radius: int) -> Coord:
"""
Returns the degree point on the circumference of a circle
"""
degree %= 360
x = center[0] + radius * math.cos((degree - 90) * math.pi / 180)
y = center[1] + radius * math.sin((degree - 90) * math.pi / 180)
return x, y
def hide_mouse():
"""
This makes the mouse transparent
"""
pygame.mouse.set_cursor(
(8, 8), (0, 0), (0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0)
)
class Button:
"""
Base button class
Runs a function when clicked
"""
# Function to run when clicked. Cannot accept args
onclick: Callable
# Text settings
text: str
fontsize: int
# Color strings must match Color.attr names
fontcolor: str
def draw(self, win: pygame.Surface, color: Color):
"""
Draw the button on the window with the current color palette
"""
raise NotImplementedError()
def is_clicked(self, pos: Coord) -> bool:
"""
Returns True if the position is within the button bounds
"""
raise NotImplementedError()
class RectButton(Button):
"""
Rectangular buttons can contain text
"""
# Top left
x1: int
y1: int
# Bottom right
x2: int
y2: int
width: int
height: int
# Box outline thickness
thickness: int
def __init__(
self,
bounds: List[int],
action: Callable,
text: str = None,
fontsize: int = cfg.layout["fonts"]["s3"],
fontcolor: str = "Black",
thickness: int = cfg.layout["button"]["outline"],
):
self.x1, self.y1, self.width, self.height = bounds
self.x2 = self.x1 + self.width
self.y2 = self.y1 + self.height
self.onclick = action
self.text = text
self.fontsize = fontsize
self.fontcolor = fontcolor
self.thickness = thickness
def __repr__(self) -> str:
return f'<RectButton "{self.text}" at ({self.x1}, {self.y1}), ({self.x2}, {self.y2})>'
def draw(self, win: pygame.Surface, color: Color):
"""
Draw the button on the window with the current color palette
"""
if self.width is not None:
bounds = ((self.x1, self.y1), (self.width, self.height))
pygame.draw.rect(win, color[self.fontcolor], bounds, self.thickness)
if self.text is not None:
font = pygame.font.Font(FONT_PATH, self.fontsize)
rendered = font.render(self.text, 1, color[self.fontcolor])
rwidth, rheight = rendered.get_size()
x = self.x1 + (self.width - rwidth) / 2
y = self.y1 + (self.height - rheight) / 2 + 1
win.blit(rendered, (x, y))
def is_clicked(self, pos: Coord) -> bool:
"""
Returns True if the position is within the button bounds
"""
return self.x1 < pos[0] < self.x2 and self.y1 < pos[1] < self.y2
class RoundButton(Button):
"""
Round buttons
"""
# Center pixel and radius
x: int
y: int
radius: int
def __init__(
self,
center: Coord,
action: Callable,
radius: int = cfg.layout["button"]["radius"],
):
self.center = center
self.radius = radius
self.onclick = action
def is_clicked(self, pos: Coord) -> bool:
"""
Returns True if the position is within the button bounds
"""
x, y = self.center
return self.radius > math.hypot(x - pos[0], y - pos[1])
class IconButton(RoundButton):
"""
Round button which contain a letter or symbol
"""
# Fill color
fill: str = "WHITE"
fontcolor: str = "BLACK"
fontsize: int = cfg.layout["fonts"]["l1"]
radius: int = cfg.layout["button"]["radius"]
def __init__(
self,
center: Coord = None,
action: Callable = None,
icon: str = None,
fontcolor: str = None,
fill: str = None,
radius: int = None,
fontsize: int = None,
):
if center:
self.center = center
if radius:
self.radius = radius
if action:
self.onclick = action
if icon:
self.icon = icon
if fontsize:
self.fontsize = fontsize
if fontcolor:
self.fontcolor = fontcolor
if fill:
self.fill = fill
def __repr__(self) -> str:
return f"<IconButton at {self.center} rad {self.radius}>"
def draw(self, win: pygame.Surface, color: Color):
"""
Draw the button on the window with the current color palette
"""
if self.fill is not None:
pygame.draw.circle(win, color[self.fill], self.center, self.radius)
if self.icon is not None:
font = pygame.font.Font(FONT_PATH, self.fontsize)
rendered = font.render(self.icon, 1, color[self.fontcolor])
win.blit(rendered, centered(rendered, self.center))
class ShutdownButton(RoundButton):
"""
Round button with a drawn shutdown symbol
"""
fontcolor: str = "WHITE"
fill: str = "RED"
def draw(self, win: pygame.Surface, color: Color):
"""
Draw the button on the window with the current color palette
"""
pygame.draw.circle(win, color[self.fill], self.center, self.radius)
pygame.draw.circle(win, color[self.fontcolor], self.center, self.radius - 6)
pygame.draw.circle(win, color[self.fill], self.center, self.radius - 9)
rect = ((self.center[0] - 2, self.center[1] - 10), (4, 20))
pygame.draw.rect(win, color[self.fontcolor], rect)
class SelectionButton(RoundButton):
"""
Round button with icons resembling selection screen
"""
fontcolor: str = "WHITE"
fill: str = "GREEN"
def draw(self, win: pygame.Surface, color: Color):
"""
Draw the button on the window with the current color palette
"""
pygame.draw.circle(win, color[self.fill], self.center, self.radius)
font = FONT_S3 if cfg.layout["large-display"] else FONT_M1
for char, direction in ((SpChar.UP_TRIANGLE, -1), (SpChar.DOWN_TRIANGLE, 1)):
tri = font.render(char, 1, color[self.fontcolor])
topleft = list(centered(tri, self.center))
topleft[1] += int(self.radius * 0.5) * direction - 3
win.blit(tri, topleft)
class CancelButton(IconButton):
center: Coord = cfg.layout["util"]
icon: str = SpChar.CANCEL
fontcolor: str = "WHITE"
fill: str = "GRAY"
def draw_func(func):
"""
Decorator wraps drawing functions with common commands
"""
def wrapper(screen):
screen.on_main = False
screen.buttons = []
func(screen)
screen.draw_buttons()
pygame.display.flip()
# This line is a hack to force the screen to redraw
pygame.event.get()
return wrapper
class METARScreen:
"""
Controls and draws UI elements
"""
ident: List[str]
old_ident: List[str]
width: int
height: int
win: pygame.Surface
c: Color
inverted: bool
update_time: float
buttons: List[Button]
layout: dict
is_large: bool
on_main: bool = False
def __init__(self, station: str, size: Coord, inverted: bool):
logger.debug("Running init")
try:
self.metar = avwx.Metar(station)
except avwx.exceptions.BadStation:
self.metar = avwx.Metar("KJFK")
self.ident = common.station_to_ident(station)
self.old_ident = copy(self.ident)
self.width, self.height = size
if cfg.fullscreen:
self.win = pygame.display.set_mode(size, pygame.FULLSCREEN)
else:
self.win = pygame.display.set_mode(size)
self.c = Color()
self.inverted = inverted
if inverted:
self.c.BLACK, self.c.WHITE = self.c.WHITE, self.c.BLACK
if cfg.hide_mouse:
hide_mouse()
self.reset_update_time()
self.buttons = []
self.layout = cfg.layout
self.is_large = self.layout["large-display"]
logger.debug("Finished running init")
@property
def station(self) -> str:
"""
The current station
"""
return common.ident_to_station(self.ident)
@classmethod
def from_session(cls, session: dict, size: Coord):
"""
Returns a new Screen from a saved session
"""
station = session.get("station", "KJFK")
inverted = session.get("inverted", True)
return cls(station, size, inverted)
def export_session(self, save: bool = True):
"""
Saves or returns a dictionary representing the session's state
"""
session = {"station": self.station, "inverted": self.inverted}
if save:
common.save_session(session)
return session
def reset_update_time(self, interval: int = None):
"""
Call to reset the update time to now plus the update interval
"""
self.update_time = time.time() + (interval or cfg.update_interval)
async def refresh_data(
self, force_main: bool = False, ignore_updated: bool = False
):
"""
Refresh existing station
"""
logger.info("Calling refresh update")
try:
updated = await self.metar.async_update()
except ConnectionError:
await self.wait_for_network()
except (TimeoutError, avwx.exceptions.SourceError):
self.error_connection()
except avwx.exceptions.InvalidRequest:
self.error_station()
except Exception as exc:
logger.exception(f"An unknown error has occurred: {exc}")
self.error_unknown()
else:
logger.info(self.metar.raw)
self.reset_update_time()
if ignore_updated:
updated = True
if updated and (self.on_main or force_main):
self.draw_main()
elif force_main and not updated:
self.error_no_data()
async def new_station(self):
"""
Update the current station from ident and display new main screen
"""
logger.info("Calling new update")
self.draw_loading_screen()
new_metar = avwx.Metar(self.station)
try:
if not await new_metar.async_update():
return self.error_no_data()
except (TimeoutError, ConnectionError, avwx.exceptions.SourceError):
self.error_connection()
except avwx.exceptions.InvalidRequest:
self.error_station()
except Exception as exc:
logger.exception(f"An unknown error has occurred: {exc}")
self.error_unknown()
else:
logger.info(new_metar.raw)
self.metar = new_metar
self.old_ident = copy(self.ident)
self.reset_update_time()
self.export_session()
self.draw_main()
async def verify_station(self):
"""
Verifies the station value before calling new data
"""
try:
station = avwx.station.Station.from_icao(self.station)
if not station.sends_reports:
return self.error_reporting()
except avwx.exceptions.BadStation:
return self.error_station()
return await self.new_station()
def cancel_station(self):
"""
Revert ident and redraw main screen
"""
self.ident = self.old_ident
if self.metar.data is None:
return self.error_no_data()
self.draw_main()
def draw_buttons(self):
"""
Draw all current buttons
"""
for button in self.buttons:
button.draw(self.win, self.c)
@draw_func
def draw_selection_screen(self):
"""
Load selection screen elements
"""
self.win.fill(self.c.WHITE)
# Draw Selection Grid
yes, no = self.layout["select"]["yes"], self.layout["select"]["no"]
self.buttons = [
IconButton(yes, self.verify_station, SpChar.CHECKMARK, "WHITE", "GREEN"),
CancelButton(no, self.cancel_station, fill="RED"),
]
upy = self.layout["select"]["row-up"]
chary = self.layout["select"]["row-char"]
downy = self.layout["select"]["row-down"]
for col in range(4):
x = self.__selection_getx(col)
self.buttons.append(
IconButton((x, upy), self.__incr_ident(col, 1), SpChar.UP_TRIANGLE)
)
self.buttons.append(
IconButton((x, downy), self.__incr_ident(col, 0), SpChar.DOWN_TRIANGLE)
)
rendered = FONT_L1.render(IDENT_CHARS[self.ident[col]], 1, self.c.BLACK)
self.win.blit(rendered, centered(rendered, (x, chary)))
def __selection_getx(self, col: int) -> int:
"""
Returns the top left x pixel for a desired column
"""
offset = self.layout["select"]["col-offset"]
spacing = self.layout["select"]["col-spacing"]
return offset + col * spacing
def __incr_ident(self, pos: int, down: bool) -> Callable:
"""
Returns a function to update and replace ident char on display
pos: 0-3 column
down: increment/decrement counter
"""
def update_func():
# Update ident
if down:
if self.ident[pos] == 0:
self.ident[pos] = len(IDENT_CHARS)
self.ident[pos] -= 1
else:
self.ident[pos] += 1
if self.ident[pos] == len(IDENT_CHARS):
self.ident[pos] = 0
# Update display
rendered = FONT_L1.render(IDENT_CHARS[self.ident[pos]], 1, self.c.BLACK)
x = self.__selection_getx(pos)
chary = self.layout["select"]["row-char"]
spacing = self.layout["select"]["col-spacing"]
region = (x - spacing / 2, chary - spacing / 2, spacing, spacing)
pygame.draw.rect(self.win, self.c.WHITE, region)
self.win.blit(rendered, centered(rendered, (x, chary)))
pygame.display.update(region)
return update_func
@draw_func
def draw_loading_screen(self):
"""
Display load screen
"""
# Reset on_main because the main screen should always display on success
self.on_main = True
self.win.fill(self.c.WHITE)
point = self.layout["error"]["line1"]
self.win.blit(FONT_M2.render("Fetching weather", 1, self.c.BLACK), point)
point = self.layout["error"]["line2"]
self.win.blit(
FONT_M2.render("data for " + self.station, 1, self.c.BLACK), point
)
def __draw_clock(self):
"""
Draw the clock components
"""
now = datetime.utcnow() if cfg.clock_utc else datetime.now(tzlocal())
label = now.tzname() or "UTC"
clock_font = globals().get("FONT_L2") or FONT_L1
clock_text = clock_font.render(now.strftime(cfg.clock_format), 1, self.c.BLACK)
x, y = self.layout["main"]["clock"]
w, h = clock_text.get_size()
pygame.draw.rect(self.win, self.c.WHITE, ((x, y), (x + w, (y + h) * 0.9)))
self.win.blit(clock_text, (x, y))
label_font = FONT_M1 if self.is_large else FONT_S3
point = self.layout["main"]["clock-label"]
self.win.blit(label_font.render(label, 1, self.c.BLACK), point)
def __draw_wind_compass(
self, data: avwx.structs.MetarData, center: List[int], radius: int
):
"""
Draw the wind direction compass
"""
wdir = data.wind_direction
speed = data.wind_speed
var = data.wind_variable_direction
pygame.draw.circle(self.win, self.c.GRAY, center, radius, 3)
if not speed.value:
text = FONT_S3.render("Calm", 1, self.c.BLACK)
elif wdir and wdir.repr == "VRB":
text = FONT_S3.render("VRB", 1, self.c.BLACK)
elif wdir:
text = FONT_M1.render(str(wdir.value).zfill(3), 1, self.c.BLACK)
rad_point = radius_point(wdir.value, center, radius)
width = 4 if self.is_large else 2
pygame.draw.line(self.win, self.c.RED, center, rad_point, width)
if var:
for point in var:
rad_point = radius_point(point.value, center, radius)
pygame.draw.line(self.win, self.c.BLUE, center, rad_point, width)
else:
text = FONT_L1.render(SpChar.CANCEL, 1, self.c.RED)
self.win.blit(text, centered(text, center))
def __draw_wind(self, data: avwx.structs.MetarData, unit: str):
"""
Draw the dynamic wind elements
"""
speed, gust = data.wind_speed, data.wind_gust
point = self.layout["main"]["wind-compass"]
radius = self.layout["main"]["wind-compass-radius"]
self.__draw_wind_compass(data, point, radius)
if speed.value:
text = FONT_S3.render(f"{speed.value} {unit}", 1, self.c.BLACK)
point = self.layout["main"]["wind-speed"]
self.win.blit(text, centered(text, point))
text = f"G: {gust.value}" if gust else "No Gust"
text = FONT_S3.render(text, 1, self.c.BLACK)
self.win.blit(text, centered(text, self.layout["main"]["wind-gust"]))
def __draw_temp_icon(self, temp: int):
"""
Draw the temperature icon
"""
therm_level = 0
if temp:
therm_level = temp // 12 + 2
if therm_level < 0:
therm_level = 0
add_i = "I" if self.inverted else ""
therm_icon = f"Therm{therm_level}{add_i}.png"
point = self.layout["main"]["temp-icon"]
self.win.blit(pygame.image.load(str(ICON_PATH / therm_icon)), point)
def __draw_temp_dew_humidity(self, data: avwx.structs.MetarData):
"""
Draw the dynamic temperature, dewpoint, and humidity elements
"""
temp = data.temperature
dew = data.dewpoint
if self.is_large:
temp_text = "Temp "
diff_text = "Std Dev "
dew_text = "Dewpoint "
hmd_text = "Humidity "
else:
temp_text = "TMP: "
diff_text = "STD: "
dew_text = "DEW: "
hmd_text = "HMD: "
# Dewpoint
dew_text += f"{dew.value}{SpChar.DEGREES}" if dew else "--"
point = self.layout["main"]["dew"]
self.win.blit(FONT_S3.render(dew_text, 1, self.c.BLACK), point)
# Temperature
if temp:
temp_text += f"{temp.value}{SpChar.DEGREES}"
if self.is_large:
temp_text += self.metar.units.temperature
temp_diff = temp.value - 15
diff_sign = "-" if temp_diff < 0 else "+"
diff_text += f"{diff_sign}{abs(temp_diff)}{SpChar.DEGREES}"
else:
temp_text += "--"
diff_text += "--"
point = self.layout["main"]["temp"]
self.win.blit(FONT_S3.render(temp_text, 1, self.c.BLACK), point)
point = self.layout["main"]["temp-stdv"]
self.win.blit(FONT_S3.render(diff_text, 1, self.c.BLACK), point)
if "temp-icon" in self.layout["main"]:
self.__draw_temp_icon(temp.value)
# Humidity
if isinstance(temp.value, int) and isinstance(dew.value, int):
relHum = (
(6.11 * 10.0 ** (7.5 * dew.value / (237.7 + dew.value)))
/ (6.11 * 10.0 ** (7.5 * temp.value / (237.7 + temp.value)))
* 100
)
hmd_text += f"{int(relHum)}%"
else:
hmd_text += "--"
point = self.layout["main"]["humid"]
self.win.blit(FONT_S3.render(hmd_text, 1, self.c.BLACK), point)
def __draw_cloud_graph(
self, clouds: List[avwx.structs.Cloud], tl: List[int], br: List[int]
):
"""
Draw cloud layers in chart
Scales everything based on top left and bottom right points
"""
tlx, tly = tl
brx, bry = br
header = FONT_S3.render("Clouds AGL", 1, self.c.BLACK)
header_height = header.get_size()[1]
header_point = midpoint(tl, (brx, tly + header_height))
self.win.blit(header, centered(header, header_point))
tly += header_height
pygame.draw.lines(
self.win, self.c.BLACK, False, ((tlx, tly), (tlx, bry), (brx, bry)), 3
)
if not clouds:
text = FONT_M2.render("CLR", 1, self.c.BLUE)
self.win.blit(text, centered(text, midpoint((tlx, tly), (brx, bry))))
return
top = 80
LRBool = 1
tlx += 5
brx -= 5
bry -= 10
for cloud in clouds[::-1]:
if cloud.base:
if cloud.base > top:
top = cloud.base
drawHeight = bry - (bry - tly) * cloud.base / top
text = FONT_S1.render(cloud.repr, 1, self.c.BLUE)
width, height = text.get_size()
liney = drawHeight + height / 2
if LRBool > 0:
self.win.blit(text, (tlx, drawHeight))
pygame.draw.line(
self.win, self.c.BLUE, (tlx + width + 2, liney), (brx, liney)
)
else:
self.win.blit(text, (brx - width, drawHeight))
pygame.draw.line(
self.win, self.c.BLUE, (tlx, liney), (brx - width - 2, liney)
)
LRBool *= -1
def __draw_wx_raw(self):
"""
Draw wx and raw report
"""
x, y = self.layout["wxraw"]["start"]
spacing = self.layout["wxraw"]["line-space"]
raw_key = "large"
wxs = [c.value for c in self.metar.data.wx_codes]
wxs.sort(key=lambda x: len(x))
if wxs:
wx_length = self.layout["wxraw"]["wx-length"]
y = self.__draw_text_lines(wxs, (x, y), wx_length, space=spacing)
raw_key = "small"
raw_font, raw_length, raw_padding = self.layout["wxraw"]["raw"][raw_key]
y += raw_padding
self.__draw_text_lines(
self.metar.data.raw, (x, y), raw_length, space=spacing, fontsize=raw_font
)
def __main_draw_dynamic(
self, data: avwx.structs.MetarData, units: avwx.structs.Units
) -> bool:
"""
Load Main dynamic foreground elements
Returns True if "Other-WX" or "Remarks" is not empty, else False
"""
if self.is_large:
altm_text = "Altm "
vis_text = "Visb "
else:
altm_text = "ALT: "
vis_text = "VIS: "
tstamp = data.time.dt
if not cfg.clock_utc:
tstamp = tstamp.astimezone(tzlocal())
tstamp = tstamp.strftime(cfg.timestamp_format)
if "title" in cfg.layout["main"]:
time_text = data.station + " " + tstamp
point = self.layout["main"]["title"]
self.win.blit(FONT_M1.render(time_text, 1, self.c.BLACK), point)
else:
self.__draw_clock()
point = self.layout["main"]["station"]
self.win.blit(FONT_M1.render(data.station, 1, self.c.BLACK), point)
if self.is_large:
point = self.layout["main"]["timestamp-label"]
self.win.blit(FONT_S3.render(f"Updated", 1, self.c.BLACK), point)
else:
tstamp = "TS: " + tstamp
point = self.layout["main"]["timestamp"]
self.win.blit(FONT_S3.render(tstamp, 1, self.c.BLACK), point)
# Current Flight Rules
fr = data.flight_rules or "N/A"
fr_color, fr_x_offset = self.layout["fr-display"][fr]
point = copy(self.layout["main"]["flight-rules"])
point[0] += fr_x_offset
self.win.blit(FONT_M1.render(fr, 1, getattr(self.c, fr_color.upper())), point)
# Wind
self.__draw_wind(data, units.wind_speed)
# Temperature / Dewpoint / Humidity
self.__draw_temp_dew_humidity(data)
# Altimeter
altm = data.altimeter
altm_text += str(altm.value) if altm else "--"
point = self.layout["main"]["altim"]
self.win.blit(FONT_S3.render(altm_text, 1, self.c.BLACK), point)
# Visibility
vis = data.visibility
vis_text += f"{vis.value}{units.visibility}" if vis else "--"
point = self.layout["main"]["vis"]
self.win.blit(FONT_S3.render(vis_text, 1, self.c.BLACK), point)
# Cloud Layers
points = self.layout["main"]["cloud-graph"]
self.__draw_cloud_graph(data.clouds, *points)
def __draw_text_lines(
self,
items: List[str],
left_point: Coord,
length: int,
header: str = None,
space: int = None,
right_x: int = None,
fontsize: int = None,
) -> int:
"""
Draw lines of text with header, columns, and line wrapping
"""
left_x, y = left_point
font = pygame.font.Font(FONT_PATH, fontsize) if fontsize else FONT_S2
if header:
text = FONT_S3.render(header, 1, self.c.BLACK)
self.win.blit(text, left_point)
y += text.get_size()[1] + space
left = True
if not isinstance(items, list):
items = [items]
for item in items:
# Column overflow control
if right_x is not None and len(item) > length:
if not left:
y += space
left = not left
x = left_x if left else right_x
# Line overflow control
if right_x is None:
while len(item) > length:
cutPoint = item[:length].rfind(" ")
text = font.render(item[:cutPoint], 1, self.c.BLACK)
self.win.blit(text, (x, y))
y += text.get_size()[1] + space
item = item[cutPoint + 1 :]
text = font.render(item, 1, self.c.BLACK)
self.win.blit(text, (x, y))
if right_x is not None:
if not left or len(item) > length:
y += text.get_size()[1] + space
left = not left
else:
y += text.get_size()[1] + space
# Don't add double new line
if not (left or len(items[-1]) > length):
y += text.get_size()[1] + space
return y
@draw_func
def draw_rmk(self):
"""
Display available Other Weather / Remarks and cancel button control
"""
left = self.layout["wxrmk"]["col1"]
right = self.layout["wxrmk"]["col2"]
y = self.layout["wxrmk"]["padding"]
line_space = self.layout["wxrmk"]["line-space"]
self.win.fill(self.c.WHITE)
# Weather
wxs = [c.value for c in self.metar.data.wx_codes]
wxs.sort(key=lambda x: len(x))
if wxs:
wx_length = self.layout["wxrmk"]["wx-length"]
y = self.__draw_text_lines(
wxs,
(left, y),
wx_length,
header="Other Weather",
space=line_space,
right_x=right,
)
# Remarks
rmk = self.metar.data.remarks
if rmk:
rmk_length = self.layout["wxrmk"]["rmk-length"]
self.__draw_text_lines(
rmk, (left, y), rmk_length, header="Remarks", space=line_space
)
self.buttons = [CancelButton(action=self.draw_main)]
@draw_func
def draw_main(self):
"""
Run main data display and options touch button control
"""
self.win.fill(self.c.WHITE)
self.__main_draw_dynamic(self.metar.data, self.metar.units)
self.buttons = [
IconButton(
self.layout["util"],
self.draw_options_bar,
SpChar.SETTINGS,
"WHITE",
"GRAY",
)
]
if self.is_large:
self.__draw_wx_raw()
else:
wx = self.metar.data.wx_codes
rmk = self.metar.data.remarks
if wx or rmk:
if wx and rmk:
text, color = "WX/RMK", "PURPLE"
elif wx:
text, color = "WX", "RED"
elif rmk:
text, color = "RMK", "BLUE"
rect = self.layout["main"]["wxrmk"]
self.buttons.append(
RectButton(rect, self.draw_rmk, text, fontcolor=color)
)
self.on_main = True
def invert_wb(self, redraw: bool = True):
"""
Invert the black and white of the display
"""
self.inverted = not self.inverted
self.c.BLACK, self.c.WHITE = self.c.WHITE, self.c.BLACK
self.export_session()
if redraw:
self.draw_main()
@draw_func
def draw_quit_screen(self) -> False:
"""
Display Shutdown/Exit option and touch button control
Returns False or exits program
"""
self.win.fill(self.c.WHITE)
if cfg.shutdown_on_exit:
text = "Shutdown the Pi?"
else:
text = "Exit the program?"
text = FONT_M2.render(text, 1, self.c.BLACK)
point = self.width / 2, self.layout["quit"]["text-y"]
self.win.blit(text, centered(text, point))
pointy, pointn = self.layout["quit"]["yes"], self.layout["quit"]["no"]
self.buttons = [
IconButton(pointy, quit, SpChar.CHECKMARK, "WHITE", "GREEN"),
CancelButton(pointn, self.draw_main, fill="RED"),
]
@draw_func
def draw_info_screen(self):
"""
Display info screen and cancel touch button control
"""
self.win.fill(self.c.WHITE)
for text, key, font in (
("METAR-RasPi", "title", FONT_M2),
("Michael duPont", "name", FONT_S3),
("[email protected]", "email", FONT_S3),
("github.com/flyinactor91/METAR-RasPi", "url", FONT_S1),
):
point = self.width / 2, self.layout["info"][key + "-y"]
text = font.render(text, 1, self.c.BLACK)
self.win.blit(text, centered(text, point))
self.buttons = [CancelButton(action=self.draw_main)]
@draw_func
def draw_options_bar(self) -> bool:
"""