diff --git a/pico_keys_sdk_import.cmake b/pico_keys_sdk_import.cmake index fa871cf..693ab4b 100644 --- a/pico_keys_sdk_import.cmake +++ b/pico_keys_sdk_import.cmake @@ -253,14 +253,19 @@ set(PICO_KEYS_SOURCES ${PICO_KEYS_SOURCES} ${CMAKE_CURRENT_LIST_DIR}/src/apdu.c ${CMAKE_CURRENT_LIST_DIR}/src/rescue.c ${CMAKE_CURRENT_LIST_DIR}/src/led/led.c - ${CMAKE_CURRENT_LIST_DIR}/src/led/led_cyw43.c - ${CMAKE_CURRENT_LIST_DIR}/src/led/led_pico.c - ${CMAKE_CURRENT_LIST_DIR}/src/led/led_pimoroni.c - ${CMAKE_CURRENT_LIST_DIR}/src/led/led_ws2812.c ) if(ESP_PLATFORM) set(PICO_KEYS_SOURCES ${PICO_KEYS_SOURCES} ${CMAKE_CURRENT_LIST_DIR}/src/led/led_neopixel.c) +else() + if (NOT ENABLE_EMULATION) + set(PICO_KEYS_SOURCES ${PICO_KEYS_SOURCES} + ${CMAKE_CURRENT_LIST_DIR}/src/led/led_cyw43.c + ${CMAKE_CURRENT_LIST_DIR}/src/led/led_pico.c + ${CMAKE_CURRENT_LIST_DIR}/src/led/led_pimoroni.c + ${CMAKE_CURRENT_LIST_DIR}/src/led/led_ws2812.c + ) + endif() endif() ## mbedTLS reports an stringop overflow for cmac.c diff --git a/src/led/led.c b/src/led/led.c index 2d8652e..2311e0d 100644 --- a/src/led/led.c +++ b/src/led/led.c @@ -26,8 +26,7 @@ #include "emulation.h" #endif -extern void led_driver_init(); -extern void led_driver_color(uint8_t, uint32_t, float); +led_driver_t *led_driver = NULL; static uint32_t led_mode = MODE_NOT_MOUNTED; @@ -69,7 +68,7 @@ void led_blinking_task() { // limit the frequency of LED status updates if (board_millis() - last_led_update_ms > 2) { - led_driver_color(led_color, led_brightness, progress); + led_driver->set_color(led_color, led_brightness, progress); last_led_update_ms = board_millis(); } @@ -83,13 +82,57 @@ void led_blinking_task() { void led_off_all() { #ifndef ENABLE_EMULATION - led_driver_color(LED_COLOR_OFF, 0, 0); + led_driver->set_color(LED_COLOR_OFF, 0, 0); #endif } +extern led_driver_t led_driver_pico; +extern led_driver_t led_driver_cyw43; +extern led_driver_t led_driver_ws2812; +extern led_driver_t led_driver_neopixel; +extern led_driver_t led_driver_pimoroni; + void led_init() { #ifndef ENABLE_EMULATION - led_driver_init(); +# // Guess default driver +#ifdef PICO_DEFAULT_LED_PIN + led_driver = &led_driver_pico; +#elif defined(CYW43_WL_GPIO_LED_PIN) + led_driver = &led_driver_cyw43; +#elif defined(PICO_DEFAULT_WS2812_PIN) + led_driver = &led_driver_ws2812; +#elif defined(ESP_PLATFORM) + led_driver = &led_driver_neopixel; +#elif defined(PIMORONI_TINY2040) || defined(PIMORONI_TINY2350) + led_driver = &led_driver_pimoroni; +#endif + if (phy_data.led_driver_present) { + switch (phy_data.led_driver) { +#ifdef ESP_PLATFORM + case PHY_LED_DRIVER_NEOPIXEL: + led_driver = &led_driver_neopixel; + break; +#else + case PHY_LED_DRIVER_PICO: + led_driver = &led_driver_pico; + break; +#ifdef CYW43_WL_GPIO_LED_PIN + case PHY_LED_DRIVER_CYW43: + led_driver = &led_driver_cyw43; + break; +#endif + case PHY_LED_DRIVER_WS2812: + led_driver = &led_driver_ws2812; + break; + case PHY_LED_DRIVER_PIMORONI: + led_driver = &led_driver_pimoroni; + break; +#endif + default: + break; + } + } + led_driver->init(); led_set_mode(MODE_NOT_MOUNTED); #endif } diff --git a/src/led/led.h b/src/led/led.h index 1d4758a..d9440cb 100644 --- a/src/led/led.h +++ b/src/led/led.h @@ -67,4 +67,11 @@ extern void led_blinking_task(); extern void led_off_all(); extern void led_init(); +typedef struct { + void (*init)(); + void (*set_color)(uint8_t color, uint32_t led_brightness, float progress); +} led_driver_t; + +extern led_driver_t *led_driver; + #endif // _LED_H_ diff --git a/src/led/led_cyw43.c b/src/led/led_cyw43.c index 36e4bcc..517df7b 100644 --- a/src/led/led_cyw43.c +++ b/src/led/led_cyw43.c @@ -21,11 +21,11 @@ #include "pico/cyw43_arch.h" -void led_driver_init() { +void led_driver_init_cyw43() { cyw43_arch_init(); } -void led_driver_color(uint8_t color, uint32_t led_brightness, float progress) { +void led_driver_color_cyw43(uint8_t color, uint32_t led_brightness, float progress) { (void)led_brightness; uint8_t gpio = CYW43_WL_GPIO_LED_PIN; if (phy_data.led_gpio_present) { @@ -34,4 +34,9 @@ void led_driver_color(uint8_t color, uint32_t led_brightness, float progress) { cyw43_arch_gpio_put(gpio, progress >= 0.5); } +led_driver_t led_driver_cyw43 = { + .init = led_driver_init_cyw43, + .set_color = led_driver_color_cyw43, +}; + #endif diff --git a/src/led/led_neopixel.c b/src/led/led_neopixel.c index b97228c..c3da4b2 100644 --- a/src/led/led_neopixel.c +++ b/src/led/led_neopixel.c @@ -45,7 +45,7 @@ tNeopixel pixel[] = { #define NEOPIXEL_PIN GPIO_NUM_27 #endif -void led_driver_init() { +void led_driver_init_neopixel() { uint8_t gpio = NEOPIXEL_PIN; if (phy_data.led_gpio_present) { gpio = phy_data.led_gpio; @@ -53,7 +53,7 @@ void led_driver_init() { neopixel = neopixel_Init(1, gpio); } -void led_driver_color(uint8_t color, uint32_t led_brightness, float progress) { +void led_driver_color_neopixel(uint8_t color, uint32_t led_brightness, float progress) { static tNeopixel spx = {.index = 0, .rgb = 0}; if (!(phy_data.opts & PHY_OPT_DIMM)) { progress = progress >= 0.5 ? 1 : 0; @@ -72,4 +72,9 @@ void led_driver_color(uint8_t color, uint32_t led_brightness, float progress) { neopixel_SetPixel(neopixel, &spx, 1); } +led_driver_t led_driver_neopixel = { + .init = led_driver_init_neopixel, + .set_color = led_driver_color_neopixel, +}; + #endif diff --git a/src/led/led_pico.c b/src/led/led_pico.c index c95c5df..fce14b7 100644 --- a/src/led/led_pico.c +++ b/src/led/led_pico.c @@ -17,11 +17,13 @@ #include "pico_keys.h" -#if defined(PICO_DEFAULT_LED_PIN) && !defined(PICO_DEFAULT_WS2812_PIN) && !defined(PIMORONI_TINY2040) && !defined(PIMORONI_TINY2350) - +#ifdef PICO_DEFAULT_LED_PIN uint8_t gpio = PICO_DEFAULT_LED_PIN; +#else +uint8_t gpio = 0; +#endif -void led_driver_init() { +void led_driver_init_pico() { if (phy_data.led_gpio_present) { gpio = phy_data.led_gpio; } @@ -29,9 +31,12 @@ void led_driver_init() { gpio_set_dir(gpio, GPIO_OUT); } -void led_driver_color(uint8_t color, uint32_t led_brightness, float progress) { +void led_driver_color_pico(uint8_t color, uint32_t led_brightness, float progress) { (void)led_brightness; gpio_put(gpio, progress >= 0.5); } -#endif +led_driver_t led_driver_pico = { + .init = led_driver_init_pico, + .set_color = led_driver_color_pico, +}; diff --git a/src/led/led_pimoroni.c b/src/led/led_pimoroni.c index 66f8c92..65c507d 100644 --- a/src/led/led_pimoroni.c +++ b/src/led/led_pimoroni.c @@ -17,8 +17,6 @@ #include "pico_keys.h" -#if defined(PIMORONI_TINY2040) || defined(PIMORONI_TINY2350) - #ifdef PIMORONI_TINY2040 #define LED_R_PIN TINY2040_LED_R_PIN #define LED_G_PIN TINY2040_LED_G_PIN @@ -27,6 +25,10 @@ #define LED_R_PIN TINY2350_LED_R_PIN #define LED_G_PIN TINY2350_LED_G_PIN #define LED_B_PIN TINY2350_LED_B_PIN +#else +#define LED_R_PIN 0 +#define LED_G_PIN 0 +#define LED_B_PIN 0 #endif uint8_t pixel[][3] = { @@ -40,7 +42,7 @@ uint8_t pixel[][3] = { {0, 0, 0} // 7: white }; -void led_driver_init() { +void led_driver_init_pimoroni() { gpio_init(LED_R_PIN); gpio_set_dir(LED_R_PIN, GPIO_OUT); gpio_init(LED_G_PIN); @@ -49,7 +51,7 @@ void led_driver_init() { gpio_set_dir(LED_B_PIN, GPIO_OUT); } -void led_driver_color(uint8_t color, uint32_t led_brightness, float progress) { +void led_driver_color_pimoroni(uint8_t color, uint32_t led_brightness, float progress) { if (progress < 0.5) { color = LED_COLOR_OFF; } @@ -58,4 +60,7 @@ void led_driver_color(uint8_t color, uint32_t led_brightness, float progress) { gpio_put(LED_B_PIN, pixel[color][2]); } -#endif +led_driver_t led_driver_pimoroni = { + .init = led_driver_init_pimoroni, + .set_color = led_driver_color_pimoroni, +}; diff --git a/src/led/led_ws2812.c b/src/led/led_ws2812.c index a5c83e9..b2832c6 100644 --- a/src/led/led_ws2812.c +++ b/src/led/led_ws2812.c @@ -16,9 +16,6 @@ */ #include "pico_keys.h" - -#ifdef PICO_DEFAULT_WS2812_PIN - #include "hardware/pio.h" #include "hardware/clocks.h" @@ -70,11 +67,14 @@ static inline void ws2812_program_init(PIO pio, uint sm, uint offset, uint pin, pio_sm_set_enabled(pio, sm, true); } -void led_driver_init() { +void led_driver_init_ws2812() { PIO pio = pio0; int sm = 0; uint offset = pio_add_program(pio, &ws2812_program); - uint8_t gpio = PICO_DEFAULT_WS2812_PIN; + uint8_t gpio = 0; +#ifdef PICO_DEFAULT_WS2812_PIN + gpio = PICO_DEFAULT_WS2812_PIN; +#endif if (phy_data.led_gpio_present) { gpio = phy_data.led_gpio; } @@ -113,7 +113,7 @@ static inline void ws2812_put_pixel(uint32_t u32_pixel) { pio_sm_put_blocking(pio0, 0, u32_pixel << 8u); } -void led_driver_color(uint8_t color, uint32_t led_brightness, float progress) { +void led_driver_color_ws2812(uint8_t color, uint32_t led_brightness, float progress) { if (!(phy_data.opts & PHY_OPT_DIMM)) { progress = progress >= 0.5 ? 1 : 0; } @@ -128,4 +128,7 @@ void led_driver_color(uint8_t color, uint32_t led_brightness, float progress) { ws2812_put_pixel(urgb_u32(pixel_color.r, pixel_color.g, pixel_color.b)); } -#endif +led_driver_t led_driver_ws2812 = { + .init = led_driver_init_ws2812, + .set_color = led_driver_color_ws2812, +};