diff --git a/weather_display.py b/weather_display.py index 1e51dd9..8c405e2 100644 --- a/weather_display.py +++ b/weather_display.py @@ -7,17 +7,20 @@ class WeatherDisplay: self.topRow = topRow self.twoDigitStartColumn = 9 self.oneDigitStartColumn = 13 - self.currentTemperature = [9, 9] + self.currentTemperature = '' self.color = colors.GREEN self.weatherFile = 'weather.txt' def showWeather(self): temperature = self.getTemperature() - if self.isTemperatureMissing(temperature): + if temperature != self.currentTemperature: self.characterDisplay.clearRow(self.topRow) - else: - self.showTemperature(list(temperature)) + + if self.isTemperatureValid(temperature): + self.showTemperature(list(temperature)) + + self.currentTemperature = temperature def getTemperature(self): try: @@ -27,27 +30,30 @@ class WeatherDisplay: print(e, flush=True) return 'error' - def isTemperatureMissing(self, temperature): - return temperature == 'error' or temperature == 'init' or temperature == 'halted' + def isTemperatureValid(self, temperature): + return not (temperature == 'error' or temperature == 'init' or temperature == 'halted') def showTemperature(self, temperature): - if temperature != self.currentTemperature: - self.characterDisplay.clearRow(self.topRow) + start = self.getStartColumn(temperature) - start = self.oneDigitStartColumn if ( - self.isOneDigit(temperature) - ) else self.twoDigitStartColumn + for c in temperature: + if c == '-': + self.characterDisplay.displayNegative( + x=start-3, + y=self.topRow, + color=self.color + ) + else: + self.characterDisplay.displayDigit( + x=start, + y=self.topRow, + digit=c, + color=self.color + ) + start = start + 4 - for c in temperature: - if c == '-': - self.characterDisplay.displayNegative( - start-3, self.topRow, self.color) - else: - self.characterDisplay.displayDigit( - start, self.topRow, c, self.color) - start = start + 4 - - self.currentTemperature = temperature + def getStartColumn(self, temperature): + return self.oneDigitStartColumn if (self.isOneDigit(temperature)) else self.twoDigitStartColumn def isOneDigit(self, temperature): return (len(temperature) == 2 and temperature[0] == '-') or len(temperature) == 1