bsp_key.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "include.h"
  2. #include "driver_com.h"
  3. #include "bsp_saradc_key.h"
  4. #include "bsp_io_key.h"
  5. #if BSP_KEY_EN
  6. #define KEY_CLR_PAIR_TRIGGER_THD 3000 // (ms)
  7. static key_status_typedef key_status;
  8. static struct {
  9. uint8_t sta;
  10. uint8_t filter;
  11. uint32_t clr_pair_tick;
  12. } bsp_key_state AT(.user_buf.bsp.key);
  13. AT(.com_rodata.key)
  14. u16 allow_multiple_key_table[] = {
  15. KEY_ID_PP | KEY_SHORT_UP,
  16. };
  17. // 处理短按、长按、连按
  18. AT(.com_text.key.process)
  19. u16 key_process(u16 key_id)
  20. {
  21. u16 key_return = KEY_ID_NO;
  22. if (key_id == KEY_ID_NO || key_id != key_status.key_id) { // 抬键消抖
  23. if (key_status.release_debounce_cnt < KEY_UP_TIMES) {
  24. key_status.release_debounce_cnt++;
  25. } else {
  26. if (key_status.key_press_cnt >= KEY_LONG_TIMES) { // 长按抬键
  27. key_return = key_status.key_id | KEY_LONG_UP;
  28. } else if (key_status.key_press_cnt >= KEY_SCAN_TIMES) { // 短按抬键
  29. key_return = key_status.key_id | KEY_SHORT_UP;
  30. }
  31. key_status.key_id = key_id;
  32. key_status.key_press_cnt = 0;
  33. }
  34. } else {
  35. key_status.key_press_cnt++;
  36. if (key_status.key_press_cnt == KEY_SCAN_TIMES) { // 去抖
  37. key_status.release_debounce_cnt = 0;
  38. key_return = key_status.key_id;
  39. } else if (key_status.key_press_cnt == KEY_LONG_TIMES) { // 长按
  40. key_return = key_status.key_id | KEY_LONG;
  41. } else if (key_status.key_press_cnt == KEY_LONG_HOLD_TIMES) { // 连按
  42. key_return = key_status.key_id | KEY_HOLD;
  43. key_status.key_press_cnt = KEY_LONG_TIMES;
  44. }
  45. }
  46. return key_return;
  47. }
  48. // mode: 0-事件检测 1-状态检测
  49. AT(.com_text.key.scan)
  50. u16 bsp_key_scan(u8 mode)
  51. {
  52. u16 key, key_id = KEY_ID_NO;
  53. #if BSP_IOKEY_EN
  54. key_id = bsp_get_io_key_id();
  55. #endif // BSP_IOKEY_EN
  56. #if BSP_ADKEY_EN
  57. if (key_id == KEY_ID_NO) {
  58. key_id = bsp_get_adkey_id(); // 根据ADC采样值返回键值
  59. }
  60. #endif // BSP_ADKEY_EN
  61. if (mode) {
  62. return key_id;
  63. }
  64. if (bsp_key_get_power_on_flag()) {
  65. if (key_id == KEY_ID_PP) {
  66. key_id = KEY_ID_NO;
  67. } else {
  68. bsp_key_set_power_on_flag(false);
  69. }
  70. }
  71. key = key_process(key_id); // 短按、长按、连按处理
  72. key = key_multi_press_process(key); // 多击处理
  73. if(key){
  74. msg_enqueue(key);
  75. }
  76. return key;
  77. }
  78. AT(.com_text.key.scan)
  79. void bsp_key_status_check(void)
  80. {
  81. uint8_t key_msg;
  82. key_msg = bsp_io_key_status_get();
  83. if (bsp_key_state.filter) {
  84. bsp_key_state.filter++;
  85. if (bsp_key_state.filter > KEY_SCAN_TIMES) {
  86. bsp_key_state.filter = 0;
  87. msg_enqueue(MSG_KEY_MOUSE_BTN_UPD | key_msg);
  88. if ((key_msg & (MOUSE_BTN_BL | MOUSE_BTN_BR)) == (MOUSE_BTN_BL | MOUSE_BTN_BR)) {
  89. bsp_key_state.clr_pair_tick = tick_get();
  90. } else {
  91. bsp_key_state.clr_pair_tick = 0;
  92. }
  93. }
  94. }
  95. if (key_msg != bsp_key_state.sta) {
  96. bsp_key_state.filter = 1;
  97. bsp_key_state.sta = key_msg;
  98. }
  99. if (bsp_key_state.clr_pair_tick && tick_check_expire(bsp_key_state.clr_pair_tick, KEY_CLR_PAIR_TRIGGER_THD)) {
  100. bsp_key_state.clr_pair_tick = 0;
  101. msg_enqueue(MSG_KEY_MOUSE_CLR_PAIR);
  102. }
  103. }
  104. void bsp_key_init(void)
  105. {
  106. memset(&key_status, 0, sizeof(key_status));
  107. memset(&bsp_key_state, 0, sizeof(bsp_key_state));
  108. multiple_key_init(100, allow_multiple_key_table, sizeof(allow_multiple_key_table) / sizeof(u16));
  109. #if BSP_ADKEY_EN
  110. bsp_adkey_init();
  111. #endif // BSP_ADKEY_EN
  112. #if BSP_IOKEY_EN
  113. bsp_io_key_init();
  114. #endif // BSP_IOKEY_EN
  115. }
  116. AT(.com_text.key.set.power_on)
  117. void bsp_key_set_power_on_flag(bool flag)
  118. {
  119. key_status.power_on_flag = flag;
  120. }
  121. AT(.com_text.key.get.power_on)
  122. bool bsp_key_get_power_on_flag(void)
  123. {
  124. return key_status.power_on_flag;
  125. }
  126. #endif // BSP_KEY_EN