Skip to content

Commit 16b51ef

Browse files
authored
Merge branch 'master' into master
2 parents 1b69535 + f7f6a29 commit 16b51ef

File tree

2 files changed

+74
-70
lines changed

2 files changed

+74
-70
lines changed

avdweb_Switch.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ WEBSITE: http://www.avdweb.nl/arduino/hardware-interfacing/simple-switch-debounc
2121
1.2.1 30-11-2018 bugfix. Initialize time variables in the constructor. Fixes false event if first call to poll was delayed
2222
1.2.2 18-10-2019 beep when a switch is pressed with using a setBeepAllCallback function
2323
1.2.3 03-04-2020 made public: deglitchPeriod, debouncePeriod, longPressPeriod, doubleClickPeriod
24+
1.2.4 14-07-2021 "static switchCallback_t _beepAllCallback = nullptr;" gives error with SAMD21, removed "= nullptr;"
2425
2526
..........................................DEGLITCHING..............................
2627

avdweb_Switch.h

Lines changed: 73 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,73 @@
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-
9-
AUTHOR: Albert van Dalen
10-
WEBSITE: http://www.avdweb.nl/arduino/hardware-interfacing/simple-switch-debouncer.html
11-
*/
12-
13-
#ifndef AVDWEB_SWITCH_H
14-
#define AVDWEB_SWITCH_H
15-
16-
typedef void (*switchCallback_t)(void*);
17-
18-
class Switch
19-
{
20-
public:
21-
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);
22-
bool poll(); // Returns 1 if switched
23-
bool switched(); // will be refreshed by poll()
24-
bool on();
25-
bool pushed(); // will be refreshed by poll()
26-
bool released(); // will be refreshed by poll()
27-
bool longPress(); // will be refreshed by poll()
28-
bool doubleClick(); // will be refreshed by poll()
29-
bool singleClick(); // will be refreshed by poll()
30-
31-
// Set methods for event callbacks
32-
void setPushedCallback(switchCallback_t cb, void* param = nullptr);
33-
void setReleasedCallback(switchCallback_t cb, void* param = nullptr);
34-
void setLongPressCallback(switchCallback_t cb, void* param = nullptr);
35-
void setDoubleClickCallback(switchCallback_t cb, void* param = nullptr);
36-
void setSingleClickCallback(switchCallback_t cb, void* param = nullptr);
37-
void setBeepAllCallback(switchCallback_t cb, void* param = nullptr);
38-
39-
unsigned long deglitchPeriod, debouncePeriod, longPressPeriod, doubleClickPeriod;
40-
41-
protected:
42-
bool process(); // not inline, used in child class
43-
void inline deglitch();
44-
void inline debounce();
45-
void inline calcLongPress();
46-
void inline calcDoubleClick();
47-
void inline calcSingleClick();
48-
void triggerCallbacks();
49-
50-
unsigned long deglitchTime, switchedTime, pushedTime, releasedTime, ms;
51-
const byte pin;
52-
const bool polarity;
53-
bool input, lastInput, equal, deglitched, debounced, _switched, _longPress, longPressDisable, _doubleClick, _singleClick, singleClickDisable;
54-
55-
// Event callbacks
56-
switchCallback_t _pushedCallback = nullptr;
57-
switchCallback_t _releasedCallback = nullptr;
58-
switchCallback_t _longPressCallback = nullptr;
59-
switchCallback_t _doubleClickCallback = nullptr;
60-
switchCallback_t _singleClickCallback = nullptr;
61-
static switchCallback_t _beepAllCallback; // static function pointer, can be used by all objects
62-
63-
void* _pushedCallbackParam = nullptr;
64-
void* _releasedCallbackParam = nullptr;
65-
void* _longPressCallbackParam = nullptr;
66-
void* _doubleClickCallbackParam = nullptr;
67-
void* _singleClickCallbackParam = nullptr;
68-
static void* _beepAllCallbackParam; // can be used by all objects
69-
};
70-
#endif
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+
9+
AUTHOR: Albert van Dalen
10+
WEBSITE: http://www.avdweb.nl/arduino/hardware-interfacing/simple-switch-debouncer.html
11+
*/
12+
13+
#ifndef AVDWEB_SWITCH_H
14+
#define AVDWEB_SWITCH_H
15+
16+
typedef void (*switchCallback_t)(void*);
17+
18+
class Switch
19+
{
20+
public:
21+
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);
22+
bool poll(); // Returns 1 if switched
23+
bool switched(); // will be refreshed by poll()
24+
bool on();
25+
bool pushed(); // will be refreshed by poll()
26+
bool released(); // will be refreshed by poll()
27+
bool longPress(); // will be refreshed by poll()
28+
bool doubleClick(); // will be refreshed by poll()
29+
bool singleClick(); // will be refreshed by poll()
30+
31+
// Set methods for event callbacks
32+
void setPushedCallback(switchCallback_t cb, void* param = nullptr);
33+
void setReleasedCallback(switchCallback_t cb, void* param = nullptr);
34+
void setLongPressCallback(switchCallback_t cb, void* param = nullptr);
35+
void setDoubleClickCallback(switchCallback_t cb, void* param = nullptr);
36+
void setSingleClickCallback(switchCallback_t cb, void* param = nullptr);
37+
void setBeepAllCallback(switchCallback_t cb, void* param = nullptr);
38+
39+
unsigned long deglitchPeriod, debouncePeriod, longPressPeriod, doubleClickPeriod;
40+
41+
protected:
42+
bool process(); // not inline, used in child class
43+
void inline deglitch();
44+
void inline debounce();
45+
void inline calcLongPress();
46+
void inline calcDoubleClick();
47+
void inline calcSingleClick();
48+
void triggerCallbacks();
49+
50+
unsigned long deglitchTime, switchedTime, pushedTime, releasedTime, ms;
51+
const byte pin;
52+
const bool polarity;
53+
bool input, lastInput, equal, deglitched, debounced, _switched, _longPress, longPressDisable, _doubleClick, _singleClick, singleClickDisable;
54+
55+
// Event callbacks
56+
switchCallback_t _pushedCallback = nullptr;
57+
switchCallback_t _releasedCallback = nullptr;
58+
switchCallback_t _longPressCallback = nullptr;
59+
switchCallback_t _doubleClickCallback = nullptr;
60+
switchCallback_t _singleClickCallback = nullptr;
61+
static switchCallback_t _beepAllCallback; // static function pointer, can be used by all objects
62+
//static switchCallback_t _beepAllCallback = nullptr; // gives error with SAMD21
63+
//static void(*_beepAllCallback)(void*) = nullptr; // static function pointer without typedef
64+
65+
void* _pushedCallbackParam = nullptr;
66+
void* _releasedCallbackParam = nullptr;
67+
void* _longPressCallbackParam = nullptr;
68+
void* _doubleClickCallbackParam = nullptr;
69+
void* _singleClickCallbackParam = nullptr;
70+
static void* _beepAllCallbackParam; // can be used by all objects
71+
//static void* _beepAllCallbackParam = nullptr; // gives error with SAMD21
72+
};
73+
#endif

0 commit comments

Comments
 (0)