Skip to content

Commit 0a4665b

Browse files
authored
Merge pull request #1627 from lesamouraipourpre/azure-gfx-helper
Azure gfx helper: Update for CP7
2 parents dfc79aa + 8514ffe commit 0a4665b

File tree

3 files changed

+71
-68
lines changed

3 files changed

+71
-68
lines changed

PyPortal_Azure_Plant_Monitor/azure_gfx_helper.py

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,48 @@
66
from adafruit_display_text.label import Label
77
from adafruit_bitmap_font import bitmap_font
88

9-
cwd = ("/"+__file__).rsplit('/', 1)[0] # the current working directory (where this file is)
9+
# the current working directory (where this file is)
10+
cwd = ("/" + __file__).rsplit("/", 1)[0]
1011

1112
# Fonts within /fonts folder
12-
main_font = cwd+"/fonts/EarthHeart-26.bdf"
13-
data_font = cwd+"/fonts/Collegiate-50.bdf"
13+
main_font = cwd + "/fonts/EarthHeart-26.bdf"
14+
data_font = cwd + "/fonts/Collegiate-50.bdf"
15+
1416

1517
class Azure_GFX(displayio.Group):
1618
def __init__(self, is_celsius):
1719
"""Creates an Azure_GFX object.
1820
:param bool is_celsius: Temperature displayed in Celsius.
1921
"""
2022
# root displayio group
21-
root_group = displayio.Group(max_size=23)
23+
root_group = displayio.Group()
2224
board.DISPLAY.show(root_group)
23-
super().__init__(max_size=15)
25+
super().__init__()
2426

2527
# temperature display option
2628
self._is_celsius = is_celsius
2729

2830
# create background icon group
29-
self._icon_group = displayio.Group(max_size=3)
31+
self._icon_group = displayio.Group()
3032
board.DISPLAY.show(self._icon_group)
3133
# create text object group
32-
self._text_group = displayio.Group(max_size=9)
34+
self._text_group = displayio.Group()
3335

3436
self._icon_sprite = None
3537
self._icon_file = None
3638
self._cwd = cwd
37-
self.set_icon(self._cwd+"/images/azure_splash.bmp")
39+
self.set_icon(self._cwd + "/images/azure_splash.bmp")
3840

39-
print('loading fonts...')
40-
glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.: '
41-
data_glyphs = b'012345678-,.:/FC'
41+
print("loading fonts...")
42+
glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.: "
43+
data_glyphs = b"012345678-,.:/FC"
4244
self.main_font = bitmap_font.load_font(main_font)
4345
self.main_font.load_glyphs(glyphs)
4446
self.data_font = bitmap_font.load_font(data_font)
4547
self.data_font.load_glyphs(data_glyphs)
46-
self.data_font.load_glyphs(('°',)) # extra glyph for temperature font
48+
self.data_font.load_glyphs(("°",)) # extra glyph for temperature font
4749

48-
print('setting up labels...')
50+
print("setting up labels...")
4951
self.title_text = Label(self.main_font, text="Azure Plant Monitor")
5052
self.title_text.x = 35
5153
self.title_text.y = 25
@@ -56,7 +58,7 @@ def __init__(self, is_celsius):
5658
self.temp_label.y = 65
5759
self._text_group.append(self.temp_label)
5860

59-
self.temp_text = Label(self.data_font, max_glyphs=10)
61+
self.temp_text = Label(self.data_font)
6062
self.temp_text.x = 200
6163
self.temp_text.y = 85
6264
self._text_group.append(self.temp_text)
@@ -66,12 +68,12 @@ def __init__(self, is_celsius):
6668
self.moisture_label.y = 135
6769
self._text_group.append(self.moisture_label)
6870

69-
self.moisture_text = Label(self.data_font, max_glyphs=10)
71+
self.moisture_text = Label(self.data_font)
7072
self.moisture_text.x = 200
7173
self.moisture_text.y = 175
7274
self._text_group.append(self.moisture_text)
7375

74-
self.azure_status_text = Label(self.main_font, max_glyphs=15)
76+
self.azure_status_text = Label(self.main_font)
7577
self.azure_status_text.x = 65
7678
self.azure_status_text.y = 225
7779
self._text_group.append(self.azure_status_text)
@@ -89,7 +91,7 @@ def display_moisture(self, moisture_data):
8991
"""Displays the moisture from the Stemma Soil Sensor.
9092
:param int moisture_data: Moisture value
9193
"""
92-
print('Moisture Level: ', moisture_data)
94+
print("Moisture Level: ", moisture_data)
9395
self.moisture_text.text = str(moisture_data)
9496

9597
def display_temp(self, temp_data):
@@ -98,22 +100,22 @@ def display_temp(self, temp_data):
98100
"""
99101
if not self._is_celsius:
100102
temp_data = (temp_data * 9 / 5) + 32 - 15
101-
print('Temperature: %0.0f°F'%temp_data)
103+
print("Temperature: %0.0f°F" % temp_data)
102104
if temp_data >= 212:
103105
self.temp_text.color = 0xFD2EE
104106
elif temp_data <= 32:
105107
self.temp_text.color = 0xFF0000
106-
self.temp_text.text = '%0.0f°F'%temp_data
107-
temp_data = '%0.0f'%temp_data
108+
self.temp_text.text = "%0.0f°F" % temp_data
109+
temp_data = "%0.0f" % temp_data
108110
return int(temp_data)
109111
else:
110-
print('Temperature: %0.0f°C'%temp_data)
112+
print("Temperature: %0.0f°C" % temp_data)
111113
if temp_data <= 0:
112114
self.temp_text.color = 0xFD2EE
113115
elif temp_data >= 100:
114116
self.temp_text.color = 0xFF0000
115-
self.temp_text.text = '%0.0f°C'%temp_data
116-
temp_data = '%0.0f'%temp_data
117+
self.temp_text.text = "%0.0f°C" % temp_data
118+
temp_data = "%0.0f" % temp_data
117119
return int(temp_data)
118120

119121
def set_icon(self, filename):
@@ -127,16 +129,19 @@ def set_icon(self, filename):
127129

128130
if not filename:
129131
return # we're done, no icon desired
132+
133+
# CircuitPython 6 & 7 compatible
130134
if self._icon_file:
131135
self._icon_file.close()
132136
self._icon_file = open(filename, "rb")
133137
icon = displayio.OnDiskBitmap(self._icon_file)
134-
try:
135-
self._icon_sprite = displayio.TileGrid(icon,
136-
pixel_shader=getattr(icon, 'pixel_shader', displayio.ColorConverter()))
137-
except TypeError:
138-
self._icon_sprite = displayio.TileGrid(icon,
139-
pixel_shader=getattr(icon, 'pixel_shader', displayio.ColorConverter()),
140-
position=(0,0))
138+
self._icon_sprite = displayio.TileGrid(
139+
icon, pixel_shader=getattr(icon, "pixel_shader", displayio.ColorConverter())
140+
)
141+
142+
# CircuitPython 7 compatible
143+
# # Remove self._icon_file - it is no longer used
144+
# icon = displayio.OnDiskBitmap(filename)
145+
# self._icon_sprite = displayio.TileGrid(icon, pixel_shader=icon.pixel_shader)
141146

142147
self._icon_group.append(self._icon_sprite)

PyPortal_Azure_Plant_Monitor/code.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,7 @@
5959

6060
# Create an instance of the Azure IoT Central device
6161
device = IoTCentralDevice(
62-
socket,
63-
esp,
64-
secrets["id_scope"],
65-
secrets["device_id"],
66-
secrets["key"]
62+
socket, esp, secrets["id_scope"], secrets["device_id"], secrets["key"]
6763
)
6864

6965
# Connect to Azure IoT Central
@@ -82,19 +78,16 @@
8278
gfx.display_moisture(moisture_level)
8379
gfx.display_temp(temperature)
8480

85-
print('Sending data to Azure')
86-
gfx.display_azure_status('Sending data...')
81+
print("Sending data to Azure")
82+
gfx.display_azure_status("Sending data...")
8783

8884
# send the temperature and moisture level to Azure
89-
message = {
90-
"Temperature": temperature,
91-
"MoistureLevel": moisture_level
92-
}
85+
message = {"Temperature": temperature, "MoistureLevel": moisture_level}
9386
device.send_telemetry(json.dumps(message))
9487
device.loop()
9588

96-
gfx.display_azure_status('Data sent!')
97-
print('Data sent!')
89+
gfx.display_azure_status("Data sent!")
90+
print("Data sent!")
9891
except (ValueError, RuntimeError) as e:
9992
print("Failed to get data, retrying\n", e)
10093
wifi.reset()

azure_gfx_helper.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,60 @@
66
from adafruit_display_text.label import Label
77
from adafruit_bitmap_font import bitmap_font
88

9-
cwd = ("/"+__file__).rsplit('/', 1)[0] # the current working directory (where this file is)
9+
# the current working directory (where this file is)
10+
cwd = ("/" + __file__).rsplit("/", 1)[0]
1011

1112
# Fonts within /fonts folder
12-
info_font = cwd+"/fonts/Nunito-Black-17.bdf"
13-
temperature_font = cwd+"/fonts/Nunito-Light-75.bdf"
13+
info_font = cwd + "/fonts/Nunito-Black-17.bdf"
14+
temperature_font = cwd + "/fonts/Nunito-Light-75.bdf"
15+
1416

1517
class Azure_GFX(displayio.Group):
1618
def __init__(self, celsius=False):
1719
"""Creates an Azure_GFX object.
1820
:param bool celsius: Temperature displayed as F or C
1921
"""
2022
# root displayio group
21-
root_group = displayio.Group(max_size=20)
23+
root_group = displayio.Group()
2224
board.DISPLAY.show(root_group)
23-
super().__init__(max_size=20)
25+
super().__init__()
2426

2527
self._celsius = celsius
2628

2729
# create background icon group
28-
self._icon_group = displayio.Group(max_size=1)
30+
self._icon_group = displayio.Group()
2931
self.append(self._icon_group)
3032
board.DISPLAY.show(self._icon_group)
3133
# create text object group
32-
self._text_group = displayio.Group(max_size=6)
34+
self._text_group = displayio.Group()
3335
self.append(self._text_group)
3436

3537
self._icon_sprite = None
3638
self._icon_file = None
3739
self._cwd = cwd
38-
self.set_icon(self._cwd+"/images/azure_splash.bmp")
40+
self.set_icon(self._cwd + "/images/azure_splash.bmp")
3941

40-
print('Loading Fonts...')
41-
glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:/ '
42+
print("Loading Fonts...")
43+
glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:/ "
4244
self.info_font = bitmap_font.load_font(info_font)
4345
self.info_font.load_glyphs(glyphs)
4446

4547
self.c_font = bitmap_font.load_font(temperature_font)
4648
self.c_font.load_glyphs(glyphs)
47-
self.c_font.load_glyphs(('°',)) # extra glyph for temperature font
49+
self.c_font.load_glyphs(("°",)) # extra glyph for temperature font
4850

49-
print('setting up labels...')
51+
print("setting up labels...")
5052
self.title_text = Label(self.info_font, text="Azure IoT Temperature Logger")
5153
self.title_text.x = 55
5254
self.title_text.y = 15
5355
self._text_group.append(self.title_text)
5456

55-
self.temp_text = Label(self.c_font, max_glyphs=8)
57+
self.temp_text = Label(self.c_font)
5658
self.temp_text.x = 25
5759
self.temp_text.y = 110
5860
self._text_group.append(self.temp_text)
5961

60-
self.azure_status_text = Label(self.info_font, max_glyphs=40)
62+
self.azure_status_text = Label(self.info_font)
6163
self.azure_status_text.x = 100
6264
self.azure_status_text.y = 220
6365
self._text_group.append(self.azure_status_text)
@@ -76,19 +78,19 @@ def display_temp(self, adt_data):
7678
"""
7779
if not self._celsius:
7880
adt_data = (adt_data * 9 / 5) + 32
79-
print('Temperature: %0.2f°F'%adt_data)
81+
print("Temperature: %0.2f°F" % adt_data)
8082
if adt_data >= 212:
8183
self.temp_text.color = 0xFD2EE
8284
elif adt_data <= 32:
8385
self.temp_text.color = 0xFF0000
84-
self.temp_text.text = '%0.2f°F'%adt_data
86+
self.temp_text.text = "%0.2f°F" % adt_data
8587
else:
86-
print('Temperature: %0.2f°C'%adt_data)
88+
print("Temperature: %0.2f°C" % adt_data)
8789
if adt_data <= 0:
8890
self.temp_text.color = 0xFD2EE
8991
elif adt_data >= 100:
9092
self.temp_text.color = 0xFF0000
91-
self.temp_text.text = '%0.2f°C'%adt_data
93+
self.temp_text.text = "%0.2f°C" % adt_data
9294

9395
def set_icon(self, filename):
9496
"""Sets the background image to a bitmap file.
@@ -101,17 +103,20 @@ def set_icon(self, filename):
101103

102104
if not filename:
103105
return # we're done, no icon desired
106+
107+
# CircuitPython 6 & 7 compatible
104108
if self._icon_file:
105109
self._icon_file.close()
106110
self._icon_file = open(filename, "rb")
107111
icon = displayio.OnDiskBitmap(self._icon_file)
108-
try:
109-
self._icon_sprite = displayio.TileGrid(icon,
110-
pixel_shader=getattr(icon, 'pixel_shader', displayio.ColorConverter()))
111-
except TypeError:
112-
self._icon_sprite = displayio.TileGrid(icon,
113-
pixel_shader=getattr(icon, 'pixel_shader', displayio.ColorConverter()),
114-
position=(0,0))
112+
self._icon_sprite = displayio.TileGrid(
113+
icon, pixel_shader=getattr(icon, "pixel_shader", displayio.ColorConverter())
114+
)
115+
116+
# CircuitPython 7 compatible
117+
# # Remove self._icon_file - it is no longer used
118+
# icon = displayio.OnDiskBitmap(filename)
119+
# self._icon_sprite = displayio.TileGrid(icon, pixel_shader=icon.pixel_shader)
115120

116121
self._icon_group.append(self._icon_sprite)
117122
try:

0 commit comments

Comments
 (0)