tp_interrupt_main.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 (80)
  17. #define TOUCHPAD_FILTER_TOUCH_PERIOD (10)
  18. static bool s_pad_activated[TOUCH_PAD_MAX];
  19. static uint32_t s_pad_init_val[TOUCH_PAD_MAX];
  20. /*
  21. Read values sensed at all available touch pads.
  22. Use 2 / 3 of read value as the threshold
  23. to trigger interrupt when the pad is touched.
  24. Note: this routine demonstrates a simple way
  25. to configure activation threshold for the touch pads.
  26. Do not touch any pads when this routine
  27. is running (on application start).
  28. */
  29. static void tp_example_set_thresholds(void)
  30. {
  31. uint16_t touch_value;
  32. for (int i = 0; i<TOUCH_PAD_MAX; i++) {
  33. //read filtered value
  34. touch_pad_read_filtered(i, &touch_value);
  35. s_pad_init_val[i] = touch_value;
  36. ESP_LOGI(TAG, "test init: touch pad [%d] val is %d", i, touch_value);
  37. //set interrupt threshold.
  38. ESP_ERROR_CHECK(touch_pad_set_thresh(i, touch_value * 2 / 3));
  39. }
  40. }
  41. /*
  42. Check if any of touch pads has been activated
  43. by reading a table updated by rtc_intr()
  44. If so, then print it out on a serial monitor.
  45. Clear related entry in the table afterwards
  46. In interrupt mode, the table is updated in touch ISR.
  47. In filter mode, we will compare the current filtered value with the initial one.
  48. If the current filtered value is less than 80% of the initial value, we can
  49. regard it as a 'touched' event.
  50. When calling touch_pad_init, a timer will be started to run the filter.
  51. This mode is designed for the situation that the pad is covered
  52. by a 2-or-3-mm-thick medium, usually glass or plastic.
  53. The difference caused by a 'touch' action could be very small, but we can still use
  54. filter mode to detect a 'touch' event.
  55. */
  56. static void tp_example_read_task(void *pvParameter)
  57. {
  58. static int show_message;
  59. int change_mode = 0;
  60. int filter_mode = 0;
  61. while (1) {
  62. if (filter_mode == 0) {
  63. //interrupt mode, enable touch interrupt
  64. touch_pad_intr_enable();
  65. for (int i = 0; i < TOUCH_PAD_MAX; i++) {
  66. if (s_pad_activated[i] == true) {
  67. ESP_LOGI(TAG, "T%d activated!", i);
  68. // Wait a while for the pad being released
  69. vTaskDelay(200 / portTICK_PERIOD_MS);
  70. // Clear information on pad activation
  71. s_pad_activated[i] = false;
  72. // Reset the counter triggering a message
  73. // that application is running
  74. show_message = 1;
  75. }
  76. }
  77. } else {
  78. //filter mode, disable touch interrupt
  79. touch_pad_intr_disable();
  80. touch_pad_clear_status();
  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", 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_LOGW(TAG, "Change mode...%s", 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. // If use interrupt trigger mode, should set touch sensor FSM mode at 'TOUCH_FSM_MODE_TIMER'.
  139. touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
  140. // Set reference voltage for charging/discharging
  141. // For most usage scenarios, we recommend using the following combination:
  142. // the high reference valtage will be 2.7V - 1V = 1.7V, The low reference voltage will be 0.5V.
  143. touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_1V);
  144. // Init touch pad IO
  145. tp_example_touch_pad_init();
  146. // Initialize and start a software filter to detect slight change of capacitance.
  147. touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD);
  148. // Set thresh hold
  149. tp_example_set_thresholds();
  150. // Register touch interrupt ISR
  151. touch_pad_isr_register(tp_example_rtc_intr, NULL);
  152. // Start a task to show what pads have been touched
  153. xTaskCreate(&tp_example_read_task, "touch_pad_read_task", 2048, NULL, 5, NULL);
  154. }