Skip to content

Commit 21fb241

Browse files
committed
Fix LEDs On / Headlights On runtime toggles overwriting config
Fix: Fix toggling LEDs or Headlights On via the UI ok konami being stored in the config under some circumstances > The toggles are now only temprorary and once toggled, writing the config won't override the current state either.
1 parent c7e0153 commit 21fb241

5 files changed

Lines changed: 73 additions & 25 deletions

File tree

src/lcm.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,20 @@ void lcm_init(LcmData *lcm, CfgHwLeds *hw_cfg) {
3434
lcm->lights_off_when_lifted = true;
3535
}
3636

37-
void lcm_configure(LcmData *lcm, const CfgLeds *cfg) {
37+
void lcm_configure(LcmData *lcm, const Leds *leds) {
3838
if (!lcm->enabled) {
3939
return;
4040
}
4141

42-
if (!cfg->on) {
42+
const CfgLeds *cfg = leds->cfg;
43+
const LedsRuntimeStatus *status = leds_get_runtime_status(leds);
44+
45+
if (!status->enabled) {
4346
lcm->brightness = 0.0f;
4447
lcm->brightness_idle = 0.0f;
4548
lcm->status_brightness = 0.0f;
4649
} else {
47-
if (cfg->headlights_on) {
50+
if (status->headlights_enabled) {
4851
lcm->brightness = cfg->headlights.brightness * 100;
4952
lcm->status_brightness = cfg->status.brightness_headlights_on * 100;
5053
} else {

src/lcm.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#pragma once
1919

2020
#include "footpad_sensor.h"
21+
#include "leds.h"
2122
#include "motor_data.h"
2223
#include "state.h"
2324

@@ -50,7 +51,7 @@ typedef struct {
5051

5152
void lcm_init(LcmData *lcm, CfgHwLeds *hw_cfg);
5253

53-
void lcm_configure(LcmData *lcm, const CfgLeds *cfg);
54+
void lcm_configure(LcmData *lcm, const Leds *leds);
5455

5556
/**
5657
* Poll request from LCM with any data that need to be passed to the package.

src/leds.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ static void reset_led_bars(
704704

705705
static bool headlights_should_be_on(const Leds *leds) {
706706
return (leds->state.state == STATE_RUNNING && leds->state.mode != MODE_FLYWHEEL) &&
707-
leds->cfg->headlights_on;
707+
leds->runtime_status.headlights_enabled;
708708
}
709709

710710
static const LedBar *target_bar(const Leds *leds, bool flip) {
@@ -767,9 +767,14 @@ void leds_init(Leds *leds) {
767767
leds->status_on_front_idle_time = 0.0f;
768768
leds->board_is_upright = false;
769769

770-
leds->split_distance = 0.0f;
770+
leds->runtime_status.enabled = false;
771+
leds->runtime_status.headlights_enabled = false;
772+
leds->runtime_status_overriden.enabled = false;
773+
leds->runtime_status_overriden.headlights_enabled = false;
774+
771775
leds->headlights_on = false;
772776
leds->direction_forward = true;
777+
leds->split_distance = 0.0f;
773778
leds->headlights_time = 0.0f;
774779
leds->animation_start = 0;
775780

@@ -872,11 +877,32 @@ void leds_configure(Leds *leds, const CfgLeds *cfg) {
872877
leds->headlights_trans.transition = cfg->headlights_transition;
873878
leds->dir_trans.transition = cfg->direction_transition;
874879

880+
if (!leds->runtime_status_overriden.enabled) {
881+
leds->runtime_status.enabled = cfg->on;
882+
}
883+
if (!leds->runtime_status_overriden.headlights_enabled) {
884+
leds->runtime_status.headlights_enabled = cfg->headlights_on;
885+
}
886+
875887
float current_time = VESC_IF->system_time();
876888
leds->status_idle_time = current_time;
877889
leds->status_on_front_idle_time = current_time;
878890
}
879891

892+
const LedsRuntimeStatus *leds_get_runtime_status(const Leds *leds) {
893+
return &leds->runtime_status;
894+
}
895+
896+
void leds_set_enabled(Leds *leds, bool value) {
897+
leds->runtime_status.enabled = value;
898+
leds->runtime_status_overriden.enabled = true;
899+
}
900+
901+
void leds_set_headlights_enabled(Leds *leds, bool value) {
902+
leds->runtime_status.headlights_enabled = value;
903+
leds->runtime_status_overriden.headlights_enabled = true;
904+
}
905+
880906
void leds_update(Leds *leds, const State *state, FootpadSensorState fs_state) {
881907
if (!leds->led_data) {
882908
return;
@@ -891,7 +917,7 @@ void leds_update(Leds *leds, const State *state, FootpadSensorState fs_state) {
891917
return;
892918
}
893919

894-
if (leds->cfg->on) {
920+
if (leds->runtime_status.enabled) {
895921
if (leds->on_off_fade == 0.0f) {
896922
full_animation_reset(leds, current_time);
897923
}
@@ -951,7 +977,7 @@ void leds_update(Leds *leds, const State *state, FootpadSensorState fs_state) {
951977

952978
// status brightness
953979
float status_brightness = leds->cfg->status.brightness_headlights_off;
954-
if (leds->cfg->headlights_on) {
980+
if (leds->runtime_status.headlights_enabled) {
955981
status_brightness = leds->cfg->status.brightness_headlights_on;
956982
}
957983
if (leds->status_idle_blend > 0.0f) {

src/leds.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ typedef struct {
3030
float split;
3131
} TransitionState;
3232

33+
typedef struct {
34+
bool enabled;
35+
bool headlights_enabled;
36+
} LedsRuntimeStatus;
37+
3338
typedef struct {
3439
LedStrip status_strip;
3540
LedStrip front_strip;
@@ -57,9 +62,13 @@ typedef struct {
5762
float status_on_front_idle_time;
5863
bool board_is_upright;
5964

60-
float split_distance;
65+
LedsRuntimeStatus runtime_status;
66+
// represents the state of a given flag being overriden at runtime
67+
LedsRuntimeStatus runtime_status_overriden;
68+
6169
bool headlights_on;
6270
bool direction_forward;
71+
float split_distance;
6372
float headlights_time;
6473
float animation_start;
6574

@@ -86,6 +95,12 @@ void leds_setup(Leds *leds, CfgHwLeds *hw_cfg, const CfgLeds *cfg);
8695

8796
void leds_configure(Leds *leds, const CfgLeds *cfg);
8897

98+
const LedsRuntimeStatus *leds_get_runtime_status(const Leds *leds);
99+
100+
void leds_set_enabled(Leds *leds, bool value);
101+
102+
void leds_set_headlights_enabled(Leds *leds, bool value);
103+
89104
void leds_update(Leds *leds, const State *state, FootpadSensorState fs_state);
90105

91106
void leds_status_confirm(Leds *leds);

src/main.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,6 @@ static void reconfigure(Data *d) {
178178
static void configure(Data *d) {
179179
state_set_disabled(&d->state, d->float_conf.disabled);
180180

181-
lcm_configure(&d->lcm, &d->float_conf.leds);
182-
183181
// Loop time in microseconds
184182
d->loop_time_us = 1e6 / d->float_conf.hertz;
185183

@@ -214,16 +212,18 @@ static void configure(Data *d) {
214212

215213
reconfigure(d);
216214

215+
lcm_configure(&d->lcm, &d->leds);
216+
217217
if (d->state.state == STATE_DISABLED) {
218218
beep_alert(d, 3, false);
219219
} else if (d->state.state != STATE_STARTUP) {
220220
beep_alert(d, 1, false);
221221
}
222222
}
223223

224-
static void leds_headlights_switch(CfgLeds *cfg_leds, LcmData *lcm, bool headlights_on) {
225-
cfg_leds->headlights_on = headlights_on;
226-
lcm_configure(lcm, cfg_leds);
224+
static void leds_headlights_switch(Leds *leds, LcmData *lcm, bool headlights_on) {
225+
leds_set_headlights_enabled(leds, headlights_on);
226+
lcm_configure(lcm, leds);
227227
}
228228

229229
static void reset_runtime_vars(Data *d) {
@@ -964,14 +964,15 @@ static void refloat_thd(void *arg) {
964964
}
965965

966966
if (d->float_conf.hardware.leds.mode != LED_MODE_OFF) {
967-
if (!d->leds.cfg->headlights_on &&
967+
const LedsRuntimeStatus *led_status = leds_get_runtime_status(&d->leds);
968+
if (!led_status->headlights_enabled &&
968969
konami_check(&d->headlights_on_konami, &d->leds, &d->footpad, &d->time)) {
969-
leds_headlights_switch(&d->float_conf.leds, &d->lcm, true);
970+
leds_headlights_switch(&d->leds, &d->lcm, true);
970971
}
971972

972-
if (d->leds.cfg->headlights_on &&
973+
if (led_status->headlights_enabled &&
973974
konami_check(&d->headlights_off_konami, &d->leds, &d->footpad, &d->time)) {
974-
leds_headlights_switch(&d->float_conf.leds, &d->lcm, false);
975+
leds_headlights_switch(&d->leds, &d->lcm, false);
975976
}
976977
}
977978

@@ -2028,7 +2029,7 @@ static void cmd_alerts_control(AlertTracker *at, uint8_t *buf, size_t len) {
20282029
}
20292030
}
20302031

2031-
static void lights_control_request(CfgLeds *leds, uint8_t *buffer, size_t len, LcmData *lcm) {
2032+
static void lights_control_request(Leds *leds, uint8_t *buffer, size_t len, LcmData *lcm) {
20322033
if (len < 5) {
20332034
return;
20342035
}
@@ -2040,25 +2041,27 @@ static void lights_control_request(CfgLeds *leds, uint8_t *buffer, size_t len, L
20402041
uint8_t value = buffer[ind++];
20412042

20422043
if (mask & 0x1) {
2043-
leds->on = value & 0x1;
2044+
leds_set_enabled(leds, value & 0x1);
20442045
}
20452046

20462047
if (mask & 0x2) {
2047-
leds->headlights_on = value & 0x2;
2048+
leds_set_headlights_enabled(leds, value & 0x2);
20482049
}
20492050

20502051
lcm_configure(lcm, leds);
20512052
}
20522053
}
20532054

2054-
static void lights_control_response(const CfgLeds *leds) {
2055+
static void lights_control_response(const Leds *leds) {
20552056
static const int bufsize = 3;
20562057
uint8_t buffer[bufsize];
20572058
int32_t ind = 0;
20582059

20592060
buffer[ind++] = 101; // Package ID
20602061
buffer[ind++] = COMMAND_LIGHTS_CONTROL;
2061-
buffer[ind++] = leds->headlights_on << 1 | leds->on;
2062+
2063+
const LedsRuntimeStatus *status = leds_get_runtime_status(leds);
2064+
buffer[ind++] = status->headlights_enabled << 1 | status->enabled;
20622065

20632066
SEND_APP_DATA(buffer, bufsize, ind);
20642067
}
@@ -2273,8 +2276,8 @@ static void on_command_received(unsigned char *buffer, unsigned int len) {
22732276
return;
22742277
}
22752278
case COMMAND_LIGHTS_CONTROL: {
2276-
lights_control_request(&d->float_conf.leds, &buffer[2], len - 2, &d->lcm);
2277-
lights_control_response(&d->float_conf.leds);
2279+
lights_control_request(&d->leds, &buffer[2], len - 2, &d->lcm);
2280+
lights_control_response(&d->leds);
22782281
return;
22792282
}
22802283
case COMMAND_DATA_RECORD_REQUEST: {

0 commit comments

Comments
 (0)