From ea6303de813a1b87a9fa0302db1cdab44acdb98b Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 3 Sep 2024 18:07:37 +0200 Subject: [PATCH] Add new led module to use colors whenever is possible. Signed-off-by: Pol Henarejos --- pico_keys_sdk_import.cmake | 29 ++++++- src/CMakeLists.txt | 2 +- src/led/led.c | 78 +++++++++++++++++ src/led/led.h | 60 +++++++++++++ src/led/led_cyw43.c | 32 +++++++ src/led/led_neopixel.c | 52 ++++++++++++ src/led/led_pico.c | 31 +++++++ src/led/led_pimoroni.c | 58 +++++++++++++ src/led/led_ws2812.c | 86 +++++++++++++++++++ src/main.c | 170 +------------------------------------ src/pico_keys.h | 15 +--- 11 files changed, 426 insertions(+), 187 deletions(-) create mode 100644 src/led/led.c create mode 100644 src/led/led.h create mode 100644 src/led/led_cyw43.c create mode 100644 src/led/led_neopixel.c create mode 100644 src/led/led_pico.c create mode 100644 src/led/led_pimoroni.c create mode 100644 src/led/led_ws2812.c diff --git a/pico_keys_sdk_import.cmake b/pico_keys_sdk_import.cmake index 00f5ddb..7630579 100644 --- a/pico_keys_sdk_import.cmake +++ b/pico_keys_sdk_import.cmake @@ -26,12 +26,13 @@ elseif (VIDPID STREQUAL "NitroFIDO2") set(USB_VID 0x20A0) set(USB_PID 0x42B1) elseif (VIDPID STREQUAL "NitroStart") - set(USB_VID 0x20A0) + set(USB_VID 0x20A0) set(USB_PID 0x4211) -elseif (VIDPID STREQUAL "NitroPro") + elseif (VIDPID STREQUAL "NitroPro") set(USB_VID 0x20A0) - set(USB_PID 0x4108) -elseif (VIDPID STREQUAL "Nitro3") + set(USB_PID 0x4108) + + elseif (VIDPID STREQUAL "Nitro3") set(USB_VID 0x20A0) set(USB_PID 0x42B2) elseif (VIDPID STREQUAL "Yubikey5") @@ -157,7 +158,26 @@ set(SOURCES ${SOURCES} ${CMAKE_CURRENT_LIST_DIR}/src/crypto_utils.c ${CMAKE_CURRENT_LIST_DIR}/src/asn1.c ${CMAKE_CURRENT_LIST_DIR}/src/apdu.c + ${CMAKE_CURRENT_LIST_DIR}/src/led/led.c ) +if (PICO_BOARD STREQUAL "pico_w") + set(SOURCES ${SOURCES} + ${CMAKE_CURRENT_LIST_DIR}/src/led/led_cyw43.c + ) +elseif (PICO_BOARD MATCHES "^pimoroni") + set(SOURCES ${SOURCES} + ${CMAKE_CURRENT_LIST_DIR}/src/led/led_pimoroni.c + ) +elseif (ESP_PLATFORM) + set(SOURCES ${SOURCES} + ${CMAKE_CURRENT_LIST_DIR}/src/led/led_neopixel.c + ) +else() + set(SOURCES ${SOURCES} + ${CMAKE_CURRENT_LIST_DIR}/src/led/led_ws2812.c + ${CMAKE_CURRENT_LIST_DIR}/src/led/led_pico.c + ) +endif() ## mbedTLS reports an stringop overflow for cmac.c if (NOT ENABLE_EMULATION AND NOT APPLE) set_source_files_properties( @@ -171,6 +191,7 @@ set(INCLUDES ${INCLUDES} ${CMAKE_CURRENT_LIST_DIR}/src/usb ${CMAKE_CURRENT_LIST_DIR}/src/fs ${CMAKE_CURRENT_LIST_DIR}/src/rng + ${CMAKE_CURRENT_LIST_DIR}/src/led ${CMAKE_CURRENT_LIST_DIR}/mbedtls/include ${CMAKE_CURRENT_LIST_DIR}/mbedtls/library ) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7a370a4..7ace948 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( SRCS ${INTERNAL_SOURCES} - INCLUDE_DIRS . fs rng usb ../mbedtls/include ../tinycbor/src + INCLUDE_DIRS . fs rng usb led ../mbedtls/include ../tinycbor/src REQUIRES bootloader_support esp_partition esp_tinyusb efuse ) diff --git a/src/led/led.c b/src/led/led.c new file mode 100644 index 0000000..d20ecad --- /dev/null +++ b/src/led/led.c @@ -0,0 +1,78 @@ + /* + * This file is part of the Pico Keys SDK distribution (https://github.com/polhenarejos/pico-keys-sdk). + * Copyright (c) 2022 Pol Henarejos. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include "pico_keys.h" +#ifdef PICO_PLATFORM +#include "bsp/board.h" +#elif defined(ESP_PLATFORM) +#include "esp_compat.h" +#elif defined(ENABLE_EMULATION) +#include "emulation.h" +#endif + +extern void led_driver_init(); +extern void led_driver_color(uint8_t); + +static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; + +void led_set_blink(uint32_t mode) { + blink_interval_ms = mode; +} + +void led_blinking_task() { +#ifndef ENABLE_EMULATION + static uint32_t start_ms = 0; + static uint8_t led_state = false; + uint8_t state = led_state; +#ifdef PICO_DEFAULT_LED_PIN_INVERTED + state = !state; +#endif + uint32_t led_color = (blink_interval_ms & LED_COLOR_MASK) >> LED_COLOR_SHIFT; + uint32_t led_off = (blink_interval_ms & LED_OFF_MASK) >> LED_OFF_SHIFT; + uint32_t led_on = (blink_interval_ms & LED_ON_MASK) >> LED_ON_SHIFT; + uint32_t led_interval = led_state ? led_on : led_off; + + // Blink every interval ms + if (board_millis() - start_ms < led_interval) { + return; // not enough time + } + start_ms += led_interval; + + if (state == false) { + led_driver_color(LED_COLOR_OFF); + } + else { + led_driver_color(led_color); + } + led_state ^= 1; // toggle +#endif +} + +void led_off_all() { +#ifndef ENABLE_EMULATION + led_driver_color(LED_COLOR_OFF); +#endif +} + +void led_init() { +#ifndef ENABLE_EMULATION + led_driver_init(); + led_set_blink(BLINK_NOT_MOUNTED); +#endif +} diff --git a/src/led/led.h b/src/led/led.h new file mode 100644 index 0000000..2d96871 --- /dev/null +++ b/src/led/led.h @@ -0,0 +1,60 @@ +/* + * This file is part of the Pico Keys SDK distribution (https://github.com/polhenarejos/pico-keys-sdk). + * Copyright (c) 2022 Pol Henarejos. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef _LED_H_ +#define _LED_H_ + +#include + +enum { + LED_COLOR_OFF = 0, + LED_COLOR_RED, + LED_COLOR_GREEN, + LED_COLOR_BLUE, + LED_COLOR_YELLOW, + LED_COLOR_MAGENTA, + LED_COLOR_CYAN, + LED_COLOR_WHITE +}; + +#define LED_OFF_BITS 14 +#define LED_OFF_SHIFT 0 +#define LED_OFF_MASK (((1 << LED_OFF_BITS) - 1) << LED_OFF_SHIFT) +#define LED_ON_BITS 14 +#define LED_ON_SHIFT LED_OFF_BITS +#define LED_ON_MASK (((1 << LED_ON_BITS) - 1) << LED_ON_SHIFT) +#define LED_COLOR_BITS 3 +#define LED_COLOR_SHIFT (LED_ON_BITS + LED_OFF_BITS) +#define LED_COLOR_MASK (((1 << LED_COLOR_BITS) - 1) << LED_COLOR_SHIFT) + +enum { + BLINK_NOT_MOUNTED = (LED_COLOR_RED << LED_COLOR_SHIFT) | (250 << LED_ON_SHIFT) | (250 << LED_OFF_SHIFT), + BLINK_MOUNTED = (LED_COLOR_GREEN << LED_COLOR_SHIFT) | (250 << LED_ON_SHIFT) | (250 << LED_OFF_SHIFT), + BLINK_SUSPENDED = (LED_COLOR_BLUE << LED_COLOR_SHIFT) | (500 << LED_ON_SHIFT) | (1000 << LED_OFF_SHIFT), + BLINK_PROCESSING = (LED_COLOR_GREEN << LED_COLOR_SHIFT) | (50 << LED_ON_SHIFT) | (50 << LED_OFF_SHIFT), + BLINK_BUTTON = (LED_COLOR_YELLOW << LED_COLOR_SHIFT) | (1000 << LED_ON_SHIFT) | (100 << LED_OFF_SHIFT), + + BLINK_ALWAYS_ON = UINT32_MAX, + BLINK_ALWAYS_OFF = 0 +}; + +extern void led_set_blink(uint32_t mode); +extern void led_blinking_task(); +extern void led_off_all(); +extern void led_init(); + +#endif // _LED_H_ diff --git a/src/led/led_cyw43.c b/src/led/led_cyw43.c new file mode 100644 index 0000000..7023bb0 --- /dev/null +++ b/src/led/led_cyw43.c @@ -0,0 +1,32 @@ +/* + * This file is part of the Pico Keys SDK distribution (https://github.com/polhenarejos/pico-keys-sdk). + * Copyright (c) 2022 Pol Henarejos. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "pico_keys.h" + +#ifdef CYW43_WL_GPIO_LED_PIN + +#include "pico/cyw43_arch.h" + +void led_driver_init() { + cyw43_arch_init(); +} + +void led_driver_color(uint8_t color) { + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, color != LED_COLOR_OFF); +} + +#endif diff --git a/src/led/led_neopixel.c b/src/led/led_neopixel.c new file mode 100644 index 0000000..c865b9e --- /dev/null +++ b/src/led/led_neopixel.c @@ -0,0 +1,52 @@ +/* + * This file is part of the Pico Keys SDK distribution (https://github.com/polhenarejos/pico-keys-sdk). + * Copyright (c) 2022 Pol Henarejos. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "pico_keys.h" + +#ifdef ESP_PLATFORM + +#include "driver/gpio.h" +#include "neopixel.h" + +tNeopixelContext neopixel = NULL; + +tNeopixel pixel[] = { + { 0, NP_RGB(0, 0, 0) }, /* off */ + { 0, NP_RGB(255, 0, 0) }, /* red */ + { 0, NP_RGB(0, 255, 0) }, /* green */ + { 0, NP_RGB(0, 0, 255) }, /* blue */ + { 0, NP_RGB(255, 255, 0) }, /* yellow */ + { 0, NP_RGB(255, 0, 255) }, /* magenta */ + { 0, NP_RGB(0, 255, 255) }, /* cyan */ + { 0, NP_RGB(255, 255, 255) }, /* white */ +}; + +void led_driver_init() { + uint8_t gpio = GPIO_NUM_48; + if (file_has_data(ef_phy)) { + if (file_read_uint8_offset(ef_phy, PHY_OPTS) & PHY_OPT_GPIO) { + gpio = file_get_data(ef_phy)[PHY_LED_GPIO]; + } + } + neopixel = neopixel_Init(1, gpio); +} + +void led_driver_color(int color) { + neopixel_SetPixel(neopixel, &pixel[color], 1); +} + +#endif diff --git a/src/led/led_pico.c b/src/led/led_pico.c new file mode 100644 index 0000000..994f0a8 --- /dev/null +++ b/src/led/led_pico.c @@ -0,0 +1,31 @@ +/* + * This file is part of the Pico Keys SDK distribution (https://github.com/polhenarejos/pico-keys-sdk). + * Copyright (c) 2022 Pol Henarejos. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "pico_keys.h" + +#ifdef PICO_DEFAULT_LED_PIN + +void led_driver_init() { + gpio_init(PICO_DEFAULT_LED_PIN); + gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); +} + +void led_driver_color(uint8_t color) { + gpio_put(PICO_DEFAULT_LED_PIN, color != LED_COLOR_OFF); +} + +#endif diff --git a/src/led/led_pimoroni.c b/src/led/led_pimoroni.c new file mode 100644 index 0000000..ffa87d3 --- /dev/null +++ b/src/led/led_pimoroni.c @@ -0,0 +1,58 @@ +/* + * This file is part of the Pico Keys SDK distribution (https://github.com/polhenarejos/pico-keys-sdk). + * Copyright (c) 2022 Pol Henarejos. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#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 +#define LED_B_PIN TINY2040_LED_B_PIN +#elif defined(PIMORONI_TINY2350) +#define LED_R_PIN TINY2350_LED_R_PIN +#define LED_G_PIN TINY2350_LED_G_PIN +#define LED_B_PIN TINY2350_LED_B_PIN +#endif + +uint8_t pixel[][3] = { + {1, 1, 1}, // 0: off + {0, 1, 1}, // 1: red + {1, 0, 1}, // 2: green + {1, 1, 0}, // 3: blue + {0, 0, 1}, // 4: yellow + {0, 1, 0}, // 5: magenta + {1, 0, 0}, // 6: cyan + {0, 0, 0} // 7: white +}; + +void led_driver_init() { + gpio_init(LED_R_PIN); + gpio_set_dir(LED_R_PIN, GPIO_OUT); + gpio_init(LED_G_PIN); + gpio_set_dir(LED_G_PIN, GPIO_OUT); + gpio_init(LED_B_PIN); + gpio_set_dir(LED_B_PIN, GPIO_OUT); +} + +void led_driver_color(uint8_t color) { + gpio_put(LED_R_PIN, pixel[color][0]); + gpio_put(LED_G_PIN, pixel[color][1]); + gpio_put(LED_B_PIN, pixel[color][2]); +} + +#endif diff --git a/src/led/led_ws2812.c b/src/led/led_ws2812.c new file mode 100644 index 0000000..80042b4 --- /dev/null +++ b/src/led/led_ws2812.c @@ -0,0 +1,86 @@ +/* + * This file is part of the Pico Keys SDK distribution (https://github.com/polhenarejos/pico-keys-sdk). + * Copyright (c) 2022 Pol Henarejos. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "pico_keys.h" + +#ifdef PICO_DEFAULT_WS2812_PIN + +#include "hardware/pio.h" +#include "hardware/clocks.h" +#define ws2812_wrap_target 0 +#define ws2812_wrap 3 +#define ws2812_T1 2 +#define ws2812_T2 5 +#define ws2812_T3 3 +static const uint16_t ws2812_program_instructions[] = { + // .wrap_target + 0x6221, // 0: out x, 1 side 0 [2] + 0x1123, // 1: jmp !x, 3 side 1 [1] + 0x1400, // 2: jmp 0 side 1 [4] + 0xa442, // 3: nop side 0 [4] + // .wrap +}; +static const struct pio_program ws2812_program = { + .instructions = ws2812_program_instructions, + .length = 4, + .origin = -1, +}; + +static inline pio_sm_config ws2812_program_get_default_config(uint offset) { + pio_sm_config c = pio_get_default_sm_config(); + sm_config_set_wrap(&c, offset + ws2812_wrap_target, offset + ws2812_wrap); + sm_config_set_sideset(&c, 1, false, false); + return c; +} +static inline void ws2812_program_init(PIO pio, uint sm, uint offset, uint pin, float freq, bool rgbw) { + pio_gpio_init(pio, pin); + pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true); + pio_sm_config c = ws2812_program_get_default_config(offset); + sm_config_set_sideset_pins(&c, pin); + sm_config_set_out_shift(&c, false, true, rgbw ? 32 : 24); + sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX); + int cycles_per_bit = ws2812_T1 + ws2812_T2 + ws2812_T3; + float div = clock_get_hz(clk_sys) / (freq * cycles_per_bit); + sm_config_set_clkdiv(&c, div); + pio_sm_init(pio, sm, offset, &c); + pio_sm_set_enabled(pio, sm, true); +} + +void led_driver_init() { + PIO pio = pio0; + int sm = 0; + uint offset = pio_add_program(pio, &ws2812_program); + + ws2812_program_init(pio, sm, offset, PICO_DEFAULT_WS2812_PIN, 800000, true); +} + +uint32_t pixel[] = { + 0x00000000, // 0: off + 0x00ff0000, // 1: red + 0xff000000, // 2: green + 0x0000ff00, // 3: blue + 0xffff0000, // 4: yellow + 0x00ffff00, // 5: magenta + 0xff00ff00, // 6: cyan + 0xffffff00 // 7: white +}; + +void led_driver_color(uint8_t color) { + pio_sm_put_blocking(pio0, 0, pixel[color]); +} + +#endif diff --git a/src/main.c b/src/main.c index 35c4d89..7e4586e 100644 --- a/src/main.c +++ b/src/main.c @@ -21,9 +21,6 @@ // Pico #if defined(ENABLE_EMULATION) -#if !defined(_MSC_VER) -#include -#endif #include "emulation.h" #elif defined(ESP_PLATFORM) #include "tusb.h" @@ -46,57 +43,6 @@ #include "pico_keys.h" #include "apdu.h" #include "usb.h" -#ifdef CYW43_WL_GPIO_LED_PIN -#include "pico/cyw43_arch.h" -#endif -#ifdef PICO_DEFAULT_WS2812_PIN -#include "hardware/pio.h" -#include "hardware/clocks.h" -#define ws2812_wrap_target 0 -#define ws2812_wrap 3 -#define ws2812_T1 2 -#define ws2812_T2 5 -#define ws2812_T3 3 -static const uint16_t ws2812_program_instructions[] = { - // .wrap_target - 0x6221, // 0: out x, 1 side 0 [2] - 0x1123, // 1: jmp !x, 3 side 1 [1] - 0x1400, // 2: jmp 0 side 1 [4] - 0xa442, // 3: nop side 0 [4] - // .wrap -}; -static const struct pio_program ws2812_program = { - .instructions = ws2812_program_instructions, - .length = 4, - .origin = -1, -}; - -static inline pio_sm_config ws2812_program_get_default_config(uint offset) { - pio_sm_config c = pio_get_default_sm_config(); - sm_config_set_wrap(&c, offset + ws2812_wrap_target, offset + ws2812_wrap); - sm_config_set_sideset(&c, 1, false, false); - return c; -} -static inline void ws2812_program_init(PIO pio, - uint sm, - uint offset, - uint pin, - float freq, - bool rgbw) { - pio_gpio_init(pio, pin); - pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true); - pio_sm_config c = ws2812_program_get_default_config(offset); - sm_config_set_sideset_pins(&c, pin); - sm_config_set_out_shift(&c, false, true, rgbw ? 32 : 24); - sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX); - int cycles_per_bit = ws2812_T1 + ws2812_T2 + ws2812_T3; - float div = clock_get_hz(clk_sys) / (freq * cycles_per_bit); - sm_config_set_clkdiv(&c, div); - pio_sm_init(pio, sm, offset, &c); - pio_sm_set_enabled(pio, sm, true); -} -#endif - extern void do_flash(); extern void low_flash_init(); @@ -144,12 +90,6 @@ int select_app(const uint8_t *aid, size_t aid_len) { int (*button_pressed_cb)(uint8_t) = NULL; -static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; - -void led_set_blink(uint32_t mode) { - blink_interval_ms = mode; -} - void execute_tasks(); static bool req_button_pending = false; @@ -191,12 +131,6 @@ int gettimeofday(struct timeval* tp, struct timezone* tzp) return 0; } #endif -uint32_t board_millis() { - struct timeval start; - gettimeofday(&start, NULL); - return start.tv_sec * 1000 + start.tv_usec / 1000; -} - #else #ifdef ESP_PLATFORM bool picok_board_button_read() { @@ -250,7 +184,7 @@ bool wait_button() { uint32_t start_button = board_millis(); bool timeout = false; cancel_button = false; - led_set_blink((1000 << 16) | 100); + led_set_blink(BLINK_BUTTON); req_button_pending = true; while (picok_board_button_read() == false && cancel_button == false) { execute_tasks(); @@ -278,81 +212,6 @@ bool wait_button() { struct apdu apdu; -#ifdef ESP_PLATFORM -#include "driver/gpio.h" -#include "neopixel.h" -tNeopixelContext neopixel = NULL; -tNeopixel pixel[] = - { - { 0, NP_RGB(0, 0, 0) }, /* off */ - { 0, NP_RGB(255, 0, 255) }, /* magenta */ - { 0, NP_RGB(255, 0, 0) }, /* green */ - { 0, NP_RGB(0, 255, 0) }, /* red */ - { 0, NP_RGB(0, 0, 255) }, /* red */ - { 0, NP_RGB(255, 255, 0) }, /* yellow */ - { 0, NP_RGB(0, 255, 255) }, /* cyan */ - { 0, NP_RGB(255, 255, 255) }, /* white */ - }; -#endif -void led_blinking_task() { - static uint32_t start_ms = 0; - static uint8_t led_state = false; -#ifdef PICO_DEFAULT_LED_PIN_INVERTED - uint32_t interval = !led_state ? blink_interval_ms & 0xffff : blink_interval_ms >> 16; -#else - uint32_t interval = led_state ? blink_interval_ms & 0xffff : blink_interval_ms >> 16; -#endif -#ifdef PICO_DEFAULT_LED_PIN - static uint8_t led_color = PICO_DEFAULT_LED_PIN; -#elif defined(PICO_DEFAULT_WS2812_PIN) -#elif defined(CYW43_WL_GPIO_LED_PIN) - static uint8_t led_color = CYW43_WL_GPIO_LED_PIN; -#endif - - // Blink every interval ms - if (board_millis() - start_ms < interval) { - return; // not enough time - } - start_ms += interval; - -#ifdef PICO_DEFAULT_LED_PIN - gpio_put(led_color, led_state); -#elif defined(PICO_DEFAULT_WS2812_PIN) - if (led_state == 0) { - pio_sm_put_blocking(pio0, 0, 0); - } - else { - pio_sm_put_blocking(pio0, 0, 0xff000000); - } -#elif defined(CYW43_WL_GPIO_LED_PIN) - cyw43_arch_gpio_put(led_color, led_state); -#elif defined(ESP_PLATFORM) - neopixel_SetPixel(neopixel, &pixel[led_state], 1); -#endif - led_state ^= 1; // toggle -} - -void led_off_all() { -#ifndef ENABLE_EMULATION -#ifdef PIMORONI_TINY2040 - gpio_put(TINY2040_LED_R_PIN, 1); - gpio_put(TINY2040_LED_G_PIN, 1); - gpio_put(TINY2040_LED_B_PIN, 1); -#elif defined(PICO_DEFAULT_LED_PIN) - gpio_put(PICO_DEFAULT_LED_PIN, 0); -#elif defined(CYW43_WL_GPIO_LED_PIN) - cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0); -#endif -#if (PICO_DEFAULT_WS2812_PIN) - PIO pio = pio0; - int sm = 0; - uint offset = pio_add_program(pio, &ws2812_program); - - ws2812_program_init(pio, sm, offset, PICO_DEFAULT_WS2812_PIN, 800000, true); -#endif -#endif -} - void init_rtc() { #ifdef PICO_PLATFORM struct timespec tv = {0}; @@ -429,23 +288,8 @@ int main(void) { board_init(); stdio_init_all(); #endif -#ifdef PIMORONI_TINY2040 - gpio_init(TINY2040_LED_R_PIN); - gpio_set_dir(TINY2040_LED_R_PIN, GPIO_OUT); - gpio_init(TINY2040_LED_G_PIN); - gpio_set_dir(TINY2040_LED_G_PIN, GPIO_OUT); - gpio_init(TINY2040_LED_B_PIN); - gpio_set_dir(TINY2040_LED_B_PIN, GPIO_OUT); -#elif defined(PICO_DEFAULT_LED_PIN) - gpio_init(PICO_DEFAULT_LED_PIN); - gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); -#elif defined(CYW43_WL_GPIO_LED_PIN) - cyw43_arch_init(); -#endif + led_init(); - led_off_all(); - - //prepare_ccid(); #else emul_init("127.0.0.1", 35963); #endif @@ -475,16 +319,6 @@ int main(void) { #endif #endif -#ifdef ESP_PLATFORM - uint8_t gpio = GPIO_NUM_48; - if (file_has_data(ef_phy)) { - if (file_read_uint8_offset(ef_phy, PHY_OPTS) & PHY_OPT_GPIO) { - gpio = file_get_data(ef_phy)[PHY_LED_GPIO]; - } - } - neopixel = neopixel_Init(1, gpio); -#endif - #ifdef ESP_PLATFORM xTaskCreatePinnedToCore(core0_loop, "core0", 4096*5, NULL, CONFIG_TINYUSB_TASK_PRIORITY - 1, &hcore0, 0); #else diff --git a/src/pico_keys.h b/src/pico_keys.h index 19a9b16..2e06918 100644 --- a/src/pico_keys.h +++ b/src/pico_keys.h @@ -23,11 +23,9 @@ #endif #include "file.h" +#include "led/led.h" #if defined(ENABLE_EMULATION) || defined(ESP_PLATFORM) #include -#ifdef ENABLE_EMULATION -extern uint32_t board_millis(); -#endif #if !defined(MIN) #if defined(_MSC_VER) #define MIN(a,b) (((a)<(b))?(a):(b)) @@ -81,17 +79,6 @@ extern int flash_clear_file(file_t *file); extern int (*button_pressed_cb)(uint8_t); -enum { - BLINK_NOT_MOUNTED = (250 << 16) | 250, - BLINK_MOUNTED = (250 << 16) | 250, - BLINK_SUSPENDED = (500 << 16) | 1000, - BLINK_PROCESSING = (50 << 16) | 50, - - BLINK_ALWAYS_ON = UINT32_MAX, - BLINK_ALWAYS_OFF = 0 -}; -extern void led_set_blink(uint32_t mode); - extern bool is_req_button_pending(); extern uint32_t button_timeout;