From 4ef5f8081edf53552ed60eb5a30b018e51da35f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Gandra=C3=9F?= Date: Mon, 2 Sep 2024 15:00:21 +0200 Subject: [PATCH 1/2] Separate DisplayAnimation state into: AnimateRainbow, AnimateMatrix, AnimateSnake --- include/FSMGlobals.h | 3 +- include/FSMState.h | 40 +++++++- src/FSM.cpp | 26 +++-- src/states/AnimateMatrix.cpp | 88 +++++++++++++++++ src/states/AnimateRainbow.cpp | 96 +++++++++++++++++++ ...{DisplayAnimation.cpp => AnimateSnake.cpp} | 85 ++++------------ src/states/MenuMain.cpp | 8 +- 7 files changed, 269 insertions(+), 77 deletions(-) create mode 100644 src/states/AnimateMatrix.cpp create mode 100644 src/states/AnimateRainbow.cpp rename src/states/{DisplayAnimation.cpp => AnimateSnake.cpp} (52%) diff --git a/include/FSMGlobals.h b/include/FSMGlobals.h index 3df7f0b..d913985 100644 --- a/include/FSMGlobals.h +++ b/include/FSMGlobals.h @@ -40,7 +40,8 @@ typedef struct { const char* lastRememberedState = "DisplayPrideFlag"; //!< Name of the state that should be resumed upon reboot uint8_t menuMainPointerIdx = 0; //!< MenuMain: Index of the menu cursor uint8_t prideFlagModeIdx = 1; //!< DisplayPrideFlag: Mode selector - uint8_t animationModeIdx = 0; //!< DisplayAnimation: Mode selector + uint8_t animRainbowIdx = 0; //!< AnimateRainbow: Mode selector + uint8_t animSnakeIdx = 0; //!< AnimateSnake: Mode selector } FSMGlobals; #endif /* FSMGLOBALS_H_ */ \ No newline at end of file diff --git a/include/FSMState.h b/include/FSMState.h index b6fad3d..5d2d407 100644 --- a/include/FSMState.h +++ b/include/FSMState.h @@ -154,9 +154,9 @@ struct DisplayPrideFlag : public FSMState { }; /** - * @brief Displays animations + * @brief Displays rainbow animations */ -struct DisplayAnimation : public FSMState { +struct AnimateRainbow : public FSMState { uint32_t tick = 0; virtual const char* getName() override; @@ -176,6 +176,42 @@ struct DisplayAnimation : public FSMState { void _animateSnake(); }; +/** + * @brief Displays matrix animation + */ +struct AnimateMatrix : public FSMState { + uint32_t tick = 0; + + virtual const char* getName() override; + virtual bool shouldBeRemembered() override; + virtual const unsigned int getTickRateMs() override; + + virtual void entry() override; + virtual void run() override; + + virtual std::unique_ptr touchEventFingerprintShortpress() override; +}; + +/** + * @brief Displays snake animations + */ +struct AnimateSnake : public FSMState { + uint32_t tick = 0; + + virtual const char* getName() override; + virtual bool shouldBeRemembered() override; + virtual const unsigned int getTickRateMs() override; + + virtual void entry() override; + virtual void run() override; + + virtual std::unique_ptr touchEventFingerprintShortpress() override; + virtual std::unique_ptr touchEventFingerprintRelease() override; + + void _animateSnake(); + void _animateKnightRider(); +}; + /** * @brief Accept and handle OTA updates */ diff --git a/src/FSM.cpp b/src/FSM.cpp index f2b920f..f371ece 100644 --- a/src/FSM.cpp +++ b/src/FSM.cpp @@ -53,8 +53,18 @@ void FSM::resume() { return; } - if (strcmp(this->globals->lastRememberedState, "DisplayAnimation") == 0) { - this->transition(std::make_unique()); + if (strcmp(this->globals->lastRememberedState, "AnimateRainbow") == 0) { + this->transition(std::make_unique()); + return; + } + + if (strcmp(this->globals->lastRememberedState, "AnimareMatrix") == 0) { + this->transition(std::make_unique()); + return; + } + + if (strcmp(this->globals->lastRememberedState, "AnimateSnake") == 0) { + this->transition(std::make_unique()); return; } @@ -204,8 +214,10 @@ void FSM::persistGlobals() { LOGF_DEBUG("(FSM) -> resumeState = %s\r\n", this->globals->lastRememberedState); pref.putUInt("prideFlagMode", this->globals->prideFlagModeIdx); LOGF_DEBUG("(FSM) -> prideFlagMode = %d\r\n", this->globals->prideFlagModeIdx); - pref.putUInt("animationMode", this->globals->animationModeIdx); - LOGF_DEBUG("(FSM) -> animationMode = %d\r\n", this->globals->animationModeIdx); + pref.putUInt("animRainbow", this->globals->animRainbowIdx); + LOGF_DEBUG("(FSM) -> animRainbow = %d\r\n", this->globals->animRainbowIdx); + pref.putUInt("animSnake", this->globals->animSnakeIdx); + LOGF_DEBUG("(FSM) -> animSnake = %d\r\n", this->globals->animSnakeIdx); pref.end(); } @@ -217,7 +229,9 @@ void FSM::restoreGlobals() { LOGF_DEBUG("(FSM) -> resumeState = %s\r\n", this->globals->lastRememberedState); this->globals->prideFlagModeIdx = pref.getUInt("prideFlagMode", 1); LOGF_DEBUG("(FSM) -> prideFlagMode = %d\r\n", this->globals->prideFlagModeIdx); - this->globals->animationModeIdx = pref.getUInt("animationMode", 0); - LOGF_DEBUG("(FSM) -> animationMode = %d\r\n", this->globals->animationModeIdx); + this->globals->animRainbowIdx = pref.getUInt("animRainbow", 0); + LOGF_DEBUG("(FSM) -> animRainbow = %d\r\n", this->globals->animRainbowIdx); + this->globals->animSnakeIdx = pref.getUInt("animSnake", 0); + LOGF_DEBUG("(FSM) -> animSnake = %d\r\n", this->globals->animSnakeIdx); pref.end(); } diff --git a/src/states/AnimateMatrix.cpp b/src/states/AnimateMatrix.cpp new file mode 100644 index 0000000..23a4d2d --- /dev/null +++ b/src/states/AnimateMatrix.cpp @@ -0,0 +1,88 @@ +// MIT License +// +// Copyright 2024 Eurofurence e.V. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the “Software”), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. + +/** + * @author Honigeintopf + */ + +#include +#include +#include + +#include "FSMState.h" + + +const char* AnimateMatrix::getName() { + return "AnimateMatrix"; +} + +bool AnimateMatrix::shouldBeRemembered() { + return true; +} + +const unsigned int AnimateMatrix::getTickRateMs() { + return 100; +} + +void AnimateMatrix::entry() { + this->tick = 0; +} + +void AnimateMatrix::run() { + // Prepare base pattern + std::vector dragon = { + CRGB::Black, + CHSV(95, 255, 110), + CHSV(95, 255, 255), + CHSV(95, 255, 110), + CRGB::Black, + CRGB::Black + }; + + std::vector bar = { + CHSV(95, 255, 110), + CHSV(95, 255, 255), + CHSV(95, 255, 110), + CRGB::Black, + CRGB::Black, + CHSV(95, 255, 110), + CHSV(95, 255, 255), + CHSV(95, 255, 110), + CRGB::Black, + CRGB::Black, + CRGB::Black, + }; + + // Calculate current pattern based on tick + std::rotate(dragon.begin(), dragon.begin() + this->tick % EFLED_DRAGON_NUM, dragon.end()); + std::rotate(bar.rbegin(), bar.rbegin() + this->tick % EFLED_EFBAR_NUM, bar.rend()); + + dragon.insert(dragon.end(), bar.begin(), bar.end()); + EFLed.setAll(dragon.data()); + + // Prepare next tick + this->tick++; +} + +std::unique_ptr AnimateMatrix::touchEventFingerprintShortpress() { + return std::make_unique(); +} diff --git a/src/states/AnimateRainbow.cpp b/src/states/AnimateRainbow.cpp new file mode 100644 index 0000000..9447d17 --- /dev/null +++ b/src/states/AnimateRainbow.cpp @@ -0,0 +1,96 @@ +// MIT License +// +// Copyright 2024 Eurofurence e.V. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the “Software”), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. + +/** + * @author Honigeintopf + */ + +#include +#include +#include + +#include "FSMState.h" + +#define ANIMATE_RAINBOW_NUM_TOTAL 3 //!< Number of available animations + +/** + * @brief Index of all animations, each consisting of a periodically called + * animation function and an associated tick rate in milliseconds. + */ +const struct { + void (AnimateRainbow::* animate)(); + const unsigned int tickrate; +} animations[ANIMATE_RAINBOW_NUM_TOTAL] = { + {.animate = &AnimateRainbow::_animateRainbowCircle, .tickrate = 20}, + {.animate = &AnimateRainbow::_animateRainbow, .tickrate = 100}, + {.animate = &AnimateRainbow::_animateRainbow, .tickrate = 20}, + +}; + +const char* AnimateRainbow::getName() { + return "AnimateRainbow"; +} + +bool AnimateRainbow::shouldBeRemembered() { + return true; +} + +const unsigned int AnimateRainbow::getTickRateMs() { + return animations[this->globals->animRainbowIdx % ANIMATE_RAINBOW_NUM_TOTAL].tickrate; +} + +void AnimateRainbow::entry() { + this->tick = 0; +} + +void AnimateRainbow::run() { + (*this.*(animations[this->globals->animRainbowIdx % ANIMATE_RAINBOW_NUM_TOTAL].animate))(); + this->tick++; +} + +std::unique_ptr AnimateRainbow::touchEventFingerprintRelease() { + this->globals->animRainbowIdx++; + this->is_globals_dirty = true; + this->tick = 0; + EFLed.clear(); + + LOGF_INFO( + "(AnimateRainbow) Changed animation mode to: %d\r\n", + this->globals->animRainbowIdx % ANIMATE_RAINBOW_NUM_TOTAL + ); + + return nullptr; +} + +std::unique_ptr AnimateRainbow::touchEventFingerprintShortpress() { + return std::make_unique(); +} + +void AnimateRainbow::_animateRainbow() { + EFLed.setAllSolid(CHSV((tick % 256), 255, 255)); +} + +void AnimateRainbow::_animateRainbowCircle() { + CRGB data[EFLED_TOTAL_NUM]; + fill_rainbow_circular(data, EFLED_TOTAL_NUM, (tick % 128)*2, true); + EFLed.setAll(data); +} diff --git a/src/states/DisplayAnimation.cpp b/src/states/AnimateSnake.cpp similarity index 52% rename from src/states/DisplayAnimation.cpp rename to src/states/AnimateSnake.cpp index 4305fc0..296815c 100644 --- a/src/states/DisplayAnimation.cpp +++ b/src/states/AnimateSnake.cpp @@ -30,69 +30,60 @@ #include "FSMState.h" -#define DISPLAY_ANIMATION_NUM_TOTAL 6 //!< Number of available animations +#define ANIMATE_SNAKE_NUM_TOTAL 2 //!< Number of available animations /** * @brief Index of all animations, each consisting of a periodically called * animation function and an associated tick rate in milliseconds. */ const struct { - void (DisplayAnimation::* animate)(); + void (AnimateSnake::* animate)(); const unsigned int tickrate; -} animations[DISPLAY_ANIMATION_NUM_TOTAL] = { - {.animate = &DisplayAnimation::_animateRainbowCircle, .tickrate = 20}, - {.animate = &DisplayAnimation::_animateRainbow, .tickrate = 100}, - {.animate = &DisplayAnimation::_animateRainbow, .tickrate = 20}, - {.animate = &DisplayAnimation::_animateMatrix, .tickrate = 100}, - {.animate = &DisplayAnimation::_animateKnightRider, .tickrate = 80}, - {.animate = &DisplayAnimation::_animateSnake, .tickrate = 80} - +} animations[ANIMATE_SNAKE_NUM_TOTAL] = { + {.animate = &AnimateSnake::_animateSnake, .tickrate = 80}, + {.animate = &AnimateSnake::_animateKnightRider, .tickrate = 80} }; -const char* DisplayAnimation::getName() { - return "DisplayAnimation"; +const char* AnimateSnake::getName() { + return "AnimateSnake"; } -bool DisplayAnimation::shouldBeRemembered() { +bool AnimateSnake::shouldBeRemembered() { return true; } -const unsigned int DisplayAnimation::getTickRateMs() { - return animations[this->globals->animationModeIdx % DISPLAY_ANIMATION_NUM_TOTAL].tickrate; +const unsigned int AnimateSnake::getTickRateMs() { + return animations[this->globals->animSnakeIdx % ANIMATE_SNAKE_NUM_TOTAL].tickrate; } -void DisplayAnimation::entry() { +void AnimateSnake::entry() { this->tick = 0; } -void DisplayAnimation::run() { - (*this.*(animations[this->globals->animationModeIdx % DISPLAY_ANIMATION_NUM_TOTAL].animate))(); +void AnimateSnake::run() { + (*this.*(animations[this->globals->animSnakeIdx % ANIMATE_SNAKE_NUM_TOTAL].animate))(); this->tick++; } -std::unique_ptr DisplayAnimation::touchEventFingerprintRelease() { - this->globals->animationModeIdx++; +std::unique_ptr AnimateSnake::touchEventFingerprintRelease() { + this->globals->animSnakeIdx++; this->is_globals_dirty = true; this->tick = 0; EFLed.clear(); LOGF_INFO( - "(DisplayAnimation) Changed animation mode to: %d\r\n", - this->globals->animationModeIdx % DISPLAY_ANIMATION_NUM_TOTAL + "(AnimateSnake) Changed animation mode to: %d\r\n", + this->globals->animSnakeIdx % ANIMATE_SNAKE_NUM_TOTAL ); return nullptr; } -std::unique_ptr DisplayAnimation::touchEventFingerprintShortpress() { +std::unique_ptr AnimateSnake::touchEventFingerprintShortpress() { return std::make_unique(); } -void DisplayAnimation::_animateRainbow() { - EFLed.setAllSolid(CHSV((tick % 256), 255, 255)); -} - -void DisplayAnimation::_animateKnightRider() { +void AnimateSnake::_animateKnightRider() { // Calculate pattern uint16_t pattern = 0b111 << (EFLED_EFBAR_NUM - 3); if (tick % 16 < 8) { @@ -116,7 +107,7 @@ void DisplayAnimation::_animateKnightRider() { EFLed.setEFBar(colors); } -void DisplayAnimation::_animateSnake() { +void AnimateSnake::_animateSnake() { std::vector pattern = { CRGB::Red, CRGB::Red, @@ -141,39 +132,3 @@ void DisplayAnimation::_animateSnake() { EFLed.setAll(pattern.data()); } -void DisplayAnimation::_animateMatrix() { - std::vector dragon = { - CRGB::Black, - CHSV(95, 255, 110), - CHSV(95, 255, 255), - CHSV(95, 255, 110), - CRGB::Black, - CRGB::Black - }; - - std::vector bar = { - CHSV(95, 255, 110), - CHSV(95, 255, 255), - CHSV(95, 255, 110), - CRGB::Black, - CRGB::Black, - CHSV(95, 255, 110), - CHSV(95, 255, 255), - CHSV(95, 255, 110), - CRGB::Black, - CRGB::Black, - CRGB::Black, - }; - - std::rotate(dragon.begin(), dragon.begin() + this->tick % EFLED_DRAGON_NUM, dragon.end()); - std::rotate(bar.rbegin(), bar.rbegin() + this->tick % EFLED_EFBAR_NUM, bar.rend()); - - dragon.insert(dragon.end(), bar.begin(), bar.end()); - EFLed.setAll(dragon.data()); -} - -void DisplayAnimation::_animateRainbowCircle() { - CRGB data[EFLED_TOTAL_NUM]; - fill_rainbow_circular(data, EFLED_TOTAL_NUM, (tick % 128)*2, true); - EFLed.setAll(data); -} diff --git a/src/states/MenuMain.cpp b/src/states/MenuMain.cpp index a614f00..c7cdd08 100644 --- a/src/states/MenuMain.cpp +++ b/src/states/MenuMain.cpp @@ -32,7 +32,7 @@ /** * @brief Number of registered menu items */ -#define MENUMAIN_NUM_MENU_ITEMS 3 +#define MENUMAIN_NUM_MENU_ITEMS 5 const char* MenuMain::getName() { @@ -59,8 +59,10 @@ std::unique_ptr MenuMain::touchEventFingerprintShortpress() { LOGF_DEBUG("(MenuMain) menuMainPointerIdx = %d\r\n", this->globals->menuMainPointerIdx); switch (this->globals->menuMainPointerIdx) { case 0: return std::make_unique(); - case 1: return std::make_unique(); - case 2: return std::make_unique(); + case 1: return std::make_unique(); + case 2: return std::make_unique(); + case 3: return std::make_unique(); + case 4: return std::make_unique(); default: return nullptr; } } From bc779df189a4d6ab32c33e57c18e99c37c4fac84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Gandra=C3=9F?= Date: Mon, 2 Sep 2024 15:58:36 +0200 Subject: [PATCH 2/2] Simplify FSM state resuming and persist at any point in time if globals got dirtied --- include/FSMGlobals.h | 10 ++++---- include/FSMState.h | 9 ++++--- src/FSM.cpp | 56 ++++++++++++++++++++--------------------- src/states/FSMState.cpp | 4 +++ 4 files changed, 43 insertions(+), 36 deletions(-) diff --git a/include/FSMGlobals.h b/include/FSMGlobals.h index d913985..bd212ee 100644 --- a/include/FSMGlobals.h +++ b/include/FSMGlobals.h @@ -37,11 +37,11 @@ * FSM::persistGlobals() and FSM::restoreGlobals() respectively. */ typedef struct { - const char* lastRememberedState = "DisplayPrideFlag"; //!< Name of the state that should be resumed upon reboot - uint8_t menuMainPointerIdx = 0; //!< MenuMain: Index of the menu cursor - uint8_t prideFlagModeIdx = 1; //!< DisplayPrideFlag: Mode selector - uint8_t animRainbowIdx = 0; //!< AnimateRainbow: Mode selector - uint8_t animSnakeIdx = 0; //!< AnimateSnake: Mode selector + uint8_t resumeStateIdx = 0; //!< Index of the state that should be resumed upon reboot + uint8_t menuMainPointerIdx = 0; //!< MenuMain: Index of the menu cursor + uint8_t prideFlagModeIdx = 1; //!< DisplayPrideFlag: Mode selector + uint8_t animRainbowIdx = 0; //!< AnimateRainbow: Mode selector + uint8_t animSnakeIdx = 0; //!< AnimateSnake: Mode selector } FSMGlobals; #endif /* FSMGLOBALS_H_ */ \ No newline at end of file diff --git a/include/FSMState.h b/include/FSMState.h index 5d2d407..a82a343 100644 --- a/include/FSMState.h +++ b/include/FSMState.h @@ -54,6 +54,12 @@ class FSMState { */ bool isGlobalsDirty(); + /** + * @brief Resets the dirty flag for FSM globals object. Should be called + * after globals were successfully persisted. + */ + void resetGlobalsDirty(); + /** * @brief Provides access to the name of this state * @@ -169,11 +175,8 @@ struct AnimateRainbow : public FSMState { virtual std::unique_ptr touchEventFingerprintShortpress() override; virtual std::unique_ptr touchEventFingerprintRelease() override; - void _animateKnightRider(); - void _animateMatrix(); void _animateRainbow(); void _animateRainbowCircle(); - void _animateSnake(); }; /** diff --git a/src/FSM.cpp b/src/FSM.cpp index f371ece..7d796c3 100644 --- a/src/FSM.cpp +++ b/src/FSM.cpp @@ -31,6 +31,8 @@ #include "FSM.h" +Preferences pref; + FSM::FSM(unsigned int tickrate_ms) : state(nullptr) , tickrate_ms(tickrate_ms) @@ -48,28 +50,17 @@ FSM::~FSM() { void FSM::resume() { this->restoreGlobals(); - if (strcmp(this->globals->lastRememberedState, "DisplayPrideFlag") == 0) { - this->transition(std::make_unique()); - return; - } - - if (strcmp(this->globals->lastRememberedState, "AnimateRainbow") == 0) { - this->transition(std::make_unique()); - return; - } - - if (strcmp(this->globals->lastRememberedState, "AnimareMatrix") == 0) { - this->transition(std::make_unique()); - return; - } + switch (this->globals->resumeStateIdx) { + case 0: this->transition(std::make_unique()); break; + case 1: this->transition(std::make_unique()); break; + case 2: this->transition(std::make_unique()); break; + case 3: this->transition(std::make_unique()); break; + default: + LOGF_WARNING("(FSM) Failed to resume to unknown state: %d\r\n", this->globals->resumeStateIdx); + this->transition(std::make_unique()); + break; - if (strcmp(this->globals->lastRememberedState, "AnimateSnake") == 0) { - this->transition(std::make_unique()); - return; } - - LOGF_WARNING("(FSM) Failed to resume to unknown state: %s\r\n", this->globals->lastRememberedState); - this->transition(std::make_unique()); } void FSM::transition(std::unique_ptr next) { @@ -84,10 +75,11 @@ void FSM::transition(std::unique_ptr next) { // Persist globals if state dirtied it or next state wants to be persisted if (next->shouldBeRemembered()) { - this->globals->lastRememberedState = next->getName(); + this->globals->resumeStateIdx = this->globals->menuMainPointerIdx; } if (this->state->isGlobalsDirty() || next->shouldBeRemembered()) { this->persistGlobals(); + this->state->resetGlobalsDirty(); } // Transition to next state @@ -142,6 +134,12 @@ void FSM::handle() { } void FSM::handle(unsigned int num_events) { + // Handle dirtied FSM globals + if (this->state->isGlobalsDirty()) { + this->persistGlobals(); + this->state->resetGlobalsDirty(); + } + // Handle state run() if ( this->state->getTickRateMs() == 0 || @@ -205,13 +203,14 @@ void FSM::handle(unsigned int num_events) { } void FSM::persistGlobals() { - Preferences pref; pref.begin(this->NVS_NAMESPACE, false); LOGF_INFO("(FSM) Persisting FSM state data to NVS area: %s\r\n", this->NVS_NAMESPACE); pref.clear(); LOG_DEBUG("(FSM) -> Clear storage area"); - pref.putString("resumeState", this->globals->lastRememberedState); - LOGF_DEBUG("(FSM) -> resumeState = %s\r\n", this->globals->lastRememberedState); + pref.putUInt("resumeStateIdx", this->globals->resumeStateIdx); + LOGF_DEBUG("(FSM) -> resumeStateIdx = %d\r\n", this->globals->resumeStateIdx); + pref.putUInt("menuIdx", this->globals->menuMainPointerIdx); + LOGF_DEBUG("(FSM) -> menuIdx = %d\r\n", this->globals->menuMainPointerIdx); pref.putUInt("prideFlagMode", this->globals->prideFlagModeIdx); LOGF_DEBUG("(FSM) -> prideFlagMode = %d\r\n", this->globals->prideFlagModeIdx); pref.putUInt("animRainbow", this->globals->animRainbowIdx); @@ -222,11 +221,12 @@ void FSM::persistGlobals() { } void FSM::restoreGlobals() { - Preferences pref; pref.begin(this->NVS_NAMESPACE, true); - LOGF_INFO("(FSM) Restored FSM state data from NVS area: %s\r\n", this->NVS_NAMESPACE); - this->globals->lastRememberedState = pref.getString("resumeState", "DisplayPrideFlag").c_str(); - LOGF_DEBUG("(FSM) -> resumeState = %s\r\n", this->globals->lastRememberedState); + LOGF_INFO("(FSM) Restoring FSM state data from NVS area: %s\r\n", this->NVS_NAMESPACE); + this->globals->resumeStateIdx = pref.getUInt("resumeStateIdx", 0); + LOGF_DEBUG("(FSM) -> resumeStateIdx = %d\r\n", this->globals->resumeStateIdx); + this->globals->menuMainPointerIdx = pref.getUInt("menuIdx", 0); + LOGF_DEBUG("(FSM) -> menuIdx = %d\r\n", this->globals->menuMainPointerIdx); this->globals->prideFlagModeIdx = pref.getUInt("prideFlagMode", 1); LOGF_DEBUG("(FSM) -> prideFlagMode = %d\r\n", this->globals->prideFlagModeIdx); this->globals->animRainbowIdx = pref.getUInt("animRainbow", 0); diff --git a/src/states/FSMState.cpp b/src/states/FSMState.cpp index a6704f2..8104520 100644 --- a/src/states/FSMState.cpp +++ b/src/states/FSMState.cpp @@ -34,6 +34,10 @@ bool FSMState::isGlobalsDirty() { return this->is_globals_dirty; } +void FSMState::resetGlobalsDirty() { + this->is_globals_dirty = false; +} + bool FSMState::shouldBeRemembered() { return false; }