many works

This commit is contained in:
Christoph Haas 2026-01-11 16:20:31 +01:00
parent 9bd764579a
commit c937450f92
7 changed files with 151 additions and 36 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
**pyc

19
README Normal file
View file

@ -0,0 +1,19 @@
verschiedene threads zur auswahl, die vom flask gestartet/gestoppt werden können.
- statisches bild anzeigen und sich beenden
-
ein task, der die ganze zeit im hintergrund läuft.
- statisches bild anzeigen
- eine Routine
ein globales bild. (locking?)
die einzelnen
https://github.com/hzeller/rpi-rgb-led-matrix
./main.py
curl http://localhost:6000/start/helloworld
feh --reload 0.15 image.png
https://pypi.org/project/finnhub-python/

View file

@ -1,24 +1,51 @@
from rgbmatrix import RGBMatrix, RGBMatrixOptions
from PIL import ImageFont, Image
# Das Bild nur speichern. Nicht die LED-Matrix ansteuern.
DEV = True
DEV = False
#DEV = True
matrix = None
MATRIX_HEIGHT = 64
MATRIX_WIDTH = 64
NUMBER_OF_PANELS = 4
class MyMatrix:
def __init__(self):
#self.matrix_width = options.cols * options.chain_length
#self.matrix_height = options.rows
try:
self.font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-ExtraLight.ttf", 18)
except IOError:
self.font = ImageFont.load_default()
if not DEV:
options = RGBMatrixOptions()
options.rows = 64
options.cols = 64
options.chain_length = 2
options.rows = MATRIX_HEIGHT
options.cols = MATRIX_WIDTH
options.chain_length = NUMBER_OF_PANELS
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.gpio_slowdown = 5 # try 2, 4, or even 5
options.disable_hardware_pulsing = True
options.drop_privileges = False
matrix = RGBMatrix(options=options)
matrix_width = 64 * options.chain_length
matrix_height = 64
self.matrix = RGBMatrix(options=options)
def do(image):
def show(self, image):
"""Save the image to disk in DEV mode. Or send it to the LED matrix panel."""
if DEV:
image.save('image.png')
elif matrix:
self.matrix.SetImage(image)
else:
matrix.SetImage(image)
print("Not in dev mode. And matrix not initialized.")
def buffer(self):
"""Get an image buffer with the right dimensions"""
return Image.new("RGB", (MATRIX_WIDTH * NUMBER_OF_PANELS, MATRIX_HEIGHT))
matrix = MyMatrix()

24
lib/helpers.py Normal file
View file

@ -0,0 +1,24 @@
def triangle(image_draw, offset_x, offset_y, direction, width, height):
"""Show triangle. Direction is -1 (down) or 1 (up)."""
points = []
color = None
if direction==1:
points = [
(width/2, 0), # n
(0, height), # sw
(width, height) # se
]
color = (0, 255, 0) # green
else:
points = [
(width/2, height), # s
(0, 0), # nw
(width,0) # ne
]
color = (255, 0, 0) # red
# Draw a filled green triangle
points = [(x+offset_x, y+offset_y) for (x,y) in points]
image_draw.polygon(points, fill=color)

View file

@ -6,6 +6,8 @@ from time import sleep
import importlib
import os
from PIL import Image
#from lib.display import show
import lib.display
SCENES_DIR = 'scenes'
scenes = {}
@ -48,7 +50,8 @@ def start_animation(scene_name):
# 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 = Thread(target=scenes[scene_name].start, args=(image_buffer, stop_event), daemon=True)
animation_thread = Thread(target=scenes[scene_name].start, args=(stop_event,), daemon=True)
animation_thread.start()
@app.route("/start/<animation_name>")
@ -66,3 +69,4 @@ def stop():
if __name__ == "__main__":
app.run(debug=True, port=6000)

47
scenes/bitcoin.py Normal file
View file

@ -0,0 +1,47 @@
#!/usr/bin/env python3
import time
from PIL import Image, ImageDraw
from lib import display
# Replace with your free Finnhub API key
API_KEY = "d3t8099r01qigeg1s7cgd3t8099r01qigeg1s7d0"
import finnhub
finnhub_client = finnhub.Client(api_key="d3t8099r01qigeg1s7cgd3t8099r01qigeg1s7d0")
def get_bitcoin_price():
symbol = "BINANCE:BTCEUR"
data = finnhub_client.quote(symbol)
return data['c']
def start(image_buffer, stop_event, text="Hello World!"):
last_price = None
trend = None
while not stop_event.is_set():
image_buffer = Image.new("RGB", (128, 64))
draw = ImageDraw.Draw(image_buffer)
price = get_bitcoin_price()
# Trend berechnen
if last_price:
if price > last_price:
trend = 1
elif price < last_price:
trend = -1
display.triangle(draw, 0, 0, trend, width=16, height=32)
text = f"BTC: {price:.0f}"
draw.text((20, 0), text, fill=(255, 255, 255), font=display.font)
display.do(image_buffer)
last_price = price
# Alle 10 Sekunden einen neuen Kurs holen. Jede Sekunde auf "stop" horchen.
for i in range(1,10):
if stop_event.is_set():
return
time.sleep(1)

View file

@ -3,29 +3,21 @@
import io
from time import sleep
from PIL import Image, ImageDraw, ImageFont
from lib import display
#from lib import display
import lib.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!"):
def start(stop_event, text="Hello World!"):
t = 0
while not stop_event.is_set():
image_buffer = Image.new("RGB", (128, 64))
#image_buffer = Image.new("RGB", (lib.display.matrix_width, lib.display.matrix_height))
image_buffer = lib.display.matrix.buffer()
draw = ImageDraw.Draw(image_buffer)
draw.text((0, 0), f"Hello World {t}", fill=(255, 255, 255), font=font)
draw.text((0, 0), f"Hello World {t}", fill=(255, 255, 255), font=lib.display.matrix.font)
t += 1
print(t)
#image_buffer.save('image.png')
display.do(image_buffer)
lib.display.matrix.show(image_buffer)
#sleep(0.2)
sleep(0.2)