forked from thomasfredericks/Bounce2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBounce2.cpp
More file actions
184 lines (162 loc) · 4.28 KB
/
Bounce2.cpp
File metadata and controls
184 lines (162 loc) · 4.28 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Please read Bounce2.h for information about the liscence and authors
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Bounce2.h"
#define DEBOUNCED_STATE 0
#define UNSTABLE_STATE 1
#define STATE_CHANGED 3
#define STATE_HELD_ON 4
#define FIRSTUPDATE 5
Bounce::Bounce(int pin, uint16_t interval_millis, uint16_t interval_retrigger, uint16_t interval_hold)
{
this->pin = pin;
interval(interval_millis);
holdinterval(interval_hold);
retriggerinterval(interval_retrigger);
previous_millis_state_changed = 0;
state = _BV(FIRSTUPDATE);
}
void Bounce::attach(int pin) {
this->pin = pin;
bool read = digitalRead(pin);
state = 0;
if (digitalRead(pin)) {
state = _BV(DEBOUNCED_STATE) | _BV(UNSTABLE_STATE);
}
#ifdef BOUNCE_LOCK_OUT
previous_millis = 0;
#else
previous_millis = millis();
#endif
}
void Bounce::attach(int pin, int mode){
pinMode(pin, mode);
this->attach(pin);
}
void Bounce::interval(uint16_t interval_millis)
{
this->interval_millis = interval_millis;
}
void Bounce::holdinterval(uint16_t interval_hold)
{
this->interval_hold = interval_hold;
}
void Bounce::retriggerinterval(uint16_t interval_retrigger)
{
this->interval_retrigger = interval_retrigger;
}
bool Bounce::update()
{
// Attaches the pin the first time update is called
if(state & _BV(FIRSTUPDATE))
{
attach(pin);
state &= ~_BV(FIRSTUPDATE);
}
else
{
#ifdef BOUNCE_LOCK_OUT
state &= ~_BV(STATE_CHANGED);
// Ignore everything if we are locked out
if (millis() - previous_millis >= interval_millis) {
bool currentState = digitalRead(pin);
if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) {
previous_millis = millis();
state ^= _BV(DEBOUNCED_STATE);
state |= _BV(STATE_CHANGED);
}
}
return state & _BV(STATE_CHANGED);
#else
// Read the state of the switch in a temporary variable.
bool currentState = digitalRead(pin);
state &= ~_BV(STATE_CHANGED);
// If the reading is different from last reading, reset the debounce counter
if ( currentState != (bool)(state & _BV(UNSTABLE_STATE)) ) {
previous_millis = millis();
state ^= _BV(UNSTABLE_STATE);
} else
if ( millis() - previous_millis >= interval_millis ) {
// We have passed the threshold time, so the input is now stable
// If it is different from last state, set the STATE_CHANGED flag
if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) {
previous_millis = millis();
state ^= _BV(DEBOUNCED_STATE);
state |= _BV(STATE_CHANGED);
}
}
#endif
//code for checking if state has been held on >= 500ms or interval_hold
// If state has changed and has not been held on previously
if(state & _BV(STATE_CHANGED) && !(state & _BV(STATE_HELD_ON)))
{
//If state has been changed before 500ms since last change, reset state change timer
if(millis() - previous_millis_state_changed < interval_hold)
{
previous_millis_state_changed = 0;
}
else
{
previous_millis_state_changed = millis();
}
}
// If state has changed and has been held on previously
else if(state & _BV(STATE_CHANGED) && (state & _BV(STATE_HELD_ON)))
{
// Immediately turn off held on state
state &= ~_BV(STATE_HELD_ON);
previous_millis_state_changed = 0;
}
else if(previous_millis_state_changed != 0)
{
//if button has been at a debounced state for >= 500ms
if(millis() - previous_millis_state_changed >= interval_hold)
{
state |= _BV(STATE_HELD_ON);
previous_millis_state_changed = 0;
}
}
return state & _BV(STATE_CHANGED);
}
}
bool Bounce::retrigger()
{
// if button is being held
if(this->held())
{
if(previous_millis_state_changed != 0)
{
// Checks if it has been longer that retrigger time
if (millis() - previous_millis_state_changed >= interval_retrigger)
{
previous_millis_state_changed = millis();
return true;
}
}
else
{
// sets the millis when first going into function
previous_millis_state_changed = millis();
}
}
return false;
}
bool Bounce::read()
{
return state & _BV(DEBOUNCED_STATE);
}
bool Bounce::rose()
{
return ( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED));
}
bool Bounce::fell()
{
return !( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED));
}
bool Bounce::held()
{
return (state & _BV(STATE_HELD_ON));
}