Neale Pickett
·
2020-05-16
bounce2.cpp
1// Please read Bounce2.h for information about the liscence and authors
2
3
4#include "bounce2.h"
5
6
7
8
9Bounce::Bounce()
10 : previous_millis(0)
11 , interval_millis(10)
12 , state(0)
13 , pin(0)
14{}
15
16void Bounce::attach(int pin) {
17 this->pin = pin;
18 state = 0;
19 if (readCurrentState()) {
20 setStateFlag(DEBOUNCED_STATE | UNSTABLE_STATE);
21 }
22#ifdef BOUNCE_LOCK_OUT
23 previous_millis = 0;
24#else
25 previous_millis = millis();
26#endif
27}
28
29void Bounce::attach(int pin, int mode){
30 setPinMode(pin, mode);
31 this->attach(pin);
32}
33
34void Bounce::interval(uint16_t interval_millis)
35{
36 this->interval_millis = interval_millis;
37}
38
39bool Bounce::update()
40{
41
42 unsetStateFlag(CHANGED_STATE);
43#ifdef BOUNCE_LOCK_OUT
44
45 // Ignore everything if we are locked out
46 if (millis() - previous_millis >= interval_millis) {
47 bool currentState = readCurrentState();
48 if ( currentState != getStateFlag(DEBOUNCED_STATE) ) {
49 previous_millis = millis();
50 changeState();
51 }
52 }
53
54
55#elif defined BOUNCE_WITH_PROMPT_DETECTION
56 // Read the state of the switch port into a temporary variable.
57 bool readState = readCurrentState();
58
59
60 if ( readState != getStateFlag(DEBOUNCED_STATE) ) {
61 // We have seen a change from the current button state.
62
63 if ( millis() - previous_millis >= interval_millis ) {
64 // We have passed the time threshold, so a new change of state is allowed.
65 // set the STATE_CHANGED flag and the new DEBOUNCED_STATE.
66 // This will be prompt as long as there has been greater than interval_misllis ms since last change of input.
67 // Otherwise debounced state will not change again until bouncing is stable for the timeout period.
68 changeState();
69 }
70 }
71
72 // If the readState is different from previous readState, reset the debounce timer - as input is still unstable
73 // and we want to prevent new button state changes until the previous one has remained stable for the timeout.
74 if ( readState != getStateFlag(UNSTABLE_STATE) ) {
75 // Update Unstable Bit to macth readState
76 toggleStateFlag(UNSTABLE_STATE);
77 previous_millis = millis();
78 }
79
80
81#else
82 // Read the state of the switch in a temporary variable.
83 bool currentState = readCurrentState();
84
85
86 // If the reading is different from last reading, reset the debounce counter
87 if ( currentState != getStateFlag(UNSTABLE_STATE) ) {
88 previous_millis = millis();
89 toggleStateFlag(UNSTABLE_STATE);
90 } else
91 if ( millis() - previous_millis >= interval_millis ) {
92 // We have passed the threshold time, so the input is now stable
93 // If it is different from last state, set the STATE_CHANGED flag
94 if (currentState != getStateFlag(DEBOUNCED_STATE) ) {
95 previous_millis = millis();
96
97
98 changeState();
99 }
100 }
101
102
103#endif
104
105 return changed();
106
107}
108
109// WIP HELD
110unsigned long Bounce::previousDuration() {
111 return durationOfPreviousState;
112}
113
114unsigned long Bounce::duration() {
115 return (millis() - stateChangeLastTime);
116}
117
118inline void Bounce::changeState() {
119 toggleStateFlag(DEBOUNCED_STATE);
120 setStateFlag(CHANGED_STATE) ;
121 durationOfPreviousState = millis() - stateChangeLastTime;
122 stateChangeLastTime = millis();
123}
124
125bool Bounce::read()
126{
127 return getStateFlag(DEBOUNCED_STATE);
128}
129
130bool Bounce::rose()
131{
132 return getStateFlag(DEBOUNCED_STATE) && getStateFlag(CHANGED_STATE);
133}
134
135bool Bounce::fell()
136{
137 return !getStateFlag(DEBOUNCED_STATE) && getStateFlag(CHANGED_STATE);
138}