Show temperature

This commit is contained in:
Mike Cifelli 2022-12-24 10:59:04 -05:00
parent 297c69ab58
commit 5168f5d3de
6 changed files with 103 additions and 14 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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

4
colors.py Normal file
View File

@ -0,0 +1,4 @@
BLACK = [0, 0, 0 ]
RED = [255, 0, 0 ]
PINK = [255, 0, 64 ]
BLUE = [0, 64, 128]

View File

@ -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

44
weather_display.py Normal file
View File

@ -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)))