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/clock.h"
85
#include "
sys/ctimer.h
"
86
87
#include <stdint.h>
88
#include <stdbool.h>
89
#include <string.h>
90
/*---------------------------------------------------------------------------*/
91
/**
92
* \brief Controls the software debounce timer duration.
93
*
94
* The platform can provide a more suitable value. This value will apply to
95
* all buttons.
96
*/
97
#ifdef BUTTON_HAL_CONF_DEBOUNCE_DURATION
98
#define BUTTON_HAL_DEBOUNCE_DURATION BUTTON_HAL_CONF_DEBOUNCE_DURATION
99
#else
100
#define BUTTON_HAL_DEBOUNCE_DURATION (CLOCK_SECOND >> 6)
101
#endif
102
/*---------------------------------------------------------------------------*/
103
/**
104
* \brief Controls whether buttons will have human-readable names
105
*
106
* Define this to zero to save code space
107
*/
108
#if BUTTON_HAL_CONF_WITH_DESCRIPTION
109
#define BUTTON_HAL_WITH_DESCRIPTION BUTTON_HAL_CONF_WITH_DESCRIPTION
110
#else
111
#define BUTTON_HAL_WITH_DESCRIPTION 1
112
#endif
113
/*---------------------------------------------------------------------------*/
114
/**
115
* \brief Number of different ports that buttons are connected to
116
*
117
* For example, if our PCB has 3 buttons conncted to pins P0.1, P0,6 and P2.3
118
* then the platform configuration should define this to value 2 (one for each
119
* of ports 0 and 2)
120
*/
121
#ifdef BUTTON_HAL_CONF_PORT_COUNT
122
#define BUTTON_HAL_PORT_COUNT BUTTON_HAL_CONF_PORT_COUNT
123
#else
124
#define BUTTON_HAL_PORT_COUNT 1
125
#endif
126
/*---------------------------------------------------------------------------*/
127
#define BUTTON_HAL_STATE_RELEASED 0
128
#define BUTTON_HAL_STATE_PRESSED 1
129
/*---------------------------------------------------------------------------*/
130
/**
131
* Optional button IDs
132
*/
133
#define BUTTON_HAL_ID_BUTTON_ZERO 0x00
134
#define BUTTON_HAL_ID_BUTTON_ONE 0x01
135
#define BUTTON_HAL_ID_BUTTON_TWO 0x02
136
#define BUTTON_HAL_ID_BUTTON_THREE 0x03
137
#define BUTTON_HAL_ID_BUTTON_FOUR 0x04
138
#define BUTTON_HAL_ID_BUTTON_FIVE 0x05
139
140
#define BUTTON_HAL_ID_USER_BUTTON BUTTON_HAL_ID_BUTTON_ZERO
141
/*---------------------------------------------------------------------------*/
142
/**
143
* \brief A logical representation of a user button
144
*/
145
typedef
struct
button_hal_button_s
button_hal_button_t
;
146
147
struct
button_hal_button_s {
148
/** Used by the s/w debounce functionality */
149
struct
ctimer debounce_ctimer;
150
151
/** A callback timer used to count duration of button presses */
152
struct
ctimer duration_ctimer;
153
154
#if BUTTON_HAL_WITH_DESCRIPTION
155
/**
156
* \brief A textual description of the button
157
*
158
* This field may only be accessed using the BUTTON_HAL_GET_DESCRIPTION()
159
* macro.
160
*/
161
const
char
*description;
162
#endif
163
/** The pin's pull configuration */
164
const
gpio_hal_pin_cfg_t
pull;
165
166
/** True if the button uses negative logic (active: low) */
167
const
bool
negative_logic;
168
169
#if GPIO_HAL_PORT_PIN_NUMBERING
170
/** The gpio port connected to the button */
171
gpio_hal_port_t
port;
172
#endif
173
174
/** The gpio pin connected to the button */
175
const
gpio_hal_pin_t
pin;
176
177
/** A counter of the duration (in seconds) of a button press */
178
uint8_t press_duration_seconds;
179
180
/**
181
* \brief A unique identifier for this button.
182
*
183
* The platform code is responsible of setting unique values here. This can
184
* be used later to determine which button generated an event. Many examples
185
* assume the existence of a button with ID == BUTTON_HAL_ID_BUTTON_ZERO,
186
* so it is good idea to use this ID for one of your platform's buttons.
187
*/
188
const
uint8_t unique_id;
189
};
190
/*---------------------------------------------------------------------------*/
191
#if BUTTON_HAL_WITH_DESCRIPTION
192
#if GPIO_HAL_PORT_PIN_NUMBERING
193
/**
194
* \brief Define a button to be used by the HAL
195
* \param name The variable name for the button
196
* \param descr A textual description
197
* \param po The port connected to the button
198
* \param pi The pin connected to the button
199
* \param nl True if the button is connected using negative logic
200
* \param u The button's pull configuration
201
* \param id A unique numeric identifier
202
*/
203
#define BUTTON_HAL_BUTTON(name, descr, po, pi, u, id, nl) \
204
static button_hal_button_t name = { \
205
.description = descr, \
206
.port = po, \
207
.pin = pi, \
208
.pull = u, \
209
.unique_id = id, \
210
.negative_logic = nl, \
211
}
212
#else
/* GPIO_HAL_PORT_PIN_NUMBERING */
213
#define BUTTON_HAL_BUTTON(name, descr, pi, u, id, nl) \
214
static button_hal_button_t name = { \
215
.description = descr, \
216
.pin = pi, \
217
.pull = u, \
218
.unique_id = id, \
219
.negative_logic = nl, \
220
}
221
#endif
/* GPIO_HAL_PORT_PIN_NUMBERING */
222
223
/**
224
* \brief Retrieve the textual description of a button
225
* \param b A pointer to the button button_hal_button_t
226
*
227
* This macro will return the value of the description field for b. If
228
* BUTTON_HAL_WITH_DESCRIPTION is 0 then this macro will return ""
229
*/
230
#define BUTTON_HAL_GET_DESCRIPTION(b) (b)->description
231
232
#else
/* BUTTON_HAL_WITH_DESCRIPTION */
233
234
#if GPIO_HAL_PORT_PIN_NUMBERING
235
#define BUTTON_HAL_BUTTON(name, descr, po, pi, u, id, nl) \
236
static button_hal_button_t name = { \
237
.port = po, \
238
.pin = pi, \
239
.pull = u, \
240
.unique_id = id, \
241
.negative_logic = nl, \
242
}
243
#else
/* GPIO_HAL_PORT_PIN_NUMBERING */
244
#define BUTTON_HAL_BUTTON(name, descr, pi, u, id, nl) \
245
static button_hal_button_t name = { \
246
.pin = pi, \
247
.pull = u, \
248
.unique_id = id, \
249
.negative_logic = nl, \
250
}
251
#endif
/* GPIO_HAL_PORT_PIN_NUMBERING */
252
253
#define BUTTON_HAL_GET_DESCRIPTION(b) ""
254
#endif
/* BUTTON_HAL_WITH_DESCRIPTION */
255
/*---------------------------------------------------------------------------*/
256
#define BUTTON_HAL_BUTTONS(...) \
257
button_hal_button_t *button_hal_buttons[] = {__VA_ARGS__, NULL}; \
258
const uint8_t button_hal_button_count = \
259
(sizeof(button_hal_buttons) / sizeof(button_hal_buttons[0])) - 1;
260
/*---------------------------------------------------------------------------*/
261
/**
262
* \brief The number of buttons on a device
263
*/
264
extern
const
uint8_t
button_hal_button_count
;
265
/*---------------------------------------------------------------------------*/
266
/**
267
* \brief A broadcast event generated when a button gets pressed
268
*/
269
extern
process_event_t
button_hal_press_event
;
270
271
/**
272
* \brief A broadcast event generated when a button gets released
273
*/
274
extern
process_event_t
button_hal_release_event
;
275
276
/**
277
* \brief A broadcast event generated every second while a button is kept pressed
278
*/
279
extern
process_event_t
button_hal_periodic_event
;
280
/*---------------------------------------------------------------------------*/
281
/**
282
* \brief Initialise the button HAL
283
*/
284
void
button_hal_init
(
void
);
285
286
/**
287
* \brief Retrieve a button by ID
288
* \param unique_id The button unique ID to search for
289
* \return A pointer to the button or NULL if not found
290
*/
291
button_hal_button_t
*
button_hal_get_by_id
(uint8_t unique_id);
292
293
/**
294
* \brief Retrieve a button by its index
295
* \param index The button's index (0, 1, ... button_hal_button_count - 1)
296
* \return A pointer to the button or NULL if not found
297
*/
298
button_hal_button_t
*
button_hal_get_by_index
(uint8_t index);
299
300
/**
301
* \brief Get the state of a button (pressed / released)
302
* \param button A pointer to the button
303
* \retval BUTTON_HAL_STATE_RELEASED The button is currently released
304
* \retval BUTTON_HAL_STATE_PRESSED The button is currently pressed
305
*/
306
uint8_t
button_hal_get_state
(
button_hal_button_t
*button);
307
/*---------------------------------------------------------------------------*/
308
#endif
/* BUTTON_HAL_H_ */
309
/*---------------------------------------------------------------------------*/
310
/**
311
* @}
312
* @}
313
*/
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:145
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