/ pyportal_weather_station / weatherstation_helper.py
weatherstation_helper.py
  1  # SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  """
  6  Helper file for
  7  pyportal_weatherstation.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  medium_font = cwd+"/fonts/Arial-16.bdf"
 18  header_font = cwd+"/fonts/Collegiate-24.bdf"
 19  glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.: '
 20  
 21  class WeatherStation_GFX(displayio.Group):
 22  
 23      def __init__(self, celsius=True):
 24          # root displayio group
 25          root_group = displayio.Group()
 26          board.DISPLAY.show(root_group)
 27          super().__init__()
 28          self._celsius = celsius
 29  
 30          # create background icon group
 31          self._icon_group = displayio.Group()
 32          board.DISPLAY.show(self._icon_group)
 33  
 34          # create text object group
 35          self._text_group = displayio.Group()
 36  
 37          self._icon_sprite = None
 38          self._icon_file = None
 39          self._cwd = cwd
 40          self.set_icon(self._cwd+"/icons/pyportal_splash.bmp")
 41  
 42          print('loading fonts...')
 43          self.medium_font = bitmap_font.load_font(medium_font)
 44          self.c_font = bitmap_font.load_font(header_font)
 45          self.medium_font.load_glyphs(glyphs)
 46          self.c_font.load_glyphs(glyphs)
 47  
 48          print('setting up Labels...')
 49          self.title_text = Label(self.c_font, text = "PyPortal Weather Station")
 50          self.title_text.x = 50
 51          self.title_text.y = 10
 52  
 53          self.io_status_text = Label(self.c_font)
 54          self.io_status_text.x = 65
 55          self.io_status_text.y = 190
 56  
 57          # Set up Labels to label sensor data
 58          self.veml_text = Label(self.medium_font)
 59          self.veml_text.x = 3
 60          self.veml_text.y = 40
 61  
 62          self.bme_temp_humid_text = Label(self.medium_font)
 63          self.bme_temp_humid_text.x = 0
 64          self.bme_temp_humid_text.y = 70
 65  
 66          self.wind_speed_text = Label(self.medium_font)
 67          self.wind_speed_text.x = 0
 68          self.wind_speed_text.y = 100
 69  
 70          self.bme_pres_alt_text = Label(self.medium_font)
 71          self.bme_pres_alt_text.x = 0
 72          self.bme_pres_alt_text.y = 130
 73  
 74          self.sgp_text = Label(self.medium_font)
 75          self.sgp_text.x = 0
 76          self.sgp_text.y = 155
 77  
 78          board.DISPLAY.show(self._text_group)
 79  
 80      def display_io_status(self, status_text):
 81          """Displays the current IO status.
 82          :param str status_text: Description of Adafruit IO status
 83          """
 84          self.io_status_text.text = status_text
 85          board.DISPLAY.refresh(target_frames_per_second=60)
 86  
 87  
 88      def display_data(self, uv_index, bme_data, sgp_data, wind_speed):
 89          """Displays the data from the sensors attached
 90          to the weathermeter pyportal and sends the data to Adafruit IO.
 91  
 92          :param float uv_index: VEML6075 uv index level.
 93          :param list sgp_data: ECO2 and TVOC data from SGP30 sensor.
 94          :param list bme_data: List of env. data from the BME280 sensor.
 95          :param float wind_speed: Wind speed from anemometer.
 96          """
 97          print('UV Index: ', uv_index)
 98          self.veml_text.text = 'UV Index: %0.1f'%uv_index
 99  
100          temperature = round(bme_data[0], 1)
101          print('Temperature: {0} C'.format(temperature))
102          humidity = round(bme_data[1], 1)
103          print('Humidity: {0}%'.format(humidity))
104          if not self._celsius:
105              temperature = (temperature * 9 / 5) + 32
106              self.bme_temp_humid_text.text = 'Temp: {0}°F, Humid: {1}%'.format(temperature, humidity)
107          else:
108              self.bme_temp_humid_text.text = 'Temp: {0}°C, Humid: {1}%'.format(temperature, humidity)
109  
110          print("Wind Speed: %f m/s" % wind_speed)
111          self.wind_speed_text.text = "Wind Speed %0.2fm/s" % wind_speed
112  
113          pressure = round(bme_data[2], 3)
114          altitude = round(bme_data[3], 1)
115          print('Altitude: %0.3f meters, Pressure: %0.2f hPa'%(altitude, pressure))
116          self.bme_pres_alt_text.text = 'Alt: {0}m, Pres: {1}hPa'.format(altitude, pressure)
117  
118          print("eCO2 = %d ppm \t TVOC = %d ppb"%(sgp_data[0], sgp_data[1]))
119          self.sgp_text.text = "eCO2: %d ppm, TVOC: %d ppb"%(sgp_data[0], sgp_data[1])
120  
121          board.DISPLAY.refresh(target_frames_per_second=60)
122  
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  
140          # CircuitPython 6 & 7 compatible
141          self._icon_sprite = displayio.TileGrid(
142              icon,
143              pixel_shader=getattr(icon, 'pixel_shader', displayio.ColorConverter()))
144  
145          # # CircuitPython 7+ compatible
146          # self._icon_sprite = displayio.TileGrid(
147          #     icon,
148          #     pixel_shader=icon.pixel_shader)
149          board.DISPLAY.refresh(target_frames_per_second=60)