#include "bsp/gpio.h" #include "bsp/mcu_power_sleep.h" #include "bsp/ml5238.h" #include "libs/shark_task.h" #include "libs/logger.h" #include "app/sox/soc.h" #include "app/sox/state.h" static uint8_t led_on_mask = 0; static void gpio_key_timer_handler(shark_timer_t *timer); static void led_charging_timer_handler(shark_timer_t *timer); static void led_irq_timer_handler(shark_timer_t *timer); void gpio_key_irq_handler(void); static shark_timer_t _led_timer = {.handler = gpio_key_timer_handler}; static shark_timer_t _led_stop_timer = {.handler = gpio_key_timer_handler}; static shark_timer_t _led_charging_timer = {.handler = led_charging_timer_handler}; static shark_timer_t _led_irq_timer = {.handler = led_irq_timer_handler}; void gpio_key_init(void){ shark_timer_post(&_led_stop_timer, 2000); enable_gpio_key_irq(1); } void gpio_key_irq_handler(void){ mcu_sleep_set_wakeup_source(WAKEUP_SOURCE_KEY); shark_timer_post(&_led_irq_timer, 0); } static void led_irq_timer_handler(shark_timer_t *timer) { if (bms_state_get()->charging) { return; } LED0_ON(1); LED1_ON(1); LED2_ON(1); LED3_ON(1); LED4_ON(1); led_on_mask = 0x1F; shark_timer_cancel(&_led_stop_timer); shark_timer_post(&_led_timer, 500); } static void led_show_by_mask(uint8_t mask) { for (int m = 0; m < 5; m++){ if (mask & (1 << m)){ if (m == 0) { LED4_ON(1); }else if(m == 1) { LED3_ON(1); }else if (m == 2) { LED2_ON(1); }else if (m == 3) { LED1_ON(1); }else { LED0_ON(1); } }else { if (m == 0) { LED4_ON(0); }else if(m == 1) { LED3_ON(0); }else if (m == 2) { LED2_ON(0); }else if (m == 3) { LED1_ON(0); }else { LED0_ON(0); } } } } static uint8_t _led_mask(uint8_t capacity){ uint8_t mask = 0x1F; if (capacity >= 80) { mask = 0x1F; }else if (capacity >= 60) { mask = 0x1E; }else if (capacity >= 40) { mask = 0x1C; }else if (capacity >= 20) { mask = 0x18; }else { //< 20 mask = 0x10; } return mask; } static uint8_t show_next = 0; static void led_charging_timer_handler(shark_timer_t *timer) { uint8_t mask = _led_mask(get_soc()->capacity); if (show_next) { if (get_soc()->capacity < 20) { mask = 0; }else { mask = _led_mask(get_soc()->capacity - 20); } } led_show_by_mask(mask); show_next = (show_next + 1) % 2; shark_timer_post(&_led_charging_timer, 300); } void show_leds_for_charging(uint8_t charging){ if (charging){ show_next = 0; shark_timer_cancel(&_led_stop_timer); shark_timer_post(&_led_charging_timer, 300); }else if (!charging){ led_show_by_mask(_led_mask(get_soc()->capacity)); shark_timer_cancel(&_led_charging_timer); shark_timer_post(&_led_stop_timer, 2000); } } static void gpio_key_timer_handler(shark_timer_t *timer) { if (timer == &_led_stop_timer) { LED0_ON(0); LED1_ON(0); LED2_ON(0); LED3_ON(0); LED4_ON(0); return; } uint8_t want_mask = _led_mask(get_soc()->capacity); if (want_mask == led_on_mask) { if (ml5238_is_discharging()) { shark_timer_post(&_led_stop_timer, 30 * 1000); }else { shark_timer_post(&_led_stop_timer, 2000); } }else { for (int m = 0; m < 5; m++){ if (led_on_mask & (1 << m)){ if (m == 0) { LED4_ON(0); }else if(m == 1) { LED3_ON(0); }else if (m == 2) { LED2_ON(0); }else if (m == 3) { LED1_ON(0); }else { LED0_ON(0); } led_on_mask &= ~(1 << m); break; } } shark_timer_post(&_led_timer, 500); } }