From 9bd764579ad7104a8379a8120328379b43809fe6 Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Tue, 18 Nov 2025 22:40:58 +0100 Subject: [PATCH] first --- lib/__init__.py | 0 lib/display.py | 24 ++++++++++++++++ main.py | 68 ++++++++++++++++++++++++++++++++++++++++++++ scenes/__init__.py | 0 scenes/helloworld.py | 31 ++++++++++++++++++++ 5 files changed, 123 insertions(+) create mode 100644 lib/__init__.py create mode 100644 lib/display.py create mode 100755 main.py create mode 100644 scenes/__init__.py create mode 100644 scenes/helloworld.py diff --git a/lib/__init__.py b/lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lib/display.py b/lib/display.py new file mode 100644 index 0000000..7f71362 --- /dev/null +++ b/lib/display.py @@ -0,0 +1,24 @@ +from rgbmatrix import RGBMatrix, RGBMatrixOptions + +# Das Bild nur speichern. Nicht die LED-Matrix ansteuern. +DEV = True + +if not DEV: + options = RGBMatrixOptions() + options.rows = 64 + options.cols = 64 + options.chain_length = 2 + options.parallel = 1 + options.hardware_mapping = 'regular' # change if using Adafruit HAT, e.g. 'adafruit-hat' + options.gpio_slowdown = 4 # try 2, 4, or even 5 + options.disable_hardware_pulsing = True + + matrix = RGBMatrix(options=options) + matrix_width = 64 * options.chain_length + matrix_height = 64 + +def do(image): + if DEV: + image.save('image.png') + else: + matrix.SetImage(image) diff --git a/main.py b/main.py new file mode 100755 index 0000000..dab96bc --- /dev/null +++ b/main.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 + +from flask import Flask, jsonify +from threading import Thread, Event +from time import sleep +import importlib +import os +from PIL import Image + +SCENES_DIR = 'scenes' +scenes = {} + +for file in os.listdir(SCENES_DIR): + if file.endswith(".py") and file != "__init__.py": + module_name = file[:-3] + module = importlib.import_module(f"{SCENES_DIR}.{module_name}") + scenes[module_name] = module + +app = Flask(__name__) + +# Thread and control event +animation_thread = None +stop_event = Event() +image_buffer = Image.new("RGB", (128, 64)) + +# Example background animation function +def run_animation(name): + print(f"Starting animation: {name}") + t = 0 + while not stop_event.is_set(): + # This is where you'd update your LED matrix or PIL image + print(f"[{name}] Frame {t}") + t += 1 + sleep(0.2) # 5 FPS + print(f"Animation {name} stopped") + +# Utility to start a new animation +def start_animation(scene_name): + global animation_thread, stop_event + if scene_name not in scenes: + print(f"Scene '{scene_name}' not found!") + return + + # Stop previous thread + if animation_thread and animation_thread.is_alive(): + stop_event.set() + animation_thread.join() + + # Reset stop event and start new thread + stop_event.clear() + animation_thread = Thread(target=scenes[scene_name].start, args=(image_buffer, stop_event), daemon=True) + animation_thread.start() + +@app.route("/start/") +def start(animation_name): + start_animation(animation_name) + return jsonify({"status": "started", "animation": animation_name}) + +@app.route("/stop") +def stop(): + global animation_thread, stop_event + if animation_thread and animation_thread.is_alive(): + stop_event.set() + animation_thread.join() + return jsonify({"status": "stopped"}) + +if __name__ == "__main__": + app.run(debug=True, port=6000) diff --git a/scenes/__init__.py b/scenes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/scenes/helloworld.py b/scenes/helloworld.py new file mode 100644 index 0000000..abc1c94 --- /dev/null +++ b/scenes/helloworld.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +import io +from time import sleep +from PIL import Image, ImageDraw, ImageFont +from lib import display + +# TODO: Derive from number of panels and panel size +matrix_height = 64 +matrix_width = 64 * 2 + +# Load a font (use a TTF font file of your choice) +try: + font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 12) +except IOError: + font = ImageFont.load_default() + +def start(image_buffer, stop_event, text="Hello World!"): + t = 0 + while not stop_event.is_set(): + image_buffer = Image.new("RGB", (128, 64)) + draw = ImageDraw.Draw(image_buffer) + draw.text((0, 0), f"Hello World {t}", fill=(255, 255, 255), font=font) + + t += 1 + print(t) + + #image_buffer.save('image.png') + display.do(image_buffer) + + sleep(0.2)