Added blink animation (Resolves #106)

This commit is contained in:
CommanderRedYT
2021-11-15 20:38:54 +01:00
parent be41e8018e
commit be05e42303
10 changed files with 126 additions and 21 deletions

View File

@ -171,6 +171,7 @@ struct LedsStVOFrontLengthAccessor : public RefAccessorSaveSettings<int16_t> { i
struct EnableLedstripStVOFrontlight : public RefAccessorSaveSettings<bool> { bool &getRef() const override { return settings.ledstrip.stvoFrontEnable; } };
struct AnimationMultiplierAccessor : public RefAccessorSaveSettings<int16_t> { int16_t &getRef() const override { return settings.ledstrip.animationMultiplier; } };
struct LedstripBrightnessAccessor : public RefAccessorSaveSettings<uint8_t> { uint8_t &getRef() const override { return settings.ledstrip.brightness; } };
struct LedstripEnableBlinkAnimation : public RefAccessorSaveSettings<bool> { bool &getRef() const override { return settings.ledstrip.enableAnimBlink; } };
#endif
// Battery

View File

@ -11,7 +11,11 @@
#include "actions/dummyaction.h"
#include "actions/ledstripblinkactions.h"
#include "actions/switchscreenaction.h"
#include "actions/toggleboolaction.h"
#include "checkboxicon.h"
#include "ledstripdefines.h"
#include "accessors/settingsaccessors.h"
#include "ledstripmenu.h"
#ifdef FEATURE_LEDSTRIP
class currentSelectedBlinkAnimationText : public virtual TextInterface { public: std::string text() const override {
@ -53,6 +57,7 @@ namespace {
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_ANIMATION_BLINKLEFT>, LedstripAnimationBlinkLeftAction>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_ANIMATION_BLINKRIGHT>, LedstripAnimationBlinkRightAction>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_ANIMATION_BLINKBOTH>, LedstripAnimationBlinkBothAction>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_LEDSTRIP_EN_BLINK_ANIM>, ToggleBoolAction, CheckboxIcon, LedstripEnableBlinkAnimation>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BACK>, SwitchScreenAction<LedstripMenu>, StaticMenuItemIcon<&espgui::icons::back>>>();
}
};

View File

@ -7,6 +7,7 @@
#include "cpputils.h"
#include "espchrono.h"
#include "ledstripdefines.h"
#include "utils.h"
using namespace std::chrono_literals;
@ -34,33 +35,97 @@ void updateLedStrip()
if (cpputils::is_in(blinkAnimation, LEDSTRIP_OVERWRITE_BLINKLEFT, LEDSTRIP_OVERWRITE_BLINKRIGHT, LEDSTRIP_OVERWRITE_BLINKBOTH))
{
std::fill(std::begin(leds), std::end(leds), CRGB{0, 0, 0});
if (espchrono::millis_clock::now().time_since_epoch() % 750ms < 375ms)
const bool enAnim = settings.ledstrip.enableAnimBlink;
if (espchrono::millis_clock::now().time_since_epoch() % 750ms < 375ms || enAnim)
{
const auto anim_to_fill = time_to_percent(750ms, 500ms, 100ms, settings.ledstrip.enableFullBlink ? (leds.size() / 2) : settings.ledstrip.bigOffset - settings.ledstrip.smallOffset, settings.ledstrip.enableFullBlink);
if (settings.ledstrip.enableBeepWhenBlink)
{
for (Controller &controller : controllers)
controller.command.buzzer.freq = 3;
if (espchrono::millis_clock::now().time_since_epoch() % 750ms < 375ms)
for (Controller &controller : controllers)
controller.command.buzzer.freq = 3;
else
for (Controller &controller : controllers)
controller.command.buzzer.freq = 0;
}
auto color = CRGB{255, 255, 0};
auto color = CRGB{255, 200, 0};
const auto center = (std::begin(leds) + (leds.size() / 2) + settings.ledstrip.centerOffset);
if (blinkAnimation != LEDSTRIP_OVERWRITE_BLINKRIGHT && !settings.ledstrip.enableFullBlink)
if (settings.ledstrip.enableFullBlink)
{
std::fill(center - settings.ledstrip.bigOffset, center - settings.ledstrip.smallOffset, color);
// Full
if (BLINK_LEFT_EXPR)
{
// Blink left
if (!enAnim)
{
std::fill(std::begin(leds), center, color);
}
else
{
std::fill(std::begin(leds)+anim_to_fill, center, color);
}
}
if (BLINK_RIGHT_EXPR)
{
// Blink right
if (!enAnim)
{
std::fill(center, std::end(leds), color);
}
else
{
std::fill(center, std::end(leds) - anim_to_fill, color);
}
}
}
else if(blinkAnimation != LEDSTRIP_OVERWRITE_BLINKRIGHT && settings.ledstrip.enableFullBlink)
else
{
std::fill(std::begin(leds), center, color);
}
if (blinkAnimation != LEDSTRIP_OVERWRITE_BLINKLEFT && !settings.ledstrip.enableFullBlink)
{
std::fill(center + settings.ledstrip.smallOffset, center + settings.ledstrip.bigOffset, color);
}
else if(blinkAnimation != LEDSTRIP_OVERWRITE_BLINKLEFT && settings.ledstrip.enableFullBlink)
{
std::fill(center, std::end(leds), color);
// Only in the back
if (BLINK_LEFT_EXPR)
{
// Blink left
if (!enAnim)
{
std::fill(center - settings.ledstrip.bigOffset, center - settings.ledstrip.smallOffset, color);
}
else
{
std::fill(center - settings.ledstrip.smallOffset - anim_to_fill, center - settings.ledstrip.smallOffset, color);
}
}
if (BLINK_RIGHT_EXPR)
{
// Blink right
if (!enAnim)
{
std::fill(center + settings.ledstrip.smallOffset, center + settings.ledstrip.bigOffset, color);
}
else
{
std::fill(center + settings.ledstrip.smallOffset, center + settings.ledstrip.smallOffset + anim_to_fill, color);
}
}
}
// Old way to blink
// if (blinkAnimation != LEDSTRIP_OVERWRITE_BLINKRIGHT && !settings.ledstrip.enableFullBlink)
// {
// std::fill(center - settings.ledstrip.bigOffset, center - settings.ledstrip.smallOffset, color);
// }
// else if(blinkAnimation != LEDSTRIP_OVERWRITE_BLINKRIGHT && settings.ledstrip.enableFullBlink)
// {
// std::fill(std::begin(leds), center, color);
// }
// if (blinkAnimation != LEDSTRIP_OVERWRITE_BLINKLEFT && !settings.ledstrip.enableFullBlink)
// {
// std::fill(center + settings.ledstrip.smallOffset, center + settings.ledstrip.bigOffset, color);
// }
// else if(blinkAnimation != LEDSTRIP_OVERWRITE_BLINKLEFT && settings.ledstrip.enableFullBlink)
// {
// std::fill(center, std::end(leds), color);
// }
} else {
if (settings.ledstrip.enableBeepWhenBlink)
{
@ -93,10 +158,10 @@ void updateLedStrip()
{
std::fill(std::begin(leds), std::end(leds), color);
}
else
else if(!settings.ledstrip.enableAnimBlink)
{
std::fill(center - settings.ledstrip.bigOffset, center - settings.ledstrip.smallOffset, color);
std::fill(center + settings.ledstrip.smallOffset, center + settings.ledstrip.bigOffset, color);
std::fill(center - settings.ledstrip.bigOffset - 2, center - settings.ledstrip.smallOffset + 2, color);
std::fill(center + settings.ledstrip.smallOffset - 2, center + settings.ledstrip.bigOffset + 2, color);
}
}
else

View File

@ -7,6 +7,7 @@
#include <FastLED.h>
#ifdef FEATURE_LEDSTRIP
#define crgb_iterator __gnu_cxx::__normal_iterator<CRGB *, std::vector<CRGB>>
enum Bobbycar_Side
{
FRONT_RIGHT,

View File

@ -18,3 +18,6 @@
#define LEDSTRIP_ANIMATION_TYPE_BETTERRAINBOW 1
#define LEDSTRIP_ANIMATION_TYPE_SPEEDSYNCANIMATION 2
#define LEDSTRIP_ANIMATION_TYPE_CUSTOMCOLOR 3
#define BLINK_LEFT_EXPR blinkAnimation != LEDSTRIP_OVERWRITE_BLINKRIGHT
#define BLINK_RIGHT_EXPR blinkAnimation != LEDSTRIP_OVERWRITE_BLINKLEFT

View File

@ -245,7 +245,8 @@ constexpr Settings::Ledstrip defaultLedstrip {
.stvoFrontLength = 10,
.stvoFrontEnable = false,
.animationMultiplier = 10,
.brightness = 255
.brightness = 255,
.enableAnimBlink = false
};
#endif

View File

@ -170,6 +170,7 @@ struct Settings
bool stvoFrontEnable;
int16_t animationMultiplier;
uint8_t brightness;
bool enableAnimBlink;
} ledstrip;
#endif
@ -302,6 +303,7 @@ void Settings::executeForEveryCommonSetting(T &&callable)
callable("ledstvoen", ledstrip.stvoFrontEnable);
callable("ledAnimMul", ledstrip.animationMultiplier);
callable("ledbrightness", ledstrip.brightness);
callable("enAnimBlink", ledstrip.enableAnimBlink);
#endif
callable("batteryCS", battery.cellsSeries);

View File

@ -293,6 +293,7 @@ constexpr char TEXT_STVO_ENABLEFRONTLIGHT[] = "StVO Front Enable";
constexpr char TEXT_ANIMATION_MULTIPLIER[] = "Animation Multiplier";
constexpr char TEXT_LEDSTRIP_BRIGHTNESS[] = "Ledstrip Brightness";
constexpr char TEXT_LEDSTRIP_ALLCUSTOMOFF[] = "All custom off";
constexpr char TEXT_LEDSTRIP_EN_BLINK_ANIM[] = "Animated Blink";
//constexpr char TEXT_BACK[] = "Back";
//LedstripSelectAnimationMenu

View File

@ -339,7 +339,8 @@ void secondsToHMS( const float seconds, uint16_t &h, uint16_t &m, uint16_t &s )
h = t;
}
std::string get_current_driving_time_string() {
std::string get_current_driving_time_string()
{
uint16_t hour{};
uint16_t minute{};
uint16_t second{};
@ -347,3 +348,27 @@ std::string get_current_driving_time_string() {
std::string out = fmt::format("Drive: {:02d}:{:02d}:{:02d}", hour, minute, second);
return out;
}
uint8_t time_to_percent(std::chrono::duration<long, std::ratio<1,1000>> repeat, std::chrono::duration<long, std::ratio<1,1000>> riseTime, std::chrono::duration<long, std::ratio<1,1000>> fullTime, size_t numLeds, bool invert)
{
const auto now = espchrono::millis_clock::now().time_since_epoch() % repeat;
int activated = invert ? numLeds : 0;
if (now <= riseTime)
{
if (invert)
{
activated = numLeds - ((now*numLeds) / riseTime);
}
else
{
activated = (now*numLeds) / riseTime;
}
}
else if (now < riseTime + fullTime)
{
activated = invert ? 0 : numLeds;
}
else
activated = invert ? numLeds : 0;
return activated;
}

View File

@ -61,3 +61,4 @@ float wattToMotorCurrent(float watt);
std::string get_current_uptime_string();
std::string get_current_driving_time_string();
void secondsToHMS( const float seconds, uint16_t &h, uint8_t &m, uint8_t &s );
uint8_t time_to_percent(std::chrono::duration<long, std::ratio<1,1000>> repeat, std::chrono::duration<long, std::ratio<1,1000>> riseTime, std::chrono::duration<long, std::ratio<1,1000>> fullTime, size_t numLeds, bool invert);