tp_read_main.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Touch Pad Read 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 "driver/touch_pad.h"
  11. #include "esp_log.h"
  12. #define TOUCH_PAD_NO_CHANGE (-1)
  13. #define TOUCH_THRESH_NO_USE (0)
  14. #define TOUCH_FILTER_MODE_EN (1)
  15. #define TOUCHPAD_FILTER_TOUCH_PERIOD (10)
  16. /*
  17. Read values sensed at all available touch pads.
  18. Print out values in a loop on a serial monitor.
  19. */
  20. static void tp_example_read_task(void *pvParameter)
  21. {
  22. uint16_t touch_value;
  23. uint16_t touch_filter_value;
  24. #if TOUCH_FILTER_MODE_EN
  25. printf("Touch Sensor filter mode read, the output format is: \nTouchpad num:[raw data, filtered data]\n\n");
  26. #else
  27. printf("Touch Sensor normal mode read, the output format is: \nTouchpad num:[raw data]\n\n");
  28. #endif
  29. while (1) {
  30. for (int i = 0; i < TOUCH_PAD_MAX; i++) {
  31. #if TOUCH_FILTER_MODE_EN
  32. // If open the filter mode, please use this API to get the touch pad count.
  33. touch_pad_read_raw_data(i, &touch_value);
  34. touch_pad_read_filtered(i, &touch_filter_value);
  35. printf("T%d:[%4d,%4d] ", i, touch_value, touch_filter_value);
  36. #else
  37. touch_pad_read(i, &touch_value);
  38. printf("T%d:[%4d] ", i, touch_value);
  39. #endif
  40. }
  41. printf("\n");
  42. vTaskDelay(200 / portTICK_PERIOD_MS);
  43. }
  44. }
  45. static void tp_example_touch_pad_init(void)
  46. {
  47. for (int i = 0;i< TOUCH_PAD_MAX;i++) {
  48. touch_pad_config(i, TOUCH_THRESH_NO_USE);
  49. }
  50. }
  51. void app_main(void)
  52. {
  53. // Initialize touch pad peripheral.
  54. // The default fsm mode is software trigger mode.
  55. touch_pad_init();
  56. // Set reference voltage for charging/discharging
  57. // In this case, the high reference valtage will be 2.7V - 1V = 1.7V
  58. // The low reference voltage will be 0.5
  59. // The larger the range, the larger the pulse count value.
  60. touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_1V);
  61. tp_example_touch_pad_init();
  62. #if TOUCH_FILTER_MODE_EN
  63. touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD);
  64. #endif
  65. // Start task to read values sensed by pads
  66. xTaskCreate(&tp_example_read_task, "touch_pad_read_task", 2048, NULL, 5, NULL);
  67. }