ESPHome 2026.2.4
Loading...
Searching...
No Matches
esp32_touch.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
7#include <esp_idf_version.h>
8
9#include <vector>
10
11#include <driver/touch_sensor.h>
12#include <freertos/FreeRTOS.h>
13#include <freertos/queue.h>
14
15namespace esphome {
16namespace esp32_touch {
17
18// IMPORTANT: Touch detection logic differs between ESP32 variants:
19// - ESP32 v1 (original): Touch detected when value < threshold (capacitance increase causes value decrease)
20// - ESP32-S2/S3 v2: Touch detected when value > threshold (capacitance increase causes value increase)
21// This inversion is due to different hardware implementations between chip generations.
22//
23// INTERRUPT BEHAVIOR:
24// - ESP32 v1: Interrupts fire when ANY pad is touched and continue while touched.
25// Releases are detected by timeout since hardware doesn't generate release interrupts.
26// - ESP32-S2/S3 v2: Hardware supports both touch and release interrupts, but release
27// interrupts are unreliable and sometimes don't fire. We now only use touch interrupts
28// and detect releases via timeout, similar to v1.
29
30static const uint32_t SETUP_MODE_LOG_INTERVAL_MS = 250;
31
33
35 public:
36 void register_touch_pad(ESP32TouchBinarySensor *pad) { this->children_.push_back(pad); }
37
38 void set_setup_mode(bool setup_mode) { this->setup_mode_ = setup_mode; }
39 void set_sleep_duration(uint16_t sleep_duration) { this->sleep_cycle_ = sleep_duration; }
40 void set_measurement_duration(uint16_t meas_cycle) { this->meas_cycle_ = meas_cycle; }
41 void set_low_voltage_reference(touch_low_volt_t low_voltage_reference) {
42 this->low_voltage_reference_ = low_voltage_reference;
43 }
44 void set_high_voltage_reference(touch_high_volt_t high_voltage_reference) {
45 this->high_voltage_reference_ = high_voltage_reference;
46 }
47 void set_voltage_attenuation(touch_volt_atten_t voltage_attenuation) {
48 this->voltage_attenuation_ = voltage_attenuation;
49 }
50
51 void setup() override;
52 void dump_config() override;
53 void loop() override;
54
55 void on_shutdown() override;
56
57#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
58 void set_filter_mode(touch_filter_mode_t filter_mode) { this->filter_mode_ = filter_mode; }
59 void set_debounce_count(uint32_t debounce_count) { this->debounce_count_ = debounce_count; }
60 void set_noise_threshold(uint32_t noise_threshold) { this->noise_threshold_ = noise_threshold; }
61 void set_jitter_step(uint32_t jitter_step) { this->jitter_step_ = jitter_step; }
62 void set_smooth_level(touch_smooth_mode_t smooth_level) { this->smooth_level_ = smooth_level; }
63 void set_denoise_grade(touch_pad_denoise_grade_t denoise_grade) { this->grade_ = denoise_grade; }
64 void set_denoise_cap(touch_pad_denoise_cap_t cap_level) { this->cap_level_ = cap_level; }
66 void set_waterproof_shield_driver(touch_pad_shield_driver_t drive_capability) {
67 this->waterproof_shield_driver_ = drive_capability;
68 }
69#else
70 void set_iir_filter(uint32_t iir_filter) { this->iir_filter_ = iir_filter; }
71#endif
72
73 protected:
74 // Common helper methods
75 void dump_config_base_();
80
81 // Helper methods for loop() logic
82 void process_setup_mode_logging_(uint32_t now);
83 bool should_check_for_releases_(uint32_t now);
85 void check_and_disable_loop_if_all_released_(size_t pads_off);
87
88 // Common members
89 std::vector<ESP32TouchBinarySensor *> children_;
90 bool setup_mode_{false};
93 uint32_t release_timeout_ms_{1500};
95
96 // Common configuration parameters
97 uint16_t sleep_cycle_{4095};
98 uint16_t meas_cycle_{65535};
99 touch_low_volt_t low_voltage_reference_{TOUCH_LVOLT_0V5};
100 touch_high_volt_t high_voltage_reference_{TOUCH_HVOLT_2V7};
101 touch_volt_atten_t voltage_attenuation_{TOUCH_HVOLT_ATTEN_0V};
102
103 // Common constants
104 static constexpr uint32_t MINIMUM_RELEASE_TIME_MS = 100;
105
106 // ==================== PLATFORM SPECIFIC ====================
107
108#ifdef USE_ESP32_VARIANT_ESP32
109 // ESP32 v1 specific
110
111 static void touch_isr_handler(void *arg);
112 QueueHandle_t touch_queue_{nullptr};
113
114 private:
115 // Touch event structure for ESP32 v1
116 // Contains touch pad info, value, and touch state for queue communication
117 struct TouchPadEventV1 {
118 touch_pad_t pad;
119 uint32_t value;
120 bool is_touched;
121 };
122
123 protected:
124 uint32_t iir_filter_{0};
125
126 bool iir_filter_enabled_() const { return this->iir_filter_ > 0; }
127
128#elif defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
129 // ESP32-S2/S3 v2 specific
130 static void touch_isr_handler(void *arg);
131 QueueHandle_t touch_queue_{nullptr};
132
133 private:
134 // Touch event structure for ESP32 v2 (S2/S3)
135 // Contains touch pad and interrupt mask for queue communication
136 struct TouchPadEventV2 {
137 touch_pad_t pad;
138 uint32_t intr_mask;
139 };
140
141 protected:
142 // Filter configuration
143 touch_filter_mode_t filter_mode_{TOUCH_PAD_FILTER_MAX};
144 uint32_t debounce_count_{0};
145 uint32_t noise_threshold_{0};
146 uint32_t jitter_step_{0};
147 touch_smooth_mode_t smooth_level_{TOUCH_PAD_SMOOTH_MAX};
148
149 // Denoise configuration
150 touch_pad_denoise_grade_t grade_{TOUCH_PAD_DENOISE_MAX};
151 touch_pad_denoise_cap_t cap_level_{TOUCH_PAD_DENOISE_CAP_MAX};
152
153 // Waterproof configuration
154 touch_pad_t waterproof_guard_ring_pad_{TOUCH_PAD_MAX};
155 touch_pad_shield_driver_t waterproof_shield_driver_{TOUCH_PAD_SHIELD_DRV_MAX};
156
157 bool filter_configured_() const {
158 return (this->filter_mode_ != TOUCH_PAD_FILTER_MAX) && (this->smooth_level_ != TOUCH_PAD_SMOOTH_MAX);
159 }
160 bool denoise_configured_() const {
161 return (this->grade_ != TOUCH_PAD_DENOISE_MAX) && (this->cap_level_ != TOUCH_PAD_DENOISE_CAP_MAX);
162 }
164 return (this->waterproof_guard_ring_pad_ != TOUCH_PAD_MAX) &&
165 (this->waterproof_shield_driver_ != TOUCH_PAD_SHIELD_DRV_MAX);
166 }
167
168 // Helper method to read touch values - non-blocking operation
169 // Returns the current touch pad value using either filtered or raw reading
170 // based on the filter configuration
171 uint32_t read_touch_value(touch_pad_t pad) const;
172
173 // Helper to update touch state with a known state and value
174 void update_touch_state_(ESP32TouchBinarySensor *child, bool is_touched, uint32_t value);
175
176 // Helper to read touch value and update state for a given child
178#endif
179
180 // Helper functions for dump_config - common to both implementations
181 static const char *get_low_voltage_reference_str(touch_low_volt_t ref) {
182 switch (ref) {
183 case TOUCH_LVOLT_0V5:
184 return "0.5V";
185 case TOUCH_LVOLT_0V6:
186 return "0.6V";
187 case TOUCH_LVOLT_0V7:
188 return "0.7V";
189 case TOUCH_LVOLT_0V8:
190 return "0.8V";
191 default:
192 return "UNKNOWN";
193 }
194 }
195
196 static const char *get_high_voltage_reference_str(touch_high_volt_t ref) {
197 switch (ref) {
198 case TOUCH_HVOLT_2V4:
199 return "2.4V";
200 case TOUCH_HVOLT_2V5:
201 return "2.5V";
202 case TOUCH_HVOLT_2V6:
203 return "2.6V";
204 case TOUCH_HVOLT_2V7:
205 return "2.7V";
206 default:
207 return "UNKNOWN";
208 }
209 }
210
211 static const char *get_voltage_attenuation_str(touch_volt_atten_t atten) {
212 switch (atten) {
213 case TOUCH_HVOLT_ATTEN_1V5:
214 return "1.5V";
215 case TOUCH_HVOLT_ATTEN_1V:
216 return "1V";
217 case TOUCH_HVOLT_ATTEN_0V5:
218 return "0.5V";
219 case TOUCH_HVOLT_ATTEN_0V:
220 return "0V";
221 default:
222 return "UNKNOWN";
223 }
224 }
225};
226
229 public:
230 ESP32TouchBinarySensor(touch_pad_t touch_pad, uint32_t threshold, uint32_t wakeup_threshold)
231 : touch_pad_(touch_pad), threshold_(threshold), wakeup_threshold_(wakeup_threshold) {}
232
233 touch_pad_t get_touch_pad() const { return this->touch_pad_; }
234 uint32_t get_threshold() const { return this->threshold_; }
235 void set_threshold(uint32_t threshold) { this->threshold_ = threshold; }
236
241 uint32_t get_value() const { return this->value_; }
242
243 uint32_t get_wakeup_threshold() const { return this->wakeup_threshold_; }
244
245#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
249 if (this->benchmark_ == 0) {
250 touch_pad_read_benchmark(this->touch_pad_, &this->benchmark_);
251 }
252 }
253#endif
254
255 protected:
257
258 touch_pad_t touch_pad_{TOUCH_PAD_MAX};
259 uint32_t threshold_{0};
260 uint32_t benchmark_{};
262 uint32_t value_{0};
263 bool last_state_{false};
264 const uint32_t wakeup_threshold_{0};
265
266 // Track last touch time for timeout-based release detection
267 // Design note: last_touch_time_ does not require synchronization primitives because:
268 // 1. ESP32 guarantees atomic 32-bit aligned reads/writes
269 // 2. ISR only writes timestamps, main loop only reads
270 // 3. Timing tolerance allows for occasional stale reads (50ms check interval)
271 // 4. Queue operations provide implicit memory barriers
272 // Using atomic/critical sections would add overhead without meaningful benefit
275};
276
277} // namespace esp32_touch
278} // namespace esphome
279
280#endif
Base class for all binary_sensor-type classes.
Simple helper class to expose a touch pad value as a binary sensor.
void ensure_benchmark_read()
Ensure benchmark value is read (v2 touch hardware only).
uint32_t get_value() const
Get the raw touch measurement value.
uint32_t value_
Stores the last raw touch measurement value.
ESP32TouchBinarySensor(touch_pad_t touch_pad, uint32_t threshold, uint32_t wakeup_threshold)
void set_jitter_step(uint32_t jitter_step)
Definition esp32_touch.h:61
void set_measurement_duration(uint16_t meas_cycle)
Definition esp32_touch.h:40
void register_touch_pad(ESP32TouchBinarySensor *pad)
Definition esp32_touch.h:36
void check_and_disable_loop_if_all_released_(size_t pads_off)
bool check_and_update_touch_state_(ESP32TouchBinarySensor *child)
static void touch_isr_handler(void *arg)
void set_debounce_count(uint32_t debounce_count)
Definition esp32_touch.h:59
void publish_initial_state_if_needed_(ESP32TouchBinarySensor *child, uint32_t now)
static const char * get_high_voltage_reference_str(touch_high_volt_t ref)
void set_low_voltage_reference(touch_low_volt_t low_voltage_reference)
Definition esp32_touch.h:41
void set_voltage_attenuation(touch_volt_atten_t voltage_attenuation)
Definition esp32_touch.h:47
void update_touch_state_(ESP32TouchBinarySensor *child, bool is_touched, uint32_t value)
static constexpr uint32_t MINIMUM_RELEASE_TIME_MS
static const char * get_low_voltage_reference_str(touch_low_volt_t ref)
void set_high_voltage_reference(touch_high_volt_t high_voltage_reference)
Definition esp32_touch.h:44
touch_pad_shield_driver_t waterproof_shield_driver_
void set_filter_mode(touch_filter_mode_t filter_mode)
Definition esp32_touch.h:58
void set_denoise_grade(touch_pad_denoise_grade_t denoise_grade)
Definition esp32_touch.h:63
void set_sleep_duration(uint16_t sleep_duration)
Definition esp32_touch.h:39
void set_waterproof_shield_driver(touch_pad_shield_driver_t drive_capability)
Definition esp32_touch.h:66
void set_noise_threshold(uint32_t noise_threshold)
Definition esp32_touch.h:60
void set_iir_filter(uint32_t iir_filter)
Definition esp32_touch.h:70
std::vector< ESP32TouchBinarySensor * > children_
Definition esp32_touch.h:89
uint32_t read_touch_value(touch_pad_t pad) const
void set_denoise_cap(touch_pad_denoise_cap_t cap_level)
Definition esp32_touch.h:64
void set_smooth_level(touch_smooth_mode_t smooth_level)
Definition esp32_touch.h:62
static const char * get_voltage_attenuation_str(touch_volt_atten_t atten)
void set_waterproof_guard_ring_pad(touch_pad_t pad)
Definition esp32_touch.h:65
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint8_t pad