| 2 |
raymond |
1 |
/*
|
|
|
2 |
* Copyright (c) 2019, ArduinoGetStarted.com. All rights reserved.
|
|
|
3 |
*
|
|
|
4 |
* Redistribution and use in source and binary forms, with or without
|
|
|
5 |
* modification, are permitted provided that the following conditions
|
|
|
6 |
* are met:
|
|
|
7 |
*
|
|
|
8 |
* - Redistributions of source code must retain the above copyright
|
|
|
9 |
* notice, this list of conditions and the following disclaimer.
|
|
|
10 |
*
|
|
|
11 |
* - Redistributions in binary form must reproduce the above copyright
|
|
|
12 |
* notice, this list of conditions and the following disclaimer in the
|
|
|
13 |
* documentation and/or other materials provided with the distribution.
|
|
|
14 |
*
|
|
|
15 |
* - Neither the name of the ArduinoGetStarted.com nor the names of its
|
|
|
16 |
* contributors may be used to endorse or promote products derived from
|
|
|
17 |
* this software without specific prior written permission.
|
|
|
18 |
*
|
|
|
19 |
* THIS SOFTWARE IS PROVIDED BY ARDUINOGETSTARTED.COM "AS IS" AND ANY EXPRESS OR
|
|
|
20 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
21 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
22 |
* DISCLAIMED. IN NO EVENT SHALL ARDUINOGETSTARTED.COM BE LIABLE FOR ANY DIRECT,
|
|
|
23 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
24 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
25 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
26 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
27 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
28 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
29 |
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
30 |
*/
|
|
|
31 |
|
|
|
32 |
#include <ezButton.h>
|
|
|
33 |
|
|
|
34 |
ezButton::ezButton(int pin): ezButton(pin, INTERNAL_PULLUP) {};
|
|
|
35 |
|
|
|
36 |
ezButton::ezButton(int pin, int mode) {
|
|
|
37 |
btnPin = pin;
|
|
|
38 |
debounceTime = 0;
|
|
|
39 |
count = 0;
|
|
|
40 |
countMode = COUNT_FALLING;
|
|
|
41 |
|
|
|
42 |
if (mode == INTERNAL_PULLUP || mode == INTERNAL_PULLDOWN) {
|
|
|
43 |
pinMode(btnPin, mode);
|
|
|
44 |
} else if (mode == EXTERNAL_PULLUP || mode == EXTERNAL_PULLDOWN) {
|
|
|
45 |
pinMode(btnPin, INPUT); // External pull-up/pull-down, set as INPUT
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
// Set the pressed and unpressed states based on the mode
|
|
|
49 |
if (mode == INTERNAL_PULLDOWN || mode == EXTERNAL_PULLDOWN) {
|
|
|
50 |
pressedState = HIGH;
|
|
|
51 |
unpressedState = LOW;
|
|
|
52 |
} else {
|
|
|
53 |
pressedState = LOW;
|
|
|
54 |
unpressedState = HIGH;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
previousSteadyState = digitalRead(btnPin);
|
|
|
58 |
lastSteadyState = previousSteadyState;
|
|
|
59 |
lastFlickerableState = previousSteadyState;
|
|
|
60 |
|
|
|
61 |
lastDebounceTime = 0;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
void ezButton::setDebounceTime(unsigned long time) {
|
|
|
65 |
debounceTime = time;
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
int ezButton::getState(void) {
|
|
|
69 |
return lastSteadyState;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
int ezButton::getStateRaw(void) {
|
|
|
73 |
return digitalRead(btnPin);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
bool ezButton::isPressed(void) {
|
|
|
77 |
if(previousSteadyState == unpressedState && lastSteadyState == pressedState)
|
|
|
78 |
return true;
|
|
|
79 |
else
|
|
|
80 |
return false;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
bool ezButton::isReleased(void) {
|
|
|
84 |
if(previousSteadyState == pressedState && lastSteadyState == unpressedState)
|
|
|
85 |
return true;
|
|
|
86 |
else
|
|
|
87 |
return false;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
void ezButton::setCountMode(int mode) {
|
|
|
91 |
countMode = mode;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
unsigned long ezButton::getCount(void) {
|
|
|
95 |
return count;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
void ezButton::resetCount(void) {
|
|
|
99 |
count = 0;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
void ezButton::loop(void) {
|
|
|
103 |
// read the state of the switch/button:
|
|
|
104 |
int currentState = digitalRead(btnPin);
|
|
|
105 |
unsigned long currentTime = millis();
|
|
|
106 |
|
|
|
107 |
// check to see if you just pressed the button
|
|
|
108 |
// (i.e. the input went from LOW to HIGH), and you've waited long enough
|
|
|
109 |
// since the last press to ignore any noise:
|
|
|
110 |
|
|
|
111 |
// If the switch/button changed, due to noise or pressing:
|
|
|
112 |
if (currentState != lastFlickerableState) {
|
|
|
113 |
// reset the debouncing timer
|
|
|
114 |
lastDebounceTime = currentTime;
|
|
|
115 |
// save the the last flickerable state
|
|
|
116 |
lastFlickerableState = currentState;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
if ((currentTime - lastDebounceTime) >= debounceTime) {
|
|
|
120 |
// whatever the reading is at, it's been there for longer than the debounce
|
|
|
121 |
// delay, so take it as the actual current state:
|
|
|
122 |
|
|
|
123 |
// save the the steady state
|
|
|
124 |
previousSteadyState = lastSteadyState;
|
|
|
125 |
lastSteadyState = currentState;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
if(previousSteadyState != lastSteadyState){
|
|
|
129 |
if(countMode == COUNT_BOTH)
|
|
|
130 |
count++;
|
|
|
131 |
else if(countMode == COUNT_FALLING){
|
|
|
132 |
if(previousSteadyState == HIGH && lastSteadyState == LOW)
|
|
|
133 |
count++;
|
|
|
134 |
}
|
|
|
135 |
else if(countMode == COUNT_RISING){
|
|
|
136 |
if(previousSteadyState == LOW && lastSteadyState == HIGH)
|
|
|
137 |
count++;
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
}
|