-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtn.h
240 lines (212 loc) · 8.97 KB
/
btn.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//*********************************************************************************
// State Button Debouncer - Platform Independent
//
// Revision: 1.6
//
// Description: Debounces buttons on a single port being used by the application.
// This module takes what the signal on a GPIO port is doing and removes
// the oscillations caused by a bouncing button and tells the application if
// the button(s) are debounced. This algorithm is robust against noise if the
// amount of allowable debouncing states is adequate. Below is an example of how
// the button debouncer would work in practice in relation to a single button:
//
// Real Signal: 0011111111111110000000000000011111111111111111110000000000
// Bouncy Signal: 0010110111111111010000000000001010111011111111110101000000
// Debounced Sig: 0000000000000011000000000000000000000000000001110000000000
//
// The debouncing algorithm used in this library is based partly on Jack
// Ganssle's state button debouncer example shown in, "A Guide to Debouncing"
// Rev 4. http://www.ganssle.com/debouncing.htm
//
// Revisions can be found here:
// https://github.com/tcleg
//
// Copyright (C) 2014 Trent Cleghorn , <[email protected]>
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//*********************************************************************************
//
// Header Guard
//
#ifndef BTN_H
#define BTN_H
//*********************************************************************************
// Headers
//*********************************************************************************
#include <stdint.h>
//
// C Binding for C++ Compilers
//
#ifdef __cplusplus
extern "C"
{
#endif
//*********************************************************************************
// Macros and Globals
//*********************************************************************************
// NUM_BUTTON_STATES should be greater than 0 and less than or equal to 255.
// The default of 8 is a roundabout good number of states to have. At a practical
// minimum, the number of button states should be at least 3. Each button state
// consumes 1 byte of RAM.
// If this number is large, the Debouncer instantiation will consume
// more RAM and take more time to debounce but will reduce the chance of having an
// incorrectly debounced button. If this is small, the Debouncer instantiation
// will consume less RAM and take less time to debounce but will be more prone
// to incorrectly determining button presses and releases.
#ifndef NUM_BUTTON_STATES
#define NUM_BUTTON_STATES 8
#endif
// Binary Equivalent
#define BUTTON_PIN_0 (0x0001) // 00000001
#define BUTTON_PIN_1 (0x0002) // 00000010
#define BUTTON_PIN_2 (0x0004) // 00000100
#define BUTTON_PIN_3 (0x0008) // 00001000
#define BUTTON_PIN_4 (0x0010) // 00010000
#define BUTTON_PIN_5 (0x0020) // 00100000
#define BUTTON_PIN_6 (0x0040) // 01000000
#define BUTTON_PIN_7 (0x0080) // 10000000
#define BUTTONS_GPIO_PERIPH SYSCTL_PERIPH_GPIOD
#define BUTTONS_GPIO_BASE GPIO_PORTD_BASE
#define NUM_BUTTONS 5
#define LEFT_BUTTON GPIO_PIN_0
#define RIGHT_BUTTON GPIO_PIN_1
#define UP_BUTTON GPIO_PIN_2
#define DOWN_BUTTON GPIO_PIN_3
#define SELECT_BUTTON GPIO_PIN_6
#define ALL_BUTTONS ( LEFT_BUTTON | RIGHT_BUTTON | UP_BUTTON | DOWN_BUTTON | SELECT_BUTTON )
typedef struct
{
//
// Holds the states that the particular port is transitioning through
//
uint8_t state[NUM_BUTTON_STATES];
//
// Keeps up with where to store the next port info in the state array
//
uint8_t index;
//
// The currently debounced state of the pins
//
uint8_t debouncedState;
//
// The pins that just changed debounced state
//
uint8_t changed;
//
// Pullups or pulldowns are being used
//
uint8_t pullType;
}
Debouncer;
//*********************************************************************************
// Prototypes
//*********************************************************************************
//
// Button Debouncer Initialize
// Description:
// Initializes the Debouncer instantiation. Should be called at least once
// on a particular instantiation before calling ButtonProcess on the
// instantiation.
// Parameters:
// port - The address of a Debouncer instantiation.
// pulledUpButtons - Specifies whether pullups or pulldowns are being used on
// the port pins. This is the ORed BUTTON_PIN_* 's that are being
// pulled up. Otherwise, the debouncer assumes that the other
// buttons are being pulled down. A 0 bit means pulldown.
// A 1 bit means pullup. For example, 00010001 means that
// button 0 and button 4 are both being pulled up. All other
// buttons have pulldowns if they represent buttons.
// Returns:
// None
//
extern void ButtonDebounceInit(Debouncer *port, uint8_t pulledUpButtons);
//
// Button Process
// Description:
// Does the calculations on debouncing the buttons on a particular
// port. This function should be called on a regular interval by the
// application such as every 0.5 milliseconds or 5 milliseconds.
// Parameters:
// port - The address of a Debouncer instantiation.
// portStatus - The particular port's status expressed as one 8 bit byte.
// Returns:
// None
//
extern void ButtonProcess(Debouncer *port, uint8_t portStatus);
//
// Button Pressed
// Description:
// Checks to see if a button(s) were immediately pressed.
// Parameters:
// port - The address of a Debouncer instantiation.
// GPIOButtonPins - The particular bits corresponding to the button pins.
// The ORed combination of BUTTON_PIN_*.
// Returns:
// The port pin buttons that have just been pressed. For example, if
// (BUTTON_PIN_5 | BUTTON_PIN_0) is passed as a parameter for
// GPIOButtonPins and if the byte that is returned expressed in binary is
// 00000001, it means that button 0 (bit 0) has just been pressed while
// button 5 (bit 5) has not been at the moment though it may have been
// previously.
//
extern uint8_t ButtonPressed(Debouncer *port, uint8_t GPIOButtonPins);
//
// Button Released
// Description:
// Checks to see if a button(s) were immediately released.
// Parameters:
// port - The address of a Debouncer instantiation.
// GPIOButtonPins - The particular bits corresponding to the button pins.
// The ORed combination of BUTTON_PIN_*.
// Returns:
// The port pin buttons that have just been released. For example, if
// (BUTTON_PIN_5 | BUTTON_PIN_0) is passed as a parameter for
// GPIOButtonPins and if the byte that is returned expressed in binary is
// 00000001, it means that button 0 (bit 0) has just been released while
// button 5 (bit 5) has not been at the moment though it may have been
// previously.
//
extern uint8_t ButtonReleased(Debouncer *port, uint8_t GPIOButtonPins);
//
// Button Current
// Description:
// Gets which buttons are currently being pressed.
// Parameters:
// port - The address of a Debouncer instantiation.
// GPIOButtonPins - The particular bits corresponding to the button pins.
// The ORed combination of BUTTON_PIN_*.
// Returns:
// The port pins the are currently being debounced. For example, if
// (BUTTON_PIN_5 | BUTTON_PIN_1) is passed as a parameter for
// GPIOButtonPins and if this function returns 00100000 in binary then
// button 1 (bit 1) is not currently being pressed and button 5 (bit 5)
// is currently being pressed while the other buttons (if they are
// buttons) are being masked out.
//
extern uint8_t ButtonCurrent(Debouncer *port, uint8_t GPIOButtonPins);
uint8_t Port1ReadBits(void);
//
// End of C Binding
//
#ifdef __cplusplus
}
#endif
#endif // BTN_H