/ PyPortal_AWS_IOT_Planter / aws_gfx_helper.py
aws_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 AWS 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 AWS_GFX(displayio.Group):
 20      def __init__(self, is_celsius=False):
 21          """Creates an AWS_GFX object for displaying plant
 22          and AWS IoT 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
 43          self._cwd = cwd
 44          self.set_icon(self._cwd+"/images/aws_splash.bmp")
 45  
 46          print('Setting up labels...')
 47          header_group = displayio.Group(scale=3)
 48          header_label = Label(font, text="    AWS 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          # AWS Status
 81          status_group = displayio.Group()
 82          self.aws_status_label = Label(font, text="Connecting to AWS IoT...")
 83          self.aws_status_label.x = int(self.display.width//3.5)
 84          self.aws_status_label.y = 200
 85          status_group.append(self.aws_status_label)
 86          self._text_group.append(status_group)
 87  
 88          self.display.show(self._text_group)
 89  
 90      def show_aws_status(self, status_text):
 91          """Displays the system status on the PyPortal
 92          :param str status_text: Description of current AWS IoT status.
 93          """
 94          self.aws_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          self.water_lvl_label.text = str(water_data)
101  
102      def show_temp(self, temp_data):
103          """Displays the temperature from the Stemma Soil Sensor.
104          :param float temp_data: Temperature value.
105          """
106          if not self._is_celsius:
107              temp_data = (temp_data * 9 / 5) + 32 - 15
108              print('Temperature: %0.0f°F'%temp_data)
109              if temp_data >= 212:
110                  self.temp_data_label.color = 0xFD2EE
111              elif temp_data <= 32:
112                  self.temp_data_label.color = 0xFF0000
113              self.temp_data_label.text = '%0.0f°F'%temp_data
114              temp_data = '%0.0f'%temp_data
115              return int(temp_data)
116          else:
117              print('Temperature: %0.0f°C'%temp_data)
118              if temp_data <= 0:
119                  self.temp_data_label.color = 0xFD2EE
120              elif temp_data >= 100:
121                  self.temp_data_label.color = 0xFF0000
122              self.temp_data_label.text = '%0.0f°C'%temp_data
123              temp_data = '%0.0f'%temp_data
124              return int(temp_data)
125  
126      def set_icon(self, filename):
127          """Sets the background image to a bitmap file.
128  
129          :param filename: The filename of the chosen icon
130          """
131          print("Set icon to ", filename)
132          if self._icon_group:
133              self._icon_group.pop()
134  
135          if not filename:
136              return  # we're done, no icon desired
137  
138          # CircuitPython 6 & 7 compatible
139          if self._icon_file:
140              self._icon_file.close()
141          self._icon_file = open(filename, "rb")
142          icon = displayio.OnDiskBitmap(self._icon_file)
143          self._icon_sprite = displayio.TileGrid(icon,
144                                                 pixel_shader=getattr(icon, 'pixel_shader', displayio.ColorConverter()))
145  
146          # # CircuitPython 7+ compatible
147          # icon = displayio.OnDiskBitmap(filename)
148          # self._icon_sprite = displayio.TileGrid(icon, pixel_shader=icon.pixel_shader)
149  
150          self._icon_group.append(self._icon_sprite)