123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- #include "Arduino.h"
- #include "avdweb_Switch.h"
- Switch::Switch(byte _pin, byte PinMode, bool polarity, int debouncePeriod, int longPressPeriod, int doubleClickPeriod, int deglitchPeriod):
- pin(_pin), polarity(polarity), deglitchPeriod(deglitchPeriod), debouncePeriod(debouncePeriod), longPressPeriod(longPressPeriod), doubleClickPeriod(doubleClickPeriod)
- { pinMode(pin, PinMode);
- switchedTime = millis();
- debounced = digitalRead(pin);
- singleClickDisable = true;
- }
- bool Switch::poll()
- { input = digitalRead(pin);
- ms = millis();
- return process();
- }
- bool Switch::process()
- { deglitch();
- debounce();
- calcSingleClick();
- calcDoubleClick();
- calcLongPress();
- if(switched())
- { switchedTime = ms;
- if(pushed())
- { pushedTime = ms;
- } else { releasedTime = ms;
- }
- }
- triggerCallbacks();
- return _switched;
- }
- void inline Switch::deglitch()
- {
- if(input == lastInput) equal = 1;
- else
- { equal = 0;
- deglitchTime = ms;
- }
- if(equal && ((ms - deglitchTime) > deglitchPeriod))
- { deglitched = input;
- deglitchTime = ms;
- }
- lastInput = input;
- }
- void inline Switch::debounce()
- {
- _switched = 0;
- if((deglitched != debounced) && ((ms - switchedTime) > debouncePeriod))
- { debounced = deglitched;
- _switched = 1;
- }
- }
- void inline Switch::calcSingleClick()
- { _singleClick = false;
- if(pushed())
- { if((ms - pushedTime) >= doubleClickPeriod)
- { singleClickDisable = false;
- } else { singleClickDisable = true;
- }
- }
- if(!singleClickDisable)
- { _singleClick = !switched() && !on() && ((releasedTime - pushedTime) <= longPressPeriod) && ((ms - pushedTime) >= doubleClickPeriod);
- singleClickDisable = _singleClick;
- }
- }
- void inline Switch::calcDoubleClick()
- { _doubleClick = pushed() && ((ms - pushedTime) < doubleClickPeriod);
- }
- void inline Switch::calcLongPress()
- { _longPress = false;
- if(released()) longPressDisable = false;
- if(!longPressDisable)
- { _longPress = !switched() && on() && ((ms - pushedTime) > longPressPeriod);
- longPressDisable = _longPress;
- }
- }
- bool Switch::switched()
- { return _switched;
- }
- bool Switch::on()
- { return !(debounced^polarity);
- }
- bool Switch::pushed()
- { return _switched && !(debounced^polarity);
- }
- bool Switch::released()
- { return _switched && (debounced^polarity);
- }
- bool Switch::longPress()
- { return _longPress;
- }
- bool Switch::doubleClick()
- { return _doubleClick;
- }
- bool Switch::singleClick()
- { return _singleClick;
- }
- void Switch::triggerCallbacks()
- {
- if(_pushedCallback && pushed())
- { _pushedCallback(_pushedCallbackParam);
- }
- else if(_releasedCallback && released())
- { _releasedCallback(_releasedCallbackParam);
- }
- if(_longPressCallback && longPress())
- { _longPressCallback(_longPressCallbackParam);
- }
- if(_doubleClickCallback && doubleClick())
- { _doubleClickCallback(_doubleClickCallbackParam);
- }
- if(_singleClickCallback && singleClick())
- { _singleClickCallback(_singleClickCallbackParam);
- }
- }
- void Switch::setPushedCallback(switchCallback_t cb, void* param)
- {
- _pushedCallback = cb;
- _pushedCallbackParam = param;
- }
- void Switch::setReleasedCallback(switchCallback_t cb, void* param)
- {
- _releasedCallback = cb;
- _releasedCallbackParam = param;
- }
- void Switch::setLongPressCallback(switchCallback_t cb, void* param)
- {
- _longPressCallback = cb;
- _longPressCallbackParam = param;
- }
- void Switch::setDoubleClickCallback(switchCallback_t cb, void* param)
- {
- _doubleClickCallback = cb;
- _doubleClickCallbackParam = param;
- }
- void Switch::setSingleClickCallback(switchCallback_t cb, void* param)
- {
- _singleClickCallback = cb;
- _singleClickCallbackParam = param;
- }
|