From 5676b5d63c82ab5beb0134aea856db2883b41a48 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Mon, 18 Jan 2021 05:53:11 +0100 Subject: [PATCH] Fixed publish_current_state() --- include/prototypes.h | 3 +++ include/state.h | 1 + src/main.cpp | 16 +++------------- src/prototypes.cpp | 13 +++++++++++++ src/state.cpp | 20 +++++++++++++++++++- 5 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 src/prototypes.cpp diff --git a/include/prototypes.h b/include/prototypes.h index 9453793..4ce4c85 100644 --- a/include/prototypes.h +++ b/include/prototypes.h @@ -2,6 +2,7 @@ #include "my_fastled.h" #include #include +#include #include "config.h" #include "node.h" #include "corner.h" @@ -26,6 +27,8 @@ enum AnimationMode { AM_STATIC }; +extern std::map animation_mode_names; + extern AnimationMode mode; extern AnimationMode temp_mode; extern unsigned long temp_mode_until; diff --git a/include/state.h b/include/state.h index 6b5bacc..080688b 100644 --- a/include/state.h +++ b/include/state.h @@ -15,6 +15,7 @@ class State { void set(String key, String value); void commit(); static void publish_current_state(); + static AnimationMode get_active_mode(); void parse_state(String state); void parse_mode(String mode); diff --git a/src/main.cpp b/src/main.cpp index 5dcc8e6..e9759ce 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -211,6 +211,8 @@ void setup() { for(Corner* corner : corners) { corner->set_color(CRGB::Black); } + + State::publish_current_state(); } void loop() { @@ -234,19 +236,7 @@ void loop() { if (speedup > 0 && (millis() - last_loop > (20 / speedup) || last_loop > millis())) { looping = false; - AnimationMode active_mode = mode; - if (temp_mode_until > 0) { - if (temp_mode_until>millis()) { - active_mode = temp_mode; - } else { - temp_mode_until = 0; - if (return_to_brightness != -1) { - FastLED.setBrightness(return_to_brightness); - return_to_brightness = -1; - } - State::publish_current_state(); - } - } + AnimationMode active_mode = State::get_active_mode(); if (active_mode == AM_CORNERS || active_mode == AM_FIRST_CORNER) { for(Corner* corner: corners) { diff --git a/src/prototypes.cpp b/src/prototypes.cpp new file mode 100644 index 0000000..051ab60 --- /dev/null +++ b/src/prototypes.cpp @@ -0,0 +1,13 @@ +#include "prototypes.h" + +std::map animation_mode_names { + {AM_CORNERS, "corners"}, + {AM_FLASH, "flash"}, + {AM_FIRST_CORNER, "first_corner"}, + {AM_NODES, "nodes"}, + {AM_FIRST_NODE, "first_node"}, + {AM_OFF, "off"}, + {AM_ERROR, "error"}, + {AM_NONE, "NONE"}, + {AM_STATIC, "static"} +}; diff --git a/src/state.cpp b/src/state.cpp index 93c6566..8b95203 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -106,12 +106,30 @@ void State::publish_current_state() { StaticJsonDocument<512> json; json["state"] = (mode==AM_OFF) ? "OFF" : "ON"; json["brightness"] = FastLED.getBrightness(); - json["effect"] = mode; + json["effect"] = animation_mode_names[get_active_mode()]; JsonObject rgb = json.createNestedObject("rgb"); rgb["r"] = color.r; rgb["g"] = color.g; rgb["b"] = color.b; String result = ""; serializeJson(json, result); + LOGln("Reporting current state: %s", result.c_str()); mqtt_publish_current_state(result); +} + +AnimationMode State::get_active_mode() { + AnimationMode active_mode = mode; + if (temp_mode_until > 0) { + if (temp_mode_until>millis()) { + active_mode = temp_mode; + } else { + temp_mode_until = 0; + if (return_to_brightness != -1) { + FastLED.setBrightness(return_to_brightness); + return_to_brightness = -1; + } + State::publish_current_state(); + } + } + return active_mode; } \ No newline at end of file