chutney/weather_display.py

60 lines
1.9 KiB
Python
Raw Normal View History

2022-12-24 10:59:04 -05:00
import colors
2022-12-24 12:10:46 -05:00
2022-12-24 10:59:04 -05:00
class WeatherDisplay:
def __init__(self, characterDisplay, topRow):
2022-12-24 10:59:04 -05:00
self.characterDisplay = characterDisplay
self.topRow = topRow
2022-12-25 14:23:31 -05:00
self.twoDigitStartColumn = 9
self.oneDigitStartColumn = 13
2022-12-26 09:22:28 -05:00
self.digitWidth = 4
2022-12-25 15:54:50 -05:00
self.currentTemperature = ''
2022-12-26 09:22:28 -05:00
self.color = colors.BLUE
self.weatherFile = 'weather.txt'
2022-12-24 10:59:04 -05:00
def showWeather(self):
2022-12-25 12:33:06 -05:00
temperature = self.getTemperature()
2022-12-25 15:54:50 -05:00
if temperature != self.currentTemperature:
2022-12-26 09:22:28 -05:00
self.currentTemperature = temperature
2022-12-24 19:10:15 -05:00
self.characterDisplay.clearRow(self.topRow)
2022-12-25 15:54:50 -05:00
if self.isTemperatureValid(temperature):
self.showTemperature(list(temperature))
2022-12-25 12:33:06 -05:00
def getTemperature(self):
try:
with open(self.weatherFile) as file:
return file.readline().strip()
except Exception as e:
print(e, flush=True)
return 'error'
2022-12-25 15:54:50 -05:00
def isTemperatureValid(self, temperature):
return not (temperature == 'error' or temperature == 'init' or temperature == 'halted')
2022-12-24 10:59:04 -05:00
def showTemperature(self, temperature):
2022-12-25 15:54:50 -05:00
start = self.getStartColumn(temperature)
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
)
2022-12-26 09:22:28 -05:00
start = start + self.digitWidth
2022-12-25 15:54:50 -05:00
def getStartColumn(self, temperature):
return self.oneDigitStartColumn if (self.isOneDigit(temperature)) else self.twoDigitStartColumn
2022-12-25 14:23:31 -05:00
def isOneDigit(self, temperature):
return (len(temperature) == 2 and temperature[0] == '-') or len(temperature) == 1