Add new led module to use colors whenever is possible.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos
2024-09-03 18:07:37 +02:00
parent 460111d29e
commit ea6303de81
11 changed files with 426 additions and 187 deletions

78
src/led/led.c Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#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
}

60
src/led/led.h Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef _LED_H_
#define _LED_H_
#include <stdint.h>
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_

32
src/led/led_cyw43.c Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#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

52
src/led/led_neopixel.c Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#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

31
src/led/led_pico.c Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#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

58
src/led/led_pimoroni.c Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#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

86
src/led/led_ws2812.c Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#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