example.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "button.h"
  2. #define BTN1 GET_PIN(E, 6)
  3. #define BTN2 GET_PIN(I, 8)
  4. /**
  5. ******************************************************************
  6. Variable declaration
  7. ******************************************************************
  8. */
  9. Button_t Button1;
  10. Button_t Button2;
  11. /**
  12. ******************************************************************
  13. Function declaration
  14. ******************************************************************
  15. */
  16. uint8_t Btn1_Level(void)
  17. {
  18. return rt_pin_read(BTN1);
  19. }
  20. void Btn1_Click_CallBack(void *btn)
  21. {
  22. LOG_I("Button1 Click!");
  23. }
  24. void Btn1_Dowm_CallBack(void *btn)
  25. {
  26. LOG_I("Button1 Down!");
  27. }
  28. void Btn1_Up_CallBack(void *btn)
  29. {
  30. LOG_I("Button1 Release!");
  31. }
  32. void Btn1_Double_CallBack(void *btn)
  33. {
  34. LOG_I("Button1 Double click!");
  35. }
  36. void Btn1_Long_CallBack(void *btn)
  37. {
  38. LOG_I("Button1 Long press!");
  39. }
  40. void Btn1_Continuos_CallBack(void *btn)
  41. {
  42. LOG_I("Button1 Press continuously!");
  43. }
  44. void Btn1_ContinuosFree_CallBack(void *btn)
  45. {
  46. LOG_I("Button1 Press continuously free!");
  47. }
  48. uint8_t Btn2_Level(void)
  49. {
  50. return rt_pin_read(BTN2);
  51. }
  52. void Btn2_Click_CallBack(void *btn)
  53. {
  54. LOG_I("Button2 Click!");
  55. }
  56. void Btn2_Dowm_CallBack(void *btn)
  57. {
  58. LOG_I("Button2 Down!");
  59. }
  60. void Btn2_Up_CallBack(void *btn)
  61. {
  62. LOG_I("Button2 Release!");
  63. }
  64. void Btn2_Double_CallBack(void *btn)
  65. {
  66. LOG_I("Button2 Double click!");
  67. }
  68. void Btn2_Long_CallBack(void *btn)
  69. {
  70. LOG_I("Button2 Long press!");
  71. }
  72. void Btn2_Continuos_CallBack(void *btn)
  73. {
  74. LOG_I("Button2 Press continuously!");
  75. }
  76. void Btn2_ContinuosFree_CallBack(void *btn)
  77. {
  78. LOG_I("Button2 Press continuously free!");
  79. }
  80. /**
  81. ******************************************************************
  82. * @brief main
  83. * @author jiejie
  84. * @version V1.0
  85. * @date 2018-xx-xx
  86. ******************************************************************
  87. */
  88. void Button_Task(void *param)
  89. {
  90. /* button run */
  91. rt_pin_mode(BTN1, PIN_MODE_INPUT);
  92. rt_pin_mode(BTN2, PIN_MODE_INPUT);
  93. /* button input1 */
  94. gpio_init_structure.Pin = GPIO_PIN_8;
  95. gpio_init_structure.Mode = GPIO_MODE_INPUT;
  96. gpio_init_structure.Pull = GPIO_PULLUP;
  97. gpio_init_structure.Speed = GPIO_SPEED_HIGH;
  98. __HAL_RCC_GPIOI_CLK_ENABLE();
  99. HAL_GPIO_Init(GPIOI, &gpio_init_structure);
  100. Button_Create("Button1",
  101. &Button1,
  102. Btn1_Level,
  103. KEY_ON);
  104. Button_Attach(&Button1,BUTTON_CLICK,Btn1_Click_CallBack); //Click
  105. Button_Attach(&Button1,BUTTON_DOWM,Btn1_Dowm_CallBack); //Down
  106. Button_Attach(&Button1,BUTTON_UP,Btn1_Up_CallBack); //Release
  107. Button_Attach(&Button1,BUTTON_DOUBLE,Btn1_Double_CallBack); //Double click
  108. Button_Attach(&Button1,BUTTON_CONTINUOS,Btn1_Continuos_CallBack); //Press continuously
  109. Button_Attach(&Button1,BUTTON_CONTINUOS_FREE,Btn1_ContinuosFree_CallBack); //Press continuously free
  110. Button_Attach(&Button1,BUTTON_LONG,Btn1_Long_CallBack); //Long press
  111. Button_Create("Button2",
  112. &Button2,
  113. Btn2_Level,
  114. KEY_ON);
  115. Button_Attach(&Button2,BUTTON_CLICK,Btn2_Click_CallBack); //Click
  116. Button_Attach(&Button2,BUTTON_DOWM,Btn2_Dowm_CallBack); //Down
  117. Button_Attach(&Button2,BUTTON_UP,Btn2_Up_CallBack); //Release
  118. Button_Attach(&Button2,BUTTON_DOUBLE,Btn2_Double_CallBack); //Double click
  119. Button_Attach(&Button2,BUTTON_CONTINUOS,Btn2_Continuos_CallBack); //Press continuously
  120. Button_Attach(&Button2,BUTTON_CONTINUOS_FREE,Btn2_ContinuosFree_CallBack); //Press continuously free
  121. Button_Attach(&Button2,BUTTON_LONG,Btn2_Long_CallBack); //Long press
  122. Get_Button_Event(&Button1);
  123. Get_Button_Event(&Button2);
  124. while(1)
  125. {
  126. Button_Process(); //Need to call the button handler function periodically
  127. Delay_ms(20);
  128. }
  129. }
  130. /**
  131. ******************************************************************
  132. * @brief BSP_Init
  133. * @author jiejie
  134. * @version V1.0
  135. * @date 2018-xx-xx
  136. ******************************************************************
  137. */
  138. static void Button_Init(void)
  139. {
  140. rt_thread_t thread;
  141. thread = rt_thread_create("button", Button_Task, NULL, 512, 20, 20);
  142. }
  143. INIT_APP_EXPORT(Button_Init);
  144. /********************************END OF FILE***************************************/