forked from italo-coelho/pushButton
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpushButton.cpp
More file actions
149 lines (129 loc) · 3.11 KB
/
pushButton.cpp
File metadata and controls
149 lines (129 loc) · 3.11 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* pushButton.cpp - Library for Push Buttons without using Delay
* Created by Ítalo Coelho, January 3rd, 2020
*/
#include "pushButton.h"
pushButton::pushButton(uint8_t pin)
{
_pin = pin;
_pinMode = PIN_MODE;
_debounce = DEBOUNCE;
pinMode(_pin, _pinMode);
}
pushButton::pushButton(uint8_t pin, uint16_t debounce, uint8_t pin_mode)
{
_pin = pin;
_pinMode = pin_mode;
_debounce = debounce;
pinMode(_pin, _pinMode);
}
/*
\brief Determine if the button was Pressed.
\note Implemented with debouncing taken in to consideration.
\return True if Pressed
*/
bool pushButton::wasPressed()
{
bool answer = false;
static uint32_t then = time();
_current = digitalRead(_pin);
if(!_current && _previous)
{
if(time() - then > DEBOUNCE)
{
answer = true;
then = time();
}
}
_previous = _current;
return answer;
}
/*
\brief Determine if the button was Released.
\note Implemented with debouncing taken in to consideration.
\return True if Released
*/
bool pushButton::wasReleased()
{
bool answer = false;
static uint32_t then = time();
_current = digitalRead(_pin);
if(_current && !_previous)
{
if(time() - then > _debounce)
{
answer = true;
then = time();
}
}
_previous = _current;
return answer;
}
/*
\brief Determine boolean state of the button as a toggle switch.
\note Implemented with debouncing taken in to consideration.
\return Current State
*/
bool pushButton::retentionState()
{
static uint32_t then = time();
_current = digitalRead(_pin);
if(!_current && _previous)
{
if(time() - then > _debounce)
{
_retention = !_retention;
then = time();
}
}
_previous = _current;
return _retention;
}
/*
\brief Calculate the time that the button has been pressed.
\note Will return 0 when button is released. Does not take debouncing in to account.
\return Time in Miliseconds
*/
uint32_t pushButton::pressedFor()
{
_current = digitalRead(_pin);
if(!_current && _previous)
{
_pressTime = time();
}
_previous = _current;
if(_current)
_pressDuration = 0;
else
_pressDuration = time() - _pressTime;
return _pressDuration;
}
/*
\brief Calculate the time that the button was pressed before it was released.
\note Will return 0, except in the first call after button is released. Does not take debouncing in to account.
\return Time in Miliseconds
*/
uint32_t pushButton::releasedAfter()
{
uint32_t answer;
uint32_t duration = pressedFor();
static uint32_t pDuration = duration;
if((duration == 0) && (pDuration > 0))
answer = pDuration;
else
answer = 0;
pDuration = duration;
return answer;
}
/*
\brief Calculate the time since the code started running.
\note Does NOT use millis() when compiled for ESP32 to avoid crashes while using FreeRTOS.
\return Time in Miliseconds
*/
uint32_t pushButton::time()
{
#ifdef ESP32
return xTaskGetTickCount() *portTICK_PERIOD_MS;
#endif //ESP32
return millis();
}