touch_wakeup.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "esp_log.h"
  9. #include "esp_sleep.h"
  10. #include "touch_element/touch_button.h"
  11. static const char *TAG = "touch_wakeup";
  12. #define TOUCH_BUTTON_NUM 5
  13. /* Touch buttons handle */
  14. static touch_button_handle_t button_handle[TOUCH_BUTTON_NUM];
  15. /* Touch buttons channel array */
  16. static const touch_pad_t channel_array[TOUCH_BUTTON_NUM] = {
  17. TOUCH_PAD_NUM1,
  18. TOUCH_PAD_NUM2,
  19. TOUCH_PAD_NUM3,
  20. TOUCH_PAD_NUM4,
  21. TOUCH_PAD_NUM5,
  22. };
  23. /* Touch buttons channel sensitivity array */
  24. static const float channel_sens_array[TOUCH_BUTTON_NUM] = {
  25. 0.03F,
  26. 0.03F,
  27. 0.03F,
  28. 0.03F,
  29. 0.03F,
  30. };
  31. /* Button event handler task */
  32. static void button_handler_task(void *arg)
  33. {
  34. (void) arg; //Unused
  35. touch_elem_message_t element_message;
  36. while (1) {
  37. /* Waiting for touch element messages */
  38. touch_element_message_receive(&element_message, portMAX_DELAY);
  39. if (element_message.element_type != TOUCH_ELEM_TYPE_BUTTON) {
  40. continue;
  41. }
  42. /* Decode message */
  43. const touch_button_message_t *button_message = touch_button_get_message(&element_message);
  44. if (button_message->event == TOUCH_BUTTON_EVT_ON_PRESS) {
  45. ESP_LOGI(TAG, "Button[%"PRIu32"] Press", (uint32_t)element_message.arg);
  46. } else if (button_message->event == TOUCH_BUTTON_EVT_ON_RELEASE) {
  47. ESP_LOGI(TAG, "Button[%"PRIu32"] Release", (uint32_t)element_message.arg);
  48. } else if (button_message->event == TOUCH_BUTTON_EVT_ON_LONGPRESS) {
  49. ESP_LOGI(TAG, "Button[%"PRIu32"] LongPress", (uint32_t)element_message.arg);
  50. }
  51. }
  52. vTaskDelete(NULL);
  53. }
  54. esp_err_t example_register_touch_wakeup(void)
  55. {
  56. /* Initialize Touch Element library */
  57. touch_elem_global_config_t global_config = TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG();
  58. ESP_ERROR_CHECK(touch_element_install(&global_config));
  59. ESP_LOGI(TAG, "Touch element library installed");
  60. touch_button_global_config_t button_global_config = TOUCH_BUTTON_GLOBAL_DEFAULT_CONFIG();
  61. ESP_ERROR_CHECK(touch_button_install(&button_global_config));
  62. ESP_LOGI(TAG, "Touch button installed");
  63. for (int i = 0; i < TOUCH_BUTTON_NUM; i++) {
  64. touch_button_config_t button_config = {
  65. .channel_num = channel_array[i],
  66. .channel_sens = channel_sens_array[i]
  67. };
  68. /* Create Touch buttons */
  69. ESP_ERROR_CHECK(touch_button_create(&button_config, &button_handle[i]));
  70. /* Set EVENT as the dispatch method */
  71. ESP_ERROR_CHECK(touch_button_set_dispatch_method(button_handle[i], TOUCH_ELEM_DISP_EVENT));
  72. /* Subscribe touch button events (On Press, On Release, On LongPress) */
  73. ESP_ERROR_CHECK(touch_button_subscribe_event(button_handle[i],
  74. TOUCH_ELEM_EVENT_ON_PRESS |
  75. TOUCH_ELEM_EVENT_ON_RELEASE |
  76. TOUCH_ELEM_EVENT_ON_LONGPRESS,
  77. (void *)channel_array[i]));
  78. }
  79. ESP_LOGI(TAG, "Touch buttons created");
  80. touch_elem_sleep_config_t sleep_config = {
  81. .sample_count = global_config.hardware.sample_count,
  82. .sleep_cycle = global_config.hardware.sleep_cycle,
  83. };
  84. /* Enable one of registered touch button as light/deep sleep wake-up source */
  85. ESP_ERROR_CHECK(touch_element_enable_light_sleep(&sleep_config));
  86. touch_element_start();
  87. xTaskCreate(&button_handler_task, "button_handler_task", 4 * 1024, NULL, 6, NULL);
  88. ESP_LOGI(TAG, "touch wakeup source is ready");
  89. return ESP_OK;
  90. }