DCC-EX Simple Throttle
Simple throttle for a DCC-EX CommandStation
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
Switch Class Reference

#include <avdweb_Switch.h>

Public Member Functions

 Switch (byte _pin, byte PinMode=INPUT_PULLUP, bool polarity=LOW, unsigned long debouncePeriod=50, unsigned long longPressPeriod=300, unsigned long doubleClickPeriod=250, unsigned long deglitchPeriod=10)
 
bool poll ()
 
bool switched ()
 
bool on ()
 
bool pushed ()
 
bool released ()
 
bool longPress ()
 
bool doubleClick ()
 
bool singleClick ()
 
void setPushedCallback (switchCallback_t cb, void *param=nullptr)
 
void setReleasedCallback (switchCallback_t cb, void *param=nullptr)
 
void setLongPressCallback (switchCallback_t cb, void *param=nullptr)
 
void setDoubleClickCallback (switchCallback_t cb, void *param=nullptr)
 
void setSingleClickCallback (switchCallback_t cb, void *param=nullptr)
 
void setBeepAllCallback (switchCallback_t cb, void *param=nullptr)
 

Public Attributes

unsigned long deglitchPeriod
 
unsigned long debouncePeriod
 
unsigned long longPressPeriod
 
unsigned long doubleClickPeriod
 

Protected Member Functions

bool process ()
 
void deglitch ()
 
void debounce ()
 
void calcLongPress ()
 
void calcDoubleClick ()
 
void calcSingleClick ()
 
void triggerCallbacks ()
 

Protected Attributes

unsigned long deglitchTime
 
unsigned long switchedTime
 
unsigned long pushedTime
 
unsigned long releasedTime
 
unsigned long ms
 
const byte pin
 
const bool polarity
 
bool input
 
bool lastInput
 
bool equal
 
bool deglitched
 
bool debounced
 
bool _switched
 
bool _longPress
 
bool longPressDisable
 
bool _doubleClick
 
bool _singleClick
 
bool singleClickDisable
 
switchCallback_t _pushedCallback = nullptr
 
switchCallback_t _releasedCallback = nullptr
 
switchCallback_t _longPressCallback = nullptr
 
switchCallback_t _doubleClickCallback = nullptr
 
switchCallback_t _singleClickCallback = nullptr
 
void * _pushedCallbackParam = nullptr
 
void * _releasedCallbackParam = nullptr
 
void * _longPressCallbackParam = nullptr
 
void * _doubleClickCallbackParam = nullptr
 
void * _singleClickCallbackParam = nullptr
 

Static Protected Attributes

static switchCallback_t _beepAllCallback = nullptr
 
static void * _beepAllCallbackParam = nullptr
 

Constructor & Destructor Documentation

◆ Switch()

Switch::Switch ( byte  _pin,
byte  PinMode = INPUT_PULLUP,
bool  polarity = LOW,
unsigned long  debouncePeriod = 50,
unsigned long  longPressPeriod = 300,
unsigned long  doubleClickPeriod = 250,
unsigned long  deglitchPeriod = 10 
)
Here is the call graph for this function:

Member Function Documentation

◆ calcDoubleClick()

void Switch::calcDoubleClick ( )
inlineprotected
Here is the call graph for this function:
Here is the caller graph for this function:

◆ calcLongPress()

void Switch::calcLongPress ( )
inlineprotected
Here is the call graph for this function:
Here is the caller graph for this function:

◆ calcSingleClick()

void Switch::calcSingleClick ( )
inlineprotected
Here is the call graph for this function:
Here is the caller graph for this function:

◆ debounce()

void Switch::debounce ( )
inlineprotected
Here is the caller graph for this function:

◆ deglitch()

void Switch::deglitch ( )
inlineprotected
Here is the caller graph for this function:

◆ doubleClick()

bool Switch::doubleClick ( )
Here is the caller graph for this function:

◆ longPress()

bool Switch::longPress ( )
Here is the caller graph for this function:

◆ on()

bool Switch::on ( )
Here is the caller graph for this function:

◆ poll()

bool Switch::poll ( )
Here is the call graph for this function:
Here is the caller graph for this function:

◆ process()

bool Switch::process ( )
protected
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pushed()

bool Switch::pushed ( )
Here is the caller graph for this function:

◆ released()

bool Switch::released ( )
Here is the caller graph for this function:

◆ setBeepAllCallback()

void Switch::setBeepAllCallback ( switchCallback_t  cb,
void *  param = nullptr 
)

◆ setDoubleClickCallback()

void Switch::setDoubleClickCallback ( switchCallback_t  cb,
void *  param = nullptr 
)

◆ setLongPressCallback()

void Switch::setLongPressCallback ( switchCallback_t  cb,
void *  param = nullptr 
)

◆ setPushedCallback()

void Switch::setPushedCallback ( switchCallback_t  cb,
void *  param = nullptr 
)

◆ setReleasedCallback()

void Switch::setReleasedCallback ( switchCallback_t  cb,
void *  param = nullptr 
)

◆ setSingleClickCallback()

void Switch::setSingleClickCallback ( switchCallback_t  cb,
void *  param = nullptr 
)

◆ singleClick()

bool Switch::singleClick ( )
Here is the caller graph for this function:

◆ switched()

bool Switch::switched ( )
Here is the caller graph for this function:

◆ triggerCallbacks()

void Switch::triggerCallbacks ( )
protected
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _beepAllCallback

switchCallback_t Switch::_beepAllCallback = nullptr
staticprotected

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 _______________________________________________| |______________

◆ _beepAllCallbackParam

void * Switch::_beepAllCallbackParam = nullptr
staticprotected

◆ _doubleClick

bool Switch::_doubleClick
protected

◆ _doubleClickCallback

switchCallback_t Switch::_doubleClickCallback = nullptr
protected

◆ _doubleClickCallbackParam

void* Switch::_doubleClickCallbackParam = nullptr
protected

◆ _longPress

bool Switch::_longPress
protected

◆ _longPressCallback

switchCallback_t Switch::_longPressCallback = nullptr
protected

◆ _longPressCallbackParam

void* Switch::_longPressCallbackParam = nullptr
protected

◆ _pushedCallback

switchCallback_t Switch::_pushedCallback = nullptr
protected

◆ _pushedCallbackParam

void* Switch::_pushedCallbackParam = nullptr
protected

◆ _releasedCallback

switchCallback_t Switch::_releasedCallback = nullptr
protected

◆ _releasedCallbackParam

void* Switch::_releasedCallbackParam = nullptr
protected

◆ _singleClick

bool Switch::_singleClick
protected

◆ _singleClickCallback

switchCallback_t Switch::_singleClickCallback = nullptr
protected

◆ _singleClickCallbackParam

void* Switch::_singleClickCallbackParam = nullptr
protected

◆ _switched

bool Switch::_switched
protected

◆ debounced

bool Switch::debounced
protected

◆ debouncePeriod

unsigned long Switch::debouncePeriod

◆ deglitched

bool Switch::deglitched
protected

◆ deglitchPeriod

unsigned long Switch::deglitchPeriod

◆ deglitchTime

unsigned long Switch::deglitchTime
protected

◆ doubleClickPeriod

unsigned long Switch::doubleClickPeriod

◆ equal

bool Switch::equal
protected

◆ input

bool Switch::input
protected

◆ lastInput

bool Switch::lastInput
protected

◆ longPressDisable

bool Switch::longPressDisable
protected

◆ longPressPeriod

unsigned long Switch::longPressPeriod

◆ ms

unsigned long Switch::ms
protected

◆ pin

const byte Switch::pin
protected

◆ polarity

const bool Switch::polarity
protected

◆ pushedTime

unsigned long Switch::pushedTime
protected

◆ releasedTime

unsigned long Switch::releasedTime
protected

◆ singleClickDisable

bool Switch::singleClickDisable
protected

◆ switchedTime

unsigned long Switch::switchedTime
protected

The documentation for this class was generated from the following files: