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