nrf_csense.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /**
  2. * Copyright (c) 2016 - 2019, 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. #ifndef NRF_CSENSE_H__
  41. #define NRF_CSENSE_H__
  42. #include <stdint.h>
  43. #include "nrf.h"
  44. #include "sdk_errors.h"
  45. #include "app_timer.h"
  46. #include "nrf_drv_csense.h"
  47. #include "nrf_csense_macros.h"
  48. #include "app_util.h"
  49. /** @file
  50. *
  51. * @defgroup nrf_csense Capacitive sensor library
  52. * @{
  53. * @ingroup app_common
  54. *
  55. * @brief Module for using the capacitive sensor library with support for many instances of sliders, wheels, and buttons.
  56. */
  57. /**
  58. * @brief Macro for returning the address of a variable.
  59. */
  60. #define NRF_CSENSE_GET_INSTANCE_ID(instance) (&instance)
  61. /**
  62. * @brief Statically allocate memory for the instance of a capacitive sensor.
  63. *
  64. * @param[in,out] name Name of the capacitive sensor instance that will be created.
  65. * @param[in] p1 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  66. */
  67. #define NRF_CSENSE_BUTTON_DEF(name, p1) NRF_CSENSE_INTERNAL_BUTTON_DEF(name, p1)
  68. /**
  69. * @brief Macro for creating a 2-pad slider instance.
  70. *
  71. * @param[in,out] name Name of the capacitive sensor instance that will be created.
  72. * @param[in] steps_no Number of relative pads. It means that the slider in its handler will give values (1, steps_no).
  73. * @param[in] p1 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  74. * @param[in] p2 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  75. */
  76. #define NRF_CSENSE_SLIDER_2_DEF(name, steps_no, p1, p2) NRF_CSENSE_INTERNAL_SLIDER_2_DEF(name, steps_no, p1, p2)
  77. /**
  78. * @brief Macro for creating a 3-pad slider instance.
  79. *
  80. * @param[in,out] name Name of the capacitive sensor instance that will be created.
  81. * @param[in] steps_no Number of relative pads. It means that the slider in its handler will give values (1, steps_no).
  82. * @param[in] p1 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  83. * @param[in] p2 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  84. * @param[in] p3 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  85. */
  86. #define NRF_CSENSE_SLIDER_3_DEF(name, steps_no, p1, p2, p3) NRF_CSENSE_INTERNAL_SLIDER_3_DEF(name, steps_no, p1, p2, p3)
  87. /**
  88. * @brief Macro for creating a 4-pad slider instance.
  89. *
  90. * @param[in,out] name Name of the capacitive sensor instance that will be created.
  91. * @param[in] steps_no Number of relative pads. It means that the slider in its handler will give values (1, steps_no).
  92. * @param[in] p1 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  93. * @param[in] p2 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  94. * @param[in] p3 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  95. * @param[in] p4 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  96. */
  97. #define NRF_CSENSE_SLIDER_4_DEF(name, steps_no, p1, p2, p3, p4) NRF_CSENSE_INTERNAL_SLIDER_4_DEF(name, steps_no, p1, p2, p3, p4)
  98. /**
  99. * @brief Macro for creating a 5-pad slider instance.
  100. *
  101. * @param[in,out] name Name of the capacitive sensor instance that will be created.
  102. * @param[in] steps_no Number of relative pads. It means that the slider in its handler will give values (1, steps_no).
  103. * @param[in] p1 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  104. * @param[in] p2 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  105. * @param[in] p3 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  106. * @param[in] p4 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  107. * @param[in] p5 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  108. */
  109. #define NRF_CSENSE_SLIDER_5_DEF(name, steps_no, p1, p2, p3, p4, p5) NRF_CSENSE_INTERNAL_SLIDER_5_DEF(name, steps_no, p1, p2, p3, p4, p5)
  110. /**
  111. * @brief Macro for creating a 3-pad wheel instance.
  112. *
  113. * @param[in,out] name Name of the capacitive sensor instance that will be created.
  114. * @param[in] steps_no Number of relative pads. It means that the slider in its handler will give values (1, steps_no).
  115. * @param[in] p1 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  116. * @param[in] p2 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  117. * @param[in] p3 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  118. */
  119. #define NRF_CSENSE_WHEEL_3_DEF(name, steps_no, p1, p2, p3) NRF_CSENSE_INTERNAL_WHEEL_3_DEF(name, steps_no, p1, p2, p3)
  120. /**
  121. * @brief Macro for creating a 4-pad wheel instance.
  122. *
  123. * @param[in,out] name Name of the capacitive sensor instance that will be created.
  124. * @param[in] steps_no Number of relative pads. It means that the slider in its handler will give values (1, steps_no).
  125. * @param[in] p1 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  126. * @param[in] p2 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  127. * @param[in] p3 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  128. * @param[in] p4 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  129. */
  130. #define NRF_CSENSE_WHEEL_4_DEF(name, steps_no, p1, p2, p3, p4) NRF_CSENSE_INTERNAL_WHEEL_4_DEF(name, steps_no, p1, p2, p3, p4)
  131. /**
  132. * @brief Macro for creating a 5-pad wheel instance.
  133. *
  134. * @param[in,out] name Name of the capacitive sensor instance that will be created.
  135. * @param[in] steps_no Number of relative pads. It means that the slider in its handler will give values (1, steps_no).
  136. * @param[in] p1 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  137. * @param[in] p2 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  138. * @param[in] p3 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  139. * @param[in] p4 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  140. * @param[in] p5 Pair of two arguments: threshold and analog_input_number. Must be passed as (analog_input_number, threshold).
  141. */
  142. #define NRF_CSENSE_WHEEL_5_DEF(name, steps_no, p1, p2, p3, p4, p5) NRF_CSENSE_INTERNAL_WHEEL_5_DEF(name, steps_no, p1, p2, p3, p4, p5)
  143. /**
  144. * @cond (NODOX)
  145. * @defgroup nrf_csense_internal Auxiliary internal types declarations
  146. * @brief Module for internal usage inside the library only.
  147. * @details These definitions are available to the user, but they should not
  148. * be accessed directly. Use the API to access them.
  149. * @{
  150. *
  151. */
  152. /*
  153. * @brief Forward declaration of capacitive sensor instance.
  154. */
  155. typedef struct nrf_csense_instance_s nrf_csense_instance_t;
  156. /*
  157. * @brief Forward declaration of a capacitive sensor pad.
  158. */
  159. typedef struct nrf_csense_pad_s nrf_csense_pad_t;
  160. /**
  161. * @brief Structure with pointer to min and max values measured on pads.
  162. */
  163. typedef struct
  164. {
  165. uint16_t max_value; //!< Max value measured on pads.
  166. uint16_t min_value; //!< Min value measured on pads.
  167. }nrf_csense_min_max_t;
  168. /**
  169. * @brief Structure with single instance parameters. This can be either a slider or a button.
  170. */
  171. struct nrf_csense_instance_s
  172. {
  173. nrf_csense_instance_t * p_next_instance; //!< Pointer to the next instance.
  174. nrf_csense_pad_t * p_nrf_csense_pad; //!< Pointer to the first pad of the module.
  175. nrf_csense_min_max_t * min_max; //!< Structure with pointers to min and max values measured on pads.
  176. uint16_t steps; //!< Number of relative pads. It means that the slider in its handler will give values (1, steps_no).
  177. uint8_t number_of_pads; //!< Number of pads that the instance is using.
  178. bool is_active; //!< Flag to indicate if the instance is active.
  179. bool is_touched; //!< Flag to indicate if the instance is touched.
  180. void * p_context; //!< General purpose pointer.
  181. };
  182. /* Structure with single pad parameters used for initialization. */
  183. struct nrf_csense_pad_s
  184. {
  185. nrf_csense_pad_t * p_next_pad; //!< Pointer to the next pad.
  186. uint16_t threshold; //!< Threshold voltage on pad/time of charging to decide if the pad was touched.
  187. uint8_t pad_index; //!< Index of the pad.
  188. uint8_t analog_input_number; //!< Analog input connected to the pad.
  189. };
  190. /** @}
  191. * @endcond
  192. */
  193. /**
  194. * @brief Enum for nrf_csense events.
  195. */
  196. typedef enum
  197. {
  198. NRF_CSENSE_BTN_EVT_PRESSED, //!< Event for pad pressed.
  199. NRF_CSENSE_BTN_EVT_RELEASED, //!< Event for pad released.
  200. NRF_CSENSE_SLIDER_EVT_PRESSED, //!< Event for pad pressed.
  201. NRF_CSENSE_SLIDER_EVT_RELEASED, //!< Event for pad released.
  202. NRF_CSENSE_SLIDER_EVT_DRAGGED, //!< Event for pad dragged.
  203. }nrf_csense_evt_type_t;
  204. /**
  205. * @brief Structure with slider event data including the measured step.
  206. */
  207. typedef struct
  208. {
  209. uint16_t step; //!< Measured step.
  210. } nrf_csense_slider_evt_t;
  211. /**
  212. * @brief Event data union for nrf_csense events.
  213. */
  214. typedef union
  215. {
  216. nrf_csense_slider_evt_t slider; //!< Structure with slider event data including the measured step.
  217. } nrf_csense_evt_param_t;
  218. /**
  219. * @brief Structure with event parameters.
  220. */
  221. typedef struct
  222. {
  223. nrf_csense_evt_type_t nrf_csense_evt_type; //!< Type of event.
  224. nrf_csense_instance_t * p_instance; //!< Pointer to instance.
  225. nrf_csense_evt_param_t params; //!< Event data union for nrf_csense events.
  226. }nrf_csense_evt_t;
  227. /**
  228. * @brief Capacitive sensor handler type.
  229. */
  230. typedef void (* nrf_csense_event_handler_t)(nrf_csense_evt_t * p_evt);
  231. /**
  232. * @brief Function for setting a handler of the instance.
  233. *
  234. * @param [in] p_instance Pointer to the instance whose steps are going to be changed.
  235. * @param [in] p_context General purpose pointer. Will be passed to the callback function.
  236. */
  237. __STATIC_INLINE void nrf_csense_instance_context_set(nrf_csense_instance_t * const p_instance, void * p_context)
  238. {
  239. p_instance->p_context = p_context;
  240. }
  241. /**
  242. * @brief Function for initializing the module. After initialization, no instances are enabled.
  243. *
  244. * @param [in] event_handler Event handler for the Capacitive Sensor module.
  245. * @param [in] ticks Time in app_timer ticks between next conversions.
  246. *
  247. * @retval NRF_ERROR_INVALID_PARAM If invalid parameter was provided.
  248. * @retval NRF_ERROR_INVALID_STATE If one of the used modules is in invalid state.
  249. * @retval NRF_ERROR_INTERNAL If an error occured while initializing one of the modules used by the capacitive sensor library.
  250. * @retval NRF_SUCCESS If the module was initialized successfully.
  251. */
  252. ret_code_t nrf_csense_init(nrf_csense_event_handler_t event_handler, uint32_t ticks);
  253. /**
  254. * @brief Function for uninitializing the module.
  255. *
  256. * @return Values returned by @ref nrf_drv_csense_uninit and @ref app_timer_stop.
  257. */
  258. ret_code_t nrf_csense_uninit(void);
  259. /**
  260. * @brief Function for adding an instance of capacitive sensor to a linked list.
  261. *
  262. * The function calls @ref nrf_csense_enable to enable the instance that was added to the linked list.
  263. *
  264. * @param [in] p_instance Pointer to the capacitive sensor instance. It is saved by the module and is used whenever the instance is referred.
  265. *
  266. * @return Values returned by @ref nrf_csense_enable.
  267. */
  268. ret_code_t nrf_csense_add(nrf_csense_instance_t * const p_instance);
  269. /**
  270. * @brief Function for enabling a single instance.
  271. *
  272. * @param [in,out] p_instance Pointer to the capacitive sensor instance. It is saved by the module and is used whenever the instance is referred.
  273. *
  274. * @return Values returned by @ref app_timer_start.
  275. */
  276. ret_code_t nrf_csense_enable(nrf_csense_instance_t * const p_instance);
  277. /**
  278. * @brief Function for disabling an instance.
  279. *
  280. * @param [in] p_instance Pointer to the instance to be disabled.
  281. *
  282. * @retval NRF_ERROR_INVALID_PARAM If the instance was already disabled.
  283. * @retval NRF_SUCCESS If the instance was disabled successfully.
  284. */
  285. ret_code_t nrf_csense_disable(nrf_csense_instance_t * const p_instance);
  286. /**
  287. * @brief Function for setting ticks between next measurements.
  288. *
  289. * @param [in] ticks New time between conversions in app_timer ticks.
  290. *
  291. * @retval NRF_ERROR_BUSY If the capacitive sensor was busy.
  292. * @retval NRF_ERROR_INVALID_PARAM If an invalid parameter was provided.
  293. * @retval NRF_ERROR_INVALID_STATE If app_timer was in invalid state.
  294. * @retval NRF_SUCCESS If ticks were set successfully.
  295. */
  296. ret_code_t nrf_csense_ticks_set(uint32_t ticks);
  297. /**
  298. * @brief Function for setting steps of an instance.
  299. *
  300. * Note that you have do disable the instance before you can change its number of steps.
  301. *
  302. * @param [in] p_instance Pointer to the instance whose steps are going to be changed.
  303. * @param [in] steps New steps value.
  304. *
  305. * @retval NRF_ERROR_BUSY If the capacitive sensor was busy.
  306. * @retval NRF_SUCCESS If steps were set successfully.
  307. */
  308. ret_code_t nrf_csense_steps_set(nrf_csense_instance_t * const p_instance, uint16_t steps);
  309. /** @} */
  310. #endif //NRF_CSENSE_H__