|
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, int debouncePeriod=50, int longPressPeriod=300, int doubleClickPeriod=250, int 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 | | - int 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 |
| 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