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.

72 lines
2.1 KiB

4 years ago
  1. #include "key.h"
  2. void zkey_init(zkey_module_t *module) {
  3. /**
  4. * @brief ʼ״̬ijʼ֮ǰģڵģ֮
  5. *
  6. */
  7. s_module = module;
  8. s_inited = true; //��־λ˵��������ʼ������
  9. for (int i = 0; i < s_module->nkey; i++) {
  10. s_module->keys[i].cur_state = zks_keep;
  11. s_module->keys[i].last_io_state = s_module->keys[i].get_key_state();
  12. s_module->keys[i].after_filter_state = s_module->keys[i].get_key_state();
  13. }
  14. }
  15. void zkey_do_loop_in_each_period(void *_null) { /**
  16. * @brief ѭѯ״̬
  17. *
  18. */
  19. if (!s_inited)
  20. return;
  21. for (int i = 0; i < s_module->nkey; i++) //��ʼ��ÿ���������в�ѯ
  22. {
  23. zkey_process_each(&s_module->keys[i]);
  24. }
  25. }
  26. void zkey_process_each(zkey_t *each) {
  27. /**
  28. * @brief Ƿûʱʱ¼
  29. *
  30. * @param each
  31. */
  32. each->keep_state_count++;
  33. bool now_io_state = each->get_key_state(); //��ȡ���ڵİ���io״̬
  34. if (each->currentstatekeep_count < 1000) {
  35. each->currentstatekeep_count++;
  36. }
  37. if (now_io_state != each->last_real_state) //�����任
  38. {
  39. each->currentstatekeep_count = 0;
  40. }
  41. if (each->currentstatekeep_count >= 1) //����״̬����ʱ������20ms,���°���״̬
  42. {
  43. each->after_filter_state = now_io_state;
  44. }
  45. each->last_real_state = now_io_state;
  46. zkey_process_each_after_filter(each, each->after_filter_state);
  47. }
  48. void zkey_process_each_after_filter(zkey_t *each, bool now_io_state) {
  49. if (now_io_state != each->last_io_state) {
  50. if (now_io_state) {
  51. each->keep_state_count = 0;
  52. each->hasProcessed = false;
  53. each->cur_state = zks_rising_edge;
  54. s_module->onkey(each, each->cur_state);
  55. } else {
  56. each->cur_state = zks_falling_edge;
  57. s_module->onkey(each, each->cur_state);
  58. each->keep_state_count = 0;
  59. }
  60. each->last_io_state = now_io_state;
  61. } else {
  62. each->cur_state = zks_keep;
  63. if (now_io_state) {
  64. s_module->onkey(each, each->cur_state);
  65. }
  66. }
  67. }