From 6345b50da06b974d7305cbd07471c886dae2bd69 Mon Sep 17 00:00:00 2001 From: TheJoeCoder <joe@radialbog9.uk> Date: Thu, 10 Oct 2024 10:12:52 +0100 Subject: [PATCH] Start ScreenState mechanic --- main.py | 68 +++++++++++++++++++++++++++++--------------------- utils/state.py | 6 +++++ 2 files changed, 45 insertions(+), 29 deletions(-) create mode 100644 utils/state.py diff --git a/main.py b/main.py index 09b5205..012531d 100644 --- a/main.py +++ b/main.py @@ -17,6 +17,7 @@ from config import PROP_AVAILABLE_WIDGETS, PROP_AVAILABLE_PATTERNS, PROP_WINDOW_ from utils.jsonencoding import get_cfg_from_request, decode_dict, encode_dict from utils.net import json_response, verify_json_request from utils.sequencing import validate_sequence +from utils.state import ScreenState app = Flask(__name__) @@ -100,6 +101,11 @@ def wr_available_fonts(): def wr_available_widgets(): return json_response(PROP_AVAILABLE_WIDGETS) + +@app.route("/api/get_state") +def wr_get_state(): + return json_response({}) # TODO + @app.route("/api/available_patterns") def wr_available_patterns(): return json_response(PROP_AVAILABLE_PATTERNS) @@ -381,6 +387,8 @@ if __name__ == "__main__": if len(pattern_sequence) == 0: raise ValueError("No patterns in sequence") + screen_state = ScreenState.WIDGET_CYCLE + initial_widget = widget_sequence[0] initial_pattern = pattern_sequence[0] @@ -444,36 +452,38 @@ if __name__ == "__main__": except queue.Empty: pass # Check if time delta has passed - # For Widgets - curr_widget = widget_sequence[current_widget_index] - if curr_widget.get("duration", -1) != -1 or force_change_widget: - # Check if the widget has existed for too long - if curr_widget.get("duration") * 1000 <= current_widget_delta or force_change_widget: - # Set the widget to the next widget - current_widget_index += 1 - if current_widget_index >= len(widget_sequence): - current_widget_index = 0 - display_queue.put("SET_WIDGET:" + - widget_sequence[current_widget_index].get("type") + ":" + - encode_dict(widget_sequence[current_widget_index].get("config_override", {})) + if screen_state == ScreenState.WIDGET_CYCLE: + # For Widgets + curr_widget = widget_sequence[current_widget_index] + if curr_widget.get("duration", -1) != -1 or force_change_widget: + # Check if the widget has existed for too long + if curr_widget.get("duration") * 1000 <= current_widget_delta or force_change_widget: + # Set the widget to the next widget + current_widget_index += 1 + if current_widget_index >= len(widget_sequence): + current_widget_index = 0 + display_queue.put("SET_WIDGET:" + + widget_sequence[current_widget_index].get("type") + ":" + + encode_dict(widget_sequence[current_widget_index].get("config_override", {})) + ) + current_widget_delta = 0 + force_change_widget = False + # For patterns + curr_pattern = pattern_sequence[current_pattern_index] + if curr_pattern.get("duration", -1) != -1 or force_change_pattern: + # Check if the pattern has existed for too long + if curr_pattern.get("duration") * 1000 <= current_pattern_delta or force_change_pattern: + # Set the pattern to the next pattern + current_pattern_index += 1 + if current_pattern_index >= len(pattern_sequence): + current_pattern_index = 0 + led_queue.put("SET_PATTERN:" + + pattern_sequence[current_pattern_index].get("type") + ":" + + encode_dict(pattern_sequence[current_pattern_index].get("config_override", {})) ) - current_widget_delta = 0 - force_change_widget = False - # For patterns - curr_pattern = pattern_sequence[current_pattern_index] - if curr_pattern.get("duration", -1) != -1 or force_change_pattern: - # Check if the pattern has existed for too long - if curr_pattern.get("duration") * 1000 <= current_pattern_delta or force_change_pattern: - # Set the pattern to the next pattern - current_pattern_index += 1 - if current_pattern_index >= len(pattern_sequence): - current_pattern_index = 0 - led_queue.put("SET_PATTERN:" + - pattern_sequence[current_pattern_index].get("type") + ":" + - encode_dict(pattern_sequence[current_pattern_index].get("config_override", {})) - ) - current_pattern_delta = 0 - force_change_pattern = False + current_pattern_delta = 0 + force_change_pattern = False + # Keep main thread alive current_widget_delta += hw_config["main_update_frequency"] time.sleep(hw_config["main_update_frequency"] / 1000) diff --git a/utils/state.py b/utils/state.py new file mode 100644 index 0000000..8f4121c --- /dev/null +++ b/utils/state.py @@ -0,0 +1,6 @@ +import enum + +class ScreenState(enum): + WIDGET_CYCLE = 0 + WIDGET_SINGLE = 1 + ALARM_CLOCK = 2 -- GitLab