| 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 |
#ifndef ezButton_h
|
|
|
33 |
#define ezButton_h
|
|
|
34 |
|
|
|
35 |
#include <Arduino.h>
|
|
|
36 |
|
|
|
37 |
#define COUNT_FALLING 0
|
|
|
38 |
#define COUNT_RISING 1
|
|
|
39 |
#define COUNT_BOTH 2
|
|
|
40 |
|
|
|
41 |
// Constants for button modes
|
|
|
42 |
#define INTERNAL_PULLUP INPUT_PULLUP
|
|
|
43 |
#ifdef INPUT_PULLDOWN
|
|
|
44 |
#define INTERNAL_PULLDOWN INPUT_PULLDOWN
|
|
|
45 |
#else
|
|
|
46 |
#define INTERNAL_PULLDOWN INPUT
|
|
|
47 |
#endif
|
|
|
48 |
|
|
|
49 |
#define EXTERNAL_PULLUP 0xFE
|
|
|
50 |
#define EXTERNAL_PULLDOWN 0xFF
|
|
|
51 |
|
|
|
52 |
class ezButton
|
|
|
53 |
{
|
|
|
54 |
private:
|
|
|
55 |
int btnPin;
|
|
|
56 |
unsigned long debounceTime;
|
|
|
57 |
unsigned long count;
|
|
|
58 |
int countMode;
|
|
|
59 |
int pressedState; // the state when the button is considered pressed
|
|
|
60 |
int unpressedState; // the state when the button is considered unpressed
|
|
|
61 |
|
|
|
62 |
int previousSteadyState; // the previous steady state from the input pin, used to detect pressed and released event
|
|
|
63 |
int lastSteadyState; // the last steady state from the input pin
|
|
|
64 |
int lastFlickerableState; // the last flickerable state from the input pin
|
|
|
65 |
|
|
|
66 |
unsigned long lastDebounceTime; // the last time the output pin was toggled
|
|
|
67 |
|
|
|
68 |
public:
|
|
|
69 |
ezButton(int pin);
|
|
|
70 |
ezButton(int pin, int mode);
|
|
|
71 |
void setDebounceTime(unsigned long time);
|
|
|
72 |
int getState(void);
|
|
|
73 |
int getStateRaw(void);
|
|
|
74 |
bool isPressed(void);
|
|
|
75 |
bool isReleased(void);
|
|
|
76 |
void setCountMode(int mode);
|
|
|
77 |
unsigned long getCount(void);
|
|
|
78 |
void resetCount(void);
|
|
|
79 |
void loop(void);
|
|
|
80 |
};
|
|
|
81 |
|
|
|
82 |
#endif
|