button.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "button.h"
  2. #include <esp_log.h>
  3. static const char* TAG = "Button";
  4. #if CONFIG_SOC_ADC_SUPPORTED
  5. Button::Button(const button_adc_config_t& adc_cfg) {
  6. button_config_t button_config = {
  7. .type = BUTTON_TYPE_ADC,
  8. .long_press_time = 1000,
  9. .short_press_time = 50,
  10. .adc_button_config = adc_cfg
  11. };
  12. button_handle_ = iot_button_create(&button_config);
  13. if (button_handle_ == NULL) {
  14. ESP_LOGE(TAG, "Failed to create button handle");
  15. return;
  16. }
  17. }
  18. #endif
  19. Button::Button(gpio_num_t gpio_num, bool active_high) : gpio_num_(gpio_num) {
  20. if (gpio_num == GPIO_NUM_NC) {
  21. return;
  22. }
  23. button_config_t button_config = {
  24. .type = BUTTON_TYPE_GPIO,
  25. .long_press_time = 1000,
  26. .short_press_time = 50,
  27. .gpio_button_config = {
  28. .gpio_num = gpio_num,
  29. .active_level = static_cast<uint8_t>(active_high ? 1 : 0)
  30. }
  31. };
  32. button_handle_ = iot_button_create(&button_config);
  33. if (button_handle_ == NULL) {
  34. ESP_LOGE(TAG, "Failed to create button handle");
  35. return;
  36. }
  37. }
  38. Button::~Button() {
  39. if (button_handle_ != NULL) {
  40. iot_button_delete(button_handle_);
  41. }
  42. }
  43. void Button::OnPressDown(std::function<void()> callback) {
  44. if (button_handle_ == nullptr) {
  45. return;
  46. }
  47. on_press_down_ = callback;
  48. iot_button_register_cb(button_handle_, BUTTON_PRESS_DOWN, [](void* handle, void* usr_data) {
  49. Button* button = static_cast<Button*>(usr_data);
  50. if (button->on_press_down_) {
  51. button->on_press_down_();
  52. }
  53. }, this);
  54. }
  55. void Button::OnPressUp(std::function<void()> callback) {
  56. if (button_handle_ == nullptr) {
  57. return;
  58. }
  59. on_press_up_ = callback;
  60. iot_button_register_cb(button_handle_, BUTTON_PRESS_UP, [](void* handle, void* usr_data) {
  61. Button* button = static_cast<Button*>(usr_data);
  62. if (button->on_press_up_) {
  63. button->on_press_up_();
  64. }
  65. }, this);
  66. }
  67. void Button::OnLongPress(std::function<void()> callback) {
  68. if (button_handle_ == nullptr) {
  69. return;
  70. }
  71. on_long_press_ = callback;
  72. iot_button_register_cb(button_handle_, BUTTON_LONG_PRESS_START, [](void* handle, void* usr_data) {
  73. Button* button = static_cast<Button*>(usr_data);
  74. if (button->on_long_press_) {
  75. button->on_long_press_();
  76. }
  77. }, this);
  78. }
  79. void Button::OnClick(std::function<void()> callback) {
  80. if (button_handle_ == nullptr) {
  81. return;
  82. }
  83. on_click_ = callback;
  84. iot_button_register_cb(button_handle_, BUTTON_SINGLE_CLICK, [](void* handle, void* usr_data) {
  85. Button* button = static_cast<Button*>(usr_data);
  86. if (button->on_click_) {
  87. button->on_click_();
  88. }
  89. }, this);
  90. }
  91. void Button::OnDoubleClick(std::function<void()> callback) {
  92. if (button_handle_ == nullptr) {
  93. return;
  94. }
  95. on_double_click_ = callback;
  96. iot_button_register_cb(button_handle_, BUTTON_DOUBLE_CLICK, [](void* handle, void* usr_data) {
  97. Button* button = static_cast<Button*>(usr_data);
  98. if (button->on_double_click_) {
  99. button->on_double_click_();
  100. }
  101. }, this);
  102. }