153 lines
5.2 KiB
Python
153 lines
5.2 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Matrix64 LED Display - Main Entry Point
|
|
|
|
Displays Home Assistant and Netdata data on a 64x64 LED matrix.
|
|
"""
|
|
from samplebase import SampleBase
|
|
from rgbmatrix import graphics
|
|
import time
|
|
import datetime
|
|
|
|
from config import LED_ROWS, LED_COLS
|
|
from weather_icons import draw_weather_icon
|
|
from home_assistant import (
|
|
get_weather, get_weather_description,
|
|
get_interior_weather, get_brightness
|
|
)
|
|
from netdata import get_hdd_temps
|
|
|
|
|
|
def get_temperature_color(temp):
|
|
"""Determine color based on temperature value."""
|
|
try:
|
|
temp_value = float(str(temp).replace('°C', '').strip())
|
|
if temp_value < 0:
|
|
return graphics.Color(0, 100, 255)
|
|
elif temp_value < 10:
|
|
return graphics.Color(100, 180, 255)
|
|
elif temp_value < 20:
|
|
return graphics.Color(0, 255, 100)
|
|
elif temp_value < 30:
|
|
return graphics.Color(255, 200, 0)
|
|
else:
|
|
return graphics.Color(255, 50, 50)
|
|
except ValueError:
|
|
return graphics.Color(255, 255, 255)
|
|
|
|
|
|
class Matrix64Display(SampleBase):
|
|
"""Main display class for the LED matrix."""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super(Matrix64Display, self).__init__(*args, **kwargs)
|
|
self.parser.add_argument(
|
|
"-t", "--text",
|
|
help="The text to scroll on the RGB LED panel",
|
|
default="Hello world!"
|
|
)
|
|
self.temperature = None
|
|
self.humidity = None
|
|
self.interior_temperature = None
|
|
self.interior_humidity = None
|
|
self.last_update = time.time()
|
|
self.weather_desc = "unknown"
|
|
self.hdd_temps = None
|
|
|
|
def update_data(self):
|
|
"""Fetch all data from APIs."""
|
|
try:
|
|
# Weather data
|
|
weather = get_weather()
|
|
if weather.get("temperature") is not None:
|
|
self.temperature = f'{weather.get("temperature")}°C'
|
|
self.humidity = weather.get("humidity")
|
|
self.weather_desc = get_weather_description()
|
|
|
|
# Interior data
|
|
interior = get_interior_weather()
|
|
if interior.get("temperature") is not None:
|
|
self.interior_temperature = f'{interior.get("temperature")}°C'
|
|
self.interior_humidity = interior.get("humidity")
|
|
|
|
except Exception as e:
|
|
print(f"Error updating data: {e}")
|
|
|
|
def update_brightness(self):
|
|
"""Update LED matrix brightness."""
|
|
try:
|
|
brightness = get_brightness()
|
|
if brightness:
|
|
self.matrix.brightness = int(float(brightness)) / 10
|
|
except Exception as e:
|
|
self.matrix.brightness = 5
|
|
print(f"Error updating brightness: {e}")
|
|
|
|
def update_hdd_temps(self):
|
|
"""Update HDD temperatures."""
|
|
self.hdd_temps = get_hdd_temps()
|
|
|
|
def run(self):
|
|
"""Main display loop."""
|
|
canvas = self.matrix.CreateFrameCanvas()
|
|
|
|
# Load fonts
|
|
font = graphics.Font()
|
|
font.LoadFont("../../../fonts/7x13.bdf")
|
|
temp_font = graphics.Font()
|
|
temp_font.LoadFont("../../../fonts/5x8.bdf")
|
|
|
|
text_color = graphics.Color(20, 75, 200)
|
|
|
|
# Initial data fetch
|
|
self.update_brightness()
|
|
self.update_data()
|
|
self.update_hdd_temps()
|
|
|
|
while True:
|
|
now = datetime.datetime.now()
|
|
time_str = f"{now.hour:02d}:{now.minute:02d}:{now.second:02d}"
|
|
current_time = time.time()
|
|
|
|
# Update every 60 seconds
|
|
if current_time - self.last_update >= 60:
|
|
self.update_data()
|
|
self.update_brightness()
|
|
self.update_hdd_temps()
|
|
self.last_update = current_time
|
|
|
|
canvas.Clear()
|
|
|
|
# Time display
|
|
graphics.DrawText(canvas, font, 4, 42, text_color, time_str)
|
|
|
|
# Weather icon
|
|
draw_weather_icon(canvas, 0, 0, self.weather_desc)
|
|
|
|
# Outdoor temperature (right-aligned)
|
|
if self.temperature:
|
|
color = get_temperature_color(self.temperature)
|
|
length = graphics.DrawText(canvas, temp_font, 0, 0, color, self.temperature)
|
|
graphics.DrawText(canvas, temp_font, 64 - length, 8, color, self.temperature)
|
|
|
|
# Interior temperature (right-aligned)
|
|
if self.interior_temperature:
|
|
color = get_temperature_color(self.interior_temperature)
|
|
length = graphics.DrawText(canvas, temp_font, 0, 0, color, self.interior_temperature)
|
|
graphics.DrawText(canvas, temp_font, 64 - length, 16, color, self.interior_temperature)
|
|
|
|
# HDD temperatures
|
|
if self.hdd_temps and len(self.hdd_temps) > 4:
|
|
row1 = " ".join(str(t[0])[:2] for t in self.hdd_temps[1:4])
|
|
row2 = " ".join(str(t[0])[:2] for t in self.hdd_temps[4:])
|
|
graphics.DrawText(canvas, temp_font, 12, 52, text_color, row1)
|
|
graphics.DrawText(canvas, temp_font, 12, 60, text_color, row2)
|
|
|
|
time.sleep(0.5)
|
|
canvas = self.matrix.SwapOnVSync(canvas)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
display = Matrix64Display()
|
|
if not display.process():
|
|
display.print_help() |