Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import displayio
- import busio
- import adafruit_ahtx0
- import adafruit_si7021
- from adafruit_display_text.label import Label
- from adafruit_bitmap_font import bitmap_font
- import board
- import microcontroller
- i2c = busio.I2C(board.SCL, board.SDA)
- sensor = adafruit_ahtx0.AHTx0(i2c)
- outside_sensor = adafruit_si7021.SI7021(i2c)
- localt = ((sensor.temperature) * 1.8) + 32
- localh = sensor.relative_humidity
- localot = ((outside_sensor.temperature) * 1.8) + 32
- localoh = outside_sensor.relative_humidity
- print("inside ",localt,", ",localh)
- print("outside ",localot,", ",localoh)
- TEMP_ARRAY = [0xFF0000, 0xFFA800, 0x0FFF00, 0x0000AA, 0x00D3FF]
- #TEMP_ARRAY = [0x00D3FF, 0x0000AA, 0x0FFF00, 0xFFA800, 0xFF0000]
- #temperature = weather["main"]["temp"]
- global TEMP_COLOR
- TEMP_COLOR = TEMP_ARRAY[int((95//localt)/2)+2]
- MAIN_COLOR = 0x9000FF # weather condition
- DESCRIPTION_COLOR = 0x00D3FF
- CITY_COLOR = 0x9000FF
- HUMIDITY_COLOR = 0x0000AA
- WIND_COLOR = 0xCCCCCC
- LOCAL_COLOR = 0x0FFF00
- LOCAL_oCOLOR = 0xA000AA
- temperature = 72
- # HOTT = 0xFF0000
- # WARM = 0xFFA800
- # NICE = 0x0FFF00
- # COLD = 0x0000AA
- # ICAF = 0x00D3FF
- cwd = ("/" + __file__).rsplit("/", 1)[
- 0
- ] # the current working directory (where this file is)
- small_font = cwd + "/fonts/Arial-12.bdf"
- medium_font = cwd + "/fonts/Arial-14.bdf"
- icon_spritesheet = cwd + "/weather-icons.bmp"
- icon_width = 16
- icon_height = 16
- scrolling_text_height = 24
- scroll_delay = 0.03
- class OpenWeather_Graphics(displayio.Group):
- def __init__(self, display, *, am_pm=True, units="imperial"):
- super().__init__(max_size=3)
- self.am_pm = am_pm
- self.celsius = False
- self.meters_speed = False
- self.display = display
- splash = displayio.Group(max_size=1)
- background = displayio.OnDiskBitmap(open("loading.bmp", "rb"))
- bg_sprite = displayio.TileGrid(
- background, pixel_shader=displayio.ColorConverter()
- )
- splash.append(bg_sprite)
- display.show(splash)
- self.root_group = displayio.Group(max_size=15)
- self.root_group.append(self)
- self._icon_group = displayio.Group(max_size=1)
- self.append(self._icon_group)
- self._text_group = displayio.Group(max_size=5)
- self.append(self._text_group)
- self._scrolling_group = displayio.Group(max_size=1)
- self.append(self._scrolling_group)
- # The label index we're currently scrolling
- self._current_label = None
- # Load the icon sprite sheet
- icons = displayio.OnDiskBitmap(open(icon_spritesheet, "rb"))
- self._icon_sprite = displayio.TileGrid(
- icons,
- pixel_shader=displayio.ColorConverter(),
- width=1,
- height=1,
- tile_width=icon_width,
- tile_height=icon_height,
- default_tile=0,
- x=0,
- y=0,
- )
- self.set_icon(None)
- self._scrolling_texts = []
- self.small_font = bitmap_font.load_font(small_font)
- self.medium_font = bitmap_font.load_font(medium_font)
- glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.: "
- self.small_font.load_glyphs(glyphs)
- self.medium_font.load_glyphs(glyphs)
- self.medium_font.load_glyphs(("°",)) # a non-ascii character we need for sure
- self.city_text = None
- print("initemp", temperature)
- print("tt ",int((95//temperature)/2)+2)
- # global LOCAL_oCOLOR
- # LOCAL_oCOLOR = TEMP_ARRAY[int((95//temperature)/2)+2]
- self.temp_text = Label(self.medium_font, max_glyphs=6)
- self.temp_text.x = 20
- self.temp_text.y = 7
- self.temp_text.color = TEMP_COLOR
- self._text_group.append(self.temp_text)
- self.description_text = Label(self.small_font, max_glyphs=60)
- self.description_text.color = DESCRIPTION_COLOR
- self._scrolling_texts.append(self.description_text)
- self.humidity_text = Label(self.small_font, max_glyphs=14)
- self.humidity_text.color = HUMIDITY_COLOR #
- self._scrolling_texts.append(self.humidity_text)
- self.localth_text = Label(self.small_font, max_glyphs=25)
- self.localth_text.color = LOCAL_COLOR #
- self._scrolling_texts.append(self.localth_text)
- # global LOCAL_oCOLOR
- # LOCAL_oCOLOR = TEMP_ARRAY[int((95//temperature)/2)+2]
- self.localoth_text = Label(self.small_font, max_glyphs=25)
- self.localoth_text.color = LOCAL_oCOLOR #
- self._scrolling_texts.append(self.localoth_text)
- self.wind_text = Label(self.small_font, max_glyphs=14)
- self.wind_text.color = WIND_COLOR
- self._scrolling_texts.append(self.wind_text)
- def display_weather(self, weather):
- # set the icon
- self.set_icon(weather["weather"][0]["icon"])
- # city_name = weather["name"] + ", " + weather["sys"]["country"]
- # print(city_name)
- # if not self.city_text:
- # self.city_text = Label(self.small_font, text=city_name)
- # self.city_text.color = CITY_COLOR
- # self._scrolling_texts.append(self.city_text)
- global temperature
- temperature = weather["main"]["temp"]
- global TEMP_COLOR
- TEMP_COLOR = TEMP_ARRAY[int((95//temperature)/2)+2]
- print("updatedT ",temperature)
- print(int(95//temperature)+2)
- if self.celsius:
- self.temp_text.text = "%d°C" % temperature
- else:
- self.temp_text.text = "%d°F" % temperature
- description = weather["weather"][0]["description"]
- description = description[0].upper() + description[1:]
- print(description)
- self.description_text.text = description
- # "thunderstorm with heavy drizzle"
- humidity = weather["main"]["humidity"]
- print(humidity)
- self.humidity_text.text = "%d%% humidity" % humidity
- bearing = ""
- wind = weather["wind"]["speed"]
- degg = weather["wind"]["deg"]
- if degg < 45 or degg > 315:
- bearing = "N"
- if degg < 135 or degg > 45:
- bearing = "E"
- if degg < 225 or degg > 135:
- bearing = "S"
- if degg < 315 or degg > 225:
- bearing = "W"
- print(wind)
- print(degg)
- if self.meters_speed:
- self.wind_text.text = "%d m/s" % wind
- else:
- self.wind_text.text = "Wind %.2f mph " % wind + bearing
- localt = ((sensor.temperature) * 1.8) + 32
- localh = sensor.relative_humidity
- print(localt)
- print(localh)
- self.localth_text.text = "%d°F " % localt + "%d%% inside" % localh
- localot = ((outside_sensor.temperature) * 1.8) + 32
- localoh = outside_sensor.relative_humidity
- print(localot)
- print(localoh)
- self.localoth_text.text = "%d°F " % localot + "%d%% outside" % localoh
- global LOCAL_oCOLOR
- LOCAL_oCOLOR = TEMP_ARRAY[int((95//temperature)/2)+2]
- self.display.show(self.root_group)
- # def local_sensors(self,
- def set_icon(self, icon_name):
- """Use icon_name to get the position of the sprite and update
- the current icon.
- :param icon_name: The icon name returned by openweathermap
- Format is always 2 numbers followed by 'd' or 'n' as the 3rd character
- """
- icon_map = ("01", "02", "03", "04", "09", "10", "11", "13", "50")
- print("Set icon to", icon_name)
- if self._icon_group:
- self._icon_group.pop()
- if icon_name is not None:
- row = None
- for index, icon in enumerate(icon_map):
- if icon == icon_name[0:2]:
- row = index
- break
- column = 0
- if icon_name[2] == "n":
- column = 1
- if row is not None:
- self._icon_sprite[0] = (row * 2) + column
- self._icon_group.append(self._icon_sprite)
- def scroll_next_label(self):
- # Start by scrolling current label off if not set to None
- if self._current_label is not None and self._scrolling_group:
- current_text = self._scrolling_texts[self._current_label]
- text_width = current_text.bounding_box[2]
- for _ in range(text_width + 1):
- self._scrolling_group.x = self._scrolling_group.x - 1
- time.sleep(scroll_delay)
- if self._current_label is not None:
- self._current_label += 1
- if self._current_label is None or self._current_label >= len(
- self._scrolling_texts
- ):
- self._current_label = 0
- # Setup the scrolling group by removing any existing
- if self._scrolling_group:
- self._scrolling_group.pop()
- # Then add the current label
- current_text = self._scrolling_texts[self._current_label]
- self._scrolling_group.append(current_text)
- # Set the position of the group to just off screen and centered vertically for lower half
- self._scrolling_group.x = self.display.width
- self._scrolling_group.y = 23
- # Run a loop until the label is offscreen again and leave function
- for _ in range(self.display.width):
- self._scrolling_group.x = self._scrolling_group.x - 1
- time.sleep(scroll_delay)
- # By blocking other code we will never leave the label half way scrolled
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement