ESPHome 2025.9.0-dev
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 float get_setup_priority() const override { return setup_priority::DATA; }
55
56 void on_shutdown() override;
57
58#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
59 void set_filter_mode(touch_filter_mode_t filter_mode) { this->filter_mode_ = filter_mode; }
60 void set_debounce_count(uint32_t debounce_count) { this->debounce_count_ = debounce_count; }
61 void set_noise_threshold(uint32_t noise_threshold) { this->noise_threshold_ = noise_threshold; }
62 void set_jitter_step(uint32_t jitter_step) { this->jitter_step_ = jitter_step; }
63 void set_smooth_level(touch_smooth_mode_t smooth_level) { this->smooth_level_ = smooth_level; }
64 void set_denoise_grade(touch_pad_denoise_grade_t denoise_grade) { this->grade_ = denoise_grade; }
65 void set_denoise_cap(touch_pad_denoise_cap_t cap_level) { this->cap_level_ = cap_level; }
67 void set_waterproof_shield_driver(touch_pad_shield_driver_t drive_capability) {
68 this->waterproof_shield_driver_ = drive_capability;
69 }
70#else
71 void set_iir_filter(uint32_t iir_filter) { this->iir_filter_ = iir_filter; }
72#endif
73
74 protected:
75 // Common helper methods
76 void dump_config_base_();
81
82 // Helper methods for loop() logic
83 void process_setup_mode_logging_(uint32_t now);
84 bool should_check_for_releases_(uint32_t now);
86 void check_and_disable_loop_if_all_released_(size_t pads_off);
88
89 // Common members
90 std::vector<ESP32TouchBinarySensor *> children_;
91 bool setup_mode_{false};
94 uint32_t release_timeout_ms_{1500};
96
97 // Common configuration parameters
98 uint16_t sleep_cycle_{4095};
99 uint16_t meas_cycle_{65535};
100 touch_low_volt_t low_voltage_reference_{TOUCH_LVOLT_0V5};
101 touch_high_volt_t high_voltage_reference_{TOUCH_HVOLT_2V7};
102 touch_volt_atten_t voltage_attenuation_{TOUCH_HVOLT_ATTEN_0V};
103
104 // Common constants
105 static constexpr uint32_t MINIMUM_RELEASE_TIME_MS = 100;
106
107 // ==================== PLATFORM SPECIFIC ====================
108
109#ifdef USE_ESP32_VARIANT_ESP32
110 // ESP32 v1 specific
111
112 static void touch_isr_handler(void *arg);
113 QueueHandle_t touch_queue_{nullptr};
114
115 private:
116 // Touch event structure for ESP32 v1
117 // Contains touch pad info, value, and touch state for queue communication
118 struct TouchPadEventV1 {
119 touch_pad_t pad;
120 uint32_t value;
121 bool is_touched;
122 };
123
124 protected:
125 uint32_t iir_filter_{0};
126
127 bool iir_filter_enabled_() const { return this->iir_filter_ > 0; }
128
129#elif defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
130 // ESP32-S2/S3 v2 specific
131 static void touch_isr_handler(void *arg);
132 QueueHandle_t touch_queue_{nullptr};
133
134 private:
135 // Touch event structure for ESP32 v2 (S2/S3)
136 // Contains touch pad and interrupt mask for queue communication
137 struct TouchPadEventV2 {
138 touch_pad_t pad;
139 uint32_t intr_mask;
140 };
141
142 protected:
143 // Filter configuration
144 touch_filter_mode_t filter_mode_{TOUCH_PAD_FILTER_MAX};
145 uint32_t debounce_count_{0};
146 uint32_t noise_threshold_{0};
147 uint32_t jitter_step_{0};
148 touch_smooth_mode_t smooth_level_{TOUCH_PAD_SMOOTH_MAX};
149
150 // Denoise configuration
151 touch_pad_denoise_grade_t grade_{TOUCH_PAD_DENOISE_MAX};
152 touch_pad_denoise_cap_t cap_level_{TOUCH_PAD_DENOISE_CAP_MAX};
153
154 // Waterproof configuration
155 touch_pad_t waterproof_guard_ring_pad_{TOUCH_PAD_MAX};
156 touch_pad_shield_driver_t waterproof_shield_driver_{TOUCH_PAD_SHIELD_DRV_MAX};
157
158 bool filter_configured_() const {
159 return (this->filter_mode_ != TOUCH_PAD_FILTER_MAX) && (this->smooth_level_ != TOUCH_PAD_SMOOTH_MAX);
160 }
161 bool denoise_configured_() const {
162 return (this->grade_ != TOUCH_PAD_DENOISE_MAX) && (this->cap_level_ != TOUCH_PAD_DENOISE_CAP_MAX);
163 }
165 return (this->waterproof_guard_ring_pad_ != TOUCH_PAD_MAX) &&
166 (this->waterproof_shield_driver_ != TOUCH_PAD_SHIELD_DRV_MAX);
167 }
168
169 // Helper method to read touch values - non-blocking operation
170 // Returns the current touch pad value using either filtered or raw reading
171 // based on the filter configuration
172 uint32_t read_touch_value(touch_pad_t pad) const;
173
174 // Helper to update touch state with a known state and value
175 void update_touch_state_(ESP32TouchBinarySensor *child, bool is_touched, uint32_t value);
176
177 // Helper to read touch value and update state for a given child
179#endif
180
181 // Helper functions for dump_config - common to both implementations
182 static const char *get_low_voltage_reference_str(touch_low_volt_t ref) {
183 switch (ref) {
184 case TOUCH_LVOLT_0V5:
185 return "0.5V";
186 case TOUCH_LVOLT_0V6:
187 return "0.6V";
188 case TOUCH_LVOLT_0V7:
189 return "0.7V";
190 case TOUCH_LVOLT_0V8:
191 return "0.8V";
192 default:
193 return "UNKNOWN";
194 }
195 }
196
197 static const char *get_high_voltage_reference_str(touch_high_volt_t ref) {
198 switch (ref) {
199 case TOUCH_HVOLT_2V4:
200 return "2.4V";
201 case TOUCH_HVOLT_2V5:
202 return "2.5V";
203 case TOUCH_HVOLT_2V6:
204 return "2.6V";
205 case TOUCH_HVOLT_2V7:
206 return "2.7V";
207 default:
208 return "UNKNOWN";
209 }
210 }
211
212 static const char *get_voltage_attenuation_str(touch_volt_atten_t atten) {
213 switch (atten) {
214 case TOUCH_HVOLT_ATTEN_1V5:
215 return "1.5V";
216 case TOUCH_HVOLT_ATTEN_1V:
217 return "1V";
218 case TOUCH_HVOLT_ATTEN_0V5:
219 return "0.5V";
220 case TOUCH_HVOLT_ATTEN_0V:
221 return "0V";
222 default:
223 return "UNKNOWN";
224 }
225 }
226};
227
230 public:
231 ESP32TouchBinarySensor(touch_pad_t touch_pad, uint32_t threshold, uint32_t wakeup_threshold)
232 : touch_pad_(touch_pad), threshold_(threshold), wakeup_threshold_(wakeup_threshold) {}
233
234 touch_pad_t get_touch_pad() const { return this->touch_pad_; }
235 uint32_t get_threshold() const { return this->threshold_; }
236 void set_threshold(uint32_t threshold) { this->threshold_ = threshold; }
237
242 uint32_t get_value() const { return this->value_; }
243
244 uint32_t get_wakeup_threshold() const { return this->wakeup_threshold_; }
245
246 protected:
248
249 touch_pad_t touch_pad_{TOUCH_PAD_MAX};
250 uint32_t threshold_{0};
251 uint32_t benchmark_{};
253 uint32_t value_{0};
254 bool last_state_{false};
255 const uint32_t wakeup_threshold_{0};
256
257 // Track last touch time for timeout-based release detection
258 // Design note: last_touch_time_ does not require synchronization primitives because:
259 // 1. ESP32 guarantees atomic 32-bit aligned reads/writes
260 // 2. ISR only writes timestamps, main loop only reads
261 // 3. Timing tolerance allows for occasional stale reads (50ms check interval)
262 // 4. Queue operations provide implicit memory barriers
263 // Using atomic/critical sections would add overhead without meaningful benefit
266};
267
268} // namespace esp32_touch
269} // namespace esphome
270
271#endif
Base class for all binary_sensor-type classes.
Simple helper class to expose a touch pad value as a binary sensor.
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:62
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:60
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:59
void set_denoise_grade(touch_pad_denoise_grade_t denoise_grade)
Definition esp32_touch.h:64
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:67
void set_noise_threshold(uint32_t noise_threshold)
Definition esp32_touch.h:61
float get_setup_priority() const override
Definition esp32_touch.h:54
void set_iir_filter(uint32_t iir_filter)
Definition esp32_touch.h:71
std::vector< ESP32TouchBinarySensor * > children_
Definition esp32_touch.h:90
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:65
void set_smooth_level(touch_smooth_mode_t smooth_level)
Definition esp32_touch.h:63
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:66
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint8_t pad