Skip to content

Commit

Permalink
LEDs: Status bar confirmation animation
Browse files Browse the repository at this point in the history
Feature: LED status bar confirmation animation for config writes and
 konami triggers.
  • Loading branch information
lukash committed Oct 20, 2024
1 parent fc243eb commit 223e5db
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/konami.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static void konami_reset(Konami *konami) {
konami->state = 0;
}

bool konami_check(Konami *konami, const FootpadSensor *fs, float current_time) {
bool konami_check(Konami *konami, Leds *leds, const FootpadSensor *fs, float current_time) {
if (konami->time > 0 && current_time - konami->time > 0.5) {
konami_reset(konami);
return false;
Expand All @@ -39,6 +39,7 @@ bool konami_check(Konami *konami, const FootpadSensor *fs, float current_time) {
++konami->state;
if (konami->state == konami->sequence_size) {
konami_reset(konami);
leds_status_confirm(leds);
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion src/konami.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include "footpad_sensor.h"
#include "leds.h"

typedef struct {
float time;
Expand All @@ -28,4 +29,4 @@ typedef struct {

void konami_init(Konami *konami, const FootpadSensorState *sequence, uint8_t sequence_size);

bool konami_check(Konami *konami, const FootpadSensor *fs, float current_time);
bool konami_check(Konami *konami, Leds *leds, const FootpadSensor *fs, float current_time);
71 changes: 71 additions & 0 deletions src/leds.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ static const uint32_t colors[] = {
#define FOOTPAD_SENSOR_COLOR 0x0000C0FF
#define RED_BAR_COLOR 0x00FF3828
#define BATTERY10_BAR_COLOR 0x00FF5038
#define CONFIRM_COLOR 0x00A040FF

#define CONFIRM_ANIMATION_DURATION 0.8f

static uint32_t color_blend(uint32_t color1, uint32_t color2, float blend) {
if (blend <= 0.0f) {
Expand Down Expand Up @@ -422,6 +425,55 @@ static void anim_disabled(Leds *leds, const LedStrip *strip, float time) {
anim_pulse(leds, strip, &disabled_bar, time / 2.0f, strip->length / 3.0f);
}

static void anim_confirm(Leds *leds, const LedStrip *strip, float time) {
time = fminf(time, 1.0f);

const float blend_time = 0.06f;
float blend = 1.0f;
if (time <= blend_time) {
blend = time / blend_time;
} else if (time >= 1.0f - blend_time) {
blend = (1.0f - time) / blend_time;
}

const float period = 1.0f - blend_time;
const float half_period = period * 0.5f;
time = fminf(time - blend_time, period);

const float pulse_ratio = 1.5f;
float p;
if (time <= half_period) {
p = time / half_period * pulse_ratio;
} else {
p = (period - time) / half_period * pulse_ratio;
}

if (p > 1.0f) {
p = 2.0f - p;
}

p = p * p;

float sides = strip->length * 0.1f;
float center = strip->length * 0.125f;
float length = strip->length * 0.5f - sides - center;
float offset = sides + length * (1.0f - p);
float feather = strip->length * 0.25f;

for (uint8_t i = 0; i < strip->length; ++i) {
float d;
if (i < strip->length * 0.5f) {
d = i - offset + 1.0f;
} else {
d = strip->length - offset - i;
}

float k = clampf(d / feather, 0.0f, 1.0f);
uint32_t color = color_blend(COLOR_BLACK, CONFIRM_COLOR, k);
led_set_color(leds, strip, i, color, strip->brightness, blend);
}
}

static void status_animate(
Leds *leds, const LedStrip *strip, float current_time, float blend, float idle_blend
) {
Expand Down Expand Up @@ -472,6 +524,12 @@ static void status_animate(
anim_fs_state(leds, strip, reverse, blend);
}
}

float conf_prog =
(current_time - leds->confirm_animation_start) * (1.0f / CONFIRM_ANIMATION_DURATION);
if (conf_prog <= 1.0f) {
anim_confirm(leds, strip, conf_prog);
}
}

static void transition_reset(const Leds *leds, TransitionState *trans, LedStrip *strip) {
Expand Down Expand Up @@ -682,6 +740,8 @@ bool leds_init(Leds *leds, CfgHwLeds *hw_cfg, const CfgLeds *cfg, FootpadSensorS
leds->headlights_time = 0.0f;
leds->animation_start = 0;

leds->confirm_animation_start = -10.0f; // Just so it doesn't trigger right after init

leds->headlights_trans.transition = LED_TRANS_FADE;
leds->headlights_trans.split = 1.0f;
leds->dir_trans.transition = LED_TRANS_FADE;
Expand Down Expand Up @@ -1052,6 +1112,17 @@ void leds_update(Leds *leds, const State *state, FootpadSensorState fs_state) {
led_driver_paint(&leds->led_driver, leds->led_data, leds->led_count);
}

void leds_status_confirm(Leds *leds) {
if (!leds->led_data) {
return;
}

float current_time = VESC_IF->system_time();
if (current_time - leds->confirm_animation_start > CONFIRM_ANIMATION_DURATION) {
leds->confirm_animation_start = current_time;
}
}

void leds_destroy(Leds *leds) {
led_driver_destroy(&leds->led_driver);

Expand Down
4 changes: 4 additions & 0 deletions src/leds.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ typedef struct {
float headlights_time;
float animation_start;

float confirm_animation_start;

TransitionState headlights_trans;
TransitionState dir_trans;

Expand All @@ -102,4 +104,6 @@ void leds_configure(Leds *leds, const CfgLeds *cfg);

void leds_update(Leds *leds, const State *state, FootpadSensorState fs_state);

void leds_status_confirm(Leds *leds);

void leds_destroy(Leds *leds);
5 changes: 4 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1369,7 +1369,9 @@ static void refloat_thd(void *arg) {
}

if (d->state.mode != MODE_FLYWHEEL && d->pitch > 75 && d->pitch < 105) {
if (konami_check(&d->flywheel_konami, &d->footpad_sensor, d->current_time)) {
if (konami_check(
&d->flywheel_konami, &d->leds, &d->footpad_sensor, d->current_time
)) {
unsigned char enabled[6] = {0x82, 0, 0, 0, 0, 1};
cmd_flywheel_toggle(d, enabled, 6);
}
Expand Down Expand Up @@ -1484,6 +1486,7 @@ static void write_cfg_to_eeprom(data *d) {
}

beep_alert(d, 1, 0);
leds_status_confirm(&d->leds);
}

static void led_thd(void *arg) {
Expand Down

0 comments on commit 223e5db

Please sign in to comment.