aboutsummaryrefslogtreecommitdiffstats
path: root/src/led.c
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2023-02-05 17:10:07 +0100
committerLouis Burda <quent.burda@gmail.com>2023-02-05 17:10:07 +0100
commitd19f5ce8b56e1199eb9e20ba83b38f8e3e915437 (patch)
tree4314e72cf02d14dd86895e328a88fb7c86e09106 /src/led.c
parentb50765e6377eda0b6b4aa292379f8b01e48ae5c0 (diff)
downloadsxkbd-d19f5ce8b56e1199eb9e20ba83b38f8e3e915437.tar.gz
sxkbd-d19f5ce8b56e1199eb9e20ba83b38f8e3e915437.zip
Make led blip asynchronous and add sync mechanism to split comms
Diffstat (limited to 'src/led.c')
-rw-r--r--src/led.c61
1 files changed, 49 insertions, 12 deletions
diff --git a/src/led.c b/src/led.c
index c8dbc4b..0216640 100644
--- a/src/led.c
+++ b/src/led.c
@@ -4,7 +4,6 @@
#include "bsp/board.h"
-#define BLINK_DELAY 500
#define ONBOARD_LED_PIN 25
static struct ws2812 onboard_led;
@@ -14,12 +13,19 @@ bool led_reset;
uint32_t led_blink_ms;
uint32_t led_rgb;
+bool led_blip_reset;
+bool led_blip;
+uint32_t led_blip_ms;
+uint32_t led_blip_rgb;
+
void
led_init(void)
{
led_reset = true;
led_mode = LED_ON;
led_rgb = SOFT_WHITE;
+ led_blip = false;
+ led_blip_rgb = HARD_WHITE;
ws2812_init(&onboard_led, pio0, ONBOARD_LED_PIN);
}
@@ -29,18 +35,44 @@ led_task(void)
static uint32_t start_ms = 0;
static bool state = false;
+ if (led_blip_reset) {
+ start_ms = board_millis();
+ led_blip = true;
+ ws2812_put(&onboard_led, led_blip_rgb);
+ state = true;
+ led_blip_reset = false;
+ }
+
+ if (led_blip) {
+ if (state && board_millis() - start_ms > led_blip_ms) {
+ ws2812_put(&onboard_led, led_rgb);
+ state = false;
+ } else if (board_millis() - start_ms > 2 * led_blip_ms) {
+ led_blip = false;
+ led_reset = true;
+ }
+ }
+
+ if (led_blip) return;
+
switch (led_mode) {
case LED_OFF:
- if (led_reset)
- ws2812_put(&onboard_led, 0);
+ if (led_reset) {
+ led_rgb = 0;
+ ws2812_put(&onboard_led, led_rgb);
+ }
break;
case LED_ON:
- if (led_reset)
+ if (led_reset) {
ws2812_put(&onboard_led, led_rgb);
+ }
break;
case LED_BLINK:
- if (led_reset)
+ if (led_reset) {
start_ms = board_millis();
+ state = true;
+ ws2812_put(&onboard_led, led_rgb);
+ }
if (board_millis() - start_ms < led_blink_ms)
return;
@@ -55,13 +87,18 @@ led_task(void)
}
void
-led_blip(uint32_t rgb)
+led_set_mode(int mode, uint32_t rgb, uint32_t ms)
{
- uint32_t start;
+ led_mode = mode;
+ led_rgb = rgb;
+ led_blink_ms = ms;
+}
- ws2812_put(&onboard_led, rgb);
- start = board_millis();
- while (board_millis() < start + BLINK_DELAY)
- tud_task();
- led_task();
+void
+led_start_blip(uint32_t rgb, uint32_t ms)
+{
+ if (led_blip) return;
+ led_blip_rgb = rgb;
+ led_blip_ms = ms;
+ led_blip_reset = true;
}