Contiki-NG
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 
164  /** True if the button uses negative logic (active: low) */
165  const bool negative_logic;
166 
167 #if GPIO_HAL_PORT_PIN_NUMBERING
168  /** The gpio port connected to the button */
169  gpio_hal_port_t port;
170 #endif
171 
172  /** The gpio pin connected to the button */
173  const gpio_hal_pin_t pin;
174 
175  /** The pin's pull configuration */
176  const gpio_hal_pin_cfg_t pull;
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  (sizeof(button_hal_buttons) / sizeof(button_hal_buttons[0])) - 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  */
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  */
308 /*---------------------------------------------------------------------------*/
309 #endif /* BUTTON_HAL_H_ */
310 /*---------------------------------------------------------------------------*/
311 /**
312  * @}
313  * @}
314  */
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 * button_hal_get_by_id(uint8_t unique_id)
Retrieve a button by ID.
Definition: button-hal.c:176
process_event_t button_hal_release_event
A broadcast event generated when a button gets released.
Definition: button-hal.c:54
button_hal_button_t * button_hal_get_by_index(uint8_t index)
Retrieve a button by its index.
Definition: button-hal.c:190
uint8_t gpio_hal_port_t
A data structure that represents ports.
Definition: gpio-hal.h:110
uint8_t button_hal_get_state(button_hal_button_t *button)
Get the state of a button (pressed / released)
Definition: button-hal.c:200
const uint8_t button_hal_button_count
The number of buttons on a device.
Definition: buttons.c:39
Header file for the callback timer
process_event_t button_hal_press_event
A broadcast event generated when a button gets pressed.
Definition: button-hal.c:53
uint8_t gpio_hal_pin_t
GPIO pin number representation.
Definition: gpio-hal.h:103
struct button_hal_button_s button_hal_button_t
A logical representation of a user button.
Definition: button-hal.h:145
Header file for the GPIO HAL.
void button_hal_init()
Initialise the button HAL.
Definition: button-hal.c:213
uint32_t gpio_hal_pin_cfg_t
GPIO pin configuration.
Definition: gpio-hal.h:118