Skip to content
Snippets Groups Projects
Verified Commit 48dffdf3 authored by TheJoeCoder's avatar TheJoeCoder
Browse files

Reformat file and add fps counter + limit

parent e2625c9a
Branches
No related tags found
No related merge requests found
...@@ -12,7 +12,9 @@ default_hw_config = { ...@@ -12,7 +12,9 @@ default_hw_config = {
"leds_length": 15, "leds_length": 15,
"flask_debug": False, "flask_debug": False,
"flask_host": "0.0.0.0", "flask_host": "0.0.0.0",
"flask_port": 5000 "flask_port": 5000,
"show_fps": False,
"fps_max": 60
} }
hw_config = default_hw_config.copy() hw_config = default_hw_config.copy()
......
import json
import os
import threading import threading
import time import time
import queue import queue
...@@ -8,12 +10,19 @@ from flask import request, Flask, Response, Request ...@@ -8,12 +10,19 @@ from flask import request, Flask, Response, Request
import ledpatterns import ledpatterns
from absled import ILedString from absled import ILedString
from config import *
import screenwidgets import screenwidgets
from config import PROP_AVAILABLE_WIDGETS, PROP_AVAILABLE_PATTERNS, PROP_WINDOW_SCALE, PROP_WINDOW_FULLSCREEN, \
PROP_SCREEN_WIDTH, PROP_SCREEN_HEIGHT, PROP_HIDE_MOUSE, PROP_LEDS_DRIVER, PROP_LEDS_LENGTH, PROP_LEDS_LOCATION, \
PROP_FLASK_DEBUG, PROP_FLASK_HOST, PROP_FLASK_PORT, hw_config
app = Flask(__name__) app = Flask(__name__)
module_config = {}
# Load defaults # Load defaults
def load_module_config():
global module_config
module_defaults = json.load(open("module_defaults.json")) module_defaults = json.load(open("module_defaults.json"))
module_config = {} module_config = {}
for module_name, module_conf in module_defaults.items(): for module_name, module_conf in module_defaults.items():
...@@ -35,6 +44,10 @@ if os.path.exists("module_config.json"): ...@@ -35,6 +44,10 @@ if os.path.exists("module_config.json"):
with open("module_config.json", "w") as f: with open("module_config.json", "w") as f:
json.dump(module_config, f, indent=4) json.dump(module_config, f, indent=4)
def save_module_config():
with open("module_config.json", "w") as f:
json.dump(module_config, f, indent=4)
mainthread_queue = queue.Queue() mainthread_queue = queue.Queue()
display_queue = queue.Queue() display_queue = queue.Queue()
led_queue = queue.Queue() led_queue = queue.Queue()
...@@ -59,6 +72,11 @@ def verify_json_request(req: Request, required_fields=None): ...@@ -59,6 +72,11 @@ def verify_json_request(req: Request, required_fields=None):
return True, None return True, None
##############
# Web Routes #
##############
@app.route("/") @app.route("/")
def wr_index(): def wr_index():
return f""" return f"""
...@@ -156,6 +174,11 @@ def wr_set_pattern(): ...@@ -156,6 +174,11 @@ def wr_set_pattern():
def wr_get_config(): def wr_get_config():
return json_response(module_config) return json_response(module_config)
#####################
# Loading functions #
#####################
def load_widget(widget_name: str, config_override: dict=None): def load_widget(widget_name: str, config_override: dict=None):
# Get the widget properties from the config file # Get the widget properties from the config file
widget_props = module_config.get(widget_name, {}).copy() widget_props = module_config.get(widget_name, {}).copy()
...@@ -185,8 +208,8 @@ def load_pattern(pattern_name: str, leds: ILedString, config_override: dict=None ...@@ -185,8 +208,8 @@ def load_pattern(pattern_name: str, leds: ILedString, config_override: dict=None
def run_screen(): def run_screen():
# Initialise Pygame and create a window # Initialise Pygame and create a window
pygame.init() pygame.init()
pg_options = 0x0 pg_options = 0x0
print(PROP_WINDOW_FULLSCREEN)
if PROP_WINDOW_FULLSCREEN: if PROP_WINDOW_FULLSCREEN:
pg_options |= pygame.FULLSCREEN pg_options |= pygame.FULLSCREEN
screen = pygame.display.set_mode( screen = pygame.display.set_mode(
...@@ -196,6 +219,9 @@ def run_screen(): ...@@ -196,6 +219,9 @@ def run_screen():
pygame.display.set_caption("Clock") pygame.display.set_caption("Clock")
pygame.mouse.set_visible(not PROP_HIDE_MOUSE) pygame.mouse.set_visible(not PROP_HIDE_MOUSE)
# Create a clock for FPS counter and timing
clock = pygame.time.Clock()
# Init widgets # Init widgets
widget_to_load = PROP_AVAILABLE_WIDGETS[0] # TODO change to load multiple widgets widget_to_load = PROP_AVAILABLE_WIDGETS[0] # TODO change to load multiple widgets
...@@ -231,6 +257,19 @@ def run_screen(): ...@@ -231,6 +257,19 @@ def run_screen():
# Draw stuff here # Draw stuff here
current_widget.draw(screen) current_widget.draw(screen)
# Display FPS counter
if hw_config["show_fps"]:
fps = clock.get_fps()
font = pygame.font.Font(None, 36)
text = font.render(f"FPS: {fps:.2f}", True, (255, 255, 255))
screen.blit(text, (10, 10))
# Tick the clock
if hw_config["fps_max"] > 0:
clock.tick(hw_config["fps_max"])
else:
clock.tick()
# Update the screen # Update the screen
pygame.display.flip() pygame.display.flip()
...@@ -282,6 +321,9 @@ def run_leds(): ...@@ -282,6 +321,9 @@ def run_leds():
if __name__ == "__main__": if __name__ == "__main__":
# Load configuration
load_module_config()
# Fork off pygame # Fork off pygame
pygame_thread = threading.Thread(target=run_screen, daemon=True) pygame_thread = threading.Thread(target=run_screen, daemon=True)
pygame_thread.start() pygame_thread.start()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment