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