key.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "key.h"
  2. #include "gpio.h"
  3. #include "multi_button.h"
  4. struct Button *btn_press;
  5. struct Button btn_key_up;
  6. struct Button btn_key0;
  7. struct Button btn_key1;
  8. struct Button btn_key2;
  9. key_button_t key_button;
  10. #define KEY_UP_ACTIVE_LEVEL 1
  11. #define KEY0_ACTIVE_LEVEL 0
  12. #define KEY1_ACTIVE_LEVEL 0
  13. #define KEY2_ACTIVE_LEVEL 0
  14. uint8_t key_read_button(uint8_t button_id)
  15. {
  16. // you can share the GPIO read function with multiple Buttons
  17. switch (button_id)
  18. {
  19. case 0:
  20. return GPIO_ReadInputDataBit(GPIO_KEY_UP_PORT, GPIO_KEY_UP_PIN);
  21. case 1:
  22. return GPIO_ReadInputDataBit(GPIO_KEY0_PORT, GPIO_KEY0_PIN);
  23. case 2:
  24. return GPIO_ReadInputDataBit(GPIO_KEY1_PORT, GPIO_KEY1_PIN);
  25. case 3:
  26. return GPIO_ReadInputDataBit(GPIO_KEY2_PORT, GPIO_KEY2_PIN);
  27. default:
  28. return 0;
  29. }
  30. }
  31. void key_press_down(void *btn)
  32. {
  33. if (NULL == btn)
  34. {
  35. key_button.i = 0;
  36. }
  37. else
  38. {
  39. btn_press = (struct Button *)btn;
  40. if (btn_press == &btn_key_up)
  41. {
  42. key_button.key_state.bits.key_up_state_flag = 1;
  43. }
  44. if (btn_press == &btn_key0)
  45. {
  46. key_button.key_state.bits.key0_state_flag = 1;
  47. }
  48. if (btn_press == &btn_key1)
  49. {
  50. key_button.key_state.bits.key1_state_flag = 1;
  51. }
  52. if (btn_press == &btn_key2)
  53. {
  54. key_button.key_state.bits.key2_state_flag = 1;
  55. }
  56. }
  57. }
  58. void key_button_init(void)
  59. {
  60. button_init(&btn_key_up, key_read_button, KEY_UP_ACTIVE_LEVEL, 0);
  61. button_init(&btn_key0, key_read_button, KEY0_ACTIVE_LEVEL, 1);
  62. button_init(&btn_key1, key_read_button, KEY1_ACTIVE_LEVEL, 2);
  63. button_init(&btn_key2, key_read_button, KEY2_ACTIVE_LEVEL, 3);
  64. button_attach(&btn_key_up, PRESS_DOWN, key_press_down);
  65. button_attach(&btn_key0, PRESS_DOWN, key_press_down);
  66. button_attach(&btn_key1, PRESS_DOWN, key_press_down);
  67. button_attach(&btn_key2, PRESS_DOWN, key_press_down);
  68. button_start(&btn_key_up);
  69. button_start(&btn_key0);
  70. button_start(&btn_key1);
  71. button_start(&btn_key2);
  72. }
  73. void key_button_task()
  74. {
  75. button_ticks();
  76. }