Merge pull request #5 from benallard/led

Add brightness to the LED mode.
This commit is contained in:
Pol Henarejos
2024-09-27 19:17:06 +02:00
committed by GitHub
11 changed files with 70 additions and 44 deletions

View File

@@ -34,7 +34,7 @@ uint8_t chain_buf[4096];
uint8_t *chain_ptr = NULL;
int process_apdu() {
led_set_blink(BLINK_PROCESSING);
led_set_mode(MODE_PROCESSING);
if (CLA(apdu) & 0x10) {
if (!is_chaining) {
chain_ptr = chain_buf;

View File

@@ -27,52 +27,60 @@
#endif
extern void led_driver_init();
extern void led_driver_color(uint8_t);
extern void led_driver_color(uint8_t, float);
static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
static uint32_t led_mode = MODE_NOT_MOUNTED;
void led_set_blink(uint32_t mode) {
blink_interval_ms = mode;
void led_set_mode(uint32_t mode) {
led_mode = mode;
}
void led_blinking_task() {
#ifndef ENABLE_EMULATION
static uint32_t start_ms = 0;
static uint32_t stop_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;
uint32_t led_max_brightness = (led_mode & LED_BTNESS_MASK) >> LED_BTNESS_SHIFT;
uint32_t led_color = (led_mode & LED_COLOR_MASK) >> LED_COLOR_SHIFT;
uint32_t led_off = (led_mode & LED_OFF_MASK) >> LED_OFF_SHIFT;
uint32_t led_on = (led_mode & LED_ON_MASK) >> LED_ON_SHIFT;
// Blink every interval ms
if (board_millis() - start_ms < led_interval) {
return; // not enough time
}
start_ms += led_interval;
// how far in the current state from 0 - 1
float progress = (board_millis() - start_ms) / (stop_ms - start_ms);
if (state == false) {
led_driver_color(LED_COLOR_OFF);
if (!state){
// fading down so 1 -> 0
progress = 1 - progress;
}
else {
led_driver_color(led_color);
// maybe quick return if progress didn't changed much ?
// current one from 0 - 1
float led_brightness = (led_max_brightness / MAX_BTNESS) * progress;
led_driver_color(led_color, led_brightness);
if (board_millis() >= stop_ms){
start_ms = stop_ms;
led_state ^= 1; // toggle
stop_ms = start_ms + (led_state ? led_on : led_off);
}
led_state ^= 1; // toggle
#endif
}
void led_off_all() {
#ifndef ENABLE_EMULATION
led_driver_color(LED_COLOR_OFF);
led_driver_color(LED_COLOR_OFF, 0);
#endif
}
void led_init() {
#ifndef ENABLE_EMULATION
led_driver_init();
led_set_blink(BLINK_NOT_MOUNTED);
led_set_mode(MODE_NOT_MOUNTED);
#endif
}

View File

@@ -31,28 +31,37 @@ enum {
LED_COLOR_WHITE
};
#define LED_OFF_BITS 14
#define LED_OFF_BITS 12
#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_BITS 12
#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)
#define LED_BTNESS_BITS 4
#define LED_BTNESS_SHIFT (LED_ON_BITS + LED_OFF_BITS + LED_COLOR_BITS)
#define LED_BTNESS_MASK (((1 << LED_BTNESS_BITS) - 1 ) << LED_BTNESS_SHIFT)
#define MAX_BTNESS ((1 << LED_BTNESS_BITS) - 1)
#define HALF_BTNESS ((1 << (LED_BTNESS_BITS - 1)) - 1)
// steady on
#define LED_ON_NO_BLINK ((1000 << LED_ON_SHIFT) | (0 << LED_OFF_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),
MODE_NOT_MOUNTED = (HALF_BTNESS << LED_BTNESS_SHIFT) | (LED_COLOR_CYAN << LED_COLOR_SHIFT) | LED_ON_NO_BLINK,
MODE_MOUNTED = (HALF_BTNESS << LED_BTNESS_SHIFT) | (LED_COLOR_GREEN << LED_COLOR_SHIFT) | LED_ON_NO_BLINK,
MODE_SUSPENDED = (HALF_BTNESS << LED_BTNESS_SHIFT) | (LED_COLOR_BLUE << LED_COLOR_SHIFT) | (1000 << LED_ON_SHIFT) | (2000 << LED_OFF_SHIFT),
MODE_PROCESSING = (MAX_BTNESS << LED_BTNESS_SHIFT) | (LED_COLOR_RED << LED_COLOR_SHIFT) | (50 << LED_ON_SHIFT) | (50 << LED_OFF_SHIFT),
MODE_BUTTON = (MAX_BTNESS << LED_BTNESS_SHIFT) | (LED_COLOR_YELLOW << LED_COLOR_SHIFT) | (1000 << LED_ON_SHIFT) | (100 << LED_OFF_SHIFT),
BLINK_ALWAYS_ON = UINT32_MAX,
BLINK_ALWAYS_OFF = 0
MODE_ALWAYS_ON = UINT32_MAX,
MODE_ALWAYS_OFF = 0
};
extern void led_set_blink(uint32_t mode);
extern void led_set_mode(uint32_t mode);
extern void led_blinking_task();
extern void led_off_all();
extern void led_init();

View File

@@ -25,7 +25,7 @@ void led_driver_init() {
cyw43_arch_init();
}
void led_driver_color(uint8_t color) {
void led_driver_color(uint8_t color, float brightness) {
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, color != LED_COLOR_OFF);
}

View File

@@ -45,7 +45,7 @@ void led_driver_init() {
neopixel = neopixel_Init(1, gpio);
}
void led_driver_color(int color) {
void led_driver_color(uint8_t color, float brightness) {
neopixel_SetPixel(neopixel, &pixel[color], 1);
}

View File

@@ -24,7 +24,7 @@ void led_driver_init() {
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
}
void led_driver_color(uint8_t color) {
void led_driver_color(uint8_t color, float brightness) {
gpio_put(PICO_DEFAULT_LED_PIN, color != LED_COLOR_OFF);
}

View File

@@ -49,7 +49,7 @@ void led_driver_init() {
gpio_set_dir(LED_B_PIN, GPIO_OUT);
}
void led_driver_color(uint8_t color) {
void led_driver_color(uint8_t color, float brightness) {
gpio_put(LED_R_PIN, pixel[color][0]);
gpio_put(LED_G_PIN, pixel[color][1]);
gpio_put(LED_B_PIN, pixel[color][2]);

View File

@@ -79,8 +79,17 @@ uint32_t pixel[] = {
0xffffff00 // 7: white
};
void led_driver_color(uint8_t color) {
pio_sm_put_blocking(pio0, 0, pixel[color]);
void led_driver_color(uint8_t color, float brightness) {
uint32_t pixel_color = pixel[color];
uint8_t r = (pixel_color >> 16) & 0xFF;
uint8_t g = (pixel_color >> 24) & 0xFF;
uint8_t b = (pixel_color >> 8) & 0xFF;
r = (uint8_t)(r * brightness);
g = (uint8_t)(g * brightness);
b = (uint8_t)(b * brightness);
pio_sm_put_blocking(pio0, 0, (g << 24) | (r << 16) | (b << 8));
}
#endif

View File

@@ -185,7 +185,7 @@ bool wait_button() {
uint32_t start_button = board_millis();
bool timeout = false;
cancel_button = false;
led_set_blink(BLINK_BUTTON);
led_set_mode(MODE_BUTTON);
req_button_pending = true;
while (picok_board_button_read() == false && cancel_button == false) {
execute_tasks();
@@ -205,7 +205,7 @@ bool wait_button() {
}
}
}
led_set_blink(BLINK_PROCESSING);
led_set_mode(MODE_PROCESSING);
req_button_pending = false;
return timeout || cancel_button;
}

View File

@@ -219,7 +219,7 @@ int driver_process_usb_packet_ccid(uint8_t itf, uint16_t rx_read) {
ccid_status = 0;
ccid_write_fast(itf, (const uint8_t *)ccid_resp_fast[itf], (uint16_t)(size_atr + 10));
led_set_blink(BLINK_MOUNTED);
led_set_mode(MODE_MOUNTED);
}
else if (ccid_header[itf]->bMessageType == CCID_POWER_OFF) {
if (ccid_status == 0) {
@@ -234,7 +234,7 @@ int driver_process_usb_packet_ccid(uint8_t itf, uint16_t rx_read) {
ccid_resp_fast[itf]->abRFU1 = 0;
ccid_write_fast(itf, (const uint8_t *)ccid_resp_fast[itf], 10);
led_set_blink(BLINK_SUSPENDED);
led_set_mode(MODE_SUSPENDED);
}
else if (ccid_header[itf]->bMessageType == CCID_SET_PARAMS ||
ccid_header[itf]->bMessageType == CCID_GET_PARAMS ||

View File

@@ -122,7 +122,7 @@ void card_start(uint8_t itf, void (*func)(void)) {
multicore_reset_core1();
multicore_launch_core1(func);
}
led_set_blink(BLINK_MOUNTED);
led_set_mode(MODE_MOUNTED);
card_locked_itf = itf;
card_locked_func = func;
}
@@ -148,7 +148,7 @@ void card_exit() {
mutex_exit(&mutex);
#endif
}
led_set_blink(BLINK_SUSPENDED);
led_set_mode(MODE_SUSPENDED);
#ifdef ESP_PLATFORM
hcore1 = NULL;
#endif
@@ -187,7 +187,7 @@ int card_status(uint8_t itf) {
if (has_m) {
if (m == EV_EXEC_FINISHED) {
timeout_stop();
led_set_blink(BLINK_MOUNTED);
led_set_mode(MODE_MOUNTED);
return CCID_OK;
}
#ifndef ENABLE_EMULATION