[esp32_touch] Restore get_value() for ESP32-S2/S3 variants (#10112)

This commit is contained in:
J. Nick Koston 2025-08-06 16:21:32 -10:00 committed by GitHub
parent 1415e02e40
commit bfb14e1cf9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 42 additions and 12 deletions

View File

@ -171,8 +171,8 @@ class ESP32TouchComponent : public Component {
// based on the filter configuration
uint32_t read_touch_value(touch_pad_t pad) const;
// Helper to update touch state with a known state
void update_touch_state_(ESP32TouchBinarySensor *child, bool is_touched);
// Helper to update touch state with a known state and value
void update_touch_state_(ESP32TouchBinarySensor *child, bool is_touched, uint32_t value);
// Helper to read touch value and update state for a given child
bool check_and_update_touch_state_(ESP32TouchBinarySensor *child);
@ -234,9 +234,13 @@ class ESP32TouchBinarySensor : public binary_sensor::BinarySensor {
touch_pad_t get_touch_pad() const { return this->touch_pad_; }
uint32_t get_threshold() const { return this->threshold_; }
void set_threshold(uint32_t threshold) { this->threshold_ = threshold; }
#ifdef USE_ESP32_VARIANT_ESP32
/// Get the raw touch measurement value.
/// @note Although this method may appear unused within the component, it is a public API
/// used by lambdas in user configurations for custom touch value processing.
/// @return The current raw touch sensor reading
uint32_t get_value() const { return this->value_; }
#endif
uint32_t get_wakeup_threshold() const { return this->wakeup_threshold_; }
protected:
@ -245,9 +249,8 @@ class ESP32TouchBinarySensor : public binary_sensor::BinarySensor {
touch_pad_t touch_pad_{TOUCH_PAD_MAX};
uint32_t threshold_{0};
uint32_t benchmark_{};
#ifdef USE_ESP32_VARIANT_ESP32
/// Stores the last raw touch measurement value.
uint32_t value_{0};
#endif
bool last_state_{false};
const uint32_t wakeup_threshold_{0};

View File

@ -100,6 +100,8 @@ void ESP32TouchComponent::process_setup_mode_logging_(uint32_t now) {
#else
// Read the value being used for touch detection
uint32_t value = this->read_touch_value(child->get_touch_pad());
// Store the value for get_value() access in lambdas
child->value_ = value;
ESP_LOGD(TAG, "Touch Pad '%s' (T%d): %d", child->get_name().c_str(), child->get_touch_pad(), value);
#endif
}

View File

@ -10,8 +10,11 @@ namespace esp32_touch {
static const char *const TAG = "esp32_touch";
// Helper to update touch state with a known state
void ESP32TouchComponent::update_touch_state_(ESP32TouchBinarySensor *child, bool is_touched) {
// Helper to update touch state with a known state and value
void ESP32TouchComponent::update_touch_state_(ESP32TouchBinarySensor *child, bool is_touched, uint32_t value) {
// Store the value for get_value() access in lambdas
child->value_ = value;
// Always update timer when touched
if (is_touched) {
child->last_touch_time_ = App.get_loop_component_start_time();
@ -21,9 +24,8 @@ void ESP32TouchComponent::update_touch_state_(ESP32TouchBinarySensor *child, boo
child->last_state_ = is_touched;
child->publish_state(is_touched);
if (is_touched) {
// ESP32-S2/S3 v2: touched when value > threshold
ESP_LOGV(TAG, "Touch Pad '%s' state: ON (value: %" PRIu32 " > threshold: %" PRIu32 ")", child->get_name().c_str(),
this->read_touch_value(child->touch_pad_), child->threshold_ + child->benchmark_);
value, child->threshold_ + child->benchmark_);
} else {
ESP_LOGV(TAG, "Touch Pad '%s' state: OFF", child->get_name().c_str());
}
@ -41,7 +43,7 @@ bool ESP32TouchComponent::check_and_update_touch_state_(ESP32TouchBinarySensor *
child->get_name().c_str(), child->touch_pad_, value, child->threshold_, child->benchmark_);
bool is_touched = value > child->benchmark_ + child->threshold_;
this->update_touch_state_(child, is_touched);
this->update_touch_state_(child, is_touched, value);
return is_touched;
}
@ -296,7 +298,9 @@ void ESP32TouchComponent::loop() {
this->check_and_update_touch_state_(child);
} else if (event.intr_mask & TOUCH_PAD_INTR_MASK_ACTIVE) {
// We only get ACTIVE interrupts now, releases are detected by timeout
this->update_touch_state_(child, true); // Always touched for ACTIVE interrupts
// Read the current value
uint32_t value = this->read_touch_value(child->touch_pad_);
this->update_touch_state_(child, true, value); // Always touched for ACTIVE interrupts
}
break;
}

View File

@ -0,0 +1,18 @@
binary_sensor:
- platform: esp32_touch
name: ESP32 Touch Pad Get Value
pin: ${pin}
threshold: 1000
id: esp32_touch_pad_get_value
on_press:
then:
- lambda: |-
// Test that get_value() compiles and works
uint32_t value = id(esp32_touch_pad_get_value).get_value();
ESP_LOGD("test", "Touch value on press: %u", value);
on_release:
then:
- lambda: |-
// Test get_value() on release
uint32_t value = id(esp32_touch_pad_get_value).get_value();
ESP_LOGD("test", "Touch value on release: %u", value);

View File

@ -2,3 +2,4 @@ substitutions:
pin: GPIO27
<<: !include common.yaml
<<: !include common-get-value.yaml

View File

@ -2,3 +2,4 @@ substitutions:
pin: GPIO12
<<: !include common-variants.yaml
<<: !include common-get-value.yaml

View File

@ -2,3 +2,4 @@ substitutions:
pin: GPIO12
<<: !include common-variants.yaml
<<: !include common-get-value.yaml