ESPHome  2022.8.0
esp32_touch.cpp
Go to the documentation of this file.
1 #ifdef USE_ESP32
2 
3 #include "esp32_touch.h"
5 #include "esphome/core/log.h"
6 #include "esphome/core/hal.h"
7 
8 namespace esphome {
9 namespace esp32_touch {
10 
11 static const char *const TAG = "esp32_touch";
12 
14  ESP_LOGCONFIG(TAG, "Setting up ESP32 Touch Hub...");
15  touch_pad_init();
16 
17  if (this->iir_filter_enabled_()) {
18  touch_pad_filter_start(this->iir_filter_);
19  }
20 
21  touch_pad_set_meas_time(this->sleep_cycle_, this->meas_cycle_);
22  touch_pad_set_voltage(this->high_voltage_reference_, this->low_voltage_reference_, this->voltage_attenuation_);
23 
24  for (auto *child : this->children_) {
25  // Disable interrupt threshold
26  touch_pad_config(child->get_touch_pad(), 0);
27  }
28 }
29 
31  ESP_LOGCONFIG(TAG, "Config for ESP32 Touch Hub:");
32  ESP_LOGCONFIG(TAG, " Meas cycle: %.2fms", this->meas_cycle_ / (8000000.0f / 1000.0f));
33  ESP_LOGCONFIG(TAG, " Sleep cycle: %.2fms", this->sleep_cycle_ / (150000.0f / 1000.0f));
34 
35  const char *lv_s;
36  switch (this->low_voltage_reference_) {
37  case TOUCH_LVOLT_0V5:
38  lv_s = "0.5V";
39  break;
40  case TOUCH_LVOLT_0V6:
41  lv_s = "0.6V";
42  break;
43  case TOUCH_LVOLT_0V7:
44  lv_s = "0.7V";
45  break;
46  case TOUCH_LVOLT_0V8:
47  lv_s = "0.8V";
48  break;
49  default:
50  lv_s = "UNKNOWN";
51  break;
52  }
53  ESP_LOGCONFIG(TAG, " Low Voltage Reference: %s", lv_s);
54 
55  const char *hv_s;
56  switch (this->high_voltage_reference_) {
57  case TOUCH_HVOLT_2V4:
58  hv_s = "2.4V";
59  break;
60  case TOUCH_HVOLT_2V5:
61  hv_s = "2.5V";
62  break;
63  case TOUCH_HVOLT_2V6:
64  hv_s = "2.6V";
65  break;
66  case TOUCH_HVOLT_2V7:
67  hv_s = "2.7V";
68  break;
69  default:
70  hv_s = "UNKNOWN";
71  break;
72  }
73  ESP_LOGCONFIG(TAG, " High Voltage Reference: %s", hv_s);
74 
75  const char *atten_s;
76  switch (this->voltage_attenuation_) {
77  case TOUCH_HVOLT_ATTEN_1V5:
78  atten_s = "1.5V";
79  break;
80  case TOUCH_HVOLT_ATTEN_1V:
81  atten_s = "1V";
82  break;
83  case TOUCH_HVOLT_ATTEN_0V5:
84  atten_s = "0.5V";
85  break;
86  case TOUCH_HVOLT_ATTEN_0V:
87  atten_s = "0V";
88  break;
89  default:
90  atten_s = "UNKNOWN";
91  break;
92  }
93  ESP_LOGCONFIG(TAG, " Voltage Attenuation: %s", atten_s);
94 
95  if (this->iir_filter_enabled_()) {
96  ESP_LOGCONFIG(TAG, " IIR Filter: %ums", this->iir_filter_);
97  } else {
98  ESP_LOGCONFIG(TAG, " IIR Filter DISABLED");
99  }
100  if (this->setup_mode_) {
101  ESP_LOGCONFIG(TAG, " Setup Mode ENABLED!");
102  }
103 
104  for (auto *child : this->children_) {
105  LOG_BINARY_SENSOR(" ", "Touch Pad", child);
106  ESP_LOGCONFIG(TAG, " Pad: T%d", child->get_touch_pad());
107  ESP_LOGCONFIG(TAG, " Threshold: %u", child->get_threshold());
108  }
109 }
110 
112  const uint32_t now = millis();
113  bool should_print = this->setup_mode_ && now - this->setup_mode_last_log_print_ > 250;
114  for (auto *child : this->children_) {
115  uint16_t value;
116  if (this->iir_filter_enabled_()) {
117  touch_pad_read_filtered(child->get_touch_pad(), &value);
118  } else {
119  touch_pad_read(child->get_touch_pad(), &value);
120  }
121 
122  child->value_ = value;
123  child->publish_state(value < child->get_threshold());
124 
125  if (should_print) {
126  ESP_LOGD(TAG, "Touch Pad '%s' (T%u): %u", child->get_name().c_str(), child->get_touch_pad(), value);
127  }
128 
129  App.feed_wdt();
130  }
131 
132  if (should_print) {
133  // Avoid spamming logs
134  this->setup_mode_last_log_print_ = now;
135  }
136 }
137 
139  bool is_wakeup_source = false;
140 
141  if (this->iir_filter_enabled_()) {
142  touch_pad_filter_stop();
143  touch_pad_filter_delete();
144  }
145 
146  for (auto *child : this->children_) {
147  if (child->get_wakeup_threshold() != 0) {
148  if (!is_wakeup_source) {
149  is_wakeup_source = true;
150  // Touch sensor FSM mode must be 'TOUCH_FSM_MODE_TIMER' to use it to wake-up.
151  touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
152  }
153 
154  // No filter available when using as wake-up source.
155  touch_pad_config(child->get_touch_pad(), child->get_wakeup_threshold());
156  }
157  }
158 
159  if (!is_wakeup_source) {
160  touch_pad_deinit();
161  }
162 }
163 
164 ESP32TouchBinarySensor::ESP32TouchBinarySensor(touch_pad_t touch_pad, uint16_t threshold, uint16_t wakeup_threshold)
165  : touch_pad_(touch_pad), threshold_(threshold), wakeup_threshold_(wakeup_threshold) {}
166 
167 } // namespace esp32_touch
168 } // namespace esphome
169 
170 #endif
bool iir_filter_enabled_() const
Is the IIR filter enabled?
Definition: esp32_touch.h:51
std::vector< ESP32TouchBinarySensor * > children_
Definition: esp32_touch.h:58
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:26
ESP32TouchBinarySensor(touch_pad_t touch_pad, uint16_t threshold, uint16_t wakeup_threshold)
Application App
Global storage of Application pointer - only one Application can exist.
Definition: a4988.cpp:4