avdweb_Switch.cpp
Switch - Arduino switch and button library with Short/Long Press,
Double Click and Beep.
Sources
https://github.com/avandalen/avdweb_Switch
Website
http://www.avdweb.nl/arduino/hardware-interfacing/simple-switch-debouncer.html
License
SPDX: GPL-3.0-or-later
Switch - Arduino switch and button library with Short/Long Press, Double Click and Beep. Copyright (C) 2012-2024 Albert van Dalen http://www.avdweb.nl Copyright (C) 2024 Abhijit Bose (aka. Boseji) :contributor:
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.
Notes
HISTORY: 1.0.0 20-04-2013 _debouncePeriod=50 1.0.1 22-05-2013 Added longPress, doubleClick 1.0.2 01-12-2015 added process(input) 1.0.3 15-01-2016 added deglitching 1.0.5 25-01-2017 file renamed to avdweb_Switch 1.1.0 28-07-2018 added callbacks (code by Sean Lanigan, added by Martin Laclaustra) 1.2.0-rc 28-07-2018 added singleclick. Reorganize, keeping variables for each event in one function 1.2.0 29-09-2018 released 1.2.1 30-11-2018 bugfix. Initialize time variables in the constructor. Fixes false event if first call to poll was delayed 1.2.2 18-10-2019 beep when a switch is pressed with using a setBeepAllCallback function 1.2.3 03-04-2020 made public: deglitchPeriod, debouncePeriod, longPressPeriod, doubleClickPeriod 1.2.4 14-07-2021 "static switchCallback_t _beepAllCallback = nullptr;" gives error with SAMD21, removed "= nullptr;"
.......................................DEGLITCHING..............................
________________ _
on | | | | _
| | | | | |
| |_| |___| |__
analog off_____|_____________________________|____________________________
________________ _ _
input _____| |_| |___| |_______________________________
poll ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
equal 0 1 1 0 1 1 1 1 1 1 1 1 0 0 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
deglitchPeriod <-----—><– <– <- <-----—><-----—><-----—
deglitched _________________| |__________________
deglitchTime ^ ^ ^ ^ ^ ^ ^
........................................DEBOUNCING.............................
debouncePeriod <-----------------------------—>
debounced _________________| |____________ _ _ _switched _________________| |_______________________________| |__________
switchedTime ^ ^
......................................DOUBLE CLICK..............................
__________ ______
debounced ________| |_______| |_____________________________
poll ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ _ _ pushed _________| |________________| |__________________________________
pushedTime ^ ^ _ _ released ____________________| |____________| |___________________________
releasedTime ^ ^
doubleClickPeriod <----------------------------------—> _ _doubleClick ___________________________| |__________________________________
......................................LONG PRESS................................
___________________________
debounced ________| |___________________________
poll ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
longPressPeriod <------------—> _ _ _switched _________| |_________________________| |________________________
longPressDisable__________________________| |_________________________ _ _longPress ___________________________| |__________________________________
......................................SINGLE CLICK..............................
__________ ______
debounced ________| |_______________________________| |_____
poll ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
longPressPeriod <------------—> doubleClickPeriod <----------------------------------—>
_switched _________| |_______| |_____________________________| |____| |___
singleClickDisable____________________________________________| |__________ _ _singleClick _______________________________________________| |______________