Initial Clock Format

This commit is contained in:
Chuck
2025-04-07 16:52:13 -05:00
parent 1262e09bf5
commit 30c3080f15
195 changed files with 1155334 additions and 0 deletions

38
src/clock.py Normal file
View File

@@ -0,0 +1,38 @@
import time
from datetime import datetime
import pytz
from config_manager import ConfigManager
from display_manager import DisplayManager
class Clock:
def __init__(self):
self.config_manager = ConfigManager()
self.display_manager = DisplayManager(self.config_manager.get_display_config())
self.timezone = pytz.timezone(self.config_manager.get_timezone())
self.clock_config = self.config_manager.get_clock_config()
def get_current_time(self) -> str:
"""Get the current time in the configured timezone."""
current_time = datetime.now(self.timezone)
return current_time.strftime(self.clock_config.get('format', '%H:%M:%S'))
def run(self):
"""Run the clock display."""
try:
while True:
current_time = self.get_current_time()
# Center the text on the display
text_width = self.display_manager.font.getsize(current_time)[0]
x = (self.display_manager.matrix.width - text_width) // 2
y = (self.display_manager.matrix.height - 24) // 2
self.display_manager.draw_text(current_time, x, y)
time.sleep(self.clock_config.get('update_interval', 1))
except KeyboardInterrupt:
print("Clock stopped by user")
finally:
self.display_manager.cleanup()
if __name__ == "__main__":
clock = Clock()
clock.run()

33
src/config_manager.py Normal file
View File

@@ -0,0 +1,33 @@
import json
import os
from typing import Dict, Any
class ConfigManager:
def __init__(self, config_path: str = "../config/config.json"):
self.config_path = config_path
self.config: Dict[str, Any] = {}
self.load_config()
def load_config(self) -> None:
"""Load configuration from JSON file."""
try:
with open(self.config_path, 'r') as f:
self.config = json.load(f)
except FileNotFoundError:
print(f"Configuration file not found at {self.config_path}")
raise
except json.JSONDecodeError:
print("Error parsing configuration file")
raise
def get_timezone(self) -> str:
"""Get the configured timezone."""
return self.config.get('timezone', 'UTC')
def get_display_config(self) -> Dict[str, Any]:
"""Get display configuration."""
return self.config.get('display', {})
def get_clock_config(self) -> Dict[str, Any]:
"""Get clock configuration."""
return self.config.get('clock', {})

39
src/display_manager.py Normal file
View File

@@ -0,0 +1,39 @@
from rgbmatrix import RGBMatrix, RGBMatrixOptions
from PIL import Image, ImageDraw, ImageFont
import time
from typing import Dict, Any
class DisplayManager:
def __init__(self, config: Dict[str, Any]):
self.config = config
self.matrix = self._setup_matrix()
self.font = ImageFont.truetype("DejaVuSans.ttf", 24)
self.image = Image.new('RGB', (self.matrix.width, self.matrix.height))
self.draw = ImageDraw.Draw(self.image)
def _setup_matrix(self) -> RGBMatrix:
"""Setup the RGB matrix with the provided configuration."""
options = RGBMatrixOptions()
options.rows = self.config.get('rows', 32)
options.cols = self.config.get('cols', 64)
options.chain_length = self.config.get('chain_length', 2)
options.hardware_mapping = 'adafruit-hat'
options.gpio_slowdown = 4
options.brightness = self.config.get('brightness', 50)
return RGBMatrix(options=options)
def clear(self):
"""Clear the display."""
self.draw.rectangle((0, 0, self.matrix.width, self.matrix.height), fill=(0, 0, 0))
self.matrix.SetImage(self.image)
def draw_text(self, text: str, x: int, y: int, color: tuple = (255, 255, 255)):
"""Draw text on the display."""
self.clear()
self.draw.text((x, y), text, font=self.font, fill=color)
self.matrix.SetImage(self.image)
def cleanup(self):
"""Clean up resources."""
self.matrix.Clear()