example_agile_button.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <agile_button.h>
  2. #include <stdlib.h>
  3. #ifdef RT_USING_FINSH
  4. #include <finsh.h>
  5. #endif
  6. static void btn_click_event_cb(agile_btn_t *btn)
  7. {
  8. rt_kprintf("[button click event] pin:%d repeat:%d, hold_time:%d\r\n", btn->pin, btn->repeat_cnt, btn->hold_time);
  9. }
  10. static void btn_hold_event_cb(agile_btn_t *btn)
  11. {
  12. rt_kprintf("[button hold event] pin:%d hold_time:%d\r\n", btn->pin, btn->hold_time);
  13. }
  14. #ifdef RT_USING_HEAP
  15. static agile_btn_t *_dbtn = RT_NULL;
  16. static void dbtn_create(int argc, char **argv)
  17. {
  18. int pin = 0;
  19. int active = 0;
  20. if (argc < 3) {
  21. rt_kprintf("dbtn_create --use dbtn_create [pin] [active]\r\n");
  22. return;
  23. }
  24. pin = atoi(argv[1]);
  25. active = atoi(argv[2]);
  26. if (_dbtn) {
  27. agile_btn_delete(_dbtn);
  28. _dbtn = RT_NULL;
  29. }
  30. if (active == PIN_HIGH)
  31. _dbtn = agile_btn_create(pin, active, PIN_MODE_INPUT_PULLDOWN);
  32. else
  33. _dbtn = agile_btn_create(pin, active, PIN_MODE_INPUT_PULLUP);
  34. agile_btn_set_event_cb(_dbtn, BTN_CLICK_EVENT, btn_click_event_cb);
  35. agile_btn_set_event_cb(_dbtn, BTN_HOLD_EVENT, btn_hold_event_cb);
  36. agile_btn_start(_dbtn);
  37. }
  38. static void dbtn_delete(void)
  39. {
  40. if (_dbtn) {
  41. agile_btn_delete(_dbtn);
  42. _dbtn = RT_NULL;
  43. }
  44. }
  45. #ifdef RT_USING_FINSH
  46. MSH_CMD_EXPORT(dbtn_create, create btn);
  47. MSH_CMD_EXPORT(dbtn_delete, delete btn);
  48. #endif
  49. #endif /* RT_USING_HEAP */
  50. static agile_btn_t _sbtn;
  51. static void sbtn_init(int argc, char **argv)
  52. {
  53. int pin = 0;
  54. int active = 0;
  55. if (argc < 3) {
  56. rt_kprintf("sbtn_init --use sbtn_init [pin] [active]\r\n");
  57. return;
  58. }
  59. pin = atoi(argv[1]);
  60. active = atoi(argv[2]);
  61. agile_btn_stop(&_sbtn);
  62. if (active == PIN_HIGH)
  63. agile_btn_init(&_sbtn, pin, active, PIN_MODE_INPUT_PULLDOWN);
  64. else
  65. agile_btn_init(&_sbtn, pin, active, PIN_MODE_INPUT_PULLUP);
  66. agile_btn_set_event_cb(&_sbtn, BTN_CLICK_EVENT, btn_click_event_cb);
  67. agile_btn_set_event_cb(&_sbtn, BTN_HOLD_EVENT, btn_hold_event_cb);
  68. agile_btn_start(&_sbtn);
  69. }
  70. #ifdef RT_USING_FINSH
  71. MSH_CMD_EXPORT(sbtn_init, init btn);
  72. #endif