key_leds.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. void gpio_key_irq_handler(void);
  12. static shark_timer_t _led_timer = {.handler = gpio_key_timer_handler};
  13. static shark_timer_t _led_stop_timer = {.handler = gpio_key_timer_handler};
  14. static shark_timer_t _led_charging_timer = {.handler = led_charging_timer_handler};
  15. void gpio_key_init(void){
  16. shark_timer_post(&_led_stop_timer, 2000);
  17. enable_gpio_key_irq(1);
  18. }
  19. void gpio_key_irq_handler(void){
  20. if (bms_state_get()->charging) {
  21. return;
  22. }
  23. LED0_ON(1);
  24. LED1_ON(1);
  25. LED2_ON(1);
  26. LED3_ON(1);
  27. LED4_ON(1);
  28. led_on_mask = 0x1F;
  29. shark_timer_cancel(&_led_stop_timer);
  30. shark_timer_post(&_led_timer, 500);
  31. mcu_sleep_set_wakeup_source(WAKEUP_SOURCE_KEY);
  32. }
  33. static void led_show_by_mask(uint8_t mask) {
  34. for (int m = 0; m < 5; m++){
  35. if (mask & (1 << m)){
  36. if (m == 0) {
  37. LED4_ON(1);
  38. }else if(m == 1) {
  39. LED3_ON(1);
  40. }else if (m == 2) {
  41. LED2_ON(1);
  42. }else if (m == 3) {
  43. LED1_ON(1);
  44. }else {
  45. LED0_ON(1);
  46. }
  47. }else {
  48. if (m == 0) {
  49. LED4_ON(0);
  50. }else if(m == 1) {
  51. LED3_ON(0);
  52. }else if (m == 2) {
  53. LED2_ON(0);
  54. }else if (m == 3) {
  55. LED1_ON(0);
  56. }else {
  57. LED0_ON(0);
  58. }
  59. }
  60. }
  61. }
  62. static uint8_t _led_mask(uint8_t capacity){
  63. uint8_t mask = 0x1F;
  64. if (capacity >= 80) {
  65. mask = 0x1F;
  66. }else if (capacity >= 60) {
  67. mask = 0x1E;
  68. }else if (capacity >= 40) {
  69. mask = 0x1C;
  70. }else if (capacity >= 20) {
  71. mask = 0x18;
  72. }else { //< 20
  73. mask = 0x10;
  74. }
  75. return mask;
  76. }
  77. static uint8_t show_next = 0;
  78. static void led_charging_timer_handler(shark_timer_t *timer) {
  79. uint8_t mask = _led_mask(get_soc()->capacity);
  80. if (show_next) {
  81. if (get_soc()->capacity < 20) {
  82. mask = 0;
  83. }else {
  84. mask = _led_mask(get_soc()->capacity - 20);
  85. }
  86. }
  87. led_show_by_mask(mask);
  88. show_next = (show_next + 1) % 2;
  89. shark_timer_post(&_led_charging_timer, 300);
  90. }
  91. void show_leds_for_charging(uint8_t charging){
  92. if (charging){
  93. show_next = 0;
  94. shark_timer_post(&_led_charging_timer, 300);
  95. }else if (!charging){
  96. shark_timer_cancel(&_led_charging_timer);
  97. }
  98. }
  99. static void gpio_key_timer_handler(shark_timer_t *timer) {
  100. if (timer == &_led_stop_timer) {
  101. LED0_ON(0);
  102. LED1_ON(0);
  103. LED2_ON(0);
  104. LED3_ON(0);
  105. LED4_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 * 000);
  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. }