テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

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