diff --git a/src/code.py b/src/code.py index 8562bb6..8eab2e0 100644 --- a/src/code.py +++ b/src/code.py @@ -80,7 +80,6 @@ light_sensor = LightSensor(settings) datetime = DateTimeProcessing(settings, network) -#settings_display = SettingsDisplay(display, datetime) key_input = KeyProcessing(settings, datetime, buzzer) weather_display = WeatherDisplay(display, icons) @@ -102,6 +101,9 @@ last_weather = time.time() settings_visited = False +# remove splash from memory +del bg, splash + print('free memory', gc.mem_free()) while True: # Always process keys first diff --git a/src/weather/open_weather.py b/src/weather/open_weather.py index 4012053..37dfd02 100644 --- a/src/weather/open_weather.py +++ b/src/weather/open_weather.py @@ -19,6 +19,7 @@ def __init__(self, weather_display, datetime, network) -> None: # TODO: check parameters here and ensure geo works. lat, lon = self._get_geo(https, zip, country, token) self._url = URL.format(https, lat, lon, weather_display.units, token) + self._missed_weather = 0 def _get_geo(self, https, zip, country, token): @@ -42,16 +43,17 @@ def get_weather(self): # TODO: reduce size of json data and purge gc return weather + def show_datetime(self) -> bool: - self._weather_display.set_time(self._datetime.get_time()) + changed = self._weather_display.set_time(self._datetime.get_time()) # Only adjust the brightness once if self._datetime.is_display_on != self._is_display_on: self._weather_display.brightness = 0.1 if self._datetime.is_display_on else 0.0 self._is_display_on = self._datetime.is_display_on - if self._datetime.is_display_on: - self._weather_display.show() + if changed and self._datetime.is_display_on: + self._weather_display.show() return self._is_display_on @@ -70,8 +72,15 @@ def show_weather(self): ) if weather == None or weather == {} or weather["main"] == None: + if self._missed_weather > 5: + self._weather_display.hide_temperature() + self._weather_display.add_test_display("Unable to contact API") + else: + self._missed_weather += 1 return - try: + + try: + self._missed_weather = 0 self._weather_display.set_temperature(weather["main"]["temp"]) self._weather_display.set_icon(weather["weather"][0]["icon"]) # add Scrolling items diff --git a/src/weather/weather_display.py b/src/weather/weather_display.py index a0f352e..7381141 100644 --- a/src/weather/weather_display.py +++ b/src/weather/weather_display.py @@ -98,12 +98,16 @@ def get_temperature(self, temp): if self.units == 'metric': unit = "%d°C" else: - unit = "%d°F" - - + unit = "%d°F" return unit % temp + def hide_temperature(self): + self.temperature.text = "" + if self._icon_group: + self._icon_group.pop() + + def set_icon(self, name): if self._current_icon == name: return @@ -127,8 +131,11 @@ def set_icon(self, name): gc.collect() - def set_time(self, time_string): - self.time.text = time_string + def set_time(self, time_string) -> bool: + if self.time.text != time_string: + self.time.text = time_string + return True + return False def set_humidity(self, humidity): @@ -153,14 +160,17 @@ def set_wind(self, wind): else: self.scroll_queue.append(f'wind {wind:.1f} m/s') - + + def add_test_display(self, text): + self.scroll_queue.append(text) + + def scroll_label(self, key_input): ''' Scrolls the label until all the text has been shown TODO: Includes a hack to check if a button has been pressed to exit early because user is trying to get into the settings menu. ''' - # Take the top item to display - + # Button press leaves items in scroll group and mucks things up while len(self._scrolling_group) > 0: self._scrolling_group.pop()