1
0

avdweb_Switch.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. Switch.cpp
  3. Copyright (C) 2012 Albert van Dalen http://www.avdweb.nl
  4. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  6. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  7. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License at http://www.gnu.org/licenses .
  8. AUTHOR: Albert van Dalen
  9. WEBSITE: http://www.avdweb.nl/arduino/hardware-interfacing/simple-switch-debouncer.html
  10. */
  11. #ifndef AVDWEB_SWITCH_H
  12. #define AVDWEB_SWITCH_H
  13. typedef void (*switchCallback_t)(void*);
  14. class Switch
  15. {
  16. public:
  17. Switch(byte _pin, byte PinMode=INPUT_PULLUP, bool polarity=LOW, int debouncePeriod=50, int longPressPeriod=300, int doubleClickPeriod=250, int deglitchPeriod=10);
  18. bool poll(); // Returns 1 if switched
  19. bool switched(); // will be refreshed by poll()
  20. bool on();
  21. bool pushed(); // will be refreshed by poll()
  22. bool released(); // will be refreshed by poll()
  23. bool longPress(); // will be refreshed by poll()
  24. bool doubleClick(); // will be refreshed by poll()
  25. bool singleClick(); // will be refreshed by poll()
  26. // Set methods for event callbacks
  27. void setPushedCallback(switchCallback_t cb, void* param = nullptr);
  28. void setReleasedCallback(switchCallback_t cb, void* param = nullptr);
  29. void setLongPressCallback(switchCallback_t cb, void* param = nullptr);
  30. void setDoubleClickCallback(switchCallback_t cb, void* param = nullptr);
  31. void setSingleClickCallback(switchCallback_t cb, void* param = nullptr);
  32. protected:
  33. bool process(); // not inline, used in child class
  34. void inline deglitch();
  35. void inline debounce();
  36. void inline calcLongPress();
  37. void inline calcDoubleClick();
  38. void inline calcSingleClick();
  39. void triggerCallbacks();
  40. unsigned long deglitchTime, switchedTime, pushedTime, releasedTime, ms;
  41. const byte pin;
  42. const int deglitchPeriod, debouncePeriod, longPressPeriod, doubleClickPeriod;
  43. const bool polarity;
  44. bool input, lastInput, equal, deglitched, debounced, _switched, _longPress, longPressDisable, _doubleClick, _singleClick, singleClickDisable;
  45. // Event callbacks
  46. switchCallback_t _pushedCallback = nullptr;
  47. switchCallback_t _releasedCallback = nullptr;
  48. switchCallback_t _longPressCallback = nullptr;
  49. switchCallback_t _doubleClickCallback = nullptr;
  50. switchCallback_t _singleClickCallback = nullptr;
  51. void* _pushedCallbackParam = nullptr;
  52. void* _releasedCallbackParam = nullptr;
  53. void* _longPressCallbackParam = nullptr;
  54. void* _doubleClickCallbackParam = nullptr;
  55. void* _singleClickCallbackParam = nullptr;
  56. };
  57. #endif