/ PyPortal_Mirror_Display / openweather_graphics.py
openweather_graphics.py
  1  # SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  import time
  6  import json
  7  import displayio
  8  from adafruit_display_text.label import Label
  9  from adafruit_bitmap_font import bitmap_font
 10  
 11  cwd = ("/"+__file__).rsplit('/', 1)[0] # the current working directory (where this file is)
 12  
 13  small_font = cwd+"/fonts/Arial-12.bdf"
 14  medium_font = cwd+"/fonts/Arial-16.bdf"
 15  large_font = cwd+"/fonts/Arial-Bold-24.bdf"
 16  
 17  class OpenWeather_Graphics(displayio.Group):
 18      def __init__(self, root_group, *, am_pm=True, celsius=True):
 19          super().__init__()
 20          self.am_pm = am_pm
 21          self.celsius = celsius
 22  
 23          root_group.append(self)
 24          self._icon_group = displayio.Group()
 25          self.append(self._icon_group)
 26          self._text_group = displayio.Group()
 27          self.append(self._text_group)
 28  
 29          self._icon_sprite = None
 30          self._icon_file = None
 31  
 32          self.small_font = bitmap_font.load_font(small_font)
 33          self.medium_font = bitmap_font.load_font(medium_font)
 34          self.large_font = bitmap_font.load_font(large_font)
 35          glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.: '
 36          self.small_font.load_glyphs(glyphs)
 37          self.medium_font.load_glyphs(glyphs)
 38          self.large_font.load_glyphs(glyphs)
 39          self.large_font.load_glyphs(('°',))  # a non-ascii character we need for sure
 40          self.city_text = None
 41  
 42          self.time_text = Label(self.medium_font)
 43          self.time_text.x = 10
 44          self.time_text.y = 40
 45          self.time_text.color = 0xFFFFFF
 46          self._text_group.append(self.time_text)
 47  
 48          self.temp_text = Label(self.large_font)
 49          self.temp_text.x = 165
 50          self.temp_text.y = 250
 51          self.temp_text.color = 0xFFFFFF
 52          self._text_group.append(self.temp_text)
 53  
 54          self.main_text = Label(self.large_font)
 55          self.main_text.x = 10
 56          self.main_text.y = 250
 57          self.main_text.color = 0xFFFFFF
 58          self._text_group.append(self.main_text)
 59  
 60          self.description_text = Label(self.small_font)
 61          self.description_text.x = 10
 62          self.description_text.y = 280
 63          self.description_text.color = 0xFFFFFF
 64          self._text_group.append(self.description_text)
 65  
 66      def display_weather(self, weather):
 67          weather = json.loads(weather)
 68  
 69          # set the icon/background
 70          weather_icon = weather['weather'][0]['icon']
 71          self.set_icon(cwd+"/icons/"+weather_icon+".bmp")
 72  
 73          city_name =  weather['name'] + ", " + weather['sys']['country']
 74          print(city_name)
 75          if not self.city_text:
 76              self.city_text = Label(self.medium_font, text=city_name)
 77              self.city_text.x = 10
 78              self.city_text.y = 12
 79              self.city_text.color = 0xFFFFFF
 80              self._text_group.append(self.city_text)
 81  
 82          self.update_time()
 83  
 84          main_text = weather['weather'][0]['main']
 85          print(main_text)
 86          self.main_text.text = main_text
 87  
 88          temperature = weather['main']['temp'] - 273.15 # its...in kelvin
 89          print(temperature)
 90          if self.celsius:
 91              self.temp_text.text = "%d°C" % temperature
 92          else:
 93              self.temp_text.text = "%d°F" % ((temperature * 9 / 5) + 32)
 94  
 95          description = weather['weather'][0]['description']
 96          description = description[0].upper() + description[1:]
 97          print(description)
 98          self.description_text.text = description
 99          # "thunderstorm with heavy drizzle"
100  
101      def update_time(self):
102          """Fetch the time.localtime(), parse it out and update the display text"""
103          now = time.localtime()
104          hour = now[3]
105          minute = now[4]
106          format_str = "%d:%02d"
107          if self.am_pm:
108              if hour >= 12:
109                  hour -= 12
110                  format_str = format_str+" PM"
111              else:
112                  format_str = format_str+" AM"
113              if hour == 0:
114                  hour = 12
115          time_str = format_str % (hour, minute)
116          print(time_str)
117          self.time_text.text = time_str
118  
119      def set_icon(self, filename):
120          """The background image to a bitmap file.
121  
122          :param filename: The filename of the chosen icon
123  
124          """
125          print("Set icon to ", filename)
126          if self._icon_group:
127              self._icon_group.pop()
128  
129          if not filename:
130              return  # we're done, no icon desired
131  
132          if self._icon_file:
133              self._icon_file.close()
134          self._icon_file = open(filename, "rb")
135  
136          icon = displayio.OnDiskBitmap(self._icon_file)
137          self._icon_sprite = displayio.TileGrid(icon, pixel_shader=icon.pixel_shader)
138  
139          self._icon_group.append(self._icon_sprite)