Skip to content

Commit

Permalink
feat: add Perlin noise animation
Browse files Browse the repository at this point in the history
Introduce new "AnimatePerlin" state to the FSM for displaying Perlin noise patterns. Updated global variables and menu selections to support the new animation mode.
  • Loading branch information
TokenRat committed Sep 6, 2024
1 parent 7dd4dee commit ca3b1a6
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/FSMGlobals.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ typedef struct {
uint8_t animSnakeIdx = 0; //!< AnimateSnake: Mode selector
uint8_t animHeartbeatHue = 0; //!< AnimateHeartbeat: Hue selector
uint8_t animHeartbeatSpeed = 1; //!< AnimateHeartbeat: Speed selector
uint8_t animPerlinSpeed = 1; //!< AnimatePerlin: Speed selector
} FSMGlobals;

#endif /* FSMGLOBALS_H_ */
17 changes: 17 additions & 0 deletions include/FSMState.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,23 @@ struct AnimateHeartbeat : public FSMState {
virtual std::unique_ptr<FSMState> touchEventNoseShortpress() override;
};

/**
* @brief Displays perlin-noise pattern
*/
struct AnimatePerlin : 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> touchEventNoseShortpress() override;
};

/**
* @brief Accept and handle OTA updates
*/
Expand Down
1 change: 1 addition & 0 deletions src/FSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ void FSM::resume() {
case 2: this->transition(std::make_unique<AnimateMatrix>()); break;
case 3: this->transition(std::make_unique<AnimateSnake>()); break;
case 4: this->transition(std::make_unique<AnimateHeartbeat>()); break;
case 5: this->transition(std::make_unique<AnimatePerlin>()); break;
default:
LOGF_WARNING("(FSM) Failed to resume to unknown state: %d\r\n", this->globals->resumeStateIdx);
this->transition(std::make_unique<DisplayPrideFlag>());
Expand Down
97 changes: 97 additions & 0 deletions src/states/AnimatePerlin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// 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.

#include <EFLed.h>
#include <EFLogging.h>
#include <map>

#include "FSMState.h"

const char *AnimatePerlin::getName() {
return "AnimatePerlin";
}

bool AnimatePerlin::shouldBeRemembered() {
return true;
}

const unsigned int AnimatePerlin::getTickRateMs() {
return 60;
}

void AnimatePerlin::entry() {
this->tick = random(500);
}

void AnimatePerlin::run() {
CRGB data[EFLED_TOTAL_NUM];
fill_solid(data, EFLED_TOTAL_NUM, CRGB::Black);

constexpr uint8_t hueScaleX = 2;
constexpr uint8_t hueScaleY = 2;
constexpr uint8_t valScaleX = 10;
constexpr uint8_t valScaleY = 10;

for (uint8_t i = 0; i < EFLED_TOTAL_NUM; i++) {

uint8_t hueF = inoise8(EFLedClass::getLEDPosition(i).x * hueScaleX, EFLedClass::getLEDPosition(i).y * hueScaleY, tick * 2 * (this->globals->animPerlinSpeed + 1));
uint16_t valueF = inoise8(EFLedClass::getLEDPosition(i).x * valScaleX, EFLedClass::getLEDPosition(i).y * valScaleY, tick * 2 * (this->globals->animPerlinSpeed + 1));

// inoise is +-64 somehow
//int n = inoise16(x << 8, y << 8) >> 8;
valueF = valueF + 50;
if (valueF > 255) {
valueF = 255;
}
data[i] = CHSV((hueF * 2) % 255, 255, static_cast<uint8_t>(valueF));
}

EFLed.setAll(data);

// Prepare next tick
this->tick = this->tick + 1;
}

std::unique_ptr<FSMState> AnimatePerlin::touchEventFingerprintShortpress() {
return std::make_unique<MenuMain>();
}

std::unique_ptr<FSMState> AnimatePerlin::touchEventNoseShortpress() {
this->globals->animPerlinSpeed = (this->globals->animPerlinSpeed + 1) % 3;
this->is_globals_dirty = true;

CRGB data[EFLED_EFBAR_NUM];
fill_solid(data, EFLED_EFBAR_NUM, CRGB::Black);
EFLed.setEFBar(data);
delay(100);
fill_solid(data, this->globals->animPerlinSpeed + 1, CRGB::Red);
EFLed.setEFBar(data);
delay(300);
fill_solid(data, this->globals->animPerlinSpeed + 1, CRGB::Black);
EFLed.setEFBar(data);
delay(200);
fill_solid(data, this->globals->animPerlinSpeed + 1, CRGB::Red);
EFLed.setEFBar(data);
delay(400);

return nullptr;
}
5 changes: 3 additions & 2 deletions src/states/MenuMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/**
* @brief Number of registered menu items
*/
#define MENUMAIN_NUM_MENU_ITEMS 6
#define MENUMAIN_NUM_MENU_ITEMS 7


const char *MenuMain::getName() {
Expand Down Expand Up @@ -64,7 +64,8 @@ std::unique_ptr<FSMState> MenuMain::touchEventFingerprintShortpress() {
case 2: return std::make_unique<AnimateMatrix>();
case 3: return std::make_unique<AnimateSnake>();
case 4: return std::make_unique<AnimateHeartbeat>();
case 5: return std::make_unique<OTAUpdate>();
case 5: return std::make_unique<AnimatePerlin>();
case 6: return std::make_unique<OTAUpdate>();
default: return nullptr;
}
}
Expand Down

0 comments on commit ca3b1a6

Please sign in to comment.