Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
avandalen authored Jul 14, 2021
2 parents 1b69535 + f7f6a29 commit 16b51ef
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 70 deletions.
1 change: 1 addition & 0 deletions avdweb_Switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ WEBSITE: http://www.avdweb.nl/arduino/hardware-interfacing/simple-switch-debounc
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..............................
Expand Down
143 changes: 73 additions & 70 deletions avdweb_Switch.h
Original file line number Diff line number Diff line change
@@ -1,70 +1,73 @@
/*
Switch.cpp
Copyright (C) 2012 Albert van Dalen http://www.avdweb.nl
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 at http://www.gnu.org/licenses .
AUTHOR: Albert van Dalen
WEBSITE: http://www.avdweb.nl/arduino/hardware-interfacing/simple-switch-debouncer.html
*/

#ifndef AVDWEB_SWITCH_H
#define AVDWEB_SWITCH_H

typedef void (*switchCallback_t)(void*);

class Switch
{
public:
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(); // Returns 1 if switched
bool switched(); // will be refreshed by poll()
bool on();
bool pushed(); // will be refreshed by poll()
bool released(); // will be refreshed by poll()
bool longPress(); // will be refreshed by poll()
bool doubleClick(); // will be refreshed by poll()
bool singleClick(); // will be refreshed by poll()

// Set methods for event callbacks
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);

unsigned long deglitchPeriod, debouncePeriod, longPressPeriod, doubleClickPeriod;

protected:
bool process(); // not inline, used in child class
void inline deglitch();
void inline debounce();
void inline calcLongPress();
void inline calcDoubleClick();
void inline calcSingleClick();
void triggerCallbacks();

unsigned long deglitchTime, switchedTime, pushedTime, releasedTime, ms;
const byte pin;
const bool polarity;
bool input, lastInput, equal, deglitched, debounced, _switched, _longPress, longPressDisable, _doubleClick, _singleClick, singleClickDisable;

// Event callbacks
switchCallback_t _pushedCallback = nullptr;
switchCallback_t _releasedCallback = nullptr;
switchCallback_t _longPressCallback = nullptr;
switchCallback_t _doubleClickCallback = nullptr;
switchCallback_t _singleClickCallback = nullptr;
static switchCallback_t _beepAllCallback; // static function pointer, can be used by all objects

void* _pushedCallbackParam = nullptr;
void* _releasedCallbackParam = nullptr;
void* _longPressCallbackParam = nullptr;
void* _doubleClickCallbackParam = nullptr;
void* _singleClickCallbackParam = nullptr;
static void* _beepAllCallbackParam; // can be used by all objects
};
#endif
/*
Switch.cpp
Copyright (C) 2012 Albert van Dalen http://www.avdweb.nl
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 at http://www.gnu.org/licenses .
AUTHOR: Albert van Dalen
WEBSITE: http://www.avdweb.nl/arduino/hardware-interfacing/simple-switch-debouncer.html
*/

#ifndef AVDWEB_SWITCH_H
#define AVDWEB_SWITCH_H

typedef void (*switchCallback_t)(void*);

class Switch
{
public:
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(); // Returns 1 if switched
bool switched(); // will be refreshed by poll()
bool on();
bool pushed(); // will be refreshed by poll()
bool released(); // will be refreshed by poll()
bool longPress(); // will be refreshed by poll()
bool doubleClick(); // will be refreshed by poll()
bool singleClick(); // will be refreshed by poll()

// Set methods for event callbacks
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);

unsigned long deglitchPeriod, debouncePeriod, longPressPeriod, doubleClickPeriod;

protected:
bool process(); // not inline, used in child class
void inline deglitch();
void inline debounce();
void inline calcLongPress();
void inline calcDoubleClick();
void inline calcSingleClick();
void triggerCallbacks();

unsigned long deglitchTime, switchedTime, pushedTime, releasedTime, ms;
const byte pin;
const bool polarity;
bool input, lastInput, equal, deglitched, debounced, _switched, _longPress, longPressDisable, _doubleClick, _singleClick, singleClickDisable;

// Event callbacks
switchCallback_t _pushedCallback = nullptr;
switchCallback_t _releasedCallback = nullptr;
switchCallback_t _longPressCallback = nullptr;
switchCallback_t _doubleClickCallback = nullptr;
switchCallback_t _singleClickCallback = nullptr;
static switchCallback_t _beepAllCallback; // static function pointer, can be used by all objects
//static switchCallback_t _beepAllCallback = nullptr; // gives error with SAMD21
//static void(*_beepAllCallback)(void*) = nullptr; // static function pointer without typedef

void* _pushedCallbackParam = nullptr;
void* _releasedCallbackParam = nullptr;
void* _longPressCallbackParam = nullptr;
void* _doubleClickCallbackParam = nullptr;
void* _singleClickCallbackParam = nullptr;
static void* _beepAllCallbackParam; // can be used by all objects
//static void* _beepAllCallbackParam = nullptr; // gives error with SAMD21
};
#endif

0 comments on commit 16b51ef

Please sign in to comment.