button.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #ifndef BUTTON_H_
  2. #define BUTTON_H_
  3. #include <driver/gpio.h>
  4. #include <iot_button.h>
  5. #include <button_types.h>
  6. #include <button_adc.h>
  7. #include <button_gpio.h>
  8. #include <functional>
  9. class Button {
  10. public:
  11. Button(button_handle_t button_handle);
  12. Button(gpio_num_t gpio_num, bool active_high = false, uint16_t long_press_time = 0, uint16_t short_press_time = 0, bool enable_power_save = false);
  13. ~Button();
  14. void OnPressDown(std::function<void()> callback);
  15. void OnPressUp(std::function<void()> callback);
  16. void OnLongPress(std::function<void()> callback);
  17. void OnClick(std::function<void()> callback);
  18. void OnDoubleClick(std::function<void()> callback);
  19. void OnMultipleClick(std::function<void()> callback, uint8_t click_count = 3);
  20. protected:
  21. gpio_num_t gpio_num_;
  22. button_handle_t button_handle_ = nullptr;
  23. std::function<void()> on_press_down_;
  24. std::function<void()> on_press_up_;
  25. std::function<void()> on_long_press_;
  26. std::function<void()> on_click_;
  27. std::function<void()> on_double_click_;
  28. std::function<void()> on_multiple_click_;
  29. };
  30. #if CONFIG_SOC_ADC_SUPPORTED
  31. class AdcButton : public Button {
  32. public:
  33. AdcButton(const button_adc_config_t& adc_config);
  34. };
  35. #endif
  36. class PowerSaveButton : public Button {
  37. public:
  38. PowerSaveButton(gpio_num_t gpio_num) : Button(gpio_num, false, 0, 0, true) {
  39. }
  40. };
  41. #endif // BUTTON_H_