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