ESPHome 2025.7.3
Loading...
Searching...
No Matches
esp32_touch_v1.cpp
Go to the documentation of this file.
1#ifdef USE_ESP32_VARIANT_ESP32
2
3#include "esp32_touch.h"
5#include "esphome/core/log.h"
6#include "esphome/core/hal.h"
7
8#include <algorithm>
9#include <cinttypes>
10
11// Include HAL for ISR-safe touch reading
12#include "hal/touch_sensor_ll.h"
13
14namespace esphome {
15namespace esp32_touch {
16
17static const char *const TAG = "esp32_touch";
18
19static const uint32_t SETUP_MODE_THRESHOLD = 0xFFFF;
20
22 // Create queue for touch events
23 // Queue size calculation: children * 4 allows for burst scenarios where ISR
24 // fires multiple times before main loop processes. This is important because
25 // ESP32 v1 scans all pads on each interrupt, potentially sending multiple events.
26 if (!this->create_touch_queue_()) {
27 return;
28 }
29
30 touch_pad_init();
31 touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
32
33 // Set up IIR filter if enabled
34 if (this->iir_filter_enabled_()) {
35 touch_pad_filter_start(this->iir_filter_);
36 }
37
38 // Configure measurement parameters
39#if ESP_IDF_VERSION_MAJOR >= 5
40 touch_pad_set_measurement_clock_cycles(this->meas_cycle_);
41 touch_pad_set_measurement_interval(this->sleep_cycle_);
42#else
43 touch_pad_set_meas_time(this->sleep_cycle_, this->meas_cycle_);
44#endif
45 touch_pad_set_voltage(this->high_voltage_reference_, this->low_voltage_reference_, this->voltage_attenuation_);
46
47 // Configure each touch pad
48 for (auto *child : this->children_) {
49 if (this->setup_mode_) {
50 touch_pad_config(child->get_touch_pad(), SETUP_MODE_THRESHOLD);
51 } else {
52 touch_pad_config(child->get_touch_pad(), child->get_threshold());
53 }
54 }
55
56 // Register ISR handler
57 esp_err_t err = touch_pad_isr_register(touch_isr_handler, this);
58 if (err != ESP_OK) {
59 ESP_LOGE(TAG, "Failed to register touch ISR: %s", esp_err_to_name(err));
61 this->mark_failed();
62 return;
63 }
64
65 // Calculate release timeout based on sleep cycle
67
68 // Enable touch pad interrupt
69 touch_pad_intr_enable();
70}
71
73 this->dump_config_base_();
74
75 if (this->iir_filter_enabled_()) {
76 ESP_LOGCONFIG(TAG, " IIR Filter: %" PRIu32 "ms", this->iir_filter_);
77 } else {
78 ESP_LOGCONFIG(TAG, " IIR Filter DISABLED");
79 }
80
81 if (this->setup_mode_) {
82 ESP_LOGCONFIG(TAG, " Setup Mode ENABLED");
83 }
84
86}
87
89 const uint32_t now = App.get_loop_component_start_time();
90
91 // Print debug info for all pads in setup mode
93
94 // Process any queued touch events from interrupts
95 // Note: Events are only sent by ISR for pads that were measured in that cycle (value != 0)
96 // This is more efficient than sending all pad states every interrupt
97 TouchPadEventV1 event;
98 while (xQueueReceive(this->touch_queue_, &event, 0) == pdTRUE) {
99 // Find the corresponding sensor - O(n) search is acceptable since events are infrequent
100 for (auto *child : this->children_) {
101 if (child->get_touch_pad() != event.pad) {
102 continue;
103 }
104
105 // Found matching pad - process it
106 child->value_ = event.value;
107
108 // The interrupt gives us the touch state directly
109 bool new_state = event.is_touched;
110
111 // Track when we last saw this pad as touched
112 if (new_state) {
113 child->last_touch_time_ = now;
114 }
115
116 // Only publish if state changed - this filters out repeated events
117 if (new_state != child->last_state_) {
118 child->initial_state_published_ = true;
119 child->last_state_ = new_state;
120 child->publish_state(new_state);
121 // Original ESP32: ISR only fires when touched, release is detected by timeout
122 // Note: ESP32 v1 uses inverted logic - touched when value < threshold
123 ESP_LOGV(TAG, "Touch Pad '%s' state: %s (value: %" PRIu32 " < threshold: %" PRIu32 ")",
124 child->get_name().c_str(), ONOFF(new_state), event.value, child->get_threshold());
125 }
126 break; // Exit inner loop after processing matching pad
127 }
128 }
129
130 // Check for released pads periodically
131 if (!this->should_check_for_releases_(now)) {
132 return;
133 }
134
135 size_t pads_off = 0;
136 for (auto *child : this->children_) {
137 // Handle initial state publication after startup
138 this->publish_initial_state_if_needed_(child, now);
139
140 if (child->last_state_) {
141 // Pad is currently in touched state - check for release timeout
142 // Using subtraction handles 32-bit rollover correctly
143 uint32_t time_diff = now - child->last_touch_time_;
144
145 // Check if we haven't seen this pad recently
146 if (time_diff > this->release_timeout_ms_) {
147 // Haven't seen this pad recently, assume it's released
148 child->last_state_ = false;
149 child->publish_state(false);
150 ESP_LOGV(TAG, "Touch Pad '%s' state: OFF (timeout)", child->get_name().c_str());
151 pads_off++;
152 }
153 } else {
154 // Pad is already off
155 pads_off++;
156 }
157 }
158
159 // Disable the loop to save CPU cycles when all pads are off and not in setup mode.
160 // The loop will be re-enabled by the ISR when any touch pad is touched.
161 // v1 hardware limitations require us to check all pads are off because:
162 // - v1 only generates interrupts on touch events (not releases)
163 // - We must poll for release timeouts in the main loop
164 // - We can only safely disable when no pads need timeout monitoring
166}
167
169 touch_pad_intr_disable();
170 touch_pad_isr_deregister(touch_isr_handler, this);
171 this->cleanup_touch_queue_();
172
173 if (this->iir_filter_enabled_()) {
174 touch_pad_filter_stop();
175 touch_pad_filter_delete();
176 }
177
178 // Configure wakeup pads if any are set
180}
181
182void IRAM_ATTR ESP32TouchComponent::touch_isr_handler(void *arg) {
183 ESP32TouchComponent *component = static_cast<ESP32TouchComponent *>(arg);
184
185 uint32_t mask = 0;
186 touch_ll_read_trigger_status_mask(&mask);
187 touch_ll_clear_trigger_status_mask();
188 touch_pad_clear_status();
189
190 // INTERRUPT BEHAVIOR: On ESP32 v1 hardware, the interrupt fires when ANY configured
191 // touch pad detects a touch (value goes below threshold). The hardware does NOT
192 // generate interrupts on release - only on touch events.
193 // The interrupt will continue to fire periodically (based on sleep_cycle) as long
194 // as any pad remains touched. This allows us to detect both new touches and
195 // continued touches, but releases must be detected by timeout in the main loop.
196
197 // Process all configured pads to check their current state
198 // Note: ESP32 v1 doesn't tell us which specific pad triggered the interrupt,
199 // so we must scan all configured pads to find which ones were touched
200 for (auto *child : component->children_) {
201 touch_pad_t pad = child->get_touch_pad();
202
203 // Read current value using ISR-safe API
204 uint32_t value;
205 if (component->iir_filter_enabled_()) {
206 uint16_t temp_value = 0;
207 touch_pad_read_filtered(pad, &temp_value);
208 value = temp_value;
209 } else {
210 // Use low-level HAL function when filter is not enabled
211 value = touch_ll_read_raw_data(pad);
212 }
213
214 // Skip pads that aren’t in the trigger mask
215 if (((mask >> pad) & 1) == 0) {
216 continue;
217 }
218
219 // IMPORTANT: ESP32 v1 touch detection logic - INVERTED compared to v2!
220 // ESP32 v1: Touch is detected when capacitance INCREASES, causing the measured value to DECREASE
221 // Therefore: touched = (value < threshold)
222 // This is opposite to ESP32-S2/S3 v2 where touched = (value > threshold)
223 bool is_touched = value < child->get_threshold();
224
225 // Always send the current state - the main loop will filter for changes
226 // We send both touched and untouched states because the ISR doesn't
227 // track previous state (to keep ISR fast and simple)
228 TouchPadEventV1 event;
229 event.pad = pad;
230 event.value = value;
231 event.is_touched = is_touched;
232
233 // Send to queue from ISR - non-blocking, drops if queue full
234 BaseType_t x_higher_priority_task_woken = pdFALSE;
235 xQueueSendFromISR(component->touch_queue_, &event, &x_higher_priority_task_woken);
236 component->enable_loop_soon_any_context();
237 if (x_higher_priority_task_woken) {
238 portYIELD_FROM_ISR();
239 }
240 }
241}
242
243} // namespace esp32_touch
244} // namespace esphome
245
246#endif // USE_ESP32_VARIANT_ESP32
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
virtual void mark_failed()
Mark this component as failed.
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
void check_and_disable_loop_if_all_released_(size_t pads_off)
void publish_initial_state_if_needed_(ESP32TouchBinarySensor *child, uint32_t now)
std::vector< ESP32TouchBinarySensor * > children_
Definition esp32_touch.h:90
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Application App
Global storage of Application pointer - only one Application can exist.
uint8_t pad