sxkbd

Firmware for RP2040-based corne split keyboard
git clone https://git.sinitax.com/sinitax/sxkbd
Log | Files | Refs | Submodules | README | LICENSE | sfeed.txt

led.c (1763B)


      1#include "led.h"
      2#include "ws2812.h"
      3#include "util.h"
      4
      5#include "bsp/board.h"
      6
      7#define ONBOARD_LED_PIN 25
      8
      9static struct ws2812 onboard_led;
     10
     11int led_mode;
     12bool led_reset;
     13uint32_t led_blink_ms;
     14uint32_t led_rgb;
     15
     16bool led_blip_reset;
     17bool led_blip;
     18uint32_t led_blip_ms;
     19uint32_t led_blip_rgb;
     20
     21void
     22led_init(void)
     23{
     24	led_reset = true;
     25	led_mode = LED_ON;
     26	led_rgb = SOFT_WHITE;
     27	led_blip = false;
     28	led_blip_rgb = HARD_WHITE;
     29	ws2812_init(&onboard_led, pio0, ONBOARD_LED_PIN);
     30}
     31
     32void
     33led_task(void)
     34{
     35	static uint32_t start_ms = 0;
     36	static bool state = false;
     37
     38	if (led_blip_reset) {
     39		start_ms = board_millis();
     40		led_blip = true;
     41		ws2812_put(&onboard_led, led_blip_rgb);
     42		state = true;
     43		led_blip_reset = false;
     44	}
     45
     46	if (led_blip) {
     47		if (state && board_millis() - start_ms > led_blip_ms) {
     48			ws2812_put(&onboard_led, led_rgb);
     49			state = false;
     50		} else if (board_millis() - start_ms > 2 * led_blip_ms) {
     51			led_blip = false;
     52			led_reset = true;
     53		}
     54	}
     55
     56	if (led_blip) return;
     57
     58	switch (led_mode) {
     59	case LED_OFF:
     60		if (led_reset) {
     61			led_rgb = 0;
     62			ws2812_put(&onboard_led, led_rgb);
     63		}
     64		break;
     65	case LED_ON:
     66		if (led_reset) {
     67			ws2812_put(&onboard_led, led_rgb);
     68		}
     69		break;
     70	case LED_BLINK:
     71		if (led_reset) {
     72			start_ms = board_millis();
     73			state = true;
     74			ws2812_put(&onboard_led, led_rgb);
     75		}
     76
     77		if (board_millis() - start_ms < led_blink_ms)
     78			return;
     79		start_ms += led_blink_ms;
     80
     81		state = !state;
     82		ws2812_put(&onboard_led, state ? led_rgb : 0);
     83		break;
     84	}
     85
     86	led_reset = false;
     87}
     88
     89void
     90led_set_mode(int mode, uint32_t rgb, uint32_t ms)
     91{
     92	led_mode = mode;
     93	led_rgb = rgb;
     94	led_blink_ms = ms;
     95}
     96
     97void
     98led_start_blip(uint32_t rgb, uint32_t ms)
     99{
    100	if (led_blip) return;
    101	led_blip_rgb = rgb;
    102	led_blip_ms = ms;
    103	led_blip_reset = true;
    104}