key_leds.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. LED0_ON(1);
  30. LED1_ON(1);
  31. LED2_ON(1);
  32. LED3_ON(1);
  33. LED4_ON(1);
  34. led_on_mask = 0x1F;
  35. shark_timer_cancel(&_led_stop_timer);
  36. shark_timer_post(&_led_timer, 500);
  37. }
  38. static void led_show_by_mask(uint8_t mask) {
  39. for (int m = 0; m < 5; m++){
  40. if (mask & (1 << m)){
  41. if (m == 0) {
  42. LED4_ON(1);
  43. }else if(m == 1) {
  44. LED3_ON(1);
  45. }else if (m == 2) {
  46. LED2_ON(1);
  47. }else if (m == 3) {
  48. LED1_ON(1);
  49. }else {
  50. LED0_ON(1);
  51. }
  52. }else {
  53. if (m == 0) {
  54. LED4_ON(0);
  55. }else if(m == 1) {
  56. LED3_ON(0);
  57. }else if (m == 2) {
  58. LED2_ON(0);
  59. }else if (m == 3) {
  60. LED1_ON(0);
  61. }else {
  62. LED0_ON(0);
  63. }
  64. }
  65. }
  66. }
  67. static uint8_t _led_mask(uint8_t capacity){
  68. uint8_t mask = 0x1F;
  69. if (capacity >= 80) {
  70. mask = 0x1F;
  71. }else if (capacity >= 60) {
  72. mask = 0x1E;
  73. }else if (capacity >= 40) {
  74. mask = 0x1C;
  75. }else if (capacity >= 20) {
  76. mask = 0x18;
  77. }else { //< 20
  78. mask = 0x10;
  79. }
  80. return mask;
  81. }
  82. static uint8_t show_next = 0;
  83. static void led_charging_timer_handler(shark_timer_t *timer) {
  84. uint8_t mask = _led_mask(get_soc()->capacity);
  85. if (show_next) {
  86. if (get_soc()->capacity < 20) {
  87. mask = 0;
  88. }else {
  89. mask = _led_mask(get_soc()->capacity - 20);
  90. }
  91. }
  92. led_show_by_mask(mask);
  93. show_next = (show_next + 1) % 2;
  94. shark_timer_post(&_led_charging_timer, 300);
  95. }
  96. void show_leds_for_charging(uint8_t charging){
  97. if (charging){
  98. show_next = 0;
  99. shark_timer_cancel(&_led_stop_timer);
  100. shark_timer_post(&_led_charging_timer, 300);
  101. }else if (!charging){
  102. led_show_by_mask(_led_mask(get_soc()->capacity));
  103. shark_timer_cancel(&_led_charging_timer);
  104. shark_timer_post(&_led_stop_timer, 2000);
  105. }
  106. }
  107. static void gpio_key_timer_handler(shark_timer_t *timer) {
  108. if (timer == &_led_stop_timer) {
  109. LED0_ON(0);
  110. LED1_ON(0);
  111. LED2_ON(0);
  112. LED3_ON(0);
  113. LED4_ON(0);
  114. return;
  115. }
  116. uint8_t want_mask = _led_mask(get_soc()->capacity);
  117. if (want_mask == led_on_mask) {
  118. if (ml5238_is_discharging()) {
  119. shark_timer_post(&_led_stop_timer, 30 * 1000);
  120. }else {
  121. shark_timer_post(&_led_stop_timer, 2000);
  122. }
  123. }else {
  124. for (int m = 0; m < 5; m++){
  125. if (led_on_mask & (1 << m)){
  126. if (m == 0) {
  127. LED4_ON(0);
  128. }else if(m == 1) {
  129. LED3_ON(0);
  130. }else if (m == 2) {
  131. LED2_ON(0);
  132. }else if (m == 3) {
  133. LED1_ON(0);
  134. }else {
  135. LED0_ON(0);
  136. }
  137. led_on_mask &= ~(1 << m);
  138. break;
  139. }
  140. }
  141. shark_timer_post(&_led_timer, 500);
  142. }
  143. }