The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
Kojto
Date:
Wed Oct 29 11:02:04 2014 +0000
Revision:
91:031413cf7a89
Release 91 of the mbed library

Changes:

- RBLAB_NANO - new target addition
- NRF51_DK - new target addition
- NRF51_DONGLE - new target addition

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 91:031413cf7a89 1 /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
Kojto 91:031413cf7a89 2 *
Kojto 91:031413cf7a89 3 * The information contained herein is property of Nordic Semiconductor ASA.
Kojto 91:031413cf7a89 4 * Terms and conditions of usage are described in detail in NORDIC
Kojto 91:031413cf7a89 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
Kojto 91:031413cf7a89 6 *
Kojto 91:031413cf7a89 7 * Licensees are granted free, non-transferable use of the information. NO
Kojto 91:031413cf7a89 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
Kojto 91:031413cf7a89 9 * the file.
Kojto 91:031413cf7a89 10 *
Kojto 91:031413cf7a89 11 */
Kojto 91:031413cf7a89 12
Kojto 91:031413cf7a89 13 /** @file
Kojto 91:031413cf7a89 14 *
Kojto 91:031413cf7a89 15 * @defgroup app_button Button Handler
Kojto 91:031413cf7a89 16 * @{
Kojto 91:031413cf7a89 17 * @ingroup app_common
Kojto 91:031413cf7a89 18 *
Kojto 91:031413cf7a89 19 * @brief Buttons handling module.
Kojto 91:031413cf7a89 20 *
Kojto 91:031413cf7a89 21 * @details The button handler uses the @ref app_gpiote to detect that a button has been
Kojto 91:031413cf7a89 22 * pushed. To handle debouncing, it will start a timer in the GPIOTE event handler.
Kojto 91:031413cf7a89 23 * The button will only be reported as pushed if the corresponding pin is still active when
Kojto 91:031413cf7a89 24 * the timer expires. If there is a new GPIOTE event while the timer is running, the timer
Kojto 91:031413cf7a89 25 * is restarted.
Kojto 91:031413cf7a89 26 * Use the USE_SCHEDULER parameter of the APP_BUTTON_INIT() macro to select if the
Kojto 91:031413cf7a89 27 * @ref app_scheduler is to be used or not.
Kojto 91:031413cf7a89 28 *
Kojto 91:031413cf7a89 29 * @note The app_button module uses the app_timer module. The user must ensure that the queue in
Kojto 91:031413cf7a89 30 * app_timer is large enough to hold the app_timer_stop() / app_timer_start() operations
Kojto 91:031413cf7a89 31 * which will be executed on each event from GPIOTE module (2 operations), as well as other
Kojto 91:031413cf7a89 32 * app_timer operations queued simultaneously in the application.
Kojto 91:031413cf7a89 33 *
Kojto 91:031413cf7a89 34 * @note Even if the scheduler is not used, app_button.h will include app_scheduler.h, so when
Kojto 91:031413cf7a89 35 * compiling, app_scheduler.h must be available in one of the compiler include paths.
Kojto 91:031413cf7a89 36 */
Kojto 91:031413cf7a89 37
Kojto 91:031413cf7a89 38 #ifndef APP_BUTTON_H__
Kojto 91:031413cf7a89 39 #define APP_BUTTON_H__
Kojto 91:031413cf7a89 40
Kojto 91:031413cf7a89 41 #include <stdint.h>
Kojto 91:031413cf7a89 42 #include <stdbool.h>
Kojto 91:031413cf7a89 43 #include "app_error.h"
Kojto 91:031413cf7a89 44 #include "app_scheduler.h"
Kojto 91:031413cf7a89 45 #include "nrf_gpio.h"
Kojto 91:031413cf7a89 46
Kojto 91:031413cf7a89 47 #define APP_BUTTON_SCHED_EVT_SIZE sizeof(app_button_event_t) /**< Size of button events being passed through the scheduler (is to be used for computing the maximum size of scheduler events). */
Kojto 91:031413cf7a89 48 #define APP_BUTTON_PUSH 1 /**< Indicates that a button is pushed. */
Kojto 91:031413cf7a89 49 #define APP_BUTTON_RELEASE 0 /**< Indicates that a button is released. */
Kojto 91:031413cf7a89 50 #define APP_BUTTON_ACTIVE_HIGH 1 /**< Indicates that a button is active high. */
Kojto 91:031413cf7a89 51 #define APP_BUTTON_ACTIVE_LOW 0 /**< Indicates that a button is active low. */
Kojto 91:031413cf7a89 52
Kojto 91:031413cf7a89 53 /**@brief Button event handler type. */
Kojto 91:031413cf7a89 54 typedef void (*app_button_handler_t)(uint8_t pin_no, uint8_t button_action);
Kojto 91:031413cf7a89 55
Kojto 91:031413cf7a89 56 /**@brief Type of function for passing events from the Button Handler module to the scheduler. */
Kojto 91:031413cf7a89 57 typedef uint32_t (*app_button_evt_schedule_func_t) (app_button_handler_t button_handler,
Kojto 91:031413cf7a89 58 uint8_t pin_no,
Kojto 91:031413cf7a89 59 uint8_t button_action);
Kojto 91:031413cf7a89 60
Kojto 91:031413cf7a89 61 /**@brief Button configuration structure. */
Kojto 91:031413cf7a89 62 typedef struct
Kojto 91:031413cf7a89 63 {
Kojto 91:031413cf7a89 64 uint8_t pin_no; /**< Pin to be used as a button. */
Kojto 91:031413cf7a89 65 uint8_t active_state; /**< APP_BUTTON_ACTIVE_HIGH or APP_BUTTON_ACTIVE_LOW. */
Kojto 91:031413cf7a89 66 nrf_gpio_pin_pull_t pull_cfg; /**< Pull-up or -down configuration. */
Kojto 91:031413cf7a89 67 app_button_handler_t button_handler; /**< Handler to be called when button is pushed. */
Kojto 91:031413cf7a89 68 } app_button_cfg_t;
Kojto 91:031413cf7a89 69
Kojto 91:031413cf7a89 70 /**@brief Pin transition direction struct. */
Kojto 91:031413cf7a89 71 typedef struct
Kojto 91:031413cf7a89 72 {
Kojto 91:031413cf7a89 73 uint32_t high_to_low; /**Pin went from high to low */
Kojto 91:031413cf7a89 74 uint32_t low_to_high; /**Pin went from low to high */
Kojto 91:031413cf7a89 75 } pin_transition_t;
Kojto 91:031413cf7a89 76
Kojto 91:031413cf7a89 77 /**@brief Macro for initializing the Button Handler module.
Kojto 91:031413cf7a89 78 *
Kojto 91:031413cf7a89 79 * @details It will initialize the specified pins as buttons, and configure the Button Handler
Kojto 91:031413cf7a89 80 * module as a GPIOTE user (but it will not enable button detection). It will also connect
Kojto 91:031413cf7a89 81 * the Button Handler module to the scheduler (if specified).
Kojto 91:031413cf7a89 82 *
Kojto 91:031413cf7a89 83 * @param[in] BUTTONS Array of buttons to be used (type app_button_cfg_t, must be
Kojto 91:031413cf7a89 84 * static!).
Kojto 91:031413cf7a89 85 * @param[in] BUTTON_COUNT Number of buttons.
Kojto 91:031413cf7a89 86 * @param[in] DETECTION_DELAY Delay from a GPIOTE event until a button is reported as pushed.
Kojto 91:031413cf7a89 87 * @param[in] USE_SCHEDULER TRUE if the application is using the event scheduler,
Kojto 91:031413cf7a89 88 * FALSE otherwise.
Kojto 91:031413cf7a89 89 */
Kojto 91:031413cf7a89 90 /*lint -emacro(506, APP_BUTTON_INIT) */ /* Suppress "Constant value Boolean */
Kojto 91:031413cf7a89 91 #define APP_BUTTON_INIT(BUTTONS, BUTTON_COUNT, DETECTION_DELAY, USE_SCHEDULER) \
Kojto 91:031413cf7a89 92 do \
Kojto 91:031413cf7a89 93 { \
Kojto 91:031413cf7a89 94 uint32_t ERR_CODE = app_button_init((BUTTONS), \
Kojto 91:031413cf7a89 95 (BUTTON_COUNT), \
Kojto 91:031413cf7a89 96 (DETECTION_DELAY), \
Kojto 91:031413cf7a89 97 (USE_SCHEDULER) ? app_button_evt_schedule : NULL); \
Kojto 91:031413cf7a89 98 APP_ERROR_CHECK(ERR_CODE); \
Kojto 91:031413cf7a89 99 } while (0)
Kojto 91:031413cf7a89 100
Kojto 91:031413cf7a89 101 /**@brief Function for initializing the Buttons.
Kojto 91:031413cf7a89 102 *
Kojto 91:031413cf7a89 103 * @details This function will initialize the specified pins as buttons, and configure the Button
Kojto 91:031413cf7a89 104 * Handler module as a GPIOTE user (but it will not enable button detection).
Kojto 91:031413cf7a89 105 *
Kojto 91:031413cf7a89 106 * @note Normally initialization should be done using the APP_BUTTON_INIT() macro, as that will take
Kojto 91:031413cf7a89 107 * care of connecting the Buttons module to the scheduler (if specified).
Kojto 91:031413cf7a89 108 *
Kojto 91:031413cf7a89 109 * @note app_button_enable() function must be called in order to enable the button detection.
Kojto 91:031413cf7a89 110 *
Kojto 91:031413cf7a89 111 * @param[in] p_buttons Array of buttons to be used (NOTE: Must be static!).
Kojto 91:031413cf7a89 112 * @param[in] button_count Number of buttons.
Kojto 91:031413cf7a89 113 * @param[in] detection_delay Delay from a GPIOTE event until a button is reported as pushed.
Kojto 91:031413cf7a89 114 * @param[in] evt_schedule_func Function for passing button events to the scheduler. Point to
Kojto 91:031413cf7a89 115 * app_button_evt_schedule() to connect to the scheduler. Set to
Kojto 91:031413cf7a89 116 * NULL to make the Buttons module call the event handler directly
Kojto 91:031413cf7a89 117 * from the delayed button push detection timeout handler.
Kojto 91:031413cf7a89 118 *
Kojto 91:031413cf7a89 119 * @return NRF_SUCCESS on success, otherwise an error code.
Kojto 91:031413cf7a89 120 */
Kojto 91:031413cf7a89 121 uint32_t app_button_init(app_button_cfg_t * p_buttons,
Kojto 91:031413cf7a89 122 uint8_t button_count,
Kojto 91:031413cf7a89 123 uint32_t detection_delay,
Kojto 91:031413cf7a89 124 app_button_evt_schedule_func_t evt_schedule_func);
Kojto 91:031413cf7a89 125
Kojto 91:031413cf7a89 126 /**@brief Function for enabling button detection.
Kojto 91:031413cf7a89 127 *
Kojto 91:031413cf7a89 128 * @retval NRF_ERROR_INVALID_PARAM GPIOTE has to many users.
Kojto 91:031413cf7a89 129 * @retval NRF_ERROR_INVALID_STATE Button or GPIOTE not initialized.
Kojto 91:031413cf7a89 130 * @retval NRF_SUCCESS Button detection successfully enabled.
Kojto 91:031413cf7a89 131 */
Kojto 91:031413cf7a89 132 uint32_t app_button_enable(void);
Kojto 91:031413cf7a89 133
Kojto 91:031413cf7a89 134 /**@brief Function for disabling button detection.
Kojto 91:031413cf7a89 135 *
Kojto 91:031413cf7a89 136 * @retval NRF_ERROR_INVALID_PARAM GPIOTE has to many users.
Kojto 91:031413cf7a89 137 * @retval NRF_ERROR_INVALID_STATE Button or GPIOTE not initialized.
Kojto 91:031413cf7a89 138 * @retval NRF_SUCCESS Button detection successfully enabled.
Kojto 91:031413cf7a89 139 */
Kojto 91:031413cf7a89 140 uint32_t app_button_disable(void);
Kojto 91:031413cf7a89 141
Kojto 91:031413cf7a89 142 /**@brief Function for checking if a button is currently being pushed.
Kojto 91:031413cf7a89 143 *
Kojto 91:031413cf7a89 144 * @param[in] pin_no Button pin to be checked.
Kojto 91:031413cf7a89 145 * @param[out] p_is_pushed Button state.
Kojto 91:031413cf7a89 146 *
Kojto 91:031413cf7a89 147 * @retval NRF_SUCCESS State successfully read.
Kojto 91:031413cf7a89 148 * @retval NRF_ERROR_INVALID_PARAM Invalid pin_no.
Kojto 91:031413cf7a89 149 */
Kojto 91:031413cf7a89 150 uint32_t app_button_is_pushed(uint8_t pin_no, bool * p_is_pushed);
Kojto 91:031413cf7a89 151
Kojto 91:031413cf7a89 152
Kojto 91:031413cf7a89 153 // Type and functions for connecting the Buttons module to the scheduler:
Kojto 91:031413cf7a89 154
Kojto 91:031413cf7a89 155 /**@cond NO_DOXYGEN */
Kojto 91:031413cf7a89 156 typedef struct
Kojto 91:031413cf7a89 157 {
Kojto 91:031413cf7a89 158 app_button_handler_t button_handler;
Kojto 91:031413cf7a89 159 uint8_t pin_no;
Kojto 91:031413cf7a89 160 uint8_t button_action;
Kojto 91:031413cf7a89 161 } app_button_event_t;
Kojto 91:031413cf7a89 162
Kojto 91:031413cf7a89 163 static __INLINE void app_button_evt_get(void * p_event_data, uint16_t event_size)
Kojto 91:031413cf7a89 164 {
Kojto 91:031413cf7a89 165 app_button_event_t * p_buttons_event = (app_button_event_t *)p_event_data;
Kojto 91:031413cf7a89 166
Kojto 91:031413cf7a89 167 APP_ERROR_CHECK_BOOL(event_size == sizeof(app_button_event_t));
Kojto 91:031413cf7a89 168 p_buttons_event->button_handler(p_buttons_event->pin_no, p_buttons_event->button_action);
Kojto 91:031413cf7a89 169 }
Kojto 91:031413cf7a89 170
Kojto 91:031413cf7a89 171 static __INLINE uint32_t app_button_evt_schedule(app_button_handler_t button_handler,
Kojto 91:031413cf7a89 172 uint8_t pin_no,
Kojto 91:031413cf7a89 173 uint8_t button_action)
Kojto 91:031413cf7a89 174 {
Kojto 91:031413cf7a89 175 app_button_event_t buttons_event;
Kojto 91:031413cf7a89 176
Kojto 91:031413cf7a89 177 buttons_event.button_handler = button_handler;
Kojto 91:031413cf7a89 178 buttons_event.pin_no = pin_no;
Kojto 91:031413cf7a89 179 buttons_event.button_action = button_action;
Kojto 91:031413cf7a89 180
Kojto 91:031413cf7a89 181 return app_sched_event_put(&buttons_event, sizeof(buttons_event), app_button_evt_get);
Kojto 91:031413cf7a89 182 }
Kojto 91:031413cf7a89 183 /**@endcond */
Kojto 91:031413cf7a89 184
Kojto 91:031413cf7a89 185 #endif // APP_BUTTON_H__
Kojto 91:031413cf7a89 186
Kojto 91:031413cf7a89 187 /** @} */