From 5168f5d3de5291da1e8e617ab1e91574cec8d7af Mon Sep 17 00:00:00 2001 From: Mike Cifelli <1836280-mike-cifelli@users.noreply.gitlab.com> Date: Sat, 24 Dec 2022 10:59:04 -0500 Subject: [PATCH] Show temperature --- character_display.py | 39 +++++++++++++++++++++++++++++++++------ chutney.py | 12 ++++++++++++ chutney.sample.cfg | 12 ++++++------ colors.py | 4 ++++ time_display.py | 6 ++++-- weather_display.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 103 insertions(+), 14 deletions(-) create mode 100644 colors.py create mode 100644 weather_display.py diff --git a/character_display.py b/character_display.py index 1ca8217..1260e48 100644 --- a/character_display.py +++ b/character_display.py @@ -11,13 +11,18 @@ # 2) * * Both Sides # 3) * Right Side # 4) * Left Side + +import colors + +BLACK = [0, 0, 0] + + class CharacterDisplay: def __init__(self, unicorn, minX=0, maxX=15): self.unicorn = unicorn self.minX = minX self.maxX = maxX - self.black = [0, 0, 0] self.digits = { 0: self.displayZero, @@ -29,7 +34,17 @@ class CharacterDisplay: 6: self.displaySix, 7: self.displaySeven, 8: self.displayEight, - 9: self.displayNine + 9: self.displayNine, + '0': self.displayZero, + '1': self.displayOne, + '2': self.displayTwo, + '3': self.displayThree, + '4': self.displayFour, + '5': self.displayFive, + '6': self.displaySix, + '7': self.displaySeven, + '8': self.displayEight, + '9': self.displayNine } def displayDigit(self, x, y, digit, color): @@ -40,18 +55,30 @@ class CharacterDisplay: def clearDigit(self, x, y): for y1 in range(y, y-5, -1): for x1 in range(x, x+3): - self.setPixel(x1, y1, self.black) + self.setPixel(x1, y1, colors.BLACK) self.unicorn.show() def displayTimeDots(self, x, y, color): - self.setPixel(x, y, self.black) + self.setPixel(x, y, colors.BLACK) self.setPixel(x, y-1, color) - self.setPixel(x, y-2, self.black) + self.setPixel(x, y-2, colors.BLACK) self.setPixel(x, y-3, color) - self.setPixel(x, y-4, self.black) + self.setPixel(x, y-4, colors.BLACK) self.unicorn.show() + def displayNegative(self, x, y, color): + self.setPixel(x, y, colors.BLACK) + self.setPixel(x, y-1, colors.BLACK) + self.setPixel(x+1, y, colors.BLACK) + self.setPixel(x+1, y-1, colors.BLACK) + self.setPixel(x, y-2, color) + self.setPixel(x+1, y-2, color) + self.setPixel(x, y-3, colors.BLACK) + self.setPixel(x, y-4, colors.BLACK) + self.setPixel(x+1, y-3, colors.BLACK) + self.setPixel(x+1, y-4, colors.BLACK) + def displayZero(self, x, y, color): self.fullLine(x, y, color) self.bothSides(x, y-1, color) diff --git a/chutney.py b/chutney.py index 67693c1..4ccf59d 100644 --- a/chutney.py +++ b/chutney.py @@ -4,6 +4,7 @@ from character_display import CharacterDisplay from signal import signal, SIGTERM from time import sleep from time_display import TimeDisplay +from weather_display import WeatherDisplay try: import unicornhathd as unicorn @@ -27,9 +28,20 @@ def main(): characterDisplay = CharacterDisplay(unicorn) timeDisplay = TimeDisplay(characterDisplay, topRow=15) + weatherDisplay = WeatherDisplay(characterDisplay, topRow=9, configFile='chutney.cfg') + + x = 0 while True: timeDisplay.showTime() + + if (x == 0): + weatherDisplay.showWeather() + print("weather updated") + + x = x + 1 + x = x % 120 + sleep(1) diff --git a/chutney.sample.cfg b/chutney.sample.cfg index 8566b03..c25e4f3 100644 --- a/chutney.sample.cfg +++ b/chutney.sample.cfg @@ -1,6 +1,6 @@ -[DEFAULT] -weather_user=user -weather_pass=password -weather_url=url -weather_lat=lat -weather_lon=lon +[weather] +host=host +user=user +pass=password +lat=lat +lon=lon diff --git a/colors.py b/colors.py new file mode 100644 index 0000000..6bdb1bb --- /dev/null +++ b/colors.py @@ -0,0 +1,4 @@ +BLACK = [0, 0, 0 ] +RED = [255, 0, 0 ] +PINK = [255, 0, 64 ] +BLUE = [0, 64, 128] diff --git a/time_display.py b/time_display.py index 4938cf9..ad83690 100644 --- a/time_display.py +++ b/time_display.py @@ -1,3 +1,5 @@ +import colors + from datetime import datetime @@ -8,8 +10,8 @@ class TimeDisplay: self.topRow = topRow self.currentHour = [-1, -1] self.currentMinute = [-1, -1] - self.currentColor = [0, 0, 0] - self.color = [255, 0, 64] + self.currentColor = colors.BLACK + self.color = colors.PINK self.hourStartColumn = -1 self.timeDotsColumn = 7 self.minuteStartColumn = 9 diff --git a/weather_display.py b/weather_display.py new file mode 100644 index 0000000..a0ba3c9 --- /dev/null +++ b/weather_display.py @@ -0,0 +1,44 @@ +import colors +import requests + +from configparser import ConfigParser + +class WeatherDisplay: + def __init__(self, characterDisplay, topRow, configFile): + self.characterDisplay = characterDisplay + self.topRow = topRow + self.currentTemperature = [9, 9] + self.currentColor = colors.BLACK + self.color = colors.BLUE + + config = ConfigParser() + config.read(configFile) + + self.host = config['weather'].get('host') + self.user = config['weather'].get('user') + self.password = config['weather'].get('pass') + self.lat = config['weather'].get('lat') + self.lon = config['weather'].get('lon') + + print(self.host) + + def showWeather(self): + self.showTemperature(self.getTemperatureCharacters(self.getTemperature())) + + def showTemperature(self, temperature): + start = 6 + + for c in temperature: + if c == '-': + self.characterDisplay.displayNegative(start, self.topRow, self.color) + start = start + 3 + else: + self.characterDisplay.displayDigit(start, self.topRow, c, self.color) + start = start + 4 + + + def getTemperature(self): + return list(filter(lambda t: t["timestep"] == 'current', requests.get(f'https://{self.host}/api/weather?lat={self.lat}&lon={self.lon}&units=metric', auth=(self.user, self.password)).json()["tomorrow"]["data"]['timelines']))[0]["intervals"][0]["values"]["temperature"] + + def getTemperatureCharacters(self, temperature): + return list(str(round(temperature)))