Button.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2019, Texas Instruments Incorporated
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  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33  /*!***************************************************************************
34  * @file Button.h
35  *
36  * @brief Button driver
37  *
38  * @anchor ti_drivers_Button_Synopsis
39  * # Synopsis #
40  *
41  * @code
42  * #include <ti/drivers/apps/Button.h>
43  *
44  * int main(void)
45  * {
46  * Button_Params params;
47  * Button_Handle handle;
48  *
49  * Button_Params_init(&params);
50  *
51  * handle = Button_open(CONFIG_BUTTON0, buttonCallbackFxn, &params);
52  *
53  * ...
54  * }
55  *
56  * void buttonCallbackFxn(Button_Handle handle, Button_EventMask events)
57  * {
58  * if (events & Button_EV_CLICK)
59  * {
60  * // Received a click, handle app condition 0 etc
61  * handleAppCond(0);
62  * }
63  * if (events & Button_EV_LONGCLICKED)
64  * {
65  * // Long press, handle app condition 1 etc
66  * handleAppCond(1);
67  * }
68  * ...
69  * }
70  * @endcode
71  *
72  * @anchor ti_drivers_Button_Examples
73  * ## Examples #
74  *
75  * * @ref ti_drivers_Button_Examples_config "Generic Configuration"
76  *
77  * ## Overview #
78  *
79  * The Button driver simplifies interfacing push buttons. For example, push
80  * buttons on LaunchPads, BoosterPacks, or custom boards may easily be managed
81  * via the Button API. A given button instance may subscribe to one or several
82  * #Button_Events. When a subscribed event occurs, the user will receive a
83  * callback with the handle of the button and the event(s) that occured.
84  *
85  * ## User requirements #
86  * Buttons use the @ref GPIO.h interface for interfacing with hardware, so a
87  * #GPIO_PinConfig array must exist and contain a config for the button pin.
88  * The user must statically allocate a #Button_Config array called
89  * Button_config. Each physical button should map to an index in
90  * Button_config.
91  *
92  * ## Defining #Button_Config, #Button_Object and #Button_HWAttrs #
93  * Each structure must be defined by the application. The following
94  * example is for a MSP432 in which two buttons are setup.
95  * The following declarations are placed in "ti_drivers_config.h"
96  * and "ti_drivers_config.c" respectively. How the GPIO configs are defined
97  * are detailed in the next example.
98  *
99  * @anchor ti_drivers_Button_Examples_config
100  *
101  * "ti_drivers_config.h"
102  * @code
103  * #define CONFIG_BUTTON_0 0 //Button number 1
104  * #define CONFIG_BUTTON_1 1 //Button number 2
105  * @endcode
106  *
107  * "ti_drivers_config.c"
108  * @code
109  * #include <Button.h>
110  *
111  * Button_Object Button_object[2];
112  *
113  * const Button_HWAttrs Button_hwAttrs[2] = {
114  * {
115  * .gpioIndex = CONFIG_S1,
116  * },
117  * {
118  * .gpioIndex = CONFIG_S2,
119  * }
120  * };
121  *
122  * const Button_Config Button_config[2] = {
123  * {
124  * .hwAttrs = &Button_hwAttrs[0],
125  * .object = &Button_object[0],
126  * },
127  * {
128  * .hwAttrs = &Button_hwAttrs[1],
129  * .object = &Button_object[1],
130  * },
131  * };
132  * @endcode
133  *
134  * ##Setting up GPIO configurations #
135  *
136  * The following example is for a MSP432.
137  * We are showing interfacing of two push buttons. Each need a GPIO pin. The
138  * following definitions are in "ti_drivers_config.h" and
139  * "ti_drivers_config.c" respectively. This example uses GPIO pins 1.1 and
140  * 1.4. The other GPIO configuration structures must exist, see @ref GPIO.h.
141  *
142  * "ti_drivers_config.h"
143  * @code
144  * #define CONFIG_S1 0
145  * #define CONFIG_S2 1
146  * @endcode
147  *
148  * "ti_drivers_config.c"
149  * @code
150  * #include <gpio.h>
151  * GPIO_PinConfig gpioPinConfigs[] = {
152  * GPIOMSP432_P1_1 | GPIO_CFG_IN_PU | GPIO_CFG_IN_INT_FALLING,
153  * GPIOMSP432_P1_4 | GPIO_CFG_IN_PU | GPIO_CFG_IN_INT_FALLING,
154  * }
155  *
156  * @endcode
157  ******************************************************************************
158  */
159 #ifndef ti_drivers_Button__include
160 #define ti_drivers_Button__include
161 
162 #include <stdint.h>
163 #include <stdbool.h>
164 
165 /* Driver Header files */
166 #include <ti/drivers/GPIO.h>
167 #include <ti/drivers/dpl/ClockP.h>
168 
169 #ifdef __cplusplus
170 extern "C" {
171 #endif
172 
173 /* Number of user defined Button configurations */
174 extern const uint_least8_t Button_count;
175 
183 typedef struct Button_Config
184 {
186  void *object;
187 
189  void const *hwAttrs;
191 
197 typedef struct Button_Config* Button_Handle;
198 
206 typedef enum Button_State
207 {
209  Button_PRESSING = 1,
211  Button_PRESSED = 2,
213  Button_LONGPRESSING = 3,
215  Button_LONGPRESSED = 4,
217  Button_RELEASING = 5,
219  Button_RELEASING_LONG = 6,
221  Button_RELEASED = 7,
223  Button_DBLPRESS_DETECTION = 8,
225  Button_DBLPRESSING = 9,
227  Button_DBLPRESSED = 10,
229  Button_RELEASING_DBLPRESSED = 11
230 } Button_State;
231 
238 typedef enum Button_Events
239 {
255 } Button_Events;
256 
258 typedef uint8_t Button_EventMask;
259 
263 typedef void (*Button_Callback)(Button_Handle buttonHandle,
264  Button_EventMask buttonEvents);
265 
272 typedef enum Button_Pull
273 {
274  /* NOTE: DO NOT change the values of DOWN/UP from (0,1) */
278 } Button_Pull;
279 
288 typedef struct Button_HWAttrs
289 {
290  uint_least8_t gpioIndex;
292 
302 typedef struct Button_StateVariables
303 {
305  Button_State state;
307  uint32_t pressedStartTime;
309  uint32_t lastPressedDuration;
310 }Button_StateVariables;
311 
316 typedef struct Button_Object
317 {
319  ClockP_Handle clockHandle;
320 
322  Button_StateVariables buttonStateVariables;
323 
325  Button_EventMask buttonEventMask;
326 
329 
332 
335 
338 
341 } Button_Object;
342 
351 typedef struct Button_Params
352 {
355 
358 
361 
363  Button_EventMask buttonEventMask;
364 } Button_Params;
365 
375 extern bool Button_close(Button_Handle handle);
376 
380 extern void Button_init();
381 
406 extern Button_Handle Button_open(uint_least8_t buttonIndex,
407  Button_Callback buttonCallback,
408  Button_Params *params);
409 
424 extern void Button_Params_init(Button_Params *params);
425 
442 extern uint32_t Button_getLastPressedDuration(Button_Handle handle);
443 
452 extern void Button_setCallback(Button_Handle handle,
453  Button_Callback buttonCallback);
454 
469 void Button_gpioCallbackFxn(uint_least8_t index);
470 
471 #ifdef __cplusplus
472 }
473 #endif
474 
475 #endif /* ti_drivers_Button__include */
void Button_init()
Function to initialize Button driver.
void Button_setCallback(Button_Handle handle, Button_Callback buttonCallback)
Function to set callback function for the button instance.
struct Button_Config * Button_Handle
A handle that is returned from a Button_open() call.
Definition: Button.h:197
struct Button_Object Button_Object
Internal to Button module. Members should not be accessed by the application.
void Button_Params_init(Button_Params *params)
Function to initialize a Button_Params struct to its defaults.
Definition: Button.h:252
uint32_t doublePressDetectiontimeout
Definition: Button.h:360
const uint_least8_t Button_count
struct Button_Config Button_Config
Button configuration.
Button_EventMask buttonEventMask
Definition: Button.h:363
Button_Pull
Button Pull settings.
Definition: Button.h:272
Internal to Button module. Members should not be accessed by the application.
Definition: Button.h:316
ClockP_Handle clockHandle
Definition: Button.h:319
Definition: Button.h:247
Button_Events
Button event flags.
Definition: Button.h:238
uint32_t longPressDuration
Definition: Button.h:357
uint8_t Button_EventMask
Event subscription and notification mask type.
Definition: Button.h:258
Button_Handle Button_open(uint_least8_t buttonIndex, Button_Callback buttonCallback, Button_Params *params)
Function to open a given Button.
Definition: Button.h:241
Definition: Button.h:276
Button Parameters.
Definition: Button.h:351
void Button_gpioCallbackFxn(uint_least8_t index)
This is the GPIO interrupt callback function which is called on a button press or release...
Button_EventMask buttonEventMask
Definition: Button.h:325
struct Button_Params Button_Params
Button Parameters.
Button_Callback buttonCallback
Definition: Button.h:328
Definition: Button.h:277
Definition: Button.h:275
void * object
Definition: Button.h:186
Button configuration.
Definition: Button.h:183
void const * hwAttrs
Definition: Button.h:189
uint32_t debounceDuration
Definition: Button.h:331
uint_least8_t gpioIndex
Definition: Button.h:290
void(* Button_Callback)(Button_Handle buttonHandle, Button_EventMask buttonEvents)
A handler to receive button callbacks.
Definition: Button.h:263
Button_Pull buttonPull
Definition: Button.h:340
General Purpose I/O driver interface.
uint32_t doublePressDetectiontimeout
Definition: Button.h:337
Definition: Button.h:245
Hardware specific settings for a button.
Definition: Button.h:288
uint32_t debounceDuration
Definition: Button.h:354
uint32_t longPressDuration
Definition: Button.h:334
bool Button_close(Button_Handle handle)
Function to close a Button specified by the Button_Handle.
uint32_t Button_getLastPressedDuration(Button_Handle handle)
Function to return the lastPressedDuration (valid only for short press, long press) ...
struct Button_HWAttrs Button_HWAttrs
Hardware specific settings for a button.
Definition: Button.h:254
Definition: Button.h:243
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale