main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * Copyright (c) 2015 - 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. * @defgroup capacitive_sensor_example_main main.c
  42. * @{
  43. * @ingroup capacitive_sensor_example
  44. * @brief Capacitive Sensor Example main file.
  45. *
  46. * This file contains the source code for a sample application that uses a capacitive sensor.
  47. *
  48. */
  49. #include <stdio.h>
  50. #include <string.h>
  51. #include "boards.h"
  52. #include "nrf_csense.h"
  53. #include "app_error.h"
  54. #include "nrf_drv_clock.h"
  55. #include "app_timer.h"
  56. #include "nrf_delay.h"
  57. #include "nrf_log.h"
  58. #include "nrf_log_ctrl.h"
  59. #include "nrf_log_default_backends.h"
  60. /* Time between RTC interrupts. */
  61. #define APP_TIMER_TICKS_TIMEOUT APP_TIMER_TICKS(50)
  62. /* Scale range. */
  63. #define RANGE 50
  64. /* Analog inputs. */
  65. #define AIN1 1
  66. #define AIN2 2
  67. #define AIN3 3
  68. #define AIN4 4
  69. #define AIN7 7
  70. /* Definition which pads use which analog inputs. */
  71. #define BUTTON AIN7
  72. #define PAD1 AIN4
  73. #define PAD4 AIN4
  74. #ifdef NRF51
  75. #define PAD2 AIN2
  76. #define PAD3 AIN3
  77. #else
  78. #define PAD2 AIN1
  79. #define PAD3 AIN2
  80. #endif
  81. /* Threshold values for pads and button. */
  82. #define THRESHOLD_PAD_1 400
  83. #define THRESHOLD_PAD_2 400
  84. #define THRESHOLD_PAD_3 400
  85. #define THRESHOLD_PAD_4 400
  86. #define THRESHOLD_BUTTON 400
  87. /*lint -e19 -save */
  88. NRF_CSENSE_BUTTON_DEF(m_button, (BUTTON, THRESHOLD_BUTTON));
  89. NRF_CSENSE_SLIDER_4_DEF(m_slider,
  90. RANGE,
  91. (PAD1, THRESHOLD_PAD_1),
  92. (PAD2, THRESHOLD_PAD_2),
  93. (PAD3, THRESHOLD_PAD_3),
  94. (PAD4, THRESHOLD_PAD_4));
  95. /*lint -restore*/
  96. /**
  97. * @brief Function for starting the internal LFCLK XTAL oscillator.
  98. *
  99. * Note that when using a SoftDevice, LFCLK is always on.
  100. *
  101. * @return Values returned by @ref nrf_drv_clock_init.
  102. */
  103. static ret_code_t clock_config(void)
  104. {
  105. ret_code_t err_code;
  106. err_code = nrf_drv_clock_init();
  107. if (err_code != NRF_SUCCESS)
  108. {
  109. return err_code;
  110. }
  111. nrf_drv_clock_lfclk_request(NULL);
  112. return NRF_SUCCESS;
  113. }
  114. /**
  115. * @brief Function for handling slider interrupts.
  116. *
  117. * @param[in] step Detected step.
  118. */
  119. static void slider_handler(uint16_t step)
  120. {
  121. static uint16_t slider_val;
  122. if (slider_val != step)
  123. {
  124. NRF_LOG_INFO("Slider value: %03d.", step);
  125. slider_val = step;
  126. }
  127. }
  128. /**
  129. * @brief Event handler for Capacitive Sensor High module.
  130. *
  131. * @param [in] p_evt_type Pointer to event data structure.
  132. */
  133. void nrf_csense_handler(nrf_csense_evt_t * p_evt)
  134. {
  135. switch (p_evt->nrf_csense_evt_type)
  136. {
  137. case NRF_CSENSE_BTN_EVT_PRESSED:
  138. break;
  139. case NRF_CSENSE_BTN_EVT_RELEASED:
  140. if (p_evt->p_instance == (&m_button))
  141. {
  142. uint16_t * btn_cnt = ((uint16_t *)p_evt->p_instance->p_context);
  143. (*btn_cnt)++;
  144. NRF_LOG_INFO("Button touched %03d times.", (*btn_cnt));
  145. }
  146. break;
  147. case NRF_CSENSE_SLIDER_EVT_PRESSED:
  148. case NRF_CSENSE_SLIDER_EVT_RELEASED:
  149. break;
  150. case NRF_CSENSE_SLIDER_EVT_DRAGGED:
  151. if ((p_evt->p_instance == (&m_slider)) && (p_evt->params.slider.step != UINT16_MAX))
  152. {
  153. /*lint -e611 -save */
  154. ((void(*)(uint16_t, uint8_t))p_evt->p_instance->p_context)(p_evt->params.slider.step, 2);
  155. /*lint -restore*/
  156. }
  157. break;
  158. default:
  159. NRF_LOG_WARNING("Unknown event.");
  160. break;
  161. }
  162. }
  163. /**
  164. * @brief Function for starting Capacitive Sensor High module.
  165. *
  166. * Function enables one slider and one button.
  167. */
  168. static void csense_start(void)
  169. {
  170. ret_code_t err_code;
  171. static uint16_t touched_counter = 0;
  172. err_code = nrf_csense_init(nrf_csense_handler, APP_TIMER_TICKS_TIMEOUT);
  173. APP_ERROR_CHECK(err_code);
  174. nrf_csense_instance_context_set(&m_button, (void*)&touched_counter);
  175. nrf_csense_instance_context_set(&m_slider, (void*)slider_handler);
  176. err_code = nrf_csense_add(&m_button);
  177. APP_ERROR_CHECK(err_code);
  178. err_code = nrf_csense_add(&m_slider);
  179. APP_ERROR_CHECK(err_code);
  180. }
  181. /**
  182. * @brief Function for main application entry.
  183. */
  184. int main(void)
  185. {
  186. ret_code_t err_code;
  187. err_code = NRF_LOG_INIT(NULL);
  188. APP_ERROR_CHECK(err_code);
  189. NRF_LOG_DEFAULT_BACKENDS_INIT();
  190. err_code = app_timer_init();
  191. APP_ERROR_CHECK(err_code);
  192. err_code = clock_config();
  193. APP_ERROR_CHECK(err_code);
  194. NRF_LOG_INFO("Capacitive sensing library example started.");
  195. csense_start();
  196. while (1)
  197. {
  198. __WFI();
  199. NRF_LOG_FLUSH();
  200. }
  201. }
  202. /** @} */