Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split animation states and improve persisting #15

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions include/FSMGlobals.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +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 animationModeIdx = 0; //!< DisplayAnimation: 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_ */
47 changes: 43 additions & 4 deletions include/FSMState.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -154,9 +160,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;
Expand All @@ -169,11 +175,44 @@ struct DisplayAnimation : public FSMState {
virtual std::unique_ptr<FSMState> touchEventFingerprintShortpress() override;
virtual std::unique_ptr<FSMState> touchEventFingerprintRelease() override;

void _animateKnightRider();
void _animateMatrix();
void _animateRainbow();
void _animateRainbowCircle();
};

/**
* @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<FSMState> 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<FSMState> touchEventFingerprintShortpress() override;
virtual std::unique_ptr<FSMState> touchEventFingerprintRelease() override;

void _animateSnake();
void _animateKnightRider();
};

/**
Expand Down
58 changes: 36 additions & 22 deletions src/FSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

#include "FSM.h"

Preferences pref;

FSM::FSM(unsigned int tickrate_ms)
: state(nullptr)
, tickrate_ms(tickrate_ms)
Expand All @@ -48,18 +50,17 @@ FSM::~FSM() {
void FSM::resume() {
this->restoreGlobals();

if (strcmp(this->globals->lastRememberedState, "DisplayPrideFlag") == 0) {
this->transition(std::make_unique<DisplayPrideFlag>());
return;
}
switch (this->globals->resumeStateIdx) {
case 0: this->transition(std::make_unique<DisplayPrideFlag>()); break;
case 1: this->transition(std::make_unique<AnimateRainbow>()); break;
case 2: this->transition(std::make_unique<AnimateMatrix>()); break;
case 3: this->transition(std::make_unique<AnimateSnake>()); break;
default:
LOGF_WARNING("(FSM) Failed to resume to unknown state: %d\r\n", this->globals->resumeStateIdx);
this->transition(std::make_unique<DisplayPrideFlag>());
break;

if (strcmp(this->globals->lastRememberedState, "DisplayAnimation") == 0) {
this->transition(std::make_unique<DisplayAnimation>());
return;
}

LOGF_WARNING("(FSM) Failed to resume to unknown state: %s\r\n", this->globals->lastRememberedState);
this->transition(std::make_unique<DisplayPrideFlag>());
}

void FSM::transition(std::unique_ptr<FSMState> next) {
Expand All @@ -74,10 +75,11 @@ void FSM::transition(std::unique_ptr<FSMState> 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
Expand Down Expand Up @@ -132,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 ||
Expand Down Expand Up @@ -195,29 +203,35 @@ 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("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();
}

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->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();
}
88 changes: 88 additions & 0 deletions src/states/AnimateMatrix.cpp
Original file line number Diff line number Diff line change
@@ -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 <EFLed.h>
#include <EFLogging.h>
#include <EFPrideFlags.h>

#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<CRGB> dragon = {
CRGB::Black,
CHSV(95, 255, 110),
CHSV(95, 255, 255),
CHSV(95, 255, 110),
CRGB::Black,
CRGB::Black
};

std::vector<CRGB> 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<FSMState> AnimateMatrix::touchEventFingerprintShortpress() {
return std::make_unique<MenuMain>();
}
96 changes: 96 additions & 0 deletions src/states/AnimateRainbow.cpp
Original file line number Diff line number Diff line change
@@ -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 <EFLed.h>
#include <EFLogging.h>
#include <EFPrideFlags.h>

#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<FSMState> 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<FSMState> AnimateRainbow::touchEventFingerprintShortpress() {
return std::make_unique<MenuMain>();
}

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);
}
Loading
Loading