BLE temperature profile using digital DS1820 or analog LM35 sensors

Dependencies:   DS1820

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers app_button.h Source File

app_button.h

Go to the documentation of this file.
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 "nordic_global.h"
00044 #include "nrf.h"
00045 #include "app_error.h "
00046 #include "app_scheduler.h "
00047 #include "nrf_gpio.h"
00048 
00049 #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). */
00050 
00051 /**@brief Button event handler type. */
00052 typedef void (*app_button_handler_t)(uint8_t pin_no);
00053 
00054 /**@brief Type of function for passing events from the Button Handler module to the scheduler. */
00055 typedef uint32_t (*app_button_evt_schedule_func_t) (app_button_handler_t button_handler,
00056                                                     uint8_t              pin_no);
00057 
00058 /**@brief Button configuration structure. */
00059 typedef struct
00060 {
00061     uint8_t              pin_no;                                /**< Pin to be used as a button. */
00062     bool                 active_high;                           /**< TRUE if pin is active high, FALSE otherwise. */
00063     nrf_gpio_pin_pull_t  pull_cfg;                              /**< Pull-up or -down configuration. */
00064     app_button_handler_t button_handler;                        /**< Handler to be called when button is pushed. */
00065 } app_button_cfg_t;
00066 
00067 /**@brief Macro for initializing the Button Handler module.
00068  *
00069  * @details It will initialize the specified pins as buttons, and configure the Button Handler
00070  *          module as a GPIOTE user (but it will not enable button detection). It will also connect
00071  *          the Button Handler module to the scheduler (if specified).
00072  *
00073  * @param[in]  BUTTONS           Array of buttons to be used (type app_button_cfg_t, must be
00074  *                               static!).
00075  * @param[in]  BUTTON_COUNT      Number of buttons.
00076  * @param[in]  DETECTION_DELAY   Delay from a GPIOTE event until a button is reported as pushed.
00077  * @param[in]  USE_SCHEDULER     TRUE if the application is using the event scheduler,
00078  *                               FALSE otherwise.
00079  */
00080 /*lint -emacro(506, APP_BUTTON_INIT) */ /* Suppress "Constant value Boolean */
00081 #define APP_BUTTON_INIT(BUTTONS, BUTTON_COUNT, DETECTION_DELAY, USE_SCHEDULER)                     \
00082     do                                                                                             \
00083     {                                                                                              \
00084         uint32_t ERR_CODE = app_button_init((BUTTONS),                                             \
00085                                             (BUTTON_COUNT),                                        \
00086                                             (DETECTION_DELAY),                                     \
00087                                             (USE_SCHEDULER) ? app_button_evt_schedule : NULL);     \
00088         APP_ERROR_CHECK(ERR_CODE);                                                                 \
00089     } while (0)
00090     
00091 /**@brief Function for initializing the Buttons.
00092  *
00093  * @details This function will initialize the specified pins as buttons, and configure the Button
00094  *          Handler module as a GPIOTE user (but it will not enable button detection).
00095  *
00096  * @note Normally initialization should be done using the APP_BUTTON_INIT() macro, as that will take
00097  *       care of connecting the Buttons module to the scheduler (if specified).
00098  *
00099  * @note app_button_enable() function must be called in order to enable the button detection.    
00100  *
00101  * @param[in]  p_buttons           Array of buttons to be used (NOTE: Must be static!).
00102  * @param[in]  button_count        Number of buttons.
00103  * @param[in]  detection_delay     Delay from a GPIOTE event until a button is reported as pushed.
00104  * @param[in]  evt_schedule_func   Function for passing button events to the scheduler. Point to
00105  *                                 app_button_evt_schedule() to connect to the scheduler. Set to
00106  *                                 NULL to make the Buttons module call the event handler directly
00107  *                                 from the delayed button push detection timeout handler.
00108  *
00109  * @return   NRF_SUCCESS on success, otherwise an error code.
00110  */
00111 uint32_t app_button_init(app_button_cfg_t *             p_buttons,
00112                          uint8_t                        button_count,
00113                          uint32_t                       detection_delay,
00114                          app_button_evt_schedule_func_t evt_schedule_func);
00115 
00116 /**@brief Function for enabling button detection.
00117  *
00118  * @retval  NRF_ERROR_INVALID_PARAM   GPIOTE has to many users.
00119  * @retval  NRF_ERROR_INVALID_STATE   Button or GPIOTE not initialized.
00120  * @retval  NRF_SUCCESS               Button detection successfully enabled.
00121  */
00122 uint32_t app_button_enable(void);
00123 
00124 /**@brief Function for disabling button detection.
00125  *
00126  * @retval  NRF_ERROR_INVALID_PARAM   GPIOTE has to many users.
00127  * @retval  NRF_ERROR_INVALID_STATE   Button or GPIOTE not initialized.
00128  * @retval  NRF_SUCCESS               Button detection successfully enabled.
00129  */
00130 uint32_t app_button_disable(void);
00131 
00132 /**@brief Function for checking if a button is currently being pushed.
00133  *
00134  * @param[in]  pin_no        Button pin to be checked.
00135  * @param[out] p_is_pushed   Button state.
00136  *
00137  * @retval     NRF_SUCCESS               State successfully read.
00138  * @retval     NRF_ERROR_INVALID_PARAM   Invalid pin_no.
00139  */
00140 uint32_t app_button_is_pushed(uint8_t pin_no, bool * p_is_pushed);
00141 
00142 
00143 // Type and functions for connecting the Buttons module to the scheduler:
00144 
00145 /**@cond NO_DOXYGEN */
00146 typedef struct
00147 {
00148     app_button_handler_t button_handler;
00149     uint8_t              pin_no;
00150 } app_button_event_t;
00151 
00152 static __INLINE void app_button_evt_get(void * p_event_data, uint16_t event_size)
00153 {
00154     app_button_event_t * p_buttons_event = (app_button_event_t *)p_event_data;
00155     
00156     APP_ERROR_CHECK_BOOL(event_size == sizeof(app_button_event_t));
00157     p_buttons_event->button_handler(p_buttons_event->pin_no);
00158 }
00159 
00160 static __INLINE uint32_t app_button_evt_schedule(app_button_handler_t button_handler,
00161                                                  uint8_t              pin_no)
00162 {
00163     app_button_event_t buttons_event;
00164     
00165     buttons_event.button_handler = button_handler;
00166     buttons_event.pin_no         = pin_no;
00167     
00168     return app_sched_event_put(&buttons_event, sizeof(buttons_event), app_button_evt_get);
00169 }
00170 /**@endcond */
00171 
00172 #endif // APP_BUTTON_H__
00173 
00174 /** @} */