/ PyPortal_Smart_Thermometer / thermometer_helper.py
thermometer_helper.py
  1  # SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  """
  6  GFX helper file for
  7  thermometer.py
  8  """
  9  import board
 10  import displayio
 11  from adafruit_display_text.label import Label
 12  from adafruit_bitmap_font import bitmap_font
 13  
 14  cwd = ("/"+__file__).rsplit('/', 1)[0] # the current working directory (where this file is)
 15  
 16  # Fonts within /fonts folder
 17  info_font = cwd+"/fonts/Nunito-Black-17.bdf"
 18  temperature_font = cwd+"/fonts/Nunito-Light-75.bdf"
 19  
 20  class Thermometer_GFX(displayio.Group):
 21      def __init__(self, celsius=True, usa_date=True):
 22          """Creates a Thermometer_GFX object.
 23          :param bool celsius: Temperature displayed as F or C
 24          :param bool usa_date: Use mon/day/year date-time formatting.
 25          """
 26          # root displayio group
 27          root_group = displayio.Group()
 28          board.DISPLAY.show(root_group)
 29          super().__init__()
 30  
 31          self._celsius = celsius
 32          self._usa_date = usa_date
 33  
 34          # create background icon group
 35          self._icon_group = displayio.Group()
 36          board.DISPLAY.show(self._icon_group)
 37          # create text object group
 38          self._text_group = displayio.Group()
 39  
 40          self._icon_sprite = None
 41          self._icon_file = None
 42          self._cwd = cwd
 43          self.set_icon(self._cwd+"/icons/pyportal_splash.bmp")
 44  
 45          print('loading fonts...')
 46          glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:/ '
 47          self.info_font = bitmap_font.load_font(info_font)
 48          self.info_font.load_glyphs(glyphs)
 49  
 50          self.c_font = bitmap_font.load_font(temperature_font)
 51          self.c_font.load_glyphs(glyphs)
 52          self.c_font.load_glyphs(('°',)) # extra glyph for temperature font
 53  
 54          print('setting up labels...')
 55          self.title_text = Label(self.info_font, text="PyPortal Thermometer")
 56          self.title_text.x = 55
 57          self.title_text.y = 15
 58          self._text_group.append(self.title_text)
 59  
 60          self.subtitle_text = Label(self.info_font, text="Analog Devices ADT7410")
 61          self.subtitle_text.x = 43
 62          self.subtitle_text.y = 35
 63          self._text_group.append(self.subtitle_text)
 64  
 65          self.temp_text = Label(self.c_font)
 66          self.temp_text.x = 25
 67          self.temp_text.y = 110
 68          self._text_group.append(self.temp_text)
 69  
 70          self.time_text = Label(self.info_font)
 71          self.time_text.x = 240
 72          self.time_text.y = 150
 73          self._text_group.append(self.time_text)
 74  
 75          self.date_text = Label(self.info_font)
 76          self.date_text.x = 30
 77          self.date_text.y = 160
 78          self._text_group.append(self.date_text)
 79  
 80          self.io_status_text = Label(self.info_font)
 81          self.io_status_text.x = 100
 82          self.io_status_text.y = 220
 83          self._text_group.append(self.io_status_text)
 84  
 85          board.DISPLAY.show(self._text_group)
 86  
 87  
 88      def display_date_time(self, io_time):
 89          """Parses and displays the time obtained from Adafruit IO, based on IP
 90          :param struct_time io_time: Structure used for date/time, returned from Adafruit IO.
 91          """
 92          self.time_text.text = '%02d:%02d'%(io_time[3],io_time[4])
 93          if not self._usa_date:
 94              self.date_text.text = '{0}/{1}/{2}'.format(io_time[2], io_time[1], io_time[0])
 95          else:
 96              self.date_text.text = '{0}/{1}/{2}'.format(io_time[1], io_time[2], io_time[0])
 97  
 98      def display_io_status(self, status_text):
 99          """Displays the current Adafruit IO status.
100          :param str status_text: Description of Adafruit IO status
101          """
102          self.io_status_text.text = status_text
103  
104      def display_temp(self, adt_data):
105          """Displays the data from the ADT7410 on the.
106          :param float adt_data: Value from the ADT7410
107          """
108          if not self._celsius:
109              adt_data = (adt_data * 9 / 5) + 32
110              print('Temperature: %0.2f°F'%adt_data)
111              if adt_data >= 212:
112                  self.temp_text.color = 0xFD2EE
113              elif adt_data <= 32:
114                  self.temp_text.color = 0xFF0000
115              self.temp_text.text = '%0.2f°F'%adt_data
116          else:
117              print('Temperature: %0.2f°C'%adt_data)
118              if adt_data <= 0:
119                  self.temp_text.color = 0xFD2EE
120              elif adt_data >= 100:
121                  self.temp_text.color = 0xFF0000
122              self.temp_text.text = '%0.2f°C'%adt_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  
138          # CircuitPython 6 & 7 compatible
139          self._icon_file = open(filename, "rb")
140          icon = displayio.OnDiskBitmap(self._icon_file)
141          self._icon_sprite = displayio.TileGrid(icon,
142                                                 pixel_shader=getattr(icon, 'pixel_shader', displayio.ColorConverter()))
143  
144          # # CircuitPython 7+ compatible
145          # icon = displayio.OnDiskBitmap(filename)
146          # self._icon_sprite = displayio.TileGrid(icon, pixel_shader=icon.pixel_shader)
147  
148          self._icon_group.append(self._icon_sprite)