diff --git a/Core/Inc/leds.h b/Core/Inc/leds.h index 475dd9e..2e334f1 100644 --- a/Core/Inc/leds.h +++ b/Core/Inc/leds.h @@ -25,16 +25,22 @@ typedef enum { ANIM_TE_STARTUP, ANIM_FT_STARTUP, ANIM_KNIGHT_RIDER, + ANIM_RAINBOW, } LEDAnimation; void led_start_animation(LEDAnimation anim); void led_anim_te_startup(ULONG _); void led_anim_ft_startup(ULONG _); void led_anim_knight_rider(ULONG _); +void led_anim_rainbow(ULONG _); + void led_anim_blinker(uint8_t r, uint8_t g, uint8_t b, uint32_t brightness_steps, uint32_t next_led_steps, uint32_t delay); +void led_hsv_to_rgb(float h, float s, float v, uint8_t *r, uint8_t *g, + uint8_t *b); + #ifdef __cplusplus } #endif diff --git a/Core/Src/leds.c b/Core/Src/leds.c index d08a9bb..b703ea4 100644 --- a/Core/Src/leds.c +++ b/Core/Src/leds.c @@ -63,12 +63,12 @@ void led_all_off() { memset(led_buf, 0, sizeof(led_buf)); } void led_thread_entry(ULONG child_thread_stack_addr) { child_thread_stack = (void *)child_thread_stack_addr; - led_start_animation(ANIM_KNIGHT_RIDER); + led_start_animation(ANIM_RAINBOW); while (1) { tx_thread_sleep(10); if (child_thread.tx_thread_state == TX_COMPLETED) { tx_thread_sleep(20); - led_start_animation(ANIM_FT_STARTUP); + led_start_animation(ANIM_TE_STARTUP); } } } @@ -99,6 +99,9 @@ void led_start_animation(LEDAnimation anim) { case ANIM_KNIGHT_RIDER: animation_entry = led_anim_knight_rider; break; + case ANIM_RAINBOW: + animation_entry = led_anim_rainbow; + break; default: return; } @@ -154,6 +157,22 @@ void led_anim_knight_rider(ULONG _) { } } +void led_anim_rainbow(ULONG _) { + size_t tail = 0; + float offset = 0.0f; + while (1) { + for (size_t i = 0; i < N_LEDS; i++) { + size_t led = (tail + i) % N_LEDS; + uint8_t r, g, b; + float hue = fmodf(offset + i * 360.0f / N_LEDS, 360.0f); + led_hsv_to_rgb(hue, 1, 1, &r, &g, &b); + led_set(led, r, g, b); + } + offset = fmodf(offset + 2.0f, 360.0f); + tx_thread_sleep(1); + } +} + void led_anim_blinker(uint8_t r, uint8_t g, uint8_t b, uint32_t brightness_steps, uint32_t next_led_steps, uint32_t delay) { @@ -196,6 +215,42 @@ void led_anim_blinker(uint8_t r, uint8_t g, uint8_t b, } } +void led_hsv_to_rgb(float h, float s, float v, uint8_t *r, uint8_t *g, + uint8_t *b) { + float c = v * s; + float x = c * (1 - fabs(fmod(h / 60.0, 2) - 1)); + float m = v - c; + float r1, g1, b1; + if (h < 60) { + r1 = c; + g1 = x; + b1 = 0; + } else if (h < 120) { + r1 = x; + g1 = c; + b1 = 0; + } else if (h < 180) { + r1 = 0; + g1 = c; + b1 = x; + } else if (h < 240) { + r1 = 0; + g1 = x; + b1 = c; + } else if (h < 300) { + r1 = x; + g1 = 0; + b1 = c; + } else { + r1 = c; + g1 = 0; + b1 = x; + } + *r = (int)((r1 + m) * 255.0 + 0.5); + *g = (int)((g1 + m) * 255.0 + 0.5); + *b = (int)((b1 + m) * 255.0 + 0.5); +} + void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *handle) { if (handle != hspi) { return;