|
@@ -1,111 +0,0 @@
|
1
|
|
-// Arduino Button Library
|
2
|
|
-// https://github.com/JChristensen/JC_Button
|
3
|
|
-// Copyright (C) 2018 by Jack Christensen and licensed under
|
4
|
|
-// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
|
5
|
|
-
|
6
|
|
-#ifndef JC_BUTTON_H_INCLUDED
|
7
|
|
-#define JC_BUTTON_H_INCLUDED
|
8
|
|
-
|
9
|
|
-#include <Arduino.h>
|
10
|
|
-
|
11
|
|
-class Button
|
12
|
|
-{
|
13
|
|
-public:
|
14
|
|
- // Button(pin, dbTime, puEnable, invert) instantiates a button object.
|
15
|
|
- //
|
16
|
|
- // Required parameter:
|
17
|
|
- // pin The Arduino pin the button is connected to
|
18
|
|
- //
|
19
|
|
- // Optional parameters:
|
20
|
|
- // dbTime Debounce time in milliseconds (default 25ms)
|
21
|
|
- // puEnable true to enable the AVR internal pullup resistor (default true)
|
22
|
|
- // invert true to interpret a low logic level as pressed (default true)
|
23
|
|
- Button(uint8_t pin, uint32_t dbTime=25, uint8_t puEnable=true, uint8_t invert=true)
|
24
|
|
- : m_pin(pin), m_dbTime(dbTime), m_puEnable(puEnable), m_invert(invert) {}
|
25
|
|
-
|
26
|
|
- // Initialize a Button object and the pin it's connected to
|
27
|
|
- void begin();
|
28
|
|
-
|
29
|
|
- // Returns the current debounced button state, true for pressed,
|
30
|
|
- // false for released. Call this function frequently to ensure
|
31
|
|
- // the sketch is responsive to user input.
|
32
|
|
- bool read();
|
33
|
|
-
|
34
|
|
- // Returns true if the button state was pressed at the last call to read().
|
35
|
|
- // Does not cause the button to be read.
|
36
|
|
- bool isPressed();
|
37
|
|
-
|
38
|
|
- // Returns true if the button state was released at the last call to read().
|
39
|
|
- // Does not cause the button to be read.
|
40
|
|
- bool isReleased();
|
41
|
|
-
|
42
|
|
- // Returns true if the button state at the last call to read() was pressed,
|
43
|
|
- // and this was a change since the previous read.
|
44
|
|
- bool wasPressed();
|
45
|
|
-
|
46
|
|
- // Returns true if the button state at the last call to read() was released,
|
47
|
|
- // and this was a change since the previous read.
|
48
|
|
- bool wasReleased();
|
49
|
|
-
|
50
|
|
- // Returns true if the button state at the last call to read() was pressed,
|
51
|
|
- // and has been in that state for at least the given number of milliseconds.
|
52
|
|
- bool pressedFor(uint32_t ms);
|
53
|
|
-
|
54
|
|
- // Returns true if the button state at the last call to read() was released,
|
55
|
|
- // and has been in that state for at least the given number of milliseconds.
|
56
|
|
- bool releasedFor(uint32_t ms);
|
57
|
|
-
|
58
|
|
- // Returns the time in milliseconds (from millis) that the button last
|
59
|
|
- // changed state.
|
60
|
|
- uint32_t lastChange();
|
61
|
|
-
|
62
|
|
-private:
|
63
|
|
- uint8_t m_pin; // arduino pin number connected to button
|
64
|
|
- uint32_t m_dbTime; // debounce time (ms)
|
65
|
|
- bool m_puEnable; // internal pullup resistor enabled
|
66
|
|
- bool m_invert; // if true, interpret logic low as pressed, else interpret logic high as pressed
|
67
|
|
- bool m_state; // current button state, true=pressed
|
68
|
|
- bool m_lastState; // previous button state
|
69
|
|
- bool m_changed; // state changed since last read
|
70
|
|
- uint32_t m_time; // time of current state (ms from millis)
|
71
|
|
- uint32_t m_lastChange; // time of last state change (ms)
|
72
|
|
-};
|
73
|
|
-
|
74
|
|
-// a derived class for a "push-on, push-off" (toggle) type button.
|
75
|
|
-// initial state can be given, default is off (false).
|
76
|
|
-class ToggleButton : public Button
|
77
|
|
-{
|
78
|
|
-public:
|
79
|
|
-
|
80
|
|
- // constructor is similar to Button, but includes the initial state for the toggle.
|
81
|
|
- ToggleButton(uint8_t pin, bool initialState=false, uint32_t dbTime=25, uint8_t puEnable=true, uint8_t invert=true)
|
82
|
|
- : Button(pin, dbTime, puEnable, invert), m_toggleState(initialState) {}
|
83
|
|
-
|
84
|
|
- // read the button and return its state.
|
85
|
|
- // should be called frequently.
|
86
|
|
- bool read()
|
87
|
|
- {
|
88
|
|
- Button::read();
|
89
|
|
- if (wasPressed())
|
90
|
|
- {
|
91
|
|
- m_toggleState = !m_toggleState;
|
92
|
|
- m_changed = true;
|
93
|
|
- }
|
94
|
|
- else
|
95
|
|
- {
|
96
|
|
- m_changed = false;
|
97
|
|
- }
|
98
|
|
- return m_toggleState;
|
99
|
|
- }
|
100
|
|
-
|
101
|
|
- // has the state changed?
|
102
|
|
- bool changed() {return m_changed;}
|
103
|
|
-
|
104
|
|
- // return the current state
|
105
|
|
- bool toggleState() {return m_toggleState;}
|
106
|
|
-
|
107
|
|
-private:
|
108
|
|
- bool m_toggleState;
|
109
|
|
- bool m_changed;
|
110
|
|
-};
|
111
|
|
-#endif
|