MomentaryButton.h 966 B

123456789101112131415161718192021222324252627282930313233
  1. #pragma once
  2. #include <Arduino.h>
  3. #define BUTTON_EVENT_NONE 0
  4. #define BUTTON_EVENT_CLICK 1
  5. #define BUTTON_EVENT_LONG_PRESS 2
  6. #define BUTTON_EVENT_DOUBLE_CLICK 3
  7. #define BUTTON_EVENT_TRIPLE_CLICK 4
  8. class MomentaryButton {
  9. int8_t _pin;
  10. int8_t prev, cancel;
  11. bool _reverse, _pull;
  12. int _long_millis;
  13. int _threshold; // analog mode
  14. unsigned long down_at;
  15. uint8_t _click_count;
  16. unsigned long _last_click_time;
  17. int _multi_click_window;
  18. bool _pending_click;
  19. bool isPressed(int level) const;
  20. public:
  21. MomentaryButton(int8_t pin, int long_press_mills=0, bool reverse=false, bool pulldownup=false, bool multiclick=true);
  22. MomentaryButton(int8_t pin, int long_press_mills, int analog_threshold);
  23. void begin();
  24. int check(bool repeat_click=false); // returns one of BUTTON_EVENT_*
  25. void cancelClick(); // suppress next BUTTON_EVENT_CLICK (if already in DOWN state)
  26. uint8_t getPin() { return _pin; }
  27. bool isPressed() const;
  28. };