Advertisement
aalh

openweather_graphics.py

Oct 25th, 2021
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.54 KB | None | 0 0
  1. import time
  2. import displayio
  3. import busio
  4. import adafruit_ahtx0
  5. import adafruit_si7021
  6. from adafruit_display_text.label import Label
  7. from adafruit_bitmap_font import bitmap_font
  8. import board
  9.  
  10. import microcontroller
  11. i2c = busio.I2C(board.SCL, board.SDA)
  12. sensor = adafruit_ahtx0.AHTx0(i2c)
  13. outside_sensor = adafruit_si7021.SI7021(i2c)
  14. localt = ((sensor.temperature) * 1.8) + 32
  15. localh = sensor.relative_humidity
  16. localot = ((outside_sensor.temperature) * 1.8) + 32
  17. localoh = outside_sensor.relative_humidity
  18.  
  19. print("inside ",localt,", ",localh)
  20. print("outside ",localot,", ",localoh)
  21. TEMP_ARRAY = [0xFF0000, 0xFFA800, 0x0FFF00, 0x0000AA, 0x00D3FF]
  22. #TEMP_ARRAY = [0x00D3FF, 0x0000AA, 0x0FFF00, 0xFFA800, 0xFF0000]
  23. #temperature = weather["main"]["temp"]
  24. global TEMP_COLOR
  25. TEMP_COLOR = TEMP_ARRAY[int((95//localt)/2)+2]
  26. MAIN_COLOR = 0x9000FF  # weather condition
  27. DESCRIPTION_COLOR = 0x00D3FF
  28. CITY_COLOR = 0x9000FF
  29. HUMIDITY_COLOR = 0x0000AA
  30. WIND_COLOR = 0xCCCCCC
  31. LOCAL_COLOR = 0x0FFF00
  32. LOCAL_oCOLOR = 0xA000AA
  33. temperature = 72
  34. # HOTT = 0xFF0000
  35. # WARM = 0xFFA800
  36. # NICE = 0x0FFF00
  37. # COLD = 0x0000AA
  38. # ICAF = 0x00D3FF
  39.  
  40. cwd = ("/" + __file__).rsplit("/", 1)[
  41.     0
  42. ]  # the current working directory (where this file is)
  43.  
  44. small_font = cwd + "/fonts/Arial-12.bdf"
  45. medium_font = cwd + "/fonts/Arial-14.bdf"
  46.  
  47. icon_spritesheet = cwd + "/weather-icons.bmp"
  48. icon_width = 16
  49. icon_height = 16
  50. scrolling_text_height = 24
  51. scroll_delay = 0.03
  52.  
  53.  
  54. class OpenWeather_Graphics(displayio.Group):
  55.     def __init__(self, display, *, am_pm=True, units="imperial"):
  56.         super().__init__(max_size=3)
  57.         self.am_pm = am_pm
  58.         self.celsius = False
  59.         self.meters_speed = False
  60.         self.display = display
  61.  
  62.         splash = displayio.Group(max_size=1)
  63.         background = displayio.OnDiskBitmap(open("loading.bmp", "rb"))
  64.         bg_sprite = displayio.TileGrid(
  65.             background, pixel_shader=displayio.ColorConverter()
  66.         )
  67.         splash.append(bg_sprite)
  68.         display.show(splash)
  69.  
  70.         self.root_group = displayio.Group(max_size=15)
  71.         self.root_group.append(self)
  72.         self._icon_group = displayio.Group(max_size=1)
  73.         self.append(self._icon_group)
  74.         self._text_group = displayio.Group(max_size=5)
  75.         self.append(self._text_group)
  76.         self._scrolling_group = displayio.Group(max_size=1)
  77.         self.append(self._scrolling_group)
  78.  
  79.         # The label index we're currently scrolling
  80.         self._current_label = None
  81.  
  82.         # Load the icon sprite sheet
  83.         icons = displayio.OnDiskBitmap(open(icon_spritesheet, "rb"))
  84.         self._icon_sprite = displayio.TileGrid(
  85.             icons,
  86.             pixel_shader=displayio.ColorConverter(),
  87.             width=1,
  88.             height=1,
  89.             tile_width=icon_width,
  90.             tile_height=icon_height,
  91.             default_tile=0,
  92.             x=0,
  93.             y=0,
  94.         )
  95.         self.set_icon(None)
  96.         self._scrolling_texts = []
  97.  
  98.         self.small_font = bitmap_font.load_font(small_font)
  99.         self.medium_font = bitmap_font.load_font(medium_font)
  100.         glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.: "
  101.         self.small_font.load_glyphs(glyphs)
  102.         self.medium_font.load_glyphs(glyphs)
  103.         self.medium_font.load_glyphs(("°",))  # a non-ascii character we need for sure
  104.  
  105.         self.city_text = None
  106.         print("initemp", temperature)
  107.         print("tt ",int((95//temperature)/2)+2)
  108. #        global LOCAL_oCOLOR
  109. #        LOCAL_oCOLOR = TEMP_ARRAY[int((95//temperature)/2)+2]
  110.         self.temp_text = Label(self.medium_font, max_glyphs=6)
  111.         self.temp_text.x = 20
  112.         self.temp_text.y = 7
  113.         self.temp_text.color = TEMP_COLOR
  114.         self._text_group.append(self.temp_text)
  115.  
  116.         self.description_text = Label(self.small_font, max_glyphs=60)
  117.         self.description_text.color = DESCRIPTION_COLOR
  118.         self._scrolling_texts.append(self.description_text)
  119.  
  120.         self.humidity_text = Label(self.small_font, max_glyphs=14)
  121.         self.humidity_text.color = HUMIDITY_COLOR  #
  122.         self._scrolling_texts.append(self.humidity_text)
  123.  
  124.         self.localth_text = Label(self.small_font, max_glyphs=25)
  125.         self.localth_text.color = LOCAL_COLOR  #
  126.         self._scrolling_texts.append(self.localth_text)
  127.  
  128. #        global LOCAL_oCOLOR
  129. #        LOCAL_oCOLOR = TEMP_ARRAY[int((95//temperature)/2)+2]
  130.         self.localoth_text = Label(self.small_font, max_glyphs=25)
  131.         self.localoth_text.color = LOCAL_oCOLOR  #
  132.         self._scrolling_texts.append(self.localoth_text)
  133.  
  134.         self.wind_text = Label(self.small_font, max_glyphs=14)
  135.         self.wind_text.color = WIND_COLOR
  136.         self._scrolling_texts.append(self.wind_text)
  137.  
  138.     def display_weather(self, weather):
  139.         # set the icon
  140.         self.set_icon(weather["weather"][0]["icon"])
  141.  
  142.         #        city_name = weather["name"] + ", " + weather["sys"]["country"]
  143.         #        print(city_name)
  144.         #        if not self.city_text:
  145.         #            self.city_text = Label(self.small_font, text=city_name)
  146.         #            self.city_text.color = CITY_COLOR
  147.         #            self._scrolling_texts.append(self.city_text)
  148.  
  149.         global temperature
  150.         temperature  = weather["main"]["temp"]
  151.         global TEMP_COLOR
  152.         TEMP_COLOR = TEMP_ARRAY[int((95//temperature)/2)+2]
  153.  
  154.         print("updatedT ",temperature)
  155.         print(int(95//temperature)+2)
  156.         if self.celsius:
  157.             self.temp_text.text = "%d°C" % temperature
  158.         else:
  159.             self.temp_text.text = "%d°F" % temperature
  160.  
  161.         description = weather["weather"][0]["description"]
  162.         description = description[0].upper() + description[1:]
  163.         print(description)
  164.         self.description_text.text = description
  165.         # "thunderstorm with heavy drizzle"
  166.  
  167.         humidity = weather["main"]["humidity"]
  168.         print(humidity)
  169.         self.humidity_text.text = "%d%% humidity" % humidity
  170.  
  171.         bearing = ""
  172.         wind = weather["wind"]["speed"]
  173.         degg = weather["wind"]["deg"]
  174.         if degg < 45 or degg > 315:
  175.             bearing = "N"
  176.         if degg < 135 or degg > 45:
  177.             bearing = "E"
  178.         if degg < 225 or degg > 135:
  179.             bearing = "S"
  180.         if degg < 315 or degg > 225:
  181.             bearing = "W"
  182.  
  183.         print(wind)
  184.         print(degg)
  185.  
  186.         if self.meters_speed:
  187.             self.wind_text.text = "%d m/s" % wind
  188.         else:
  189.             self.wind_text.text = "Wind %.2f mph " % wind + bearing
  190.  
  191.  
  192.         localt = ((sensor.temperature) * 1.8) + 32
  193.         localh = sensor.relative_humidity
  194.         print(localt)
  195.         print(localh)
  196.         self.localth_text.text = "%d°F " % localt + "%d%% inside" % localh
  197.  
  198.         localot = ((outside_sensor.temperature) * 1.8) + 32
  199.         localoh = outside_sensor.relative_humidity
  200.         print(localot)
  201.         print(localoh)
  202.         self.localoth_text.text = "%d°F " % localot + "%d%% outside" % localoh
  203.         global LOCAL_oCOLOR
  204.         LOCAL_oCOLOR = TEMP_ARRAY[int((95//temperature)/2)+2]
  205.  
  206.  
  207.         self.display.show(self.root_group)
  208.  
  209. #    def local_sensors(self,
  210.  
  211.     def set_icon(self, icon_name):
  212.         """Use icon_name to get the position of the sprite and update
  213.        the current icon.
  214.  
  215.        :param icon_name: The icon name returned by openweathermap
  216.  
  217.        Format is always 2 numbers followed by 'd' or 'n' as the 3rd character
  218.        """
  219.  
  220.         icon_map = ("01", "02", "03", "04", "09", "10", "11", "13", "50")
  221.  
  222.         print("Set icon to", icon_name)
  223.         if self._icon_group:
  224.             self._icon_group.pop()
  225.         if icon_name is not None:
  226.             row = None
  227.             for index, icon in enumerate(icon_map):
  228.                 if icon == icon_name[0:2]:
  229.                     row = index
  230.                     break
  231.             column = 0
  232.             if icon_name[2] == "n":
  233.                 column = 1
  234.             if row is not None:
  235.                 self._icon_sprite[0] = (row * 2) + column
  236.                 self._icon_group.append(self._icon_sprite)
  237.  
  238.     def scroll_next_label(self):
  239.         # Start by scrolling current label off if not set to None
  240.         if self._current_label is not None and self._scrolling_group:
  241.             current_text = self._scrolling_texts[self._current_label]
  242.             text_width = current_text.bounding_box[2]
  243.             for _ in range(text_width + 1):
  244.                 self._scrolling_group.x = self._scrolling_group.x - 1
  245.                 time.sleep(scroll_delay)
  246.  
  247.         if self._current_label is not None:
  248.             self._current_label += 1
  249.         if self._current_label is None or self._current_label >= len(
  250.             self._scrolling_texts
  251.         ):
  252.             self._current_label = 0
  253.  
  254.         # Setup the scrolling group by removing any existing
  255.         if self._scrolling_group:
  256.             self._scrolling_group.pop()
  257.         # Then add the current label
  258.         current_text = self._scrolling_texts[self._current_label]
  259.         self._scrolling_group.append(current_text)
  260.  
  261.         # Set the position of the group to just off screen and centered vertically for lower half
  262.         self._scrolling_group.x = self.display.width
  263.         self._scrolling_group.y = 23
  264.  
  265.         # Run a loop until the label is offscreen again and leave function
  266.         for _ in range(self.display.width):
  267.             self._scrolling_group.x = self._scrolling_group.x - 1
  268.             time.sleep(scroll_delay)
  269.         # By blocking other code we will never leave the label half way scrolled
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement