fix DRS LED flicker

This commit is contained in:
Leonard Gies 2025-04-18 20:34:54 +02:00
parent 80e38cc036
commit 0913466808
Signed by: l.gies
GPG Key ID: 6F6FB9338EE44F71
3 changed files with 26 additions and 14 deletions

View File

@ -183,6 +183,8 @@ typedef struct {
ConePosition cone_pos[NUM_CONES];
ParamType last_param_confirmed;
uint8_t drs_led_active;
} VehicleState;
extern VehicleState vehicle_state;

View File

@ -88,7 +88,10 @@ void led_thread_entry(ULONG child_thread_stack_addr) {
float speed =
(vehicle_state.speed - LED_SPEED_MIN) / (LED_SPEED_MAX - LED_SPEED_MIN);
float num_leds_exact = speed * N_LEDS;
num_leds_exact = fmin(N_LEDS, fmax(0, num_leds_exact));
// clamp num_leds_exact to maximum of N_LEDS and exclude first LED if DRS is
// active (vehicle_state.drs_led_active = 1)
num_leds_exact =
fmin(N_LEDS, fmax(vehicle_state.drs_led_active, num_leds_exact));
int num_leds = num_leds_exact;
float num_leds_frac = num_leds_exact - num_leds;
@ -98,12 +101,13 @@ void led_thread_entry(ULONG child_thread_stack_addr) {
uint8_t r, g, b;
led_hsv_to_rgb(hue, 1.0f, 1.0f, &r, &g, &b);
// Fully illuminate first n LEDs
for (int i = 0; i < num_leds; i++) {
// Fully illuminate first n LEDs (start at second LED if DRS is active)
for (int i = vehicle_state.drs_led_active; i < num_leds; i++) {
led_set(i, r, g, b);
}
// Partially illuminate n+1th LED
if (num_leds < N_LEDS) {
// Partially illuminate n+1th LED if DRS LED is not active or n+1th led is
// not DRS LED
if (num_leds < N_LEDS && (!vehicle_state.drs_led_active || num_leds > 0)) {
led_hsv_to_rgb(hue, 1.0f, num_leds_frac, &r, &g, &b);
led_set(num_leds, r, g, b);
}
@ -111,6 +115,11 @@ void led_thread_entry(ULONG child_thread_stack_addr) {
for (int i = num_leds + 1; i < N_LEDS; i++) {
led_set(i, 0, 0, 0);
}
// set DRS LED (LED 0)
if (vehicle_state.drs_led_active) {
led_set(0, 0, 0, 255);
}
}
}
@ -310,9 +319,9 @@ void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *handle) {
asm("nop" ::: "memory");
}
HAL_GPIO_WritePin(LED_LE_GPIO_Port, LED_LE_Pin, GPIO_PIN_RESET);
for (size_t i = 0; i < 10; i++) {
asm("nop" ::: "memory");
}
// for (size_t i = 0; i < 10; i++) {
// asm("nop" ::: "memory");
// }
HAL_SPI_Transmit_DMA(hspi, (const uint8_t *)&led_buf[led_buf_idx], 3);
}

View File

@ -9,11 +9,10 @@
#include "stm32h7xx_hal_gpio.h"
#include "tx_api.h"
#include "vehicle.h"
#include "leds.h"
#include "vehicle_state.h"
#define DRS_BUTTON_IDX (6)
#define DRS_PRESS_WAIT_CYCLES (1000)
#define DRS_PRESS_WAIT_CYCLES (10)
static int drs_press_buf_cycles = 0;
void ui_thread_entry(ULONG _) {
@ -50,13 +49,15 @@ void ui_thread_entry(ULONG _) {
tx_event_flags_set(&gui_update_events, GUI_UPDATE_NEXT_SCREEN, TX_OR);
}
// check for DRS button
if (button_states[DRS_BUTTON_IDX] == GPIO_PIN_SET) {
// Set leftmost led to blue to indicate DRS activation
drs_press_buf_cycles = DRS_PRESS_WAIT_CYCLES;
led_set(0, 0, 0, 255);
} if (drs_press_buf_cycles < 0) {
vehicle_state.drs_led_active = 1;
}
if (drs_press_buf_cycles < 0) {
// Assume no longer active, turn off
led_set(0, 0, 0, 0);
vehicle_state.drs_led_active = 0;
} else if (drs_press_buf_cycles >= 0) {
drs_press_buf_cycles--;
}