allow args. README.

This commit is contained in:
Christoph Haas 2026-01-16 23:52:42 +01:00
parent 529262ea4d
commit f10caa3918
7 changed files with 71 additions and 16 deletions

View file

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

View file

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

11
main.py
View file

@ -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/<animation_name>")
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__":

View file

@ -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
while not stop_event.is_set():
image_buffer = Image.new("RGB", (128, 64))
image_buffer = lib.display.matrix.buffer()
draw = ImageDraw.Draw(image_buffer)
symbol = ''
while not stop_event.is_set():
# 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)

View file

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

31
scenes/color.py Normal file
View file

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

View file

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