first
This commit is contained in:
commit
9bd764579a
5 changed files with 123 additions and 0 deletions
0
lib/__init__.py
Normal file
0
lib/__init__.py
Normal file
24
lib/display.py
Normal file
24
lib/display.py
Normal file
|
|
@ -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)
|
||||
68
main.py
Executable file
68
main.py
Executable file
|
|
@ -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/<animation_name>")
|
||||
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)
|
||||
0
scenes/__init__.py
Normal file
0
scenes/__init__.py
Normal file
31
scenes/helloworld.py
Normal file
31
scenes/helloworld.py
Normal file
|
|
@ -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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue