diff --git a/esphome/components/esp32_touch/__init__.py b/esphome/components/esp32_touch/__init__.py index d358791413..10ad339b12 100644 --- a/esphome/components/esp32_touch/__init__.py +++ b/esphome/components/esp32_touch/__init__.py @@ -325,8 +325,9 @@ async def to_code(config): # V2/V3: charge_times (approximate conversion from duration) # The old API used clock cycles; the new API uses charge_times count. # Default is 500 for V2/V3. Use measurement_duration as a rough scaling factor. + # 65535 / 8192 ≈ 7.9999 maps the microsecond duration to charge_times. charge_times = int( - round(config[CONF_MEASUREMENT_DURATION].total_microseconds * 7.99987793) + round(config[CONF_MEASUREMENT_DURATION].total_microseconds * (65535 / 8192)) ) charge_times = max(charge_times, 1) cg.add(touch.set_charge_times(charge_times)) diff --git a/esphome/components/esp32_touch/esp32_touch.cpp b/esphome/components/esp32_touch/esp32_touch.cpp index 3bca1cafba..ef9ca1f96c 100644 --- a/esphome/components/esp32_touch/esp32_touch.cpp +++ b/esphome/components/esp32_touch/esp32_touch.cpp @@ -18,6 +18,8 @@ static const char *const TAG = "esp32_touch"; static constexpr uint32_t SETUP_MODE_LOG_INTERVAL_MS = 250; static constexpr uint32_t RELEASE_TIMEOUT_MS = 1500; static constexpr uint32_t RELEASE_CHECK_INTERVAL_MS = RELEASE_TIMEOUT_MS / 4; +static constexpr uint32_t ONESHOT_SCAN_COUNT = 3; +static constexpr uint32_t ONESHOT_SCAN_TIMEOUT_MS = 2000; // V1: called from esp_timer context (software filter) // V2/V3: called from ISR context @@ -53,6 +55,7 @@ void ESP32TouchComponent::setup() { touch_sensor_sample_config_t sample_cfg = TOUCH_SENSOR_V1_DEFAULT_SAMPLE_CONFIG( this->charge_duration_ms_, this->low_voltage_reference_, this->high_voltage_reference_); #elif defined(USE_ESP32_VARIANT_ESP32P4) + // div_num=8 (data scaling divisor), coarse_freq_tune=2, fine_freq_tune=2 touch_sensor_sample_config_t sample_cfg = TOUCH_SENSOR_V3_DEFAULT_SAMPLE_CONFIG(8, 2, 2); sample_cfg.charge_times = this->charge_times_; #else @@ -212,8 +215,11 @@ void ESP32TouchComponent::setup() { } // Do initial oneshot scans to populate baseline values - for (int i = 0; i < 3; i++) { - touch_sensor_trigger_oneshot_scanning(this->sens_handle_, 2000); + for (int32_t i = 0; i < ONESHOT_SCAN_COUNT; i++) { + err = touch_sensor_trigger_oneshot_scanning(this->sens_handle_, ONESHOT_SCAN_TIMEOUT_MS); + if (err != ESP_OK) { + ESP_LOGW(TAG, "Oneshot scan %d failed: %s", i, esp_err_to_name(err)); + } } err = touch_sensor_start_continuous_scanning(this->sens_handle_);