mediCAL's first BLE project
Fork of nRF51822 by
nordic/nrf-sdk/app_common/app_button.h@69:d9f51b65a3c8, 2014-11-02 (annotated)
- Committer:
- antoniorohit
- Date:
- Sun Nov 02 20:43:14 2014 +0000
- Revision:
- 69:d9f51b65a3c8
- Parent:
- 65:98215c4f3a25
First rev of BLE program for nRF51822 which provides BLE connectivity for team mediCAL's PILLar;
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> |
Rohit Grover |
65:98215c4f3a25 | 43 | #include "nrf.h" |
bogdanm | 0:eff01767de02 | 44 | #include "app_error.h" |
bogdanm | 0:eff01767de02 | 45 | #include "app_scheduler.h" |
bogdanm | 0:eff01767de02 | 46 | #include "nrf_gpio.h" |
bogdanm | 0:eff01767de02 | 47 | |
Rohit Grover |
37:c29c330d942c | 48 | #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 | 49 | #define APP_BUTTON_PUSH 1 /**< Indicates that a button is pushed. */ |
Rohit Grover |
37:c29c330d942c | 50 | #define APP_BUTTON_RELEASE 0 /**< Indicates that a button is released. */ |
Rohit Grover |
37:c29c330d942c | 51 | #define APP_BUTTON_ACTIVE_HIGH 1 /**< Indicates that a button is active high. */ |
Rohit Grover |
37:c29c330d942c | 52 | #define APP_BUTTON_ACTIVE_LOW 0 /**< Indicates that a button is active low. */ |
bogdanm | 0:eff01767de02 | 53 | |
bogdanm | 0:eff01767de02 | 54 | /**@brief Button event handler type. */ |
Rohit Grover |
37:c29c330d942c | 55 | typedef void (*app_button_handler_t)(uint8_t pin_no, uint8_t button_action); |
bogdanm | 0:eff01767de02 | 56 | |
bogdanm | 0:eff01767de02 | 57 | /**@brief Type of function for passing events from the Button Handler module to the scheduler. */ |
bogdanm | 0:eff01767de02 | 58 | typedef uint32_t (*app_button_evt_schedule_func_t) (app_button_handler_t button_handler, |
Rohit Grover |
37:c29c330d942c | 59 | uint8_t pin_no, |
Rohit Grover |
37:c29c330d942c | 60 | uint8_t button_action); |
bogdanm | 0:eff01767de02 | 61 | |
bogdanm | 0:eff01767de02 | 62 | /**@brief Button configuration structure. */ |
bogdanm | 0:eff01767de02 | 63 | typedef struct |
bogdanm | 0:eff01767de02 | 64 | { |
Rohit Grover |
37:c29c330d942c | 65 | uint8_t pin_no; /**< Pin to be used as a button. */ |
Rohit Grover |
37:c29c330d942c | 66 | uint8_t active_state; /**< APP_BUTTON_ACTIVE_HIGH or APP_BUTTON_ACTIVE_LOW. */ |
Rohit Grover |
37:c29c330d942c | 67 | nrf_gpio_pin_pull_t pull_cfg; /**< Pull-up or -down configuration. */ |
Rohit Grover |
37:c29c330d942c | 68 | app_button_handler_t button_handler; /**< Handler to be called when button is pushed. */ |
bogdanm | 0:eff01767de02 | 69 | } app_button_cfg_t; |
bogdanm | 0:eff01767de02 | 70 | |
Rohit Grover |
37:c29c330d942c | 71 | /**@brief Pin transition direction struct. */ |
Rohit Grover |
37:c29c330d942c | 72 | typedef struct |
Rohit Grover |
37:c29c330d942c | 73 | { |
Rohit Grover |
37:c29c330d942c | 74 | uint32_t high_to_low; /**Pin went from high to low */ |
Rohit Grover |
37:c29c330d942c | 75 | uint32_t low_to_high; /**Pin went from low to high */ |
Rohit Grover |
37:c29c330d942c | 76 | } pin_transition_t; |
Rohit Grover |
37:c29c330d942c | 77 | |
bogdanm | 0:eff01767de02 | 78 | /**@brief Macro for initializing the Button Handler module. |
bogdanm | 0:eff01767de02 | 79 | * |
bogdanm | 0:eff01767de02 | 80 | * @details It will initialize the specified pins as buttons, and configure the Button Handler |
bogdanm | 0:eff01767de02 | 81 | * module as a GPIOTE user (but it will not enable button detection). It will also connect |
bogdanm | 0:eff01767de02 | 82 | * the Button Handler module to the scheduler (if specified). |
bogdanm | 0:eff01767de02 | 83 | * |
bogdanm | 0:eff01767de02 | 84 | * @param[in] BUTTONS Array of buttons to be used (type app_button_cfg_t, must be |
bogdanm | 0:eff01767de02 | 85 | * static!). |
bogdanm | 0:eff01767de02 | 86 | * @param[in] BUTTON_COUNT Number of buttons. |
bogdanm | 0:eff01767de02 | 87 | * @param[in] DETECTION_DELAY Delay from a GPIOTE event until a button is reported as pushed. |
bogdanm | 0:eff01767de02 | 88 | * @param[in] USE_SCHEDULER TRUE if the application is using the event scheduler, |
bogdanm | 0:eff01767de02 | 89 | * FALSE otherwise. |
bogdanm | 0:eff01767de02 | 90 | */ |
bogdanm | 0:eff01767de02 | 91 | /*lint -emacro(506, APP_BUTTON_INIT) */ /* Suppress "Constant value Boolean */ |
bogdanm | 0:eff01767de02 | 92 | #define APP_BUTTON_INIT(BUTTONS, BUTTON_COUNT, DETECTION_DELAY, USE_SCHEDULER) \ |
bogdanm | 0:eff01767de02 | 93 | do \ |
bogdanm | 0:eff01767de02 | 94 | { \ |
bogdanm | 0:eff01767de02 | 95 | uint32_t ERR_CODE = app_button_init((BUTTONS), \ |
bogdanm | 0:eff01767de02 | 96 | (BUTTON_COUNT), \ |
bogdanm | 0:eff01767de02 | 97 | (DETECTION_DELAY), \ |
bogdanm | 0:eff01767de02 | 98 | (USE_SCHEDULER) ? app_button_evt_schedule : NULL); \ |
bogdanm | 0:eff01767de02 | 99 | APP_ERROR_CHECK(ERR_CODE); \ |
bogdanm | 0:eff01767de02 | 100 | } while (0) |
Rohit Grover |
65:98215c4f3a25 | 101 | |
bogdanm | 0:eff01767de02 | 102 | /**@brief Function for initializing the Buttons. |
bogdanm | 0:eff01767de02 | 103 | * |
bogdanm | 0:eff01767de02 | 104 | * @details This function will initialize the specified pins as buttons, and configure the Button |
bogdanm | 0:eff01767de02 | 105 | * Handler module as a GPIOTE user (but it will not enable button detection). |
bogdanm | 0:eff01767de02 | 106 | * |
bogdanm | 0:eff01767de02 | 107 | * @note Normally initialization should be done using the APP_BUTTON_INIT() macro, as that will take |
bogdanm | 0:eff01767de02 | 108 | * care of connecting the Buttons module to the scheduler (if specified). |
bogdanm | 0:eff01767de02 | 109 | * |
Rohit Grover |
65:98215c4f3a25 | 110 | * @note app_button_enable() function must be called in order to enable the button detection. |
bogdanm | 0:eff01767de02 | 111 | * |
bogdanm | 0:eff01767de02 | 112 | * @param[in] p_buttons Array of buttons to be used (NOTE: Must be static!). |
bogdanm | 0:eff01767de02 | 113 | * @param[in] button_count Number of buttons. |
bogdanm | 0:eff01767de02 | 114 | * @param[in] detection_delay Delay from a GPIOTE event until a button is reported as pushed. |
bogdanm | 0:eff01767de02 | 115 | * @param[in] evt_schedule_func Function for passing button events to the scheduler. Point to |
bogdanm | 0:eff01767de02 | 116 | * app_button_evt_schedule() to connect to the scheduler. Set to |
bogdanm | 0:eff01767de02 | 117 | * NULL to make the Buttons module call the event handler directly |
bogdanm | 0:eff01767de02 | 118 | * from the delayed button push detection timeout handler. |
bogdanm | 0:eff01767de02 | 119 | * |
bogdanm | 0:eff01767de02 | 120 | * @return NRF_SUCCESS on success, otherwise an error code. |
bogdanm | 0:eff01767de02 | 121 | */ |
bogdanm | 0:eff01767de02 | 122 | uint32_t app_button_init(app_button_cfg_t * p_buttons, |
bogdanm | 0:eff01767de02 | 123 | uint8_t button_count, |
bogdanm | 0:eff01767de02 | 124 | uint32_t detection_delay, |
bogdanm | 0:eff01767de02 | 125 | app_button_evt_schedule_func_t evt_schedule_func); |
bogdanm | 0:eff01767de02 | 126 | |
bogdanm | 0:eff01767de02 | 127 | /**@brief Function for enabling button detection. |
bogdanm | 0:eff01767de02 | 128 | * |
bogdanm | 0:eff01767de02 | 129 | * @retval NRF_ERROR_INVALID_PARAM GPIOTE has to many users. |
bogdanm | 0:eff01767de02 | 130 | * @retval NRF_ERROR_INVALID_STATE Button or GPIOTE not initialized. |
bogdanm | 0:eff01767de02 | 131 | * @retval NRF_SUCCESS Button detection successfully enabled. |
bogdanm | 0:eff01767de02 | 132 | */ |
bogdanm | 0:eff01767de02 | 133 | uint32_t app_button_enable(void); |
bogdanm | 0:eff01767de02 | 134 | |
bogdanm | 0:eff01767de02 | 135 | /**@brief Function for disabling button detection. |
bogdanm | 0:eff01767de02 | 136 | * |
bogdanm | 0:eff01767de02 | 137 | * @retval NRF_ERROR_INVALID_PARAM GPIOTE has to many users. |
bogdanm | 0:eff01767de02 | 138 | * @retval NRF_ERROR_INVALID_STATE Button or GPIOTE not initialized. |
bogdanm | 0:eff01767de02 | 139 | * @retval NRF_SUCCESS Button detection successfully enabled. |
bogdanm | 0:eff01767de02 | 140 | */ |
bogdanm | 0:eff01767de02 | 141 | uint32_t app_button_disable(void); |
bogdanm | 0:eff01767de02 | 142 | |
bogdanm | 0:eff01767de02 | 143 | /**@brief Function for checking if a button is currently being pushed. |
bogdanm | 0:eff01767de02 | 144 | * |
Rohit Grover |
65:98215c4f3a25 | 145 | * @param[in] button_id Button index (in the app_button_cfg_t array given to app_button_init) to be checked. |
bogdanm | 0:eff01767de02 | 146 | * @param[out] p_is_pushed Button state. |
bogdanm | 0:eff01767de02 | 147 | * |
bogdanm | 0:eff01767de02 | 148 | * @retval NRF_SUCCESS State successfully read. |
Rohit Grover |
65:98215c4f3a25 | 149 | * @retval NRF_ERROR_INVALID_PARAM Invalid button index. |
bogdanm | 0:eff01767de02 | 150 | */ |
Rohit Grover |
65:98215c4f3a25 | 151 | uint32_t app_button_is_pushed(uint8_t button_id, bool * p_is_pushed); |
bogdanm | 0:eff01767de02 | 152 | |
bogdanm | 0:eff01767de02 | 153 | |
bogdanm | 0:eff01767de02 | 154 | // Type and functions for connecting the Buttons module to the scheduler: |
bogdanm | 0:eff01767de02 | 155 | |
bogdanm | 0:eff01767de02 | 156 | /**@cond NO_DOXYGEN */ |
bogdanm | 0:eff01767de02 | 157 | typedef struct |
bogdanm | 0:eff01767de02 | 158 | { |
bogdanm | 0:eff01767de02 | 159 | app_button_handler_t button_handler; |
bogdanm | 0:eff01767de02 | 160 | uint8_t pin_no; |
Rohit Grover |
37:c29c330d942c | 161 | uint8_t button_action; |
bogdanm | 0:eff01767de02 | 162 | } app_button_event_t; |
bogdanm | 0:eff01767de02 | 163 | |
bogdanm | 0:eff01767de02 | 164 | static __INLINE void app_button_evt_get(void * p_event_data, uint16_t event_size) |
bogdanm | 0:eff01767de02 | 165 | { |
bogdanm | 0:eff01767de02 | 166 | app_button_event_t * p_buttons_event = (app_button_event_t *)p_event_data; |
Rohit Grover |
65:98215c4f3a25 | 167 | |
bogdanm | 0:eff01767de02 | 168 | APP_ERROR_CHECK_BOOL(event_size == sizeof(app_button_event_t)); |
Rohit Grover |
37:c29c330d942c | 169 | p_buttons_event->button_handler(p_buttons_event->pin_no, p_buttons_event->button_action); |
bogdanm | 0:eff01767de02 | 170 | } |
bogdanm | 0:eff01767de02 | 171 | |
bogdanm | 0:eff01767de02 | 172 | static __INLINE uint32_t app_button_evt_schedule(app_button_handler_t button_handler, |
Rohit Grover |
37:c29c330d942c | 173 | uint8_t pin_no, |
Rohit Grover |
37:c29c330d942c | 174 | uint8_t button_action) |
bogdanm | 0:eff01767de02 | 175 | { |
bogdanm | 0:eff01767de02 | 176 | app_button_event_t buttons_event; |
Rohit Grover |
65:98215c4f3a25 | 177 | |
bogdanm | 0:eff01767de02 | 178 | buttons_event.button_handler = button_handler; |
bogdanm | 0:eff01767de02 | 179 | buttons_event.pin_no = pin_no; |
Rohit Grover |
37:c29c330d942c | 180 | buttons_event.button_action = button_action; |
Rohit Grover |
65:98215c4f3a25 | 181 | |
bogdanm | 0:eff01767de02 | 182 | return app_sched_event_put(&buttons_event, sizeof(buttons_event), app_button_evt_get); |
bogdanm | 0:eff01767de02 | 183 | } |
bogdanm | 0:eff01767de02 | 184 | /**@endcond */ |
bogdanm | 0:eff01767de02 | 185 | |
bogdanm | 0:eff01767de02 | 186 | #endif // APP_BUTTON_H__ |
bogdanm | 0:eff01767de02 | 187 | |
bogdanm | 0:eff01767de02 | 188 | /** @} */ |