Advertisement
aalh

curr openweather_graphics.py

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