Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of nRF51822 by
app_button.h
00001 /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved. 00002 * 00003 * The information contained herein is property of Nordic Semiconductor ASA. 00004 * Terms and conditions of usage are described in detail in NORDIC 00005 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. 00006 * 00007 * Licensees are granted free, non-transferable use of the information. NO 00008 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from 00009 * the file. 00010 * 00011 */ 00012 00013 /** @file 00014 * 00015 * @defgroup app_button Button Handler 00016 * @{ 00017 * @ingroup app_common 00018 * 00019 * @brief Buttons handling module. 00020 * 00021 * @details The button handler uses the @ref app_gpiote to detect that a button has been 00022 * pushed. To handle debouncing, it will start a timer in the GPIOTE event handler. 00023 * The button will only be reported as pushed if the corresponding pin is still active when 00024 * the timer expires. If there is a new GPIOTE event while the timer is running, the timer 00025 * is restarted. 00026 * Use the USE_SCHEDULER parameter of the APP_BUTTON_INIT() macro to select if the 00027 * @ref app_scheduler is to be used or not. 00028 * 00029 * @note The app_button module uses the app_timer module. The user must ensure that the queue in 00030 * app_timer is large enough to hold the app_timer_stop() / app_timer_start() operations 00031 * which will be executed on each event from GPIOTE module (2 operations), as well as other 00032 * app_timer operations queued simultaneously in the application. 00033 * 00034 * @note Even if the scheduler is not used, app_button.h will include app_scheduler.h, so when 00035 * compiling, app_scheduler.h must be available in one of the compiler include paths. 00036 */ 00037 00038 #ifndef APP_BUTTON_H__ 00039 #define APP_BUTTON_H__ 00040 00041 #include <stdint.h> 00042 #include <stdbool.h> 00043 #include "app_error.h " 00044 #include "app_scheduler.h " 00045 #include "nrf_gpio.h" 00046 00047 #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). */ 00048 #define APP_BUTTON_PUSH 1 /**< Indicates that a button is pushed. */ 00049 #define APP_BUTTON_RELEASE 0 /**< Indicates that a button is released. */ 00050 #define APP_BUTTON_ACTIVE_HIGH 1 /**< Indicates that a button is active high. */ 00051 #define APP_BUTTON_ACTIVE_LOW 0 /**< Indicates that a button is active low. */ 00052 00053 /**@brief Button event handler type. */ 00054 typedef void (*app_button_handler_t)(uint8_t pin_no, uint8_t button_action); 00055 00056 /**@brief Type of function for passing events from the Button Handler module to the scheduler. */ 00057 typedef uint32_t (*app_button_evt_schedule_func_t) (app_button_handler_t button_handler, 00058 uint8_t pin_no, 00059 uint8_t button_action); 00060 00061 /**@brief Button configuration structure. */ 00062 typedef struct 00063 { 00064 uint8_t pin_no; /**< Pin to be used as a button. */ 00065 uint8_t active_state; /**< APP_BUTTON_ACTIVE_HIGH or APP_BUTTON_ACTIVE_LOW. */ 00066 nrf_gpio_pin_pull_t pull_cfg; /**< Pull-up or -down configuration. */ 00067 app_button_handler_t button_handler; /**< Handler to be called when button is pushed. */ 00068 } app_button_cfg_t; 00069 00070 /**@brief Pin transition direction struct. */ 00071 typedef struct 00072 { 00073 uint32_t high_to_low; /**Pin went from high to low */ 00074 uint32_t low_to_high; /**Pin went from low to high */ 00075 } pin_transition_t; 00076 00077 /**@brief Macro for initializing the Button Handler module. 00078 * 00079 * @details It will initialize the specified pins as buttons, and configure the Button Handler 00080 * module as a GPIOTE user (but it will not enable button detection). It will also connect 00081 * the Button Handler module to the scheduler (if specified). 00082 * 00083 * @param[in] BUTTONS Array of buttons to be used (type app_button_cfg_t, must be 00084 * static!). 00085 * @param[in] BUTTON_COUNT Number of buttons. 00086 * @param[in] DETECTION_DELAY Delay from a GPIOTE event until a button is reported as pushed. 00087 * @param[in] USE_SCHEDULER TRUE if the application is using the event scheduler, 00088 * FALSE otherwise. 00089 */ 00090 /*lint -emacro(506, APP_BUTTON_INIT) */ /* Suppress "Constant value Boolean */ 00091 #define APP_BUTTON_INIT(BUTTONS, BUTTON_COUNT, DETECTION_DELAY, USE_SCHEDULER) \ 00092 do \ 00093 { \ 00094 uint32_t ERR_CODE = app_button_init((BUTTONS), \ 00095 (BUTTON_COUNT), \ 00096 (DETECTION_DELAY), \ 00097 (USE_SCHEDULER) ? app_button_evt_schedule : NULL); \ 00098 APP_ERROR_CHECK(ERR_CODE); \ 00099 } while (0) 00100 00101 /**@brief Function for initializing the Buttons. 00102 * 00103 * @details This function will initialize the specified pins as buttons, and configure the Button 00104 * Handler module as a GPIOTE user (but it will not enable button detection). 00105 * 00106 * @note Normally initialization should be done using the APP_BUTTON_INIT() macro, as that will take 00107 * care of connecting the Buttons module to the scheduler (if specified). 00108 * 00109 * @note app_button_enable() function must be called in order to enable the button detection. 00110 * 00111 * @param[in] p_buttons Array of buttons to be used (NOTE: Must be static!). 00112 * @param[in] button_count Number of buttons. 00113 * @param[in] detection_delay Delay from a GPIOTE event until a button is reported as pushed. 00114 * @param[in] evt_schedule_func Function for passing button events to the scheduler. Point to 00115 * app_button_evt_schedule() to connect to the scheduler. Set to 00116 * NULL to make the Buttons module call the event handler directly 00117 * from the delayed button push detection timeout handler. 00118 * 00119 * @return NRF_SUCCESS on success, otherwise an error code. 00120 */ 00121 uint32_t app_button_init(app_button_cfg_t * p_buttons, 00122 uint8_t button_count, 00123 uint32_t detection_delay, 00124 app_button_evt_schedule_func_t evt_schedule_func); 00125 00126 /**@brief Function for enabling button detection. 00127 * 00128 * @retval NRF_ERROR_INVALID_PARAM GPIOTE has to many users. 00129 * @retval NRF_ERROR_INVALID_STATE Button or GPIOTE not initialized. 00130 * @retval NRF_SUCCESS Button detection successfully enabled. 00131 */ 00132 uint32_t app_button_enable(void); 00133 00134 /**@brief Function for disabling button detection. 00135 * 00136 * @retval NRF_ERROR_INVALID_PARAM GPIOTE has to many users. 00137 * @retval NRF_ERROR_INVALID_STATE Button or GPIOTE not initialized. 00138 * @retval NRF_SUCCESS Button detection successfully enabled. 00139 */ 00140 uint32_t app_button_disable(void); 00141 00142 /**@brief Function for checking if a button is currently being pushed. 00143 * 00144 * @param[in] pin_no Button pin to be checked. 00145 * @param[out] p_is_pushed Button state. 00146 * 00147 * @retval NRF_SUCCESS State successfully read. 00148 * @retval NRF_ERROR_INVALID_PARAM Invalid pin_no. 00149 */ 00150 uint32_t app_button_is_pushed(uint8_t pin_no, bool * p_is_pushed); 00151 00152 00153 // Type and functions for connecting the Buttons module to the scheduler: 00154 00155 /**@cond NO_DOXYGEN */ 00156 typedef struct 00157 { 00158 app_button_handler_t button_handler; 00159 uint8_t pin_no; 00160 uint8_t button_action; 00161 } app_button_event_t; 00162 00163 static __INLINE void app_button_evt_get(void * p_event_data, uint16_t event_size) 00164 { 00165 app_button_event_t * p_buttons_event = (app_button_event_t *)p_event_data; 00166 00167 APP_ERROR_CHECK_BOOL(event_size == sizeof(app_button_event_t)); 00168 p_buttons_event->button_handler(p_buttons_event->pin_no, p_buttons_event->button_action); 00169 } 00170 00171 static __INLINE uint32_t app_button_evt_schedule(app_button_handler_t button_handler, 00172 uint8_t pin_no, 00173 uint8_t button_action) 00174 { 00175 app_button_event_t buttons_event; 00176 00177 buttons_event.button_handler = button_handler; 00178 buttons_event.pin_no = pin_no; 00179 buttons_event.button_action = button_action; 00180 00181 return app_sched_event_put(&buttons_event, sizeof(buttons_event), app_button_evt_get); 00182 } 00183 /**@endcond */ 00184 00185 #endif // APP_BUTTON_H__ 00186 00187 /** @} */
Generated on Tue Jul 12 2022 14:11:53 by
