key_leds.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "bsp/gpio.h"
  2. #include "bsp/mcu_power_sleep.h"
  3. #include "bsp/ml5238.h"
  4. #include "libs/shark_task.h"
  5. #include "libs/logger.h"
  6. #include "app/sox/soc.h"
  7. #include "app/sox/state.h"
  8. static uint8_t led_on_mask = 0;
  9. static void gpio_key_timer_handler(shark_timer_t *timer);
  10. static void led_charging_timer_handler(shark_timer_t *timer);
  11. static void led_irq_timer_handler(shark_timer_t *timer);
  12. void gpio_key_irq_handler(void);
  13. static shark_timer_t _led_timer = {.handler = gpio_key_timer_handler};
  14. static shark_timer_t _led_stop_timer = {.handler = gpio_key_timer_handler};
  15. static shark_timer_t _led_charging_timer = {.handler = led_charging_timer_handler};
  16. static shark_timer_t _led_irq_timer = {.handler = led_irq_timer_handler};
  17. void gpio_key_init(void){
  18. shark_timer_post(&_led_stop_timer, 2000);
  19. enable_gpio_key_irq(1);
  20. }
  21. void gpio_key_irq_handler(void){
  22. mcu_sleep_set_wakeup_source(WAKEUP_SOURCE_KEY);
  23. shark_timer_post(&_led_irq_timer, 0);
  24. }
  25. static void led_irq_timer_handler(shark_timer_t *timer) {
  26. if (bms_state_get()->charging) {
  27. return;
  28. }
  29. LED_ALL_ON(1);
  30. led_on_mask = 0x1F;
  31. shark_timer_cancel(&_led_stop_timer);
  32. shark_timer_post(&_led_timer, 500);
  33. }
  34. static void led_show_by_mask(uint8_t mask) {
  35. for (int m = 0; m < 5; m++){
  36. if (mask & (1 << m)){
  37. if (m == 0) {
  38. LED4_ON(1);
  39. }else if(m == 1) {
  40. LED3_ON(1);
  41. }else if (m == 2) {
  42. LED2_ON(1);
  43. }else if (m == 3) {
  44. LED1_ON(1);
  45. }else {
  46. LED0_ON(1);
  47. }
  48. }else {
  49. if (m == 0) {
  50. LED4_ON(0);
  51. }else if(m == 1) {
  52. LED3_ON(0);
  53. }else if (m == 2) {
  54. LED2_ON(0);
  55. }else if (m == 3) {
  56. LED1_ON(0);
  57. }else {
  58. LED0_ON(0);
  59. }
  60. }
  61. }
  62. }
  63. static uint8_t _led_mask(uint8_t capacity){
  64. uint8_t mask = 0x1F;
  65. if (capacity >= 80) {
  66. mask = 0x1F;
  67. }else if (capacity >= 60) {
  68. mask = 0x1E;
  69. }else if (capacity >= 40) {
  70. mask = 0x1C;
  71. }else if (capacity >= 20) {
  72. mask = 0x18;
  73. }else { //< 20
  74. mask = 0x10;
  75. }
  76. return mask;
  77. }
  78. static uint8_t show_next = 0;
  79. static void led_charging_timer_handler(shark_timer_t *timer) {
  80. uint8_t mask = _led_mask(get_soc()->capacity);
  81. if (show_next) {
  82. if (get_soc()->capacity < 20) {
  83. mask = 0;
  84. }else {
  85. mask = _led_mask(get_soc()->capacity - 20);
  86. }
  87. }
  88. led_show_by_mask(mask);
  89. show_next = (show_next + 1) % 2;
  90. shark_timer_post(&_led_charging_timer, 300);
  91. }
  92. void show_leds_for_charging(uint8_t charging){
  93. if (charging){
  94. show_next = 0;
  95. shark_timer_cancel(&_led_stop_timer);
  96. shark_timer_post(&_led_charging_timer, 300);
  97. }else if (!charging){
  98. led_show_by_mask(_led_mask(get_soc()->capacity));
  99. shark_timer_cancel(&_led_charging_timer);
  100. shark_timer_post(&_led_stop_timer, 2000);
  101. }
  102. }
  103. static void gpio_key_timer_handler(shark_timer_t *timer) {
  104. if (timer == &_led_stop_timer) {
  105. LED_ALL_ON(0);
  106. return;
  107. }
  108. uint8_t want_mask = _led_mask(get_soc()->capacity);
  109. if (want_mask == led_on_mask) {
  110. if (ml5238_is_discharging()) {
  111. shark_timer_post(&_led_stop_timer, 30 * 1000);
  112. }else {
  113. shark_timer_post(&_led_stop_timer, 2000);
  114. }
  115. }else {
  116. for (int m = 0; m < 5; m++){
  117. if (led_on_mask & (1 << m)){
  118. if (m == 0) {
  119. LED4_ON(0);
  120. }else if(m == 1) {
  121. LED3_ON(0);
  122. }else if (m == 2) {
  123. LED2_ON(0);
  124. }else if (m == 3) {
  125. LED1_ON(0);
  126. }else {
  127. LED0_ON(0);
  128. }
  129. led_on_mask &= ~(1 << m);
  130. break;
  131. }
  132. }
  133. shark_timer_post(&_led_timer, 500);
  134. }
  135. }