1#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
11static const char *
const TAG =
"esp32_touch";
27 ESP_LOGV(TAG,
"Touch Pad '%s' state: ON (value: %" PRIu32
" > threshold: %" PRIu32
")", child->
get_name().
c_str(),
30 ESP_LOGV(TAG,
"Touch Pad '%s' state: OFF", child->
get_name().
c_str());
42 "Checking touch state for '%s' (T%d): value = %" PRIu32
", threshold = %" PRIu32
", benchmark = %" PRIu32,
57 esp_err_t init_err = touch_pad_init();
58 if (init_err != ESP_OK) {
59 ESP_LOGE(TAG,
"Failed to initialize touch pad: %s", esp_err_to_name(init_err));
66 esp_err_t config_err = touch_pad_config(child->touch_pad_);
67 if (config_err != ESP_OK) {
68 ESP_LOGE(TAG,
"Failed to configure touch pad %d: %s", child->touch_pad_, esp_err_to_name(config_err));
74 touch_filter_config_t filter_info = {
81 touch_pad_filter_set_config(&filter_info);
82 touch_pad_filter_enable();
86 touch_pad_denoise_t denoise = {
90 touch_pad_denoise_set_config(&denoise);
91 touch_pad_denoise_enable();
95 touch_pad_waterproof_t waterproof = {
99 touch_pad_waterproof_set_config(&waterproof);
100 touch_pad_waterproof_enable();
105 touch_pad_set_charge_discharge_times(this->
meas_cycle_);
111 touch_pad_timeout_set(
false, TOUCH_PAD_THRESHOLD_MAX);
115 touch_pad_isr_register(
touch_isr_handler,
this,
static_cast<touch_pad_intr_mask_t
>(TOUCH_PAD_INTR_MASK_ALL));
117 ESP_LOGE(TAG,
"Failed to register touch ISR: %s", esp_err_to_name(err));
124 for (
auto *child : this->children_) {
125 if (child->threshold_ != 0) {
126 touch_pad_set_thresh(child->touch_pad_, child->threshold_);
134 touch_pad_intr_enable(
static_cast<touch_pad_intr_mask_t
>(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_TIMEOUT));
137 touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
140 touch_pad_fsm_start();
150 const char *filter_mode_s;
152 case TOUCH_PAD_FILTER_IIR_4:
153 filter_mode_s =
"IIR_4";
155 case TOUCH_PAD_FILTER_IIR_8:
156 filter_mode_s =
"IIR_8";
158 case TOUCH_PAD_FILTER_IIR_16:
159 filter_mode_s =
"IIR_16";
161 case TOUCH_PAD_FILTER_IIR_32:
162 filter_mode_s =
"IIR_32";
164 case TOUCH_PAD_FILTER_IIR_64:
165 filter_mode_s =
"IIR_64";
167 case TOUCH_PAD_FILTER_IIR_128:
168 filter_mode_s =
"IIR_128";
170 case TOUCH_PAD_FILTER_IIR_256:
171 filter_mode_s =
"IIR_256";
173 case TOUCH_PAD_FILTER_JITTER:
174 filter_mode_s =
"JITTER";
177 filter_mode_s =
"UNKNOWN";
182 " Debounce count: %" PRIu32
"\n"
183 " Noise threshold coefficient: %" PRIu32
"\n"
184 " Jitter filter step size: %" PRIu32,
186 const char *smooth_level_s;
188 case TOUCH_PAD_SMOOTH_OFF:
189 smooth_level_s =
"OFF";
191 case TOUCH_PAD_SMOOTH_IIR_2:
192 smooth_level_s =
"IIR_2";
194 case TOUCH_PAD_SMOOTH_IIR_4:
195 smooth_level_s =
"IIR_4";
197 case TOUCH_PAD_SMOOTH_IIR_8:
198 smooth_level_s =
"IIR_8";
201 smooth_level_s =
"UNKNOWN";
204 ESP_LOGCONFIG(TAG,
" Smooth level: %s", smooth_level_s);
210 case TOUCH_PAD_DENOISE_BIT12:
213 case TOUCH_PAD_DENOISE_BIT10:
216 case TOUCH_PAD_DENOISE_BIT8:
219 case TOUCH_PAD_DENOISE_BIT4:
226 ESP_LOGCONFIG(TAG,
" Denoise grade: %s", grade_s);
228 const char *cap_level_s;
230 case TOUCH_PAD_DENOISE_CAP_L0:
233 case TOUCH_PAD_DENOISE_CAP_L1:
236 case TOUCH_PAD_DENOISE_CAP_L2:
239 case TOUCH_PAD_DENOISE_CAP_L3:
242 case TOUCH_PAD_DENOISE_CAP_L4:
245 case TOUCH_PAD_DENOISE_CAP_L5:
248 case TOUCH_PAD_DENOISE_CAP_L6:
251 case TOUCH_PAD_DENOISE_CAP_L7:
255 cap_level_s =
"UNKNOWN";
258 ESP_LOGCONFIG(TAG,
" Denoise capacitance level: %s", cap_level_s);
262 ESP_LOGCONFIG(TAG,
" Setup Mode ENABLED");
282 TouchPadEventV2 event;
283 while (xQueueReceive(this->
touch_queue_, &event, 0) == pdTRUE) {
284 ESP_LOGD(TAG,
"Event received, mask = 0x%" PRIx32
", pad = %d", event.intr_mask, event.pad);
286 if (event.intr_mask & TOUCH_PAD_INTR_MASK_TIMEOUT) {
288 touch_pad_timeout_resume();
290 }
else if (!(event.intr_mask & TOUCH_PAD_INTR_MASK_ACTIVE)) {
296 for (
auto *child : this->children_) {
297 if (child->touch_pad_ == event.pad) {
298 if (event.intr_mask & TOUCH_PAD_INTR_MASK_TIMEOUT) {
301 }
else if (event.intr_mask & TOUCH_PAD_INTR_MASK_ACTIVE) {
318 for (
auto *child : this->children_) {
319 child->ensure_benchmark_read();
323 if (child->last_state_) {
326 uint32_t time_diff = now - child->last_touch_time_;
338 ESP_LOGVV(TAG,
"Touch Pad '%s' still touched after %" PRIu32
"ms timeout, resetting timer",
339 child->get_name().c_str(), this->release_timeout_ms_);
358 touch_pad_intr_disable(TOUCH_PAD_INTR_MASK_ACTIVE);
367 ESP32TouchComponent *
component =
static_cast<ESP32TouchComponent *
>(arg);
368 BaseType_t x_higher_priority_task_woken = pdFALSE;
371 TouchPadEventV2 event;
372 event.intr_mask = touch_pad_read_intr_status_mask();
373 event.pad = touch_pad_get_current_meas_channel();
376 xQueueSendFromISR(
component->touch_queue_, &event, &x_higher_priority_task_woken);
377 component->enable_loop_soon_any_context();
379 if (x_higher_priority_task_woken) {
380 portYIELD_FROM_ISR();
391 touch_pad_filter_read_smooth(
pad, &value);
394 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_
uint32_t value_
Stores the last raw touch measurement value.
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 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_()
void update_touch_state_(ESP32TouchBinarySensor *child, bool is_touched, uint32_t value)
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
const Component * component
Providing packet encoding functions for exchanging data with a remote host.
Application App
Global storage of Application pointer - only one Application can exist.