From f10caa3918831a227ea8838f3f89016dac7c8c0e Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Fri, 16 Jan 2026 23:52:42 +0100 Subject: [PATCH] allow args. README. --- README.md | 11 +++++++++++ lib/display.py | 6 ++++-- main.py | 11 +++++++---- scenes/bitcoin.py | 24 ++++++++++++++++-------- scenes/clock.py | 2 +- scenes/color.py | 31 +++++++++++++++++++++++++++++++ scenes/colorclock.py | 2 +- 7 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 scenes/color.py diff --git a/README.md b/README.md index abb08d2..86f85d2 100644 --- a/README.md +++ b/README.md @@ -137,3 +137,14 @@ Ist das Geflacker dann weg? - gpio-slowdown auf 4 - nach dem stop (ctrl-c) und start 10 sekunden warten - mit "gunicorn -w 1 -k gthread main:app" laufen lassen + +# Szenen + +curl "http://localhost:8000/start/color?red=255&green=255&blue=255" +curl http://localhost:8000/start/bitcoin +curl http://localhost:8000/start/clock +curl http://localhost:8000/start/colorclock +curl http://localhost:8000/start/helloworld +curl http://localhost:8000/stop +curl http://localhost:8000/reset + diff --git a/lib/display.py b/lib/display.py index 7dfa83d..93cb291 100644 --- a/lib/display.py +++ b/lib/display.py @@ -18,8 +18,10 @@ class MyMatrix: #self.matrix_height = options.rows try: - self.font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-ExtraLight.ttf", 18) - self.large_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-ExtraLight.ttf", 32) + #self.font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-ExtraLight.ttf", 18) + self.font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 18) + self.large_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 32) + #self.large_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-ExtraLight.ttf", 32) except IOError: self.font = ImageFont.load_default() self.large_font = ImageFont.load_default() diff --git a/main.py b/main.py index f9dd92c..b371682 100755 --- a/main.py +++ b/main.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -from flask import Flask, jsonify +from flask import Flask, jsonify, request from threading import Thread, Event from time import sleep import importlib @@ -38,7 +38,7 @@ def run_animation(name): print(f"Animation {name} stopped") # Utility to start a new animation -def start_animation(scene_name): +def start_animation(scene_name, args): global animation_thread, stop_event if scene_name not in scenes: print(f"Scene '{scene_name}' not found!") @@ -51,12 +51,12 @@ def start_animation(scene_name): # Reset stop event and start new thread stop_event.clear() - animation_thread = Thread(target=scenes[scene_name].start, args=(stop_event,), daemon=True) + animation_thread = Thread(target=scenes[scene_name].start, args=(stop_event, args), daemon=True) animation_thread.start() @app.route("/start/") def start(animation_name): - start_animation(animation_name) + start_animation(animation_name, args=request.args) return jsonify({"status": "started", "animation": animation_name}) @app.route("/reset") @@ -66,10 +66,13 @@ def reset(): @app.route("/stop") def stop(): + # Stop running thread global animation_thread, stop_event if animation_thread and animation_thread.is_alive(): stop_event.set() animation_thread.join() + # Clear the display + lib.display.matrix.matrix.Clear() return jsonify({"status": "stopped"}) if __name__ == "__main__": diff --git a/scenes/bitcoin.py b/scenes/bitcoin.py index 6ca873f..ffcf26e 100644 --- a/scenes/bitcoin.py +++ b/scenes/bitcoin.py @@ -2,7 +2,7 @@ import time from PIL import Image, ImageDraw -from lib import display +import lib.display # Replace with your free Finnhub API key API_KEY = "d3t8099r01qigeg1s7cgd3t8099r01qigeg1s7d0" @@ -12,14 +12,18 @@ finnhub_client = finnhub.Client(api_key="d3t8099r01qigeg1s7cgd3t8099r01qigeg1s7d def get_bitcoin_price(): symbol = "BINANCE:BTCEUR" data = finnhub_client.quote(symbol) + #print("getting finnhub") return data['c'] -def start(image_buffer, stop_event, text="Hello World!"): +def start(stop_event, args): last_price = None trend = None + image_buffer = lib.display.matrix.buffer() + draw = ImageDraw.Draw(image_buffer) + symbol = '⏶' while not stop_event.is_set(): - image_buffer = Image.new("RGB", (128, 64)) - draw = ImageDraw.Draw(image_buffer) + # All to black + draw.rectangle( ((0,0),(lib.display.matrix.width-1,lib.display.matrix.height-1)), fill=(0,0,0) ) price = get_bitcoin_price() @@ -27,16 +31,19 @@ def start(image_buffer, stop_event, text="Hello World!"): if last_price: if price > last_price: trend = 1 + symbol = "🡅" elif price < last_price: trend = -1 + symbol = "🡇" - display.triangle(draw, 0, 0, trend, width=16, height=32) + #display.triangle(draw, 0, 0, trend, width=16, height=32) + print(symbol) - text = f"BTC: {price:.0f}€" + text = f"{symbol} BTC: {price:.0f}€" - draw.text((20, 0), text, fill=(255, 255, 255), font=display.font) + draw.text((20, 0), text, fill=(255, 255, 255), font=lib.display.matrix.large_font) - display.do(image_buffer) + lib.display.matrix.show(image_buffer) last_price = price # Alle 10 Sekunden einen neuen Kurs holen. Jede Sekunde auf "stop" horchen. @@ -45,3 +52,4 @@ def start(image_buffer, stop_event, text="Hello World!"): return time.sleep(1) + diff --git a/scenes/clock.py b/scenes/clock.py index b188b80..8bde947 100644 --- a/scenes/clock.py +++ b/scenes/clock.py @@ -9,7 +9,7 @@ import datetime import locale locale.setlocale(locale.LC_TIME, "de_DE.UTF-8") -def start(stop_event, text="Hello World!"): +def start(stop_event, args): image_buffer = lib.display.matrix.buffer() draw = ImageDraw.Draw(image_buffer) while not stop_event.is_set(): diff --git a/scenes/color.py b/scenes/color.py new file mode 100644 index 0000000..6d28d7a --- /dev/null +++ b/scenes/color.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +import io +from time import sleep +from PIL import Image, ImageDraw, ImageFont +import lib.display + +import datetime +import locale +locale.setlocale(locale.LC_TIME, "de_DE.UTF-8") + +def start(stop_event, args): + image_buffer = lib.display.matrix.buffer() + draw = ImageDraw.Draw(image_buffer) + + #print(int(args.get('red', 0))) + + # All to color + draw.rectangle( + ( (0,0), (lib.display.matrix.width-1,lib.display.matrix.height-1) ), + fill=( + int(args.get('red', 0)), + int(args.get('green', 0)), + int(args.get('blue', 0)) + ) + ) + lib.display.matrix.show(image_buffer) + + while not stop_event.is_set(): + sleep(0.5) + diff --git a/scenes/colorclock.py b/scenes/colorclock.py index b380a5f..9c013ce 100644 --- a/scenes/colorclock.py +++ b/scenes/colorclock.py @@ -9,7 +9,7 @@ import datetime import locale locale.setlocale(locale.LC_TIME, "de_DE.UTF-8") -def start(stop_event, text="Hello World!"): +def start(stop_event, args): while not stop_event.is_set(): image_buffer = lib.display.matrix.buffer() draw = ImageDraw.Draw(image_buffer)