BUTTON.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "main.h"
  2. #include "MultiButton.h"
  3. static void KEY_GPIO_Config(void);
  4. static uint8_t read_key1_pin(void);
  5. static uint8_t read_key2_pin(void);
  6. static void key1_shortPress_handle(void *btn);
  7. static void key2_shortPress_handle(void *btn);
  8. static void key1_longPress_handle(void *btn);
  9. static void key2_longPress_handle(void *btn);
  10. static void KEY_Init(void);
  11. static void KEY_Scan(void);
  12. KEYClassStruct KEYClass = {
  13. .Init = KEY_Init,
  14. .Scan = KEY_Scan,
  15. .Read_key1 = read_key1_pin,
  16. .Read_key2 = read_key2_pin
  17. };
  18. // 按键结构体
  19. struct Button key1;
  20. struct Button key2;
  21. #define KEY1_PORT_CLK (RCC_APB2Periph_GPIOB)
  22. #define KEY1_PORT GPIOB
  23. #define KEY1_PIN GPIO_Pin_6
  24. #define KEY2_PORT_CLK (RCC_APB2Periph_GPIOB)
  25. #define KEY2_PORT GPIOB
  26. #define KEY2_PIN GPIO_Pin_7
  27. static void KEY_GPIO_Config(void)
  28. {
  29. GPIO_InitTypeDef GPIO_InitStructure;
  30. /*开启按键GPIO口的时钟*/
  31. RCC_APB2PeriphClockCmd(KEY1_PORT_CLK | KEY2_PORT_CLK,ENABLE);
  32. /*初始化GPIO*/
  33. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  34. GPIO_InitStructure.GPIO_Pin = KEY1_PIN;
  35. GPIO_Init(KEY1_PORT,&GPIO_InitStructure);
  36. GPIO_InitStructure.GPIO_Pin = KEY2_PIN;
  37. GPIO_Init(KEY2_PORT,&GPIO_InitStructure);
  38. }
  39. static uint8_t read_key1_pin(void)
  40. {
  41. return GPIO_ReadInputDataBit(KEY1_PORT, KEY1_PIN);
  42. }
  43. static uint8_t read_key2_pin(void)
  44. {
  45. return GPIO_ReadInputDataBit(KEY2_PORT, KEY2_PIN);
  46. }
  47. // key1短按切换页面
  48. static void key1_shortPress_handle(void *btn)
  49. {
  50. #ifdef DEBUG_printf
  51. printf("key1 short press\n");
  52. #endif
  53. }
  54. // key2短按切换选项 ·················
  55. static void key2_shortPress_handle(void *btn)
  56. {
  57. #ifdef DEBUG_printf
  58. printf("key2 short press\n");
  59. #endif
  60. }
  61. // key1长按
  62. static void key1_longPress_handle(void *btn)
  63. {
  64. #ifdef DEBUG_printf
  65. printf("key1 long press\n");
  66. #endif
  67. }
  68. // key2长按
  69. static void key2_longPress_handle(void *btn)
  70. {
  71. #ifdef DEBUG_printf
  72. printf("key2 long press\n");
  73. #endif
  74. }
  75. static void KEY_Init(void)
  76. {
  77. KEY_GPIO_Config();
  78. button_init(&key1, read_key1_pin, 0);
  79. button_init(&key2, read_key2_pin, 0);
  80. button_attach(&key1, SINGLE_CLICK, key1_shortPress_handle);
  81. button_attach(&key2, SINGLE_CLICK, key2_shortPress_handle);
  82. button_attach(&key1, LONG_PRESS_START, key1_longPress_handle);
  83. button_attach(&key2, LONG_PRESS_START, key2_longPress_handle);
  84. button_start(&key1);
  85. button_start(&key2);
  86. }
  87. static void KEY_Scan(void)
  88. {
  89. button_ticks();
  90. }