|
| 1 | +""" |
| 2 | +GFX Helper for PyPortal AWS IoT Plant Monitor |
| 3 | +""" |
| 4 | +import board |
| 5 | +import displayio |
| 6 | +import terminalio |
| 7 | +from adafruit_display_text.label import Label |
| 8 | + |
| 9 | + # the current working directory (where this file is) |
| 10 | +cwd = ("/"+__file__).rsplit('/', 1)[0] |
| 11 | + |
| 12 | +# GFX Font |
| 13 | +font = terminalio.FONT |
| 14 | + |
| 15 | +class AWS_GFX(displayio.Group): |
| 16 | + def __init__(self, is_celsius=False): |
| 17 | + """Creates an AWS_GFX object for displaying plant |
| 18 | + and AWS IoT status. |
| 19 | + :param bool is_celsius: Temperature displayed in Celsius. |
| 20 | + """ |
| 21 | + # root displayio group |
| 22 | + root_group = displayio.Group(max_size=23) |
| 23 | + self.display = board.DISPLAY |
| 24 | + self.display.show(root_group) |
| 25 | + super().__init__(max_size=15) |
| 26 | + |
| 27 | + # temperature display option |
| 28 | + self._is_celsius = is_celsius |
| 29 | + |
| 30 | + # create background icon group |
| 31 | + self._icon_group = displayio.Group(max_size=3) |
| 32 | + self.append(self._icon_group) |
| 33 | + self.display.show(self._icon_group) |
| 34 | + # create text object group |
| 35 | + self._text_group = displayio.Group(max_size=40) |
| 36 | + self.append(self._text_group) |
| 37 | + |
| 38 | + print("Displaying splash screen") |
| 39 | + self._icon_sprite = None |
| 40 | + self._icon_file = None |
| 41 | + self._cwd = cwd |
| 42 | + self.set_icon(self._cwd+"/images/aws_splash.bmp") |
| 43 | + |
| 44 | + print('Setting up labels...') |
| 45 | + header_group = displayio.Group(scale=3) |
| 46 | + header_label = Label(font, text=" AWS IoT\n Planter") |
| 47 | + header_label.x = (self.display.width // 2) // 22 |
| 48 | + header_label.y = 15 |
| 49 | + header_group.append(header_label) |
| 50 | + self._text_group.append(header_group) |
| 51 | + |
| 52 | + # Temperature Display |
| 53 | + temp_group = displayio.Group(scale=2, max_size=400) |
| 54 | + temp_label = Label(font, text="Temperature: ") |
| 55 | + temp_label.x = (self.display.width//2) // 11 |
| 56 | + temp_label.y = 55 |
| 57 | + temp_group.append(temp_label) |
| 58 | + |
| 59 | + self.temp_data_label = Label(font, text="75 F") |
| 60 | + self.temp_data_label.x = (self.display.width//3) |
| 61 | + self.temp_data_label.y = 55 |
| 62 | + temp_group.append(self.temp_data_label) |
| 63 | + self._text_group.append(temp_group) |
| 64 | + |
| 65 | + # Water Level |
| 66 | + water_group = displayio.Group(scale=2, max_size=2) |
| 67 | + self.water_level = Label(font, text="Water Level: ") |
| 68 | + self.water_level.x = (self.display.width//2) // 11 |
| 69 | + self.water_level.y = 75 |
| 70 | + water_group.append(self.water_level) |
| 71 | + |
| 72 | + self.water_lvl_label = Label(font, text="350") |
| 73 | + self.water_lvl_label.x = (self.display.width//3) |
| 74 | + self.water_lvl_label.y = 75 |
| 75 | + temp_group.append(self.water_lvl_label) |
| 76 | + self._text_group.append(water_group) |
| 77 | + |
| 78 | + # AWS Status |
| 79 | + status_group = displayio.Group() |
| 80 | + self.aws_status_label = Label(font, text="Connecting to AWS IoT...") |
| 81 | + self.aws_status_label.x = int(self.display.width//3.5) |
| 82 | + self.aws_status_label.y = 200 |
| 83 | + status_group.append(self.aws_status_label) |
| 84 | + self._text_group.append(status_group) |
| 85 | + |
| 86 | + self.display.show(self._text_group) |
| 87 | + |
| 88 | + def show_aws_status(self, status_text): |
| 89 | + """Displays the system status on the PyPortal |
| 90 | + :param str status_text: Description of current AWS IoT status. |
| 91 | + """ |
| 92 | + self.aws_status_label.text = status_text |
| 93 | + |
| 94 | + def show_water_level(self, water_data): |
| 95 | + """Displays the water level from the Stemma Soil Sensor. |
| 96 | + :param int water_data: water value |
| 97 | + """ |
| 98 | + self.water_lvl_label.text = str(water_data) |
| 99 | + |
| 100 | + def show_temp(self, temp_data): |
| 101 | + """Displays the temperature from the Stemma Soil Sensor. |
| 102 | + :param float temp_data: Temperature value. |
| 103 | + """ |
| 104 | + if not self._is_celsius: |
| 105 | + temp_data = (temp_data * 9 / 5) + 32 - 15 |
| 106 | + print('Temperature: %0.0f°F'%temp_data) |
| 107 | + if temp_data >= 212: |
| 108 | + self.temp_data_label.color = 0xFD2EE |
| 109 | + elif temp_data <= 32: |
| 110 | + self.temp_data_label.color = 0xFF0000 |
| 111 | + self.temp_data_label = '%0.0f°F'%temp_data |
| 112 | + temp_data = '%0.0f'%temp_data |
| 113 | + return int(temp_data) |
| 114 | + else: |
| 115 | + print('Temperature: %0.0f°C'%temp_data) |
| 116 | + if temp_data <= 0: |
| 117 | + self.temp_data_label.color = 0xFD2EE |
| 118 | + elif temp_data >= 100: |
| 119 | + self.temp_data_label.color = 0xFF0000 |
| 120 | + self.temp_data_label.text = '%0.0f°C'%temp_data |
| 121 | + temp_data = '%0.0f'%temp_data |
| 122 | + return int(temp_data) |
| 123 | + |
| 124 | + def set_icon(self, filename): |
| 125 | + """Sets the background image to a bitmap file. |
| 126 | +
|
| 127 | + :param filename: The filename of the chosen icon |
| 128 | + """ |
| 129 | + print("Set icon to ", filename) |
| 130 | + if self._icon_group: |
| 131 | + self._icon_group.pop() |
| 132 | + |
| 133 | + if not filename: |
| 134 | + return # we're done, no icon desired |
| 135 | + if self._icon_file: |
| 136 | + self._icon_file.close() |
| 137 | + self._icon_file = open(filename, "rb") |
| 138 | + icon = displayio.OnDiskBitmap(self._icon_file) |
| 139 | + try: |
| 140 | + self._icon_sprite = displayio.TileGrid(icon, |
| 141 | + pixel_shader=displayio.ColorConverter()) |
| 142 | + except TypeError: |
| 143 | + self._icon_sprite = displayio.TileGrid(icon, |
| 144 | + pixel_shader=displayio.ColorConverter(), |
| 145 | + position=(0,0)) |
| 146 | + |
| 147 | + self._icon_group.append(self._icon_sprite) |
| 148 | + self.display.refresh_soon() |
| 149 | + self.display.wait_for_frame() |
0 commit comments