ESPHome 2026.3.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
8#include <vector>
9
10#include <driver/touch_sens.h>
11#include <freertos/FreeRTOS.h>
12#include <freertos/queue.h>
13
14namespace esphome::esp32_touch {
15
16// IMPORTANT: Touch detection logic differs between ESP32 variants:
17// - ESP32 v1 (original): Touch detected when value < threshold (absolute threshold, capacitance increase causes
18// value decrease)
19// - ESP32-S2/S3 v2, ESP32-P4 v3: Touch detected when (smooth - benchmark) > threshold (relative threshold)
20//
21// CALLBACK BEHAVIOR:
22// - ESP32 v1: on_active/on_inactive fire from a software filter timer (esp_timer context).
23// The software filter MUST be configured for these callbacks to fire.
24// - ESP32-S2/S3 v2, ESP32-P4 v3: on_active/on_inactive fire from hardware ISR context.
25// Release detection via on_inactive is used, with timeout as safety fallback.
26
27class ESP32TouchBinarySensor;
28
29class ESP32TouchComponent final : public Component {
30 public:
31 void register_touch_pad(ESP32TouchBinarySensor *pad) { this->children_.push_back(pad); }
32
33 void set_setup_mode(bool setup_mode) { this->setup_mode_ = setup_mode; }
34 void set_meas_interval_us(float meas_interval_us) { this->meas_interval_us_ = meas_interval_us; }
35
36#ifdef USE_ESP32_VARIANT_ESP32
37 void set_charge_duration_ms(float charge_duration_ms) { this->charge_duration_ms_ = charge_duration_ms; }
38#else
39 void set_charge_times(uint32_t charge_times) { this->charge_times_ = charge_times; }
40#endif
41
42#if !defined(USE_ESP32_VARIANT_ESP32P4)
43 void set_low_voltage_reference(touch_volt_lim_l_t low_voltage_reference) {
44 this->low_voltage_reference_ = low_voltage_reference;
45 }
46 void set_high_voltage_reference(touch_volt_lim_h_t high_voltage_reference) {
47 this->high_voltage_reference_ = high_voltage_reference;
48 }
49#endif
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) || defined(USE_ESP32_VARIANT_ESP32P4)
58 void set_filter_mode(touch_benchmark_filter_mode_t filter_mode) {
59 this->filter_mode_ = filter_mode;
60 this->filter_configured_ = true;
61 }
62 void set_debounce_count(uint32_t debounce_count) {
63 this->debounce_count_ = debounce_count;
64 this->filter_configured_ = true;
65 }
66 void set_noise_threshold(uint32_t noise_threshold) {
67 this->noise_threshold_ = noise_threshold;
68 this->filter_configured_ = true;
69 }
70 void set_jitter_step(uint32_t jitter_step) {
71 this->jitter_step_ = jitter_step;
72 this->filter_configured_ = true;
73 }
74 void set_smooth_level(touch_smooth_filter_mode_t smooth_level) {
75 this->smooth_level_ = smooth_level;
76 this->filter_configured_ = true;
77 }
78#if SOC_TOUCH_SUPPORT_DENOISE_CHAN
79 void set_denoise_grade(touch_denoise_chan_resolution_t denoise_grade) {
80 this->denoise_grade_ = denoise_grade;
81 this->denoise_configured_ = true;
82 }
83 void set_denoise_cap(touch_denoise_chan_cap_t cap_level) {
84 this->denoise_cap_level_ = cap_level;
85 this->denoise_configured_ = true;
86 }
87#endif
88 void set_waterproof_guard_ring_pad(int channel_id) {
89 this->waterproof_guard_ring_pad_ = channel_id;
90 this->waterproof_configured_ = true;
91 }
92 void set_waterproof_shield_driver(uint32_t drive_capability) {
93 this->waterproof_shield_driver_ = drive_capability;
94 this->waterproof_configured_ = true;
95 }
96#else
97 void set_iir_filter(uint32_t iir_filter) { this->iir_filter_ = iir_filter; }
98#endif
99
100 protected:
101 // Unified touch event for queue communication
102 struct TouchEvent {
105 };
106
107 // Common helper methods
108 bool create_touch_queue_();
111
112 // Helper methods for loop() logic
113 void process_setup_mode_logging_(uint32_t now);
115
116 // Unified callbacks for new API
117 static bool on_active_cb(touch_sensor_handle_t handle, const touch_active_event_data_t *event, void *ctx);
118 static bool on_inactive_cb(touch_sensor_handle_t handle, const touch_inactive_event_data_t *event, void *ctx);
119
120 // Common members
121 std::vector<ESP32TouchBinarySensor *> children_;
122 bool setup_mode_{false};
124
125 // Controller handle (new API)
126 touch_sensor_handle_t sens_handle_{nullptr};
127 QueueHandle_t touch_queue_{nullptr};
128
129 // Common configuration parameters
130 float meas_interval_us_{320.0f};
131
132#ifdef USE_ESP32_VARIANT_ESP32
134#else
135 uint32_t charge_times_{500};
136#endif
137
138#if !defined(USE_ESP32_VARIANT_ESP32P4)
139 touch_volt_lim_l_t low_voltage_reference_{TOUCH_VOLT_LIM_L_0V5};
140 touch_volt_lim_h_t high_voltage_reference_{TOUCH_VOLT_LIM_H_2V7};
141#endif
142
143#ifdef USE_ESP32_VARIANT_ESP32
144 // ESP32 v1 specific
145 uint32_t iir_filter_{0};
146
147 bool iir_filter_enabled_() const { return this->iir_filter_ > 0; }
148
149#elif defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32P4)
150 // ESP32-S2/S3/P4 v2/v3 specific
151
152 // Filter configuration - use sentinel values to detect "not configured"
153 touch_benchmark_filter_mode_t filter_mode_{TOUCH_BM_JITTER_FILTER};
154 uint32_t debounce_count_{0};
155 uint32_t noise_threshold_{0};
156 uint32_t jitter_step_{0};
157 touch_smooth_filter_mode_t smooth_level_{TOUCH_SMOOTH_NO_FILTER};
159
160#if SOC_TOUCH_SUPPORT_DENOISE_CHAN
161 // Denoise configuration
162 touch_denoise_chan_resolution_t denoise_grade_{TOUCH_DENOISE_CHAN_RESOLUTION_BIT12};
163 touch_denoise_chan_cap_t denoise_cap_level_{TOUCH_DENOISE_CHAN_CAP_5PF};
165#endif
166
167 // Waterproof configuration
171#endif
172};
173
176 public:
177 ESP32TouchBinarySensor(int channel_id, uint32_t threshold, uint32_t wakeup_threshold)
178 : channel_id_(channel_id), threshold_(threshold), wakeup_threshold_(wakeup_threshold) {}
179
180 int get_channel_id() const { return this->channel_id_; }
181 uint32_t get_threshold() const { return this->threshold_; }
182 void set_threshold(uint32_t threshold) { this->threshold_ = threshold; }
183
188 uint32_t get_value() const { return this->value_; }
189
190 uint32_t get_wakeup_threshold() const { return this->wakeup_threshold_; }
191
192 protected:
194
196 touch_channel_handle_t chan_handle_{nullptr};
197 uint32_t threshold_{0};
198 uint32_t benchmark_{0};
200 uint32_t value_{0};
201 bool last_state_{false};
202 const uint32_t wakeup_threshold_{0};
203
204 // Track last touch time for timeout-based release detection
206};
207
208} // namespace esphome::esp32_touch
209
210#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.
ESP32TouchBinarySensor(int channel_id, uint32_t threshold, uint32_t wakeup_threshold)
uint32_t value_
Stores the last raw touch measurement value.
void set_jitter_step(uint32_t jitter_step)
Definition esp32_touch.h:70
void set_high_voltage_reference(touch_volt_lim_h_t high_voltage_reference)
Definition esp32_touch.h:46
void set_denoise_grade(touch_denoise_chan_resolution_t denoise_grade)
Definition esp32_touch.h:79
touch_smooth_filter_mode_t smooth_level_
static bool on_inactive_cb(touch_sensor_handle_t handle, const touch_inactive_event_data_t *event, void *ctx)
void set_charge_times(uint32_t charge_times)
Definition esp32_touch.h:39
void register_touch_pad(ESP32TouchBinarySensor *pad)
Definition esp32_touch.h:31
void set_filter_mode(touch_benchmark_filter_mode_t filter_mode)
Definition esp32_touch.h:58
void set_debounce_count(uint32_t debounce_count)
Definition esp32_touch.h:62
touch_benchmark_filter_mode_t filter_mode_
void set_smooth_level(touch_smooth_filter_mode_t smooth_level)
Definition esp32_touch.h:74
void publish_initial_state_if_needed_(ESP32TouchBinarySensor *child, uint32_t now)
touch_denoise_chan_resolution_t denoise_grade_
static bool on_active_cb(touch_sensor_handle_t handle, const touch_active_event_data_t *event, void *ctx)
void set_waterproof_guard_ring_pad(int channel_id)
Definition esp32_touch.h:88
touch_denoise_chan_cap_t denoise_cap_level_
void set_noise_threshold(uint32_t noise_threshold)
Definition esp32_touch.h:66
void set_waterproof_shield_driver(uint32_t drive_capability)
Definition esp32_touch.h:92
void set_iir_filter(uint32_t iir_filter)
Definition esp32_touch.h:97
std::vector< ESP32TouchBinarySensor * > children_
void set_meas_interval_us(float meas_interval_us)
Definition esp32_touch.h:34
void set_low_voltage_reference(touch_volt_lim_l_t low_voltage_reference)
Definition esp32_touch.h:43
void set_charge_duration_ms(float charge_duration_ms)
Definition esp32_touch.h:37
void set_denoise_cap(touch_denoise_chan_cap_t cap_level)
Definition esp32_touch.h:83
uint8_t pad