From fd1ea198fa187e02792837c9cba5ca63c4670021 Mon Sep 17 00:00:00 2001 From: Mike Cifelli <1836280-mike-cifelli@users.noreply.gitlab.com> Date: Sun, 8 Jan 2023 10:34:02 -0500 Subject: [PATCH] Center temperature and add hot/cold colors --- chutney/weather/weather_display.py | 67 ++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/chutney/weather/weather_display.py b/chutney/weather/weather_display.py index 8d90570..c65d953 100644 --- a/chutney/weather/weather_display.py +++ b/chutney/weather/weather_display.py @@ -20,7 +20,8 @@ class WeatherDisplay: self.symbolDisplay.clearRow(self.topRow) if self.isTemperatureValid(temperature): - self.showTemperature(list(temperature)) + self.updateColor(temperature) + self.showTemperature(temperature) def getTemperature(self): try: @@ -33,33 +34,57 @@ class WeatherDisplay: def isTemperatureValid(self, temperature): return not (temperature == 'error' or temperature == 'init' or temperature == 'halted') + def updateColor(self, temperature): + if int(temperature) > 0: + self.color = colors.ORANGE + else: + self.color = colors.BLUE + def showTemperature(self, temperature): - start = self.getStartColumn(temperature) + column = self.getStartColumn(temperature) - for c in temperature: + for c in list(temperature): if c == '-': - self.symbolDisplay.displayNegative( - x=start-3, - y=self.topRow, - color=self.color - ) + offset = 2 if self.startsWithOne(temperature) else 3 + self.showNegative(column - offset) else: - self.symbolDisplay.displayDigit( - x=start, - y=self.topRow, - digit=c, - color=self.color - ) - start = start + self.digitWidth + self.showDigit(column, c) + column = column + self.digitWidth - self.symbolDisplay.displayDegree( - x=start, + self.showDegree(column) + + def getStartColumn(self, temperature): + offset = 1 if self.startsWithOne(temperature) else 0 + startColumn = self.oneDigitStartColumn if self.isOneDigit(temperature) else self.twoDigitStartColumn + + return startColumn - offset + + def startsWithOne(self, temperature): + absoluteTemperature = abs(int(temperature)) + + return absoluteTemperature == 1 or absoluteTemperature//10 == 1 + + def isOneDigit(self, temperature): + return abs(int(temperature))//10 == 0 + + def showNegative(self, column): + self.symbolDisplay.displayNegative( + x=column, y=self.topRow, color=self.color ) - def getStartColumn(self, temperature): - return self.oneDigitStartColumn if (self.isOneDigit(temperature)) else self.twoDigitStartColumn + def showDigit(self, column, digit): + self.symbolDisplay.displayDigit( + x=column, + y=self.topRow, + digit=digit, + color=self.color + ) - def isOneDigit(self, temperature): - return (len(temperature) == 2 and temperature[0] == '-') or len(temperature) == 1 + def showDegree(self, column): + self.symbolDisplay.displayDegree( + x=column, + y=self.topRow, + color=self.color + )