app_button.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * Copyright (c) 2012 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. *
  42. * @defgroup app_button Button Handler
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief Buttons handling module.
  47. *
  48. * @details The button handler uses the @ref app_gpiote to detect that a button has been
  49. * pushed. To handle debouncing, it will start a timer in the GPIOTE event handler.
  50. * The button will only be reported as pushed if the corresponding pin is still active when
  51. * the timer expires. If there is a new GPIOTE event while the timer is running, the timer
  52. * is restarted.
  53. *
  54. * @note The app_button module uses the app_timer module. The user must ensure that the queue in
  55. * app_timer is large enough to hold the app_timer_stop() / app_timer_start() operations
  56. * which will be executed on each event from GPIOTE module (2 operations), as well as other
  57. * app_timer operations queued simultaneously in the application.
  58. *
  59. * @note Even if the scheduler is not used, app_button.h will include app_scheduler.h, so when
  60. * compiling, app_scheduler.h must be available in one of the compiler include paths.
  61. */
  62. #ifndef APP_BUTTON_H__
  63. #define APP_BUTTON_H__
  64. #include <stdint.h>
  65. #include <stdbool.h>
  66. #include "nrf.h"
  67. #include "app_error.h"
  68. #include "nrf_gpio.h"
  69. #ifdef __cplusplus
  70. extern "C" {
  71. #endif
  72. #define APP_BUTTON_PUSH 1 /**< Indicates that a button is pushed. */
  73. #define APP_BUTTON_RELEASE 0 /**< Indicates that a button is released. */
  74. #define APP_BUTTON_ACTIVE_HIGH 1 /**< Indicates that a button is active high. */
  75. #define APP_BUTTON_ACTIVE_LOW 0 /**< Indicates that a button is active low. */
  76. /**@brief Button event handler type. */
  77. typedef void (*app_button_handler_t)(uint8_t pin_no, uint8_t button_action);
  78. /**@brief Button configuration structure. */
  79. typedef struct
  80. {
  81. uint8_t pin_no; /**< Pin to be used as a button. */
  82. uint8_t active_state; /**< APP_BUTTON_ACTIVE_HIGH or APP_BUTTON_ACTIVE_LOW. */
  83. #if defined(BUTTON_HIGH_ACCURACY_ENABLED) && (BUTTON_HIGH_ACCURACY_ENABLED == 1)
  84. bool hi_accuracy; /**< True if GPIOTE high accuracy (IN_EVENT) is used. */
  85. #endif
  86. nrf_gpio_pin_pull_t pull_cfg; /**< Pull-up or -down configuration. */
  87. app_button_handler_t button_handler; /**< Handler to be called when button is pushed. */
  88. } app_button_cfg_t;
  89. /**@brief Function for initializing the Buttons.
  90. *
  91. * @details This function will initialize the specified pins as buttons, and configure the Button
  92. * Handler module as a GPIOTE user (but it will not enable button detection).
  93. *
  94. * @note app_button_enable() function must be called in order to enable the button detection.
  95. *
  96. * @param[in] p_buttons Array of buttons to be used (NOTE: Must be static!).
  97. * @param[in] button_count Number of buttons.
  98. * @param[in] detection_delay Delay from a GPIOTE event until a button is reported as pushed.
  99. *
  100. * @return NRF_SUCCESS on success, otherwise an error code.
  101. */
  102. uint32_t app_button_init(app_button_cfg_t const * p_buttons,
  103. uint8_t button_count,
  104. uint32_t detection_delay);
  105. /**@brief Function for enabling button detection.
  106. *
  107. * @retval NRF_SUCCESS Module successfully enabled.
  108. */
  109. uint32_t app_button_enable(void);
  110. /**@brief Function for disabling button detection.
  111. *
  112. * @retval NRF_SUCCESS Button detection successfully disabled. Error code otherwise.
  113. */
  114. uint32_t app_button_disable(void);
  115. /**@brief Function for checking if a button is currently being pushed.
  116. *
  117. * @param[in] button_id Button index (in the app_button_cfg_t array given to app_button_init) to be checked.
  118. *
  119. * @return Button state.
  120. */
  121. bool app_button_is_pushed(uint8_t button_id);
  122. #ifdef __cplusplus
  123. }
  124. #endif
  125. #endif // APP_BUTTON_H__
  126. /** @} */