1+ """
2+ GFX Helper for pyportal_azure_iot_temperature.py
3+ """
4+ import board
5+ import displayio
6+ from adafruit_display_text .label import Label
7+ from adafruit_bitmap_font import bitmap_font
8+
9+ cwd = ("/" + __file__ ).rsplit ('/' , 1 )[0 ] # the current working directory (where this file is)
10+
11+ # Fonts within /fonts folder
12+ info_font = cwd + "/fonts/Nunito-Black-17.bdf"
13+ temperature_font = cwd + "/fonts/Nunito-Light-75.bdf"
14+
15+ class Azure_GFX (displayio .Group ):
16+ def __init__ (self , celsius = False ):
17+ """Creates an Azure_GFX object.
18+ :param bool celsius: Temperature displayed as F or C
19+ """
20+ # root displayio group
21+ root_group = displayio .Group (max_size = 20 )
22+ board .DISPLAY .show (root_group )
23+ super ().__init__ (max_size = 20 )
24+
25+ self ._celsius = celsius
26+
27+ # create background icon group
28+ self ._icon_group = displayio .Group (max_size = 1 )
29+ self .append (self ._icon_group )
30+ board .DISPLAY .show (self ._icon_group )
31+ # create text object group
32+ self ._text_group = displayio .Group (max_size = 6 )
33+ self .append (self ._text_group )
34+
35+ self ._icon_sprite = None
36+ self ._icon_file = None
37+ self ._cwd = cwd
38+ self .set_icon (self ._cwd + "/images/azure_splash.bmp" )
39+
40+ print ('Loading Fonts...' )
41+ glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:/ '
42+ self .info_font = bitmap_font .load_font (info_font )
43+ self .info_font .load_glyphs (glyphs )
44+
45+ self .c_font = bitmap_font .load_font (temperature_font )
46+ self .c_font .load_glyphs (glyphs )
47+ self .c_font .load_glyphs (('°' ,)) # extra glyph for temperature font
48+
49+ print ('setting up labels...' )
50+ self .title_text = Label (self .info_font , text = "Azure IoT Temperature Logger" )
51+ self .title_text .x = 55
52+ self .title_text .y = 15
53+ self ._text_group .append (self .title_text )
54+
55+ self .temp_text = Label (self .c_font , max_glyphs = 8 )
56+ self .temp_text .x = 25
57+ self .temp_text .y = 110
58+ self ._text_group .append (self .temp_text )
59+
60+ self .azure_status_text = Label (self .info_font , max_glyphs = 40 )
61+ self .azure_status_text .x = 100
62+ self .azure_status_text .y = 220
63+ self ._text_group .append (self .azure_status_text )
64+
65+ board .DISPLAY .show (self ._text_group )
66+
67+ def display_azure_status (self , status_text ):
68+ """Displays the current Azure IoT status.
69+ :param str status_text: Description of Azure IoT status
70+ """
71+ self .azure_status_text .text = status_text
72+
73+ def display_temp (self , adt_data ):
74+ """Displays the data from the ADT7410 on the.
75+ :param float adt_data: Value from the ADT7410
76+ """
77+ if not self ._celsius :
78+ adt_data = (adt_data * 9 / 5 ) + 32
79+ print ('Temperature: %0.2f°F' % adt_data )
80+ if adt_data >= 212 :
81+ self .temp_text .color = 0xFD2EE
82+ elif adt_data <= 32 :
83+ self .temp_text .color = 0xFF0000
84+ self .temp_text .text = '%0.2f°F' % adt_data
85+ else :
86+ print ('Temperature: %0.2f°C' % adt_data )
87+ if adt_data <= 0 :
88+ self .temp_text .color = 0xFD2EE
89+ elif adt_data >= 100 :
90+ self .temp_text .color = 0xFF0000
91+ self .temp_text .text = '%0.2f°C' % adt_data
92+
93+ def set_icon (self , filename ):
94+ """Sets the background image to a bitmap file.
95+
96+ :param filename: The filename of the chosen icon
97+ """
98+ print ("Set icon to " , filename )
99+ if self ._icon_group :
100+ self ._icon_group .pop ()
101+
102+ if not filename :
103+ return # we're done, no icon desired
104+ if self ._icon_file :
105+ self ._icon_file .close ()
106+ self ._icon_file = open (filename , "rb" )
107+ icon = displayio .OnDiskBitmap (self ._icon_file )
108+ try :
109+ self ._icon_sprite = displayio .TileGrid (icon ,
110+ pixel_shader = displayio .ColorConverter ())
111+ except TypeError :
112+ self ._icon_sprite = displayio .TileGrid (icon ,
113+ pixel_shader = displayio .ColorConverter (),
114+ position = (0 ,0 ))
115+
116+ self ._icon_group .append (self ._icon_sprite )
117+ board .DISPLAY .refresh_soon ()
118+ board .DISPLAY .wait_for_frame ()
119+
0 commit comments