-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuttonObject.h
160 lines (82 loc) · 4.46 KB
/
buttonObject.h
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
/* ---------------------- Button Object Library ------------------
Library to simplify the implementation of buttons for inclusion with
User Interfaces.
Imogen Wren
Pan Galactic Tech
09/08/2020
Released for public use
// buttonObject::buttonMaster(long holdTime)
> Function >
This method calls all methods required to return global variables:
// pressCount
// longPressCount
// releaseCount
// longPress - bool
// shortPress - bool
Which can be used for reliable user interfaces.
Other methods can be used individually for more specific functions
*/
#ifndef buttonObject_h
#define buttonObject_h
#if (ARDUINO >=100)
#include <Arduino.h>
#else
#include <wProgram.h>
#endif
#define BUTTON_PULL_HIGH 1
#define BUTTON_PULL_LOW 0
#define buttonMaster buttonLoop // Sets up Synonym for buttonHold : buttonMaster
#define buttonHold buttonLoop // Alternative Synonyms for
class buttonObject {
// Constants
// These varaibles are set in the .begin function:
int buttonPin; // global variable to hold the number of the button pin
int onState; // global variable to hold the active state of the button (pull HIGH or pull LOW)
public:
// Constructor
buttonObject(int attachTo, int activeState): // Alternative way of setting out constructor. Will allow the use of further constructors
buttonPin(attachTo),
onState(activeState)
{
}
// Begin Method
void begin();
// Basic Methods
uint8_t sampleButton(); // Samples the button and returns true state of the button
uint8_t detectButton(); // Calls method to sample the button, compares this to the predefined active/onState (PULL_HIGH or PULL_LOW)
// Ultra Debounce Button Methods:
//Based on:
//https://hackaday.com/2015/12/10/embed-with-elliot-debounce-your-noisy-buttons-part-ii/#more-180185
void updateButton(uint8_t *buttonHistory); // Updates buttonHistory bitstream
uint8_t buttonPressed(uint8_t *buttonHistory); // Returns true if button has been pressed (Rising Edge Detected)
uint8_t buttonReleased(uint8_t *buttonHistory); // Returns true if button has been released (Falling Edge Detected)
uint8_t buttonDown(uint8_t *buttonHistory); // Returns true if the button is held down (Constant High detected)
uint8_t buttonUp(uint8_t *buttonHistory); // Returns ture if button is not pressed (Constant Low detected)
bool buttonLoop(uint32_t holdTime = 1000); // - Aliased with buttonMaster & buttonHold Method // Variable passed is required millis value for a long press - Defaults to 1 second
void buttonReset(); // Method for resetting shortPress & longPress bools once software has performed the required action
void buttonStats(); // Method to print button stats
// buttonMaster Method
//> This method calls all methods required to return:
//pressCount
//longPressCount
//releaseCount
//shortPress - bool
//longPress - bool
// as global public variables
// Variables
// Variables to count the different actions of the button.
// More useful for testing than interfacting with other functions
uint8_t pressCount; // Counts the total number of button presses
uint8_t releaseCount; // counts the total number of times the button is released after pressing.
uint8_t longPressCount; // Counts the total number of times the button has detected a long press.
//N.B. release count + longPressCount = pressCount
// Bool functions for directly interfacing with other functions
bool shortPress; // variable to return true when button is short tapped (turns true on release) (latching, must be reset)
bool longPress; // variable returns true on long press (returns true on hold) (latching, must be reset)
private:
//Variables
uint8_t buttonHistory = 0; // Stores the recent history of the button - Private as no need to access this outside of library (hmmmm)
uint32_t pressTime; // Stores the time at which the button was first pressed in order to time long presses.
bool buttonLockout; // when true button functions are locked, to allow for correct operation after long presses
};
#endif