app_button.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /** @file
  13. *
  14. * @defgroup app_button Button Handler
  15. * @{
  16. * @ingroup app_common
  17. *
  18. * @brief Buttons handling module.
  19. *
  20. * @details The button handler uses the @ref app_gpiote to detect that a button has been
  21. * pushed. To handle debouncing, it will start a timer in the GPIOTE event handler.
  22. * The button will only be reported as pushed if the corresponding pin is still active when
  23. * the timer expires. If there is a new GPIOTE event while the timer is running, the timer
  24. * is restarted.
  25. *
  26. * @note The app_button module uses the app_timer module. The user must ensure that the queue in
  27. * app_timer is large enough to hold the app_timer_stop() / app_timer_start() operations
  28. * which will be executed on each event from GPIOTE module (2 operations), as well as other
  29. * app_timer operations queued simultaneously in the application.
  30. *
  31. * @note Even if the scheduler is not used, app_button.h will include app_scheduler.h, so when
  32. * compiling, app_scheduler.h must be available in one of the compiler include paths.
  33. */
  34. #ifndef APP_BUTTON_H__
  35. #define APP_BUTTON_H__
  36. #include <stdint.h>
  37. #include <stdbool.h>
  38. #include "nrf.h"
  39. #include "app_error.h"
  40. #include "nrf_gpio.h"
  41. #define APP_BUTTON_PUSH 1 /**< Indicates that a button is pushed. */
  42. #define APP_BUTTON_RELEASE 0 /**< Indicates that a button is released. */
  43. #define APP_BUTTON_ACTIVE_HIGH 1 /**< Indicates that a button is active high. */
  44. #define APP_BUTTON_ACTIVE_LOW 0 /**< Indicates that a button is active low. */
  45. /**@brief Button event handler type. */
  46. typedef void (*app_button_handler_t)(uint8_t pin_no, uint8_t button_action);
  47. /**@brief Button configuration structure. */
  48. typedef struct
  49. {
  50. uint8_t pin_no; /**< Pin to be used as a button. */
  51. uint8_t active_state; /**< APP_BUTTON_ACTIVE_HIGH or APP_BUTTON_ACTIVE_LOW. */
  52. nrf_gpio_pin_pull_t pull_cfg; /**< Pull-up or -down configuration. */
  53. app_button_handler_t button_handler; /**< Handler to be called when button is pushed. */
  54. } app_button_cfg_t;
  55. /**@brief Pin transition direction struct. */
  56. typedef struct
  57. {
  58. uint32_t high_to_low; /**Pin went from high to low */
  59. uint32_t low_to_high; /**Pin went from low to high */
  60. } pin_transition_t;
  61. /**@brief Function for initializing the Buttons.
  62. *
  63. * @details This function will initialize the specified pins as buttons, and configure the Button
  64. * Handler module as a GPIOTE user (but it will not enable button detection).
  65. *
  66. * @note Normally initialization should be done using the APP_BUTTON_INIT() macro
  67. *
  68. * @note app_button_enable() function must be called in order to enable the button detection.
  69. *
  70. * @param[in] p_buttons Array of buttons to be used (NOTE: Must be static!).
  71. * @param[in] button_count Number of buttons.
  72. * @param[in] detection_delay Delay from a GPIOTE event until a button is reported as pushed.
  73. *
  74. * @return NRF_SUCCESS on success, otherwise an error code.
  75. */
  76. uint32_t app_button_init(app_button_cfg_t * p_buttons,
  77. uint8_t button_count,
  78. uint32_t detection_delay);
  79. /**@brief Function for enabling button detection.
  80. *
  81. * @retval NRF_SUCCESS Module successfully enabled.
  82. */
  83. uint32_t app_button_enable(void);
  84. /**@brief Function for disabling button detection.
  85. *
  86. * @retval NRF_SUCCESS Button detection successfully disabled. Error code otherwise.
  87. */
  88. uint32_t app_button_disable(void);
  89. /**@brief Function for checking if a button is currently being pushed.
  90. *
  91. * @param[in] button_id Button index (in the app_button_cfg_t array given to app_button_init) to be checked.
  92. * @param[out] p_is_pushed Button state.
  93. *
  94. * @retval NRF_SUCCESS State successfully read.
  95. * @retval NRF_ERROR_INVALID_PARAM Invalid button index.
  96. */
  97. uint32_t app_button_is_pushed(uint8_t button_id, bool * p_is_pushed);
  98. #endif // APP_BUTTON_H__
  99. /** @} */