dev_key.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "dev_key.h"
  2. struct Button *btn_press;
  3. struct Button btn_key_up;
  4. struct Button btn_key0;
  5. struct Button btn_key1;
  6. struct Button btn_key2;
  7. dev_key_t dev_key;
  8. #define KEY_UP_ACTIVE_LEVEL 1
  9. #define KEY0_ACTIVE_LEVEL 0
  10. #define KEY1_ACTIVE_LEVEL 0
  11. #define KEY2_ACTIVE_LEVEL 0
  12. uint8_t dev_key_read_button(uint8_t button_id)
  13. {
  14. // you can share the GPIO read function with multiple Buttons
  15. switch (button_id)
  16. {
  17. case 0:
  18. return GPIO_ReadInputDataBit(HAL_GPIO_KEY_UP_PORT, HAL_GPIO_KEY_UP_PIN);
  19. case 1:
  20. return GPIO_ReadInputDataBit(HAL_GPIO_KEY0_PORT, HAL_GPIO_KEY0_PIN);
  21. case 2:
  22. return GPIO_ReadInputDataBit(HAL_GPIO_KEY1_PORT, HAL_GPIO_KEY1_PIN);
  23. case 3:
  24. return GPIO_ReadInputDataBit(HAL_GPIO_KEY2_PORT, HAL_GPIO_KEY2_PIN);
  25. default:
  26. return 0;
  27. }
  28. }
  29. void dev_key_press_down(void *btn)
  30. {
  31. if (NULL == btn)
  32. {
  33. dev_key.i = 0;
  34. }
  35. else
  36. {
  37. btn_press = (struct Button *)btn;
  38. if (btn_press == &btn_key_up)
  39. {
  40. dev_key.dev_key_state.bits.key_up_state_flag = 1;
  41. }
  42. if (btn_press == &btn_key0)
  43. {
  44. dev_key.dev_key_state.bits.key0_state_flag = 1;
  45. }
  46. if (btn_press == &btn_key1)
  47. {
  48. dev_key.dev_key_state.bits.key1_state_flag = 1;
  49. }
  50. if (btn_press == &btn_key2)
  51. {
  52. dev_key.dev_key_state.bits.key2_state_flag = 1;
  53. }
  54. }
  55. }
  56. void dev_key_button_init(void)
  57. {
  58. button_init(&btn_key_up, dev_key_read_button, KEY_UP_ACTIVE_LEVEL, 0);
  59. button_init(&btn_key0, dev_key_read_button, KEY0_ACTIVE_LEVEL, 1);
  60. button_init(&btn_key1, dev_key_read_button, KEY1_ACTIVE_LEVEL, 2);
  61. button_init(&btn_key2, dev_key_read_button, KEY2_ACTIVE_LEVEL, 3);
  62. button_attach(&btn_key_up, PRESS_DOWN, dev_key_press_down);
  63. button_attach(&btn_key0, PRESS_DOWN, dev_key_press_down);
  64. button_attach(&btn_key1, PRESS_DOWN, dev_key_press_down);
  65. button_attach(&btn_key2, PRESS_DOWN, dev_key_press_down);
  66. button_start(&btn_key_up);
  67. button_start(&btn_key0);
  68. button_start(&btn_key1);
  69. button_start(&btn_key2);
  70. }
  71. void dev_key_button_task()
  72. {
  73. button_ticks();
  74. }