forked from avdwebLibraries/avdweb_Switch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavdweb_Switch.h
More file actions
108 lines (98 loc) · 4.1 KB
/
avdweb_Switch.h
File metadata and controls
108 lines (98 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* avdweb_Switch.h
*
* 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/>.
*/
#ifndef AVDWEB_SWITCH_H
#define AVDWEB_SWITCH_H
#include <Arduino.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 isLongPressed(); // 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