From e20176f3ade5a514d43e95c976a3cdc83b9e15e4 Mon Sep 17 00:00:00 2001 From: greyhash Date: Mon, 18 Apr 2022 13:29:35 +0200 Subject: [PATCH 1/7] New Animations: Snake, Efficiency --- main/ledstrip.cpp | 23 +++++++++++++++++++++++ main/ledstrip.h | 6 +++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/main/ledstrip.cpp b/main/ledstrip.cpp index 81de640..2b66822 100644 --- a/main/ledstrip.cpp +++ b/main/ledstrip.cpp @@ -11,11 +11,13 @@ #include "ota.h" #include "time_bobbycar.h" #include "utils.h" +#include "drivingstatistics.h" using namespace std::chrono_literals; std::vector leds; uint8_t gHue = 0; +uint8_t gLedPosition = 0; uint16_t blinkAnimation = LEDSTRIP_OVERWRITE_NONE; @@ -225,6 +227,8 @@ void showAnimation() case LedstripAnimation::BetterRainbow: showBetterRainbow(); break; case LedstripAnimation::SpeedSync: showSpeedSyncAnimation(); break; case LedstripAnimation::CustomColor: showCustomColor(); break; + case LedstripAnimation::SnakeAnimation: showSnakeAnimation(); break; + case LedstripAnimation::EfficiencyAnimation: showEfficiencyAnimation(); break; default: showDefaultLedstrip(); } } @@ -324,6 +328,25 @@ void showDefaultLedstrip() } } +void showSnakeAnimation() +{ + fadeToBlackBy(&*std::begin(leds), leds.size(), 20); + if(gLedPosition == leds.size()){gLedPosition = 0;} + uint8_t snake_2_pos = floor(leds.size()/2) + gLedPosition; + if(snake_2_pos > leds.size()){snake_2_pos -= leds.size();} + if(gHue == 255){gHue = 0;} + leds[gLedPosition] |= CHSV(gHue, 255, 255); + leds[snake_2_pos] |= CHSV(gHue, 255, 255); + gLedPosition += 1; + gHue += 5; +} + +void showEfficiencyAnimation() +{ + uint16_t color = getEfficiencyClassColor(); + std::fill(std::begin(leds), std::end(leds), CRGB(((((color >> 11) & 0x1F) * 527) + 23) >> 6, ((((color >> 5) & 0x3F) * 259) + 33) >> 6, (((color & 0x1F) * 527) + 23) >> 6)); +} + void showCustomColor() { const auto eighth_length = leds.size() / 8; diff --git a/main/ledstrip.h b/main/ledstrip.h index 1aa7d75..a0d0e54 100644 --- a/main/ledstrip.h +++ b/main/ledstrip.h @@ -20,7 +20,9 @@ DECLARE_BOBBYTYPESAFE_ENUM(OtaAnimationModes, : uint8_t, OtaAnimationModesValues x(DefaultRainbow) \ x(BetterRainbow) \ x(SpeedSync) \ - x(CustomColor) + x(CustomColor) \ + x(SnakeAnimation) \ + x(EfficiencyAnimation) DECLARE_BOBBYTYPESAFE_ENUM(LedstripAnimation, : uint8_t, LedstripAnimationValues) enum Bobbycar_Side @@ -45,6 +47,8 @@ void showAnimation(); void showBetterRainbow(); void showSpeedSyncAnimation(); void showCustomColor(); +void showSnakeAnimation(); +void showEfficiencyAnimation(); void showOtaAnimation(); void initLedStrip(); From 989da449b0493b983d1c3a26d1aa73c2dfc2271f Mon Sep 17 00:00:00 2001 From: greyhash Date: Mon, 18 Apr 2022 14:18:17 +0200 Subject: [PATCH 2/7] Snake Animation: Speedsync and animation multiplier --- main/ledstrip.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/main/ledstrip.cpp b/main/ledstrip.cpp index 2b66822..51d6066 100644 --- a/main/ledstrip.cpp +++ b/main/ledstrip.cpp @@ -17,7 +17,7 @@ using namespace std::chrono_literals; std::vector leds; uint8_t gHue = 0; -uint8_t gLedPosition = 0; +float gLedPosition = 0; uint16_t blinkAnimation = LEDSTRIP_OVERWRITE_NONE; @@ -330,14 +330,18 @@ void showDefaultLedstrip() void showSnakeAnimation() { - fadeToBlackBy(&*std::begin(leds), leds.size(), 20); - if(gLedPosition == leds.size()){gLedPosition = 0;} - uint8_t snake_2_pos = floor(leds.size()/2) + gLedPosition; - if(snake_2_pos > leds.size()){snake_2_pos -= leds.size();} + float leds_per_cycle = 1. / std::max(uint8_t(1), uint8_t(configs.ledstrip.animationMultiplier.value)); + leds_per_cycle = leds_per_cycle * (avgSpeedKmh + 1); + fadeToBlackBy(&*std::begin(leds), leds.size(), floor(20*leds_per_cycle)); + if(gLedPosition >= leds.size()){gLedPosition = 0;} if(gHue == 255){gHue = 0;} - leds[gLedPosition] |= CHSV(gHue, 255, 255); - leds[snake_2_pos] |= CHSV(gHue, 255, 255); - gLedPosition += 1; + for(int i = floor(gLedPosition); i < floor(gLedPosition + leds_per_cycle); i++){ + leds[i] |= CHSV(gHue, 255, 255); + uint8_t snake_2_pos = floor(leds.size()/2) + i; + if(snake_2_pos > leds.size()){snake_2_pos -= leds.size();} + leds[snake_2_pos] |= CHSV(gHue, 255, 255); + } + gLedPosition += leds_per_cycle; gHue += 5; } From 5ad64d81d0582a9ec4b04c633fccf88e5688a706 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Mon, 18 Apr 2022 15:04:05 +0200 Subject: [PATCH 3/7] Cleanups --- main/ledstrip.cpp | 47 ++++++++++++++++++++++++++++++++++------------- main/ledstrip.h | 1 + 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/main/ledstrip.cpp b/main/ledstrip.cpp index 51d6066..0d82946 100644 --- a/main/ledstrip.cpp +++ b/main/ledstrip.cpp @@ -17,7 +17,7 @@ using namespace std::chrono_literals; std::vector leds; uint8_t gHue = 0; -float gLedPosition = 0; +int16_t gLedPosition = 0; uint16_t blinkAnimation = LEDSTRIP_OVERWRITE_NONE; @@ -177,7 +177,7 @@ void updateLedStrip() } } - if (have_disabled_beeper == false && (!(cpputils::is_in(blinkAnimation, LEDSTRIP_OVERWRITE_BLINKLEFT, LEDSTRIP_OVERWRITE_BLINKRIGHT, LEDSTRIP_OVERWRITE_BLINKBOTH)) || !configs.ledstrip.enableBeepWhenBlink.value)) + if (!have_disabled_beeper && (!(cpputils::is_in(blinkAnimation, LEDSTRIP_OVERWRITE_BLINKLEFT, LEDSTRIP_OVERWRITE_BLINKRIGHT, LEDSTRIP_OVERWRITE_BLINKBOTH)) || !configs.ledstrip.enableBeepWhenBlink.value)) { for (Controller &controller : controllers) controller.command.buzzer.freq = 0; @@ -286,12 +286,15 @@ void fill_rainbow_invert_at( struct CRGB * pFirstLED, int numToFill, int invertA hsv.hue = initialhue; hsv.val = 255; hsv.sat = 240; - for( int i = 0; i < numToFill; i++) { + for (int i = 0; i < numToFill; i++) { hsv.hue = huecalc; pFirstLED[i] = hsv; - if(i>invertAtLed){ + if (i>invertAtLed) + { huecalc -= deltahue; - }else{ + } + else + { huecalc += deltahue; } } @@ -330,17 +333,29 @@ void showDefaultLedstrip() void showSnakeAnimation() { - float leds_per_cycle = 1. / std::max(uint8_t(1), uint8_t(configs.ledstrip.animationMultiplier.value)); - leds_per_cycle = leds_per_cycle * (avgSpeedKmh + 1); + const int16_t leds_per_cycle = floor(1. / std::max(1, configs.ledstrip.animationMultiplier.value)) * (avgSpeedKmh + 1); fadeToBlackBy(&*std::begin(leds), leds.size(), floor(20*leds_per_cycle)); - if(gLedPosition >= leds.size()){gLedPosition = 0;} - if(gHue == 255){gHue = 0;} - for(int i = floor(gLedPosition); i < floor(gLedPosition + leds_per_cycle); i++){ + if (gLedPosition >= leds.size()) + { + gLedPosition = 0; + } + + if (gHue == 255) { + gHue = 0; + } + + for(int16_t i = floor(gLedPosition); i < floor(gLedPosition + leds_per_cycle); i++) + { leds[i] |= CHSV(gHue, 255, 255); - uint8_t snake_2_pos = floor(leds.size()/2) + i; - if(snake_2_pos > leds.size()){snake_2_pos -= leds.size();} + uint8_t snake_2_pos = floor(leds.size() / 2) + i; + + if (snake_2_pos > leds.size()) + { + snake_2_pos -= leds.size(); + } leds[snake_2_pos] |= CHSV(gHue, 255, 255); } + gLedPosition += leds_per_cycle; gHue += 5; } @@ -348,7 +363,13 @@ void showSnakeAnimation() void showEfficiencyAnimation() { uint16_t color = getEfficiencyClassColor(); - std::fill(std::begin(leds), std::end(leds), CRGB(((((color >> 11) & 0x1F) * 527) + 23) >> 6, ((((color >> 5) & 0x3F) * 259) + 33) >> 6, (((color & 0x1F) * 527) + 23) >> 6)); + std::fill(std::begin(leds), + std::end(leds), + CRGB( + ((((color >> 11) & 0x1F) * 527) + 23) >> 6, + ((((color >> 5) & 0x3F) * 259) + 33) >> 6, + (((color & 0x1F) * 527) + 23) >> 6) + ); } void showCustomColor() diff --git a/main/ledstrip.h b/main/ledstrip.h index a0d0e54..cf9e328 100644 --- a/main/ledstrip.h +++ b/main/ledstrip.h @@ -39,6 +39,7 @@ enum Bobbycar_Side extern std::vector leds; extern uint8_t gHue; +extern int16_t gLedPosition; extern uint16_t blinkAnimation; From 936408a17a6ee549591c676bf45c36f4c54ebefe Mon Sep 17 00:00:00 2001 From: greyhash Date: Mon, 18 Apr 2022 16:39:47 +0200 Subject: [PATCH 4/7] Fixed snake animation --- main/ledstrip.cpp | 4 ++-- main/ledstrip.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/main/ledstrip.cpp b/main/ledstrip.cpp index 0d82946..905619e 100644 --- a/main/ledstrip.cpp +++ b/main/ledstrip.cpp @@ -17,7 +17,7 @@ using namespace std::chrono_literals; std::vector leds; uint8_t gHue = 0; -int16_t gLedPosition = 0; +float gLedPosition = 0; // yes, this is intendet as a float value! Do NOT change! uint16_t blinkAnimation = LEDSTRIP_OVERWRITE_NONE; @@ -333,7 +333,7 @@ void showDefaultLedstrip() void showSnakeAnimation() { - const int16_t leds_per_cycle = floor(1. / std::max(1, configs.ledstrip.animationMultiplier.value)) * (avgSpeedKmh + 1); + const float leds_per_cycle = (1. / std::max(1, configs.ledstrip.animationMultiplier.value)) * (avgSpeedKmh + 1); // yes, this is intendet as a float value! Do NOT change! fadeToBlackBy(&*std::begin(leds), leds.size(), floor(20*leds_per_cycle)); if (gLedPosition >= leds.size()) { diff --git a/main/ledstrip.h b/main/ledstrip.h index cf9e328..467f484 100644 --- a/main/ledstrip.h +++ b/main/ledstrip.h @@ -39,7 +39,7 @@ enum Bobbycar_Side extern std::vector leds; extern uint8_t gHue; -extern int16_t gLedPosition; +extern float gLedPosition; // yes, this is intendet as a float value! Do NOT change! extern uint16_t blinkAnimation; From eb3e1862b2c07f88af765d941327a34810afa3f1 Mon Sep 17 00:00:00 2001 From: greyhash Date: Mon, 18 Apr 2022 16:45:11 +0200 Subject: [PATCH 5/7] Moved to unsigned (Bugfix) --- main/ledstrip.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main/ledstrip.cpp b/main/ledstrip.cpp index 905619e..c566ef9 100644 --- a/main/ledstrip.cpp +++ b/main/ledstrip.cpp @@ -366,9 +366,9 @@ void showEfficiencyAnimation() std::fill(std::begin(leds), std::end(leds), CRGB( - ((((color >> 11) & 0x1F) * 527) + 23) >> 6, - ((((color >> 5) & 0x3F) * 259) + 33) >> 6, - (((color & 0x1F) * 527) + 23) >> 6) + ((((color >> 11) & 0x1F) * 527u) + 23u) >> 6, + ((((color >> 5) & 0x3F) * 259u) + 33u) >> 6, + (((color & 0x1F) * 527u) + 23u) >> 6) ); } From 48f9e32cd4992b20787e3125775534ffd5117815 Mon Sep 17 00:00:00 2001 From: greyhash Date: Tue, 19 Apr 2022 09:12:40 +0200 Subject: [PATCH 6/7] New fancy SpeedOMeter --- main/ledstrip.cpp | 48 +++++++++++++++++++++++++++++++++++++---------- main/ledstrip.h | 2 +- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/main/ledstrip.cpp b/main/ledstrip.cpp index c566ef9..3a1e34a 100644 --- a/main/ledstrip.cpp +++ b/main/ledstrip.cpp @@ -3,6 +3,7 @@ // 3rdparty lib includes #include #include +#include // local includes #include "globals.h" @@ -11,7 +12,6 @@ #include "ota.h" #include "time_bobbycar.h" #include "utils.h" -#include "drivingstatistics.h" using namespace std::chrono_literals; @@ -228,7 +228,7 @@ void showAnimation() case LedstripAnimation::SpeedSync: showSpeedSyncAnimation(); break; case LedstripAnimation::CustomColor: showCustomColor(); break; case LedstripAnimation::SnakeAnimation: showSnakeAnimation(); break; - case LedstripAnimation::EfficiencyAnimation: showEfficiencyAnimation(); break; + case LedstripAnimation::SpeedOMeter: showEfficiencyAnimation(); break; default: showDefaultLedstrip(); } } @@ -362,14 +362,42 @@ void showSnakeAnimation() void showEfficiencyAnimation() { - uint16_t color = getEfficiencyClassColor(); - std::fill(std::begin(leds), - std::end(leds), - CRGB( - ((((color >> 11) & 0x1F) * 527u) + 23u) >> 6, - ((((color >> 5) & 0x3F) * 259u) + 33u) >> 6, - (((color & 0x1F) * 527u) + 23u) >> 6) - ); + //uint16_t color = getEfficiencyClassColor(); + //std::fill(std::begin(leds), + // std::end(leds), + // CRGB( + // ((((color >> 11) & 0x1F) * 527u) + 23u) >> 6, + // ((((color >> 5) & 0x3F) * 259u) + 33u) >> 6, + // (((color & 0x1F) * 527u) + 23u) >> 6) + // ); + if (const auto avgVoltage = controllers.getAvgVoltage(); avgVoltage) + { + auto watt = sumCurrent * *avgVoltage; + auto w_per_kmh = watt / std::abs(avgSpeedKmh); + CRGB color = 0; + if(isinf(w_per_kmh) || isnan(w_per_kmh)){ + color = 0; + } + else if(w_per_kmh <= -40){ + color = CRGB(255, 0, 255); + } + else if(w_per_kmh < -20){ + color = CRGB(255, 0, cpputils::mapValueClamped(w_per_kmh, -40, -20, 255., 0.)); + } + else if(w_per_kmh < 0){ + color = CRGB(255, cpputils::mapValueClamped(w_per_kmh, -20, 0, 0., 255.), 0); + } + else if(w_per_kmh < 20){ + color = CRGB(cpputils::mapValueClamped(w_per_kmh, 0, 20, 255., 0.), 255, 0); + } + else if(w_per_kmh < 40){ + color = CRGB(0, cpputils::mapValueClamped(w_per_kmh, 20, 40, 255., 0.), cpputils::mapValueClamped(w_per_kmh, 20, 40, 0., 255.)); + } + else{ + color = CRGB(0, 0, 255); + } + std::fill(std::begin(leds), std::end(leds), color); + } } void showCustomColor() diff --git a/main/ledstrip.h b/main/ledstrip.h index 467f484..2b177c7 100644 --- a/main/ledstrip.h +++ b/main/ledstrip.h @@ -22,7 +22,7 @@ DECLARE_BOBBYTYPESAFE_ENUM(OtaAnimationModes, : uint8_t, OtaAnimationModesValues x(SpeedSync) \ x(CustomColor) \ x(SnakeAnimation) \ - x(EfficiencyAnimation) + x(SpeedOMeter) DECLARE_BOBBYTYPESAFE_ENUM(LedstripAnimation, : uint8_t, LedstripAnimationValues) enum Bobbycar_Side From d5e863eb6bd438555b46ddf2b74c3a7240c745db Mon Sep 17 00:00:00 2001 From: greyhash Date: Fri, 22 Apr 2022 22:54:29 +0200 Subject: [PATCH 7/7] Cleanups --- main/ledstrip.cpp | 69 +++++++++++++++++++++++------------------------ main/ledstrip.h | 4 +-- 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/main/ledstrip.cpp b/main/ledstrip.cpp index 3a1e34a..059fd88 100644 --- a/main/ledstrip.cpp +++ b/main/ledstrip.cpp @@ -228,7 +228,7 @@ void showAnimation() case LedstripAnimation::SpeedSync: showSpeedSyncAnimation(); break; case LedstripAnimation::CustomColor: showCustomColor(); break; case LedstripAnimation::SnakeAnimation: showSnakeAnimation(); break; - case LedstripAnimation::SpeedOMeter: showEfficiencyAnimation(); break; + case LedstripAnimation::GasOMeter: showGasOMeterAnimation(); break; default: showDefaultLedstrip(); } } @@ -360,44 +360,43 @@ void showSnakeAnimation() gHue += 5; } -void showEfficiencyAnimation() +void showGasOMeterAnimation() { - //uint16_t color = getEfficiencyClassColor(); - //std::fill(std::begin(leds), - // std::end(leds), - // CRGB( - // ((((color >> 11) & 0x1F) * 527u) + 23u) >> 6, - // ((((color >> 5) & 0x3F) * 259u) + 33u) >> 6, - // (((color & 0x1F) * 527u) + 23u) >> 6) - // ); if (const auto avgVoltage = controllers.getAvgVoltage(); avgVoltage) + { + auto watt = sumCurrent * *avgVoltage; + auto w_per_kmh = watt / std::abs(avgSpeedKmh); + CRGB color = 0; + if (isinf(w_per_kmh) || isnan(w_per_kmh)) { - auto watt = sumCurrent * *avgVoltage; - auto w_per_kmh = watt / std::abs(avgSpeedKmh); - CRGB color = 0; - if(isinf(w_per_kmh) || isnan(w_per_kmh)){ - color = 0; - } - else if(w_per_kmh <= -40){ - color = CRGB(255, 0, 255); - } - else if(w_per_kmh < -20){ - color = CRGB(255, 0, cpputils::mapValueClamped(w_per_kmh, -40, -20, 255., 0.)); - } - else if(w_per_kmh < 0){ - color = CRGB(255, cpputils::mapValueClamped(w_per_kmh, -20, 0, 0., 255.), 0); - } - else if(w_per_kmh < 20){ - color = CRGB(cpputils::mapValueClamped(w_per_kmh, 0, 20, 255., 0.), 255, 0); - } - else if(w_per_kmh < 40){ - color = CRGB(0, cpputils::mapValueClamped(w_per_kmh, 20, 40, 255., 0.), cpputils::mapValueClamped(w_per_kmh, 20, 40, 0., 255.)); - } - else{ - color = CRGB(0, 0, 255); - } - std::fill(std::begin(leds), std::end(leds), color); + color = 0; } + else if (w_per_kmh <= -40) + { + color = CRGB(255, 0, 255); + } + else if (w_per_kmh < -20) + { + color = CRGB(255, 0, cpputils::mapValueClamped(w_per_kmh, -40, -20, 255., 0.)); + } + else if (w_per_kmh < 0) + { + color = CRGB(255, cpputils::mapValueClamped(w_per_kmh, -20, 0, 0., 255.), 0); + } + else if (w_per_kmh < 20) + { + color = CRGB(cpputils::mapValueClamped(w_per_kmh, 0, 20, 255., 0.), 255, 0); + } + else if (w_per_kmh < 40) + { + color = CRGB(0, cpputils::mapValueClamped(w_per_kmh, 20, 40, 255., 0.), cpputils::mapValueClamped(w_per_kmh, 20, 40, 0., 255.)); + } + else + { + color = CRGB(0, 0, 255); + } + std::fill(std::begin(leds), std::end(leds), color); + } } void showCustomColor() diff --git a/main/ledstrip.h b/main/ledstrip.h index 2b177c7..a65f145 100644 --- a/main/ledstrip.h +++ b/main/ledstrip.h @@ -22,7 +22,7 @@ DECLARE_BOBBYTYPESAFE_ENUM(OtaAnimationModes, : uint8_t, OtaAnimationModesValues x(SpeedSync) \ x(CustomColor) \ x(SnakeAnimation) \ - x(SpeedOMeter) + x(GasOMeter) DECLARE_BOBBYTYPESAFE_ENUM(LedstripAnimation, : uint8_t, LedstripAnimationValues) enum Bobbycar_Side @@ -49,7 +49,7 @@ void showBetterRainbow(); void showSpeedSyncAnimation(); void showCustomColor(); void showSnakeAnimation(); -void showEfficiencyAnimation(); +void showGasOMeterAnimation(); void showOtaAnimation(); void initLedStrip();