Contiki-NG
Toggle main menu visibility
Loading...
Searching...
No Matches
button-hal.h
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2017, George Oikonomou - http://www.spd.gr
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
*
9
* 1. Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* 2. 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
* 3. Neither the name of the copyright holder nor the names of its
15
* contributors may be used to endorse or promote products derived
16
* from this software without specific prior written permission.
17
*
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23
* 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)
28
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29
* OF THE POSSIBILITY OF SUCH DAMAGE.
30
*/
31
/*---------------------------------------------------------------------------*/
32
/**
33
* \addtogroup dev
34
* @{
35
*/
36
/*---------------------------------------------------------------------------*/
37
/**
38
* \defgroup button_hal Button HAL
39
*
40
* Hardware abstraction layer for user buttons.
41
*
42
* This HAL enables an abstraction of general-purpose / user buttons or
43
* similar peripherals (e.g. a reed relay can also be abstracted through this
44
* HAL). The HAL handles software debounce timers internally, therefore the
45
* platform-specific button driver does not need to worry about debouncing.
46
*
47
* The platform developer needs to define a variable of type
48
* \c button_hal_button_t for each user button. Within this variable, the
49
* developer needs to specify the GPIO pin where the button is attached,
50
* whether the button uses negative logic, and whether the GPIO pin should
51
* be configured with internal pullup/down. The developer also needs to provide
52
* a unique index for each button, as well as a description.
53
*
54
* With those in place, the HAL will generate the following process events:
55
*
56
* - button_hal_press_event: Upon press of the button
57
* - button_hal_release_event: Upon release of the button
58
* - button_hal_periodic_event: Generated every second that the user button is
59
* kept pressed.
60
*
61
* With those events in place, an application can perform an action:
62
*
63
* - Immediately after the button gets pressed.
64
* - After the button has been pressed for N seconds.
65
* - Immediately upon release of the button. This action can vary depending
66
* on how long the button had been pressed for.
67
*
68
* A platform with user buttons can either implement this API (recommended) or
69
* the older button_sensor API. Some examples will not work if this API is not
70
* implemented.
71
*
72
* This API requires the platform to first support the GPIO HAL API.
73
* @{
74
*
75
* \file
76
* Header file for the button HAL
77
*/
78
/*---------------------------------------------------------------------------*/
79
#ifndef BUTTON_HAL_H_
80
#define BUTTON_HAL_H_
81
/*---------------------------------------------------------------------------*/
82
#include "contiki.h"
83
#include "
dev/gpio-hal.h
"
84
#include "
sys/cc.h
"
85
#include "sys/clock.h"
86
#include "
sys/ctimer.h
"
87
88
#include <stdint.h>
89
#include <stdbool.h>
90
#include <string.h>
91
/*---------------------------------------------------------------------------*/
92
/**
93
* \brief Controls the software debounce timer duration.
94
*
95
* The platform can provide a more suitable value. This value will apply to
96
* all buttons.
97
*/
98
#ifdef BUTTON_HAL_CONF_DEBOUNCE_DURATION
99
#define BUTTON_HAL_DEBOUNCE_DURATION BUTTON_HAL_CONF_DEBOUNCE_DURATION
100
#else
101
#define BUTTON_HAL_DEBOUNCE_DURATION (CLOCK_SECOND >> 6)
102
#endif
103
/*---------------------------------------------------------------------------*/
104
/**
105
* \brief Controls whether buttons will have human-readable names
106
*
107
* Define this to zero to save code space
108
*/
109
#if BUTTON_HAL_CONF_WITH_DESCRIPTION
110
#define BUTTON_HAL_WITH_DESCRIPTION BUTTON_HAL_CONF_WITH_DESCRIPTION
111
#else
112
#define BUTTON_HAL_WITH_DESCRIPTION 1
113
#endif
114
/*---------------------------------------------------------------------------*/
115
/**
116
* \brief Number of different ports that buttons are connected to
117
*
118
* For example, if our PCB has 3 buttons conncted to pins P0.1, P0,6 and P2.3
119
* then the platform configuration should define this to value 2 (one for each
120
* of ports 0 and 2)
121
*/
122
#ifdef BUTTON_HAL_CONF_PORT_COUNT
123
#define BUTTON_HAL_PORT_COUNT BUTTON_HAL_CONF_PORT_COUNT
124
#else
125
#define BUTTON_HAL_PORT_COUNT 1
126
#endif
127
/*---------------------------------------------------------------------------*/
128
#define BUTTON_HAL_STATE_RELEASED 0
129
#define BUTTON_HAL_STATE_PRESSED 1
130
/*---------------------------------------------------------------------------*/
131
/**
132
* Optional button IDs
133
*/
134
#define BUTTON_HAL_ID_BUTTON_ZERO 0x00
135
#define BUTTON_HAL_ID_BUTTON_ONE 0x01
136
#define BUTTON_HAL_ID_BUTTON_TWO 0x02
137
#define BUTTON_HAL_ID_BUTTON_THREE 0x03
138
#define BUTTON_HAL_ID_BUTTON_FOUR 0x04
139
#define BUTTON_HAL_ID_BUTTON_FIVE 0x05
140
141
#define BUTTON_HAL_ID_USER_BUTTON BUTTON_HAL_ID_BUTTON_ZERO
142
/*---------------------------------------------------------------------------*/
143
/**
144
* \brief A logical representation of a user button
145
*/
146
typedef
struct
button_hal_button_s
button_hal_button_t
;
147
148
struct
button_hal_button_s {
149
/** Used by the s/w debounce functionality */
150
struct
ctimer debounce_ctimer;
151
152
/** A callback timer used to count duration of button presses */
153
struct
ctimer duration_ctimer;
154
155
#if BUTTON_HAL_WITH_DESCRIPTION
156
/**
157
* \brief A textual description of the button
158
*
159
* This field may only be accessed using the BUTTON_HAL_GET_DESCRIPTION()
160
* macro.
161
*/
162
const
char
*description;
163
#endif
164
/** The pin's pull configuration */
165
const
gpio_hal_pin_cfg_t
pull;
166
167
/** True if the button uses negative logic (active: low) */
168
const
bool
negative_logic;
169
170
#if GPIO_HAL_PORT_PIN_NUMBERING
171
/** The gpio port connected to the button */
172
gpio_hal_port_t
port;
173
#endif
174
175
/** The gpio pin connected to the button */
176
const
gpio_hal_pin_t
pin;
177
178
/** A counter of the duration (in seconds) of a button press */
179
uint8_t press_duration_seconds;
180
181
/**
182
* \brief A unique identifier for this button.
183
*
184
* The platform code is responsible of setting unique values here. This can
185
* be used later to determine which button generated an event. Many examples
186
* assume the existence of a button with ID == BUTTON_HAL_ID_BUTTON_ZERO,
187
* so it is good idea to use this ID for one of your platform's buttons.
188
*/
189
const
uint8_t unique_id;
190
};
191
/*---------------------------------------------------------------------------*/
192
#if BUTTON_HAL_WITH_DESCRIPTION
193
#if GPIO_HAL_PORT_PIN_NUMBERING
194
/**
195
* \brief Define a button to be used by the HAL
196
* \param name The variable name for the button
197
* \param descr A textual description
198
* \param po The port connected to the button
199
* \param pi The pin connected to the button
200
* \param nl True if the button is connected using negative logic
201
* \param u The button's pull configuration
202
* \param id A unique numeric identifier
203
*/
204
#define BUTTON_HAL_BUTTON(name, descr, po, pi, u, id, nl) \
205
static button_hal_button_t name = { \
206
.description = descr, \
207
.port = po, \
208
.pin = pi, \
209
.pull = u, \
210
.unique_id = id, \
211
.negative_logic = nl, \
212
}
213
#else
/* GPIO_HAL_PORT_PIN_NUMBERING */
214
#define BUTTON_HAL_BUTTON(name, descr, pi, u, id, nl) \
215
static button_hal_button_t name = { \
216
.description = descr, \
217
.pin = pi, \
218
.pull = u, \
219
.unique_id = id, \
220
.negative_logic = nl, \
221
}
222
#endif
/* GPIO_HAL_PORT_PIN_NUMBERING */
223
224
/**
225
* \brief Retrieve the textual description of a button
226
* \param b A pointer to the button button_hal_button_t
227
*
228
* This macro will return the value of the description field for b. If
229
* BUTTON_HAL_WITH_DESCRIPTION is 0 then this macro will return ""
230
*/
231
#define BUTTON_HAL_GET_DESCRIPTION(b) (b)->description
232
233
#else
/* BUTTON_HAL_WITH_DESCRIPTION */
234
235
#if GPIO_HAL_PORT_PIN_NUMBERING
236
#define BUTTON_HAL_BUTTON(name, descr, po, pi, u, id, nl) \
237
static button_hal_button_t name = { \
238
.port = po, \
239
.pin = pi, \
240
.pull = u, \
241
.unique_id = id, \
242
.negative_logic = nl, \
243
}
244
#else
/* GPIO_HAL_PORT_PIN_NUMBERING */
245
#define BUTTON_HAL_BUTTON(name, descr, pi, u, id, nl) \
246
static button_hal_button_t name = { \
247
.pin = pi, \
248
.pull = u, \
249
.unique_id = id, \
250
.negative_logic = nl, \
251
}
252
#endif
/* GPIO_HAL_PORT_PIN_NUMBERING */
253
254
#define BUTTON_HAL_GET_DESCRIPTION(b) ""
255
#endif
/* BUTTON_HAL_WITH_DESCRIPTION */
256
/*---------------------------------------------------------------------------*/
257
#define BUTTON_HAL_BUTTONS(...) \
258
button_hal_button_t *button_hal_buttons[] = {__VA_ARGS__, NULL}; \
259
const uint8_t button_hal_button_count = \
260
CC_ARRAY_LENGTH(button_hal_buttons) - 1;
261
/*---------------------------------------------------------------------------*/
262
/**
263
* \brief The number of buttons on a device
264
*/
265
extern
const
uint8_t
button_hal_button_count
;
266
/*---------------------------------------------------------------------------*/
267
/**
268
* \brief A broadcast event generated when a button gets pressed
269
*/
270
extern
process_event_t
button_hal_press_event
;
271
272
/**
273
* \brief A broadcast event generated when a button gets released
274
*/
275
extern
process_event_t
button_hal_release_event
;
276
277
/**
278
* \brief A broadcast event generated every second while a button is kept pressed
279
*/
280
extern
process_event_t
button_hal_periodic_event
;
281
/*---------------------------------------------------------------------------*/
282
/**
283
* \brief Initialise the button HAL
284
*/
285
void
button_hal_init
(
void
);
286
287
/**
288
* \brief Retrieve a button by ID
289
* \param unique_id The button unique ID to search for
290
* \return A pointer to the button or NULL if not found
291
*/
292
button_hal_button_t
*
button_hal_get_by_id
(uint8_t unique_id);
293
294
/**
295
* \brief Retrieve a button by its index
296
* \param index The button's index (0, 1, ... button_hal_button_count - 1)
297
* \return A pointer to the button or NULL if not found
298
*/
299
button_hal_button_t
*
button_hal_get_by_index
(uint8_t index);
300
301
/**
302
* \brief Get the state of a button (pressed / released)
303
* \param button A pointer to the button
304
* \retval BUTTON_HAL_STATE_RELEASED The button is currently released
305
* \retval BUTTON_HAL_STATE_PRESSED The button is currently pressed
306
*/
307
uint8_t
button_hal_get_state
(
button_hal_button_t
*button);
308
/*---------------------------------------------------------------------------*/
309
#endif
/* BUTTON_HAL_H_ */
310
/*---------------------------------------------------------------------------*/
311
/**
312
* @}
313
* @}
314
*/
cc.h
Default definitions of C compiler quirk work-arounds.
ctimer.h
Header file for the callback timer.
gpio-hal.h
Header file for the GPIO HAL.
button_hal_press_event
process_event_t button_hal_press_event
A broadcast event generated when a button gets pressed.
Definition
button-hal.c:53
button_hal_get_state
uint8_t button_hal_get_state(button_hal_button_t *button)
Get the state of a button (pressed / released).
Definition
button-hal.c:200
button_hal_get_by_id
button_hal_button_t * button_hal_get_by_id(uint8_t unique_id)
Retrieve a button by ID.
Definition
button-hal.c:176
button_hal_release_event
process_event_t button_hal_release_event
A broadcast event generated when a button gets released.
Definition
button-hal.c:54
button_hal_init
void button_hal_init()
Initialise the button HAL.
Definition
button-hal.c:213
button_hal_get_by_index
button_hal_button_t * button_hal_get_by_index(uint8_t index)
Retrieve a button by its index.
Definition
button-hal.c:190
button_hal_periodic_event
process_event_t button_hal_periodic_event
A broadcast event generated every second while a button is kept pressed.
Definition
button-hal.c:55
button_hal_button_t
struct button_hal_button_s button_hal_button_t
A logical representation of a user button.
Definition
button-hal.h:146
button_hal_button_count
const uint8_t button_hal_button_count
The number of buttons on a device.
Definition
board-buttons.c:64
gpio_hal_pin_cfg_t
uint32_t gpio_hal_pin_cfg_t
GPIO pin configuration.
Definition
gpio-hal.h:118
gpio_hal_port_t
uint8_t gpio_hal_port_t
A data structure that represents ports.
Definition
gpio-hal.h:110
gpio_hal_pin_t
uint8_t gpio_hal_pin_t
GPIO pin number representation.
Definition
gpio-hal.h:103
os
dev
button-hal.h
Generated on
for Contiki-NG by
1.17.0