You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.3 KiB

4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
  1. #include "key.h"
  2. static zkey_module_t *s_module;
  3. static bool s_inited;
  4. static void prv_zkey_process_each_after_filter(zkey_t *each, bool now_io_state) {
  5. if (now_io_state != each->last_io_state) {
  6. if (now_io_state) {
  7. each->keep_state_count = 0;
  8. each->hasProcessed = false;
  9. each->cur_state = zks_rising_edge;
  10. s_module->onkey(each, each->cur_state);
  11. } else {
  12. each->cur_state = zks_falling_edge;
  13. s_module->onkey(each, each->cur_state);
  14. each->keep_state_count = 0;
  15. }
  16. each->last_io_state = now_io_state;
  17. } else {
  18. each->cur_state = zks_keep;
  19. if (now_io_state) {
  20. s_module->onkey(each, each->cur_state);
  21. }
  22. }
  23. }
  24. void zkey_process_each(zkey_t *each);
  25. void zkey_init(zkey_module_t *module) {
  26. /**
  27. * @brief
  28. *
  29. */
  30. s_module = module;
  31. s_inited = true; //标志位说明按键初始化完成
  32. for (int i = 0; i < s_module->nkey; i++) {
  33. s_module->keys[i].cur_state = zks_keep;
  34. s_module->keys[i].last_io_state = s_module->keys[i].get_key_state();
  35. s_module->keys[i].after_filter_state = s_module->keys[i].get_key_state();
  36. }
  37. }
  38. void zkey_process_each(zkey_t *each) {
  39. /**
  40. * @brief
  41. *
  42. * @param each
  43. */
  44. each->keep_state_count++;
  45. bool now_io_state = each->get_key_state(); //获取现在的按键io状态
  46. if (each->currentstatekeep_count < 1000) {
  47. each->currentstatekeep_count++;
  48. }
  49. if (now_io_state != each->last_real_state) //发生变换
  50. {
  51. each->currentstatekeep_count = 0;
  52. }
  53. if (each->currentstatekeep_count >= 1) //按键状态保持时间大于20ms,更新按键状态
  54. {
  55. each->after_filter_state = now_io_state;
  56. }
  57. each->last_real_state = now_io_state;
  58. prv_zkey_process_each_after_filter(each, each->after_filter_state);
  59. }
  60. void zkey_do_loop_in_each_period(void *_null) { /**
  61. * @brief
  62. *
  63. */
  64. if (!s_inited) return;
  65. for (int i = 0; i < s_module->nkey; i++) //开始对每个按键进行查询
  66. {
  67. zkey_process_each(&s_module->keys[i]);
  68. }
  69. }