1#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
11static const char *
const TAG =
"esp32_touch";
25 ESP_LOGV(TAG,
"Touch Pad '%s' state: ON (value: %" PRIu32
" > threshold: %" PRIu32
")", child->
get_name().
c_str(),
28 ESP_LOGV(TAG,
"Touch Pad '%s' state: OFF", child->
get_name().
c_str());
40 "Checking touch state for '%s' (T%d): value = %" PRIu32
", threshold = %" PRIu32
", benchmark = %" PRIu32,
55 esp_err_t init_err = touch_pad_init();
56 if (init_err != ESP_OK) {
57 ESP_LOGE(TAG,
"Failed to initialize touch pad: %s", esp_err_to_name(init_err));
64 esp_err_t config_err = touch_pad_config(child->touch_pad_);
65 if (config_err != ESP_OK) {
66 ESP_LOGE(TAG,
"Failed to configure touch pad %d: %s", child->touch_pad_, esp_err_to_name(config_err));
72 touch_filter_config_t filter_info = {
79 touch_pad_filter_set_config(&filter_info);
80 touch_pad_filter_enable();
84 touch_pad_denoise_t denoise = {
88 touch_pad_denoise_set_config(&denoise);
89 touch_pad_denoise_enable();
93 touch_pad_waterproof_t waterproof = {
97 touch_pad_waterproof_set_config(&waterproof);
98 touch_pad_waterproof_enable();
103 touch_pad_set_charge_discharge_times(this->
meas_cycle_);
107 touch_pad_timeout_set(
true, TOUCH_PAD_THRESHOLD_MAX);
111 touch_pad_isr_register(
touch_isr_handler,
this,
static_cast<touch_pad_intr_mask_t
>(TOUCH_PAD_INTR_MASK_ALL));
113 ESP_LOGE(TAG,
"Failed to register touch ISR: %s", esp_err_to_name(err));
120 for (
auto *child : this->children_) {
121 if (child->threshold_ != 0) {
122 touch_pad_set_thresh(child->touch_pad_, child->threshold_);
130 touch_pad_intr_enable(
static_cast<touch_pad_intr_mask_t
>(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_TIMEOUT));
133 touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
136 touch_pad_fsm_start();
146 const char *filter_mode_s;
148 case TOUCH_PAD_FILTER_IIR_4:
149 filter_mode_s =
"IIR_4";
151 case TOUCH_PAD_FILTER_IIR_8:
152 filter_mode_s =
"IIR_8";
154 case TOUCH_PAD_FILTER_IIR_16:
155 filter_mode_s =
"IIR_16";
157 case TOUCH_PAD_FILTER_IIR_32:
158 filter_mode_s =
"IIR_32";
160 case TOUCH_PAD_FILTER_IIR_64:
161 filter_mode_s =
"IIR_64";
163 case TOUCH_PAD_FILTER_IIR_128:
164 filter_mode_s =
"IIR_128";
166 case TOUCH_PAD_FILTER_IIR_256:
167 filter_mode_s =
"IIR_256";
169 case TOUCH_PAD_FILTER_JITTER:
170 filter_mode_s =
"JITTER";
173 filter_mode_s =
"UNKNOWN";
178 " Debounce count: %" PRIu32
"\n"
179 " Noise threshold coefficient: %" PRIu32
"\n"
180 " Jitter filter step size: %" PRIu32,
182 const char *smooth_level_s;
184 case TOUCH_PAD_SMOOTH_OFF:
185 smooth_level_s =
"OFF";
187 case TOUCH_PAD_SMOOTH_IIR_2:
188 smooth_level_s =
"IIR_2";
190 case TOUCH_PAD_SMOOTH_IIR_4:
191 smooth_level_s =
"IIR_4";
193 case TOUCH_PAD_SMOOTH_IIR_8:
194 smooth_level_s =
"IIR_8";
197 smooth_level_s =
"UNKNOWN";
200 ESP_LOGCONFIG(TAG,
" Smooth level: %s", smooth_level_s);
206 case TOUCH_PAD_DENOISE_BIT12:
209 case TOUCH_PAD_DENOISE_BIT10:
212 case TOUCH_PAD_DENOISE_BIT8:
215 case TOUCH_PAD_DENOISE_BIT4:
222 ESP_LOGCONFIG(TAG,
" Denoise grade: %s", grade_s);
224 const char *cap_level_s;
226 case TOUCH_PAD_DENOISE_CAP_L0:
229 case TOUCH_PAD_DENOISE_CAP_L1:
232 case TOUCH_PAD_DENOISE_CAP_L2:
235 case TOUCH_PAD_DENOISE_CAP_L3:
238 case TOUCH_PAD_DENOISE_CAP_L4:
241 case TOUCH_PAD_DENOISE_CAP_L5:
244 case TOUCH_PAD_DENOISE_CAP_L6:
247 case TOUCH_PAD_DENOISE_CAP_L7:
251 cap_level_s =
"UNKNOWN";
254 ESP_LOGCONFIG(TAG,
" Denoise capacitance level: %s", cap_level_s);
258 ESP_LOGCONFIG(TAG,
" Setup Mode ENABLED");
278 TouchPadEventV2 event;
279 while (xQueueReceive(this->
touch_queue_, &event, 0) == pdTRUE) {
280 ESP_LOGD(TAG,
"Event received, mask = 0x%" PRIx32
", pad = %d", event.intr_mask, event.pad);
282 if (event.intr_mask & TOUCH_PAD_INTR_MASK_TIMEOUT) {
284 touch_pad_timeout_resume();
286 }
else if (!(event.intr_mask & TOUCH_PAD_INTR_MASK_ACTIVE)) {
292 for (
auto *child : this->children_) {
293 if (child->touch_pad_ == event.pad) {
294 if (event.intr_mask & TOUCH_PAD_INTR_MASK_TIMEOUT) {
297 }
else if (event.intr_mask & TOUCH_PAD_INTR_MASK_ACTIVE) {
312 for (
auto *child : this->children_) {
313 if (child->benchmark_ == 0)
314 touch_pad_read_benchmark(child->touch_pad_, &child->benchmark_);
318 if (child->last_state_) {
321 uint32_t time_diff = now - child->last_touch_time_;
333 ESP_LOGVV(TAG,
"Touch Pad '%s' still touched after %" PRIu32
"ms timeout, resetting timer",
334 child->get_name().c_str(), this->release_timeout_ms_);
353 touch_pad_intr_disable(
static_cast<touch_pad_intr_mask_t
>(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_TIMEOUT));
362 ESP32TouchComponent *component =
static_cast<ESP32TouchComponent *
>(arg);
363 BaseType_t x_higher_priority_task_woken = pdFALSE;
366 TouchPadEventV2 event;
367 event.intr_mask = touch_pad_read_intr_status_mask();
368 event.pad = touch_pad_get_current_meas_channel();
371 xQueueSendFromISR(component->touch_queue_, &event, &x_higher_priority_task_woken);
372 component->enable_loop_soon_any_context();
374 if (x_higher_priority_task_woken) {
375 portYIELD_FROM_ISR();
386 touch_pad_filter_read_smooth(
pad, &value);
389 touch_pad_read_raw_data(
pad, &value);
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.
const StringRef & get_name() const
constexpr const char * c_str() const
void publish_state(bool new_state)
Publish a new state to the front-end.
Simple helper class to expose a touch pad value as a binary sensor.
uint32_t last_touch_time_
touch_volt_atten_t voltage_attenuation_
touch_filter_mode_t filter_mode_
bool create_touch_queue_()
bool filter_configured_() const
static void touch_isr_handler(void *arg)
touch_pad_t waterproof_guard_ring_pad_
void update_touch_state_(ESP32TouchBinarySensor *child, bool is_touched)
void check_and_disable_loop_if_all_released_(size_t pads_off)
bool check_and_update_touch_state_(ESP32TouchBinarySensor *child)
void publish_initial_state_if_needed_(ESP32TouchBinarySensor *child, uint32_t now)
QueueHandle_t touch_queue_
bool waterproof_configured_() const
touch_pad_denoise_grade_t grade_
void calculate_release_timeout_()
bool denoise_configured_() const
void on_shutdown() override
touch_high_volt_t high_voltage_reference_
touch_pad_shield_driver_t waterproof_shield_driver_
uint32_t noise_threshold_
touch_smooth_mode_t smooth_level_
void process_setup_mode_logging_(uint32_t now)
uint32_t release_timeout_ms_
void dump_config_sensors_()
std::vector< ESP32TouchBinarySensor * > children_
uint32_t read_touch_value(touch_pad_t pad) const
touch_pad_denoise_cap_t cap_level_
touch_low_volt_t low_voltage_reference_
void cleanup_touch_queue_()
void configure_wakeup_pads_()
bool should_check_for_releases_(uint32_t now)
void dump_config() override
Providing packet encoding functions for exchanging data with a remote host.
Application App
Global storage of Application pointer - only one Application can exist.