keys.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "bsp/gpio.h"
  2. #include "bsp/mcu_power_sleep.h"
  3. #include "libs/shark_task.h"
  4. #include "libs/logger.h"
  5. #include "app/sox/soc.h"
  6. static uint8_t led_on_mask = 0;
  7. static void gpio_key_timer_handler(shark_timer_t *timer);
  8. void gpio_key_irq_handler(void);
  9. static shark_timer_t _led_timer = {.handler = gpio_key_timer_handler};
  10. static shark_timer_t _led_stop_timer = {.handler = gpio_key_timer_handler};
  11. void gpio_key_init(void){
  12. shark_timer_post(&_led_stop_timer, 2000);
  13. enable_gpio_key_irq(1);
  14. }
  15. void gpio_key_irq_handler(void){
  16. LED0_ON(1);
  17. LED1_ON(1);
  18. LED2_ON(1);
  19. LED3_ON(1);
  20. LED4_ON(1);
  21. led_on_mask = 0x1F;
  22. shark_timer_cancel(&_led_stop_timer);
  23. shark_timer_post(&_led_timer, 500);
  24. mcu_sleep_set_wakeup_source(WAKEUP_SOURCE_KEY);
  25. }
  26. static uint8_t _led_mask(uint8_t capacity){
  27. uint8_t mask = 0x1F;
  28. if (capacity >= 80) {
  29. mask = 0x1F;
  30. }else if (capacity >= 60) {
  31. mask = 0x1E;
  32. }else if (capacity >= 40) {
  33. mask = 0x1C;
  34. }else if (capacity >= 20) {
  35. mask = 0x18;
  36. }else {
  37. mask = 0x10;
  38. }
  39. return mask;
  40. }
  41. static void gpio_key_timer_handler(shark_timer_t *timer) {
  42. if (timer == &_led_stop_timer) {
  43. LED0_ON(0);
  44. LED1_ON(0);
  45. LED2_ON(0);
  46. LED3_ON(0);
  47. LED4_ON(0);
  48. return;
  49. }
  50. uint8_t want_mask = _led_mask(get_soc()->capacity);
  51. if (want_mask == led_on_mask) {
  52. shark_timer_post(&_led_stop_timer, 2000);
  53. }else {
  54. for (int m = 0; m < 5; m++){
  55. if (led_on_mask & (1 << m)){
  56. if (m == 0) {
  57. LED4_ON(0);
  58. }else if(m == 1) {
  59. LED3_ON(0);
  60. }else if (m == 2) {
  61. LED2_ON(0);
  62. }else if (m == 3) {
  63. LED1_ON(0);
  64. }else {
  65. LED0_ON(0);
  66. }
  67. led_on_mask &= ~(1 << m);
  68. break;
  69. }
  70. }
  71. shark_timer_post(&_led_timer, 500);
  72. }
  73. }