button.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "button.h"
  2. #include <button_gpio.h>
  3. #include <esp_log.h>
  4. #define TAG "Button"
  5. #if CONFIG_SOC_ADC_SUPPORTED
  6. AdcButton::AdcButton(const button_adc_config_t& adc_config) : Button(nullptr) {
  7. button_config_t btn_config = {
  8. .long_press_time = 2000,
  9. .short_press_time = 0,
  10. };
  11. ESP_ERROR_CHECK(iot_button_new_adc_device(&btn_config, &adc_config, &button_handle_));
  12. }
  13. #endif
  14. Button::Button(button_handle_t button_handle) : button_handle_(button_handle) {
  15. }
  16. Button::Button(gpio_num_t gpio_num, bool active_high, uint16_t long_press_time, uint16_t short_press_time, bool enable_power_save) : gpio_num_(gpio_num) {
  17. if (gpio_num == GPIO_NUM_NC) {
  18. return;
  19. }
  20. button_config_t button_config = {
  21. .long_press_time = long_press_time,
  22. .short_press_time = short_press_time
  23. };
  24. button_gpio_config_t gpio_config = {
  25. .gpio_num = gpio_num,
  26. .active_level = static_cast<uint8_t>(active_high ? 1 : 0),
  27. .enable_power_save = enable_power_save,
  28. .disable_pull = false
  29. };
  30. ESP_ERROR_CHECK(iot_button_new_gpio_device(&button_config, &gpio_config, &button_handle_));
  31. }
  32. Button::~Button() {
  33. if (button_handle_ != NULL) {
  34. iot_button_delete(button_handle_);
  35. }
  36. }
  37. void Button::OnPressDown(std::function<void()> callback) {
  38. if (button_handle_ == nullptr) {
  39. return;
  40. }
  41. on_press_down_ = callback;
  42. iot_button_register_cb(button_handle_, BUTTON_PRESS_DOWN, nullptr, [](void* handle, void* usr_data) {
  43. Button* button = static_cast<Button*>(usr_data);
  44. if (button->on_press_down_) {
  45. button->on_press_down_();
  46. }
  47. }, this);
  48. }
  49. void Button::OnPressUp(std::function<void()> callback) {
  50. if (button_handle_ == nullptr) {
  51. return;
  52. }
  53. on_press_up_ = callback;
  54. iot_button_register_cb(button_handle_, BUTTON_PRESS_UP, nullptr, [](void* handle, void* usr_data) {
  55. Button* button = static_cast<Button*>(usr_data);
  56. if (button->on_press_up_) {
  57. button->on_press_up_();
  58. }
  59. }, this);
  60. }
  61. void Button::OnLongPress(std::function<void()> callback) {
  62. if (button_handle_ == nullptr) {
  63. return;
  64. }
  65. on_long_press_ = callback;
  66. iot_button_register_cb(button_handle_, BUTTON_LONG_PRESS_START, nullptr, [](void* handle, void* usr_data) {
  67. Button* button = static_cast<Button*>(usr_data);
  68. if (button->on_long_press_) {
  69. button->on_long_press_();
  70. }
  71. }, this);
  72. }
  73. void Button::OnClick(std::function<void()> callback) {
  74. if (button_handle_ == nullptr) {
  75. return;
  76. }
  77. on_click_ = callback;
  78. iot_button_register_cb(button_handle_, BUTTON_SINGLE_CLICK, nullptr, [](void* handle, void* usr_data) {
  79. Button* button = static_cast<Button*>(usr_data);
  80. if (button->on_click_) {
  81. button->on_click_();
  82. }
  83. }, this);
  84. }
  85. void Button::OnDoubleClick(std::function<void()> callback) {
  86. if (button_handle_ == nullptr) {
  87. return;
  88. }
  89. on_double_click_ = callback;
  90. iot_button_register_cb(button_handle_, BUTTON_DOUBLE_CLICK, nullptr, [](void* handle, void* usr_data) {
  91. Button* button = static_cast<Button*>(usr_data);
  92. if (button->on_double_click_) {
  93. button->on_double_click_();
  94. }
  95. }, this);
  96. }
  97. void Button::OnMultipleClick(std::function<void()> callback, uint8_t click_count) {
  98. if (button_handle_ == nullptr) {
  99. return;
  100. }
  101. on_multiple_click_ = callback;
  102. button_event_args_t event_args = {
  103. .multiple_clicks = {
  104. .clicks = click_count
  105. }
  106. };
  107. iot_button_register_cb(button_handle_, BUTTON_MULTIPLE_CLICK, &event_args, [](void* handle, void* usr_data) {
  108. Button* button = static_cast<Button*>(usr_data);
  109. if (button->on_multiple_click_) {
  110. button->on_multiple_click_();
  111. }
  112. }, this);
  113. }