54 lines
1.8 KiB
Python
54 lines
1.8 KiB
Python
import colors
|
|
|
|
|
|
class WeatherDisplay:
|
|
def __init__(self, characterDisplay, topRow):
|
|
self.characterDisplay = characterDisplay
|
|
self.topRow = topRow
|
|
self.twoDigitStartColumn = 9
|
|
self.oneDigitStartColumn = 13
|
|
self.currentTemperature = [9, 9]
|
|
self.color = colors.GREEN
|
|
self.weatherFile = 'weather.txt'
|
|
|
|
def showWeather(self):
|
|
temperature = self.getTemperature()
|
|
|
|
if self.isTemperatureMissing(temperature):
|
|
self.characterDisplay.clearRow(self.topRow)
|
|
else:
|
|
self.showTemperature(list(temperature))
|
|
|
|
def getTemperature(self):
|
|
try:
|
|
with open(self.weatherFile) as file:
|
|
return file.readline().strip()
|
|
except Exception as e:
|
|
print(e, flush=True)
|
|
return 'error'
|
|
|
|
def isTemperatureMissing(self, temperature):
|
|
return temperature == 'error' or temperature == 'init' or temperature == 'halted'
|
|
|
|
def showTemperature(self, temperature):
|
|
if temperature != self.currentTemperature:
|
|
self.characterDisplay.clearRow(self.topRow)
|
|
|
|
start = self.oneDigitStartColumn if (
|
|
self.isOneDigit(temperature)
|
|
) else self.twoDigitStartColumn
|
|
|
|
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 isOneDigit(self, temperature):
|
|
return (len(temperature) == 2 and temperature[0] == '-') or len(temperature) == 1
|