Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.7 KiB

  1. /*
  2. Debounce buttons and trigger events
  3. Copyright (C) 2015 by Xose Pérez <xose dot perez at gmail dot com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef _DEBOUNCE_EVENT_h
  16. #define _DEBOUNCE_EVENT_h
  17. #define DEBOUNCE_DELAY 50
  18. #define LONG_CLICK_DELAY 1000
  19. #define DOUBLE_CLICK_DELAY 500
  20. #define EVENT_NONE 0
  21. #define EVENT_CHANGED 1
  22. #define EVENT_PRESSED 2
  23. #define EVENT_RELEASED 3
  24. #define EVENT_SINGLE_CLICK 3
  25. #define EVENT_DOUBLE_CLICK 4
  26. #define EVENT_LONG_CLICK 5
  27. typedef void(*callback_t)(uint8_t pin, uint8_t event);
  28. class DebounceEvent {
  29. private:
  30. uint8_t _pin;
  31. uint8_t _status;
  32. uint8_t _event;
  33. unsigned long _this_start;
  34. unsigned long _last_start;
  35. uint8_t _defaultStatus;
  36. unsigned long _delay;
  37. callback_t _callback = false;
  38. public:
  39. DebounceEvent(uint8_t pin, callback_t callback, uint8_t defaultStatus = HIGH, unsigned long delay = DEBOUNCE_DELAY);
  40. DebounceEvent(uint8_t pin, uint8_t defaultStatus = HIGH, unsigned long delay = DEBOUNCE_DELAY);
  41. bool pressed();
  42. bool loop();
  43. uint8_t getEvent();
  44. };
  45. #endif