tp_interrupt_main.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* Touch Pad Interrupt Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "esp_log.h"
  11. #include "driver/touch_pad.h"
  12. #include "soc/rtc_cntl_reg.h"
  13. #include "soc/sens_reg.h"
  14. static const char* TAG = "Touch pad";
  15. #define TOUCH_THRESH_NO_USE (0)
  16. #define TOUCH_THRESH_PERCENT (99)
  17. static bool s_pad_activated[TOUCH_PAD_MAX];
  18. static uint32_t s_pad_init_val[TOUCH_PAD_MAX];
  19. /*
  20. Read values sensed at all available touch pads.
  21. Use 2 / 3 of read value as the threshold
  22. to trigger interrupt when the pad is touched.
  23. Note: this routine demonstrates a simple way
  24. to configure activation threshold for the touch pads.
  25. Do not touch any pads when this routine
  26. is running (on application start).
  27. */
  28. static void tp_example_set_thresholds(void)
  29. {
  30. uint16_t touch_value;
  31. //delay some time in order to make the filter work and get a initial value
  32. vTaskDelay(500/portTICK_PERIOD_MS);
  33. for (int i = 0; i<TOUCH_PAD_MAX; i++) {
  34. //read filtered value
  35. touch_pad_read_filtered(i, &touch_value);
  36. s_pad_init_val[i] = touch_value;
  37. ESP_LOGI(TAG, "test init touch val: %d\n", touch_value);
  38. //set interrupt threshold.
  39. ESP_ERROR_CHECK(touch_pad_set_thresh(i, touch_value * 2 / 3));
  40. }
  41. }
  42. /*
  43. Check if any of touch pads has been activated
  44. by reading a table updated by rtc_intr()
  45. If so, then print it out on a serial monitor.
  46. Clear related entry in the table afterwards
  47. In interrupt mode, the table is updated in touch ISR.
  48. In filter mode, we will compare the current filtered value with the initial one.
  49. If the current filtered value is less than 99% of the initial value, we can
  50. regard it as a 'touched' event.
  51. When calling touch_pad_init, a timer will be started to run the filter.
  52. This mode is designed for the situation that the pad is covered
  53. by a 2-or-3-mm-thick medium, usually glass or plastic.
  54. The difference caused by a 'touch' action could be very small, but we can still use
  55. filter mode to detect a 'touch' event.
  56. */
  57. static void tp_example_read_task(void *pvParameter)
  58. {
  59. static int show_message;
  60. int change_mode = 0;
  61. int filter_mode = 0;
  62. while (1) {
  63. if (filter_mode == 0) {
  64. //interrupt mode, enable touch interrupt
  65. touch_pad_intr_enable();
  66. for (int i = 0; i < TOUCH_PAD_MAX; i++) {
  67. if (s_pad_activated[i] == true) {
  68. ESP_LOGI(TAG, "T%d activated!", i);
  69. // Wait a while for the pad being released
  70. vTaskDelay(200 / portTICK_PERIOD_MS);
  71. // Clear information on pad activation
  72. s_pad_activated[i] = false;
  73. // Reset the counter triggering a message
  74. // that application is running
  75. show_message = 1;
  76. }
  77. }
  78. } else {
  79. //filter mode, disable touch interrupt
  80. touch_pad_intr_disable();
  81. for (int i = 0; i < TOUCH_PAD_MAX; i++) {
  82. uint16_t value = 0;
  83. touch_pad_read_filtered(i, &value);
  84. if (value < s_pad_init_val[i] * TOUCH_THRESH_PERCENT / 100) {
  85. ESP_LOGI(TAG, "T%d activated!", i);
  86. ESP_LOGI(TAG, "value: %d; init val: %d\n", value, s_pad_init_val[i]);
  87. vTaskDelay(200 / portTICK_PERIOD_MS);
  88. // Reset the counter to stop changing mode.
  89. change_mode = 1;
  90. show_message = 1;
  91. }
  92. }
  93. }
  94. vTaskDelay(10 / portTICK_PERIOD_MS);
  95. // If no pad is touched, every couple of seconds, show a message
  96. // that application is running
  97. if (show_message++ % 500 == 0) {
  98. ESP_LOGI(TAG, "Waiting for any pad being touched...");
  99. }
  100. // Change mode if no pad is touched for a long time.
  101. // We can compare the two different mode.
  102. if (change_mode++ % 2000 == 0) {
  103. filter_mode = !filter_mode;
  104. ESP_LOGI(TAG, "Change mode...%s\n", filter_mode == 0? "interrupt mode": "filter mode");
  105. }
  106. }
  107. }
  108. /*
  109. Handle an interrupt triggered when a pad is touched.
  110. Recognize what pad has been touched and save it in a table.
  111. */
  112. static void tp_example_rtc_intr(void * arg)
  113. {
  114. uint32_t pad_intr = touch_pad_get_status();
  115. //clear interrupt
  116. touch_pad_clear_status();
  117. for (int i = 0; i < TOUCH_PAD_MAX; i++) {
  118. if ((pad_intr >> i) & 0x01) {
  119. s_pad_activated[i] = true;
  120. }
  121. }
  122. }
  123. /*
  124. * Before reading touch pad, we need to initialize the RTC IO.
  125. */
  126. static void tp_example_touch_pad_init()
  127. {
  128. for (int i = 0;i< TOUCH_PAD_MAX;i++) {
  129. //init RTC IO and mode for touch pad.
  130. touch_pad_config(i, TOUCH_THRESH_NO_USE);
  131. }
  132. }
  133. void app_main()
  134. {
  135. // Initialize touch pad peripheral, it will start a timer to run a filter
  136. ESP_LOGI(TAG, "Initializing touch pad");
  137. touch_pad_init();
  138. // Initialize and start a software filter to detect slight change of capacitance.
  139. touch_pad_filter_start(10);
  140. // Set measuring time and sleep time
  141. // In this case, measurement will sustain 0xffff / 8Mhz = 8.19ms
  142. // Meanwhile, sleep time between two measurement will be 0x1000 / 150Khz = 27.3 ms
  143. touch_pad_set_meas_time(0x1000, 0xffff);
  144. //set reference voltage for charging/discharging
  145. // In this case, the high reference valtage will be 2.4V - 1.5V = 0.9V
  146. // The low reference voltage will be 0.8V, so that the procedure of charging
  147. // and discharging would be very fast.
  148. touch_pad_set_voltage(TOUCH_HVOLT_2V4, TOUCH_LVOLT_0V8, TOUCH_HVOLT_ATTEN_1V5);
  149. // Init touch pad IO
  150. tp_example_touch_pad_init();
  151. // Set thresh hold
  152. tp_example_set_thresholds();
  153. // Register touch interrupt ISR
  154. touch_pad_isr_register(tp_example_rtc_intr, NULL);
  155. // Start a task to show what pads have been touched
  156. xTaskCreate(&tp_example_read_task, "touch_pad_read_task", 2048, NULL, 5, NULL);
  157. }