add speed sync for led animation

This commit is contained in:
Peter Poetzi
2021-10-01 18:01:32 +02:00
parent 57bbade3c8
commit 9b41663096
2 changed files with 44 additions and 2 deletions

View File

@ -92,5 +92,6 @@ set(BOBBYCAR_BUILDFLAGS
-DPINS_LEDSTRIP=33
-DLEDSTRIP_LENGTH=288
# -DLEDSTRIP_WRONG_DIRECTION
-DLEDSTRIP_ANIMATION_DEFAULT=1
-DLEDSTRIP_ANIMATION_DEFAULT=2
-DLEDS_PER_METER=144
)

View File

@ -1,4 +1,5 @@
#pragma once
#ifdef FEATURE_LEDSTRIP
// 3rdparty lib includes
#include <FastLED.h>
@ -109,10 +110,50 @@ void showBetterRainbow() {
std::fill(std::begin(leds), std::end(leds), CRGB{0, 0, 0});
}
void fill_rainbow_invert_at( struct CRGB * pFirstLED, int numToFill, int invertAtLed,
uint8_t initialhue,
float deltahue )
{
float huecalc = initialhue;
CHSV hsv;
hsv.hue = initialhue;
hsv.val = 255;
hsv.sat = 240;
for( int i = 0; i < numToFill; i++) {
hsv.hue = huecalc;
pFirstLED[i] = hsv;
if(i>invertAtLed){
huecalc -= deltahue;
}else{
huecalc += deltahue;
}
}
}
void showSpeedSyncAnimation() {
if (settings.ledstrip.enableLedAnimation)
{
// Code that shows static animation relative to the ground
#ifdef LEDS_PER_METER
const float leds_per_meter = LEDS_PER_METER;
#else
const float leds_per_meter = 144;
#endif
static auto last_interval = espchrono::millis_clock::now();
auto difference_ms = espchrono::ago(last_interval).count();
static float hue_result = 0;
const float hue_per_led = .1;
const float meter_per_second = avgSpeedKmh / 3.6;
const float leds_per_second = meter_per_second * leds_per_meter;
const float hue_per_second = leds_per_second * hue_per_led;
hue_result += hue_per_second * difference_ms / 1000.f;
fill_rainbow_invert_at(&*std::begin(leds), leds.size(),leds.size()/2, hue_result,-hue_per_led);
last_interval = espchrono::millis_clock::now();
}
else
std::fill(std::begin(leds), std::end(leds), CRGB{0, 0, 0});