mirror of
https://github.com/esphome/esphome.git
synced 2025-09-15 01:32:19 +01:00
touch ups
This commit is contained in:
@@ -15,6 +15,8 @@
|
|||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace esp32_touch {
|
namespace esp32_touch {
|
||||||
|
|
||||||
|
static const uint32_t SETUP_MODE_LOG_INTERVAL_MS = 250;
|
||||||
|
|
||||||
class ESP32TouchBinarySensor;
|
class ESP32TouchBinarySensor;
|
||||||
|
|
||||||
struct TouchPadEvent {
|
struct TouchPadEvent {
|
||||||
|
@@ -95,10 +95,9 @@ void ESP32TouchComponent::dump_config() {
|
|||||||
|
|
||||||
void ESP32TouchComponent::loop() {
|
void ESP32TouchComponent::loop() {
|
||||||
const uint32_t now = App.get_loop_component_start_time();
|
const uint32_t now = App.get_loop_component_start_time();
|
||||||
bool should_print = this->setup_mode_ && now - this->setup_mode_last_log_print_ > 250;
|
|
||||||
|
|
||||||
// Print debug info for all pads in setup mode
|
// Print debug info for all pads in setup mode
|
||||||
if (should_print) {
|
if (this->setup_mode_ && now - this->setup_mode_last_log_print_ > SETUP_MODE_LOG_INTERVAL_MS) {
|
||||||
for (auto *child : this->children_) {
|
for (auto *child : this->children_) {
|
||||||
ESP_LOGD(TAG, "Touch Pad '%s' (T%" PRIu32 "): %" PRIu32, child->get_name().c_str(),
|
ESP_LOGD(TAG, "Touch Pad '%s' (T%" PRIu32 "): %" PRIu32, child->get_name().c_str(),
|
||||||
(uint32_t) child->get_touch_pad(), child->value_);
|
(uint32_t) child->get_touch_pad(), child->value_);
|
||||||
|
@@ -268,54 +268,43 @@ void ESP32TouchComponent::loop() {
|
|||||||
|
|
||||||
// Handle active/inactive events
|
// Handle active/inactive events
|
||||||
if (event.intr_mask & (TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE)) {
|
if (event.intr_mask & (TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE)) {
|
||||||
// For INACTIVE events, we need to check which pad was released
|
bool is_touch_event = (event.intr_mask & TOUCH_PAD_INTR_MASK_ACTIVE) != 0;
|
||||||
// The pad number is in event.pad
|
|
||||||
if (event.intr_mask & TOUCH_PAD_INTR_MASK_INACTIVE) {
|
|
||||||
// Find the child for this pad
|
|
||||||
for (auto *child : this->children_) {
|
|
||||||
if (child->get_touch_pad() == event.pad) {
|
|
||||||
// Read current value
|
|
||||||
uint32_t value = 0;
|
|
||||||
if (this->filter_configured_()) {
|
|
||||||
touch_pad_filter_read_smooth(event.pad, &value);
|
|
||||||
} else {
|
|
||||||
touch_pad_read_benchmark(event.pad, &value);
|
|
||||||
}
|
|
||||||
|
|
||||||
child->value_ = value;
|
// For INACTIVE events, we check specific pad. For ACTIVE events, check pad status mask
|
||||||
|
for (auto *child : this->children_) {
|
||||||
|
touch_pad_t pad = child->get_touch_pad();
|
||||||
|
bool should_process = false;
|
||||||
|
|
||||||
// This is an INACTIVE event, so not touched
|
if (is_touch_event) {
|
||||||
child->last_state_ = false;
|
// ACTIVE event - check if this pad is in the status mask
|
||||||
child->publish_state(false);
|
should_process = (event.pad_status & BIT(pad)) != 0;
|
||||||
ESP_LOGD(TAG, "Touch Pad '%s' released (value: %d, threshold: %d)", child->get_name().c_str(), value,
|
} else {
|
||||||
child->get_threshold());
|
// INACTIVE event - check if this is the specific pad that was released
|
||||||
break;
|
should_process = (pad == event.pad);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (event.intr_mask & TOUCH_PAD_INTR_MASK_ACTIVE) {
|
|
||||||
// For ACTIVE events, check the pad status mask
|
|
||||||
for (auto *child : this->children_) {
|
|
||||||
touch_pad_t pad = child->get_touch_pad();
|
|
||||||
|
|
||||||
// Check if this pad is in the status mask
|
if (should_process) {
|
||||||
if (event.pad_status & BIT(pad)) {
|
// Read current value
|
||||||
// Read current value
|
uint32_t value = 0;
|
||||||
uint32_t value = 0;
|
if (this->filter_configured_()) {
|
||||||
if (this->filter_configured_()) {
|
touch_pad_filter_read_smooth(pad, &value);
|
||||||
touch_pad_filter_read_smooth(pad, &value);
|
} else {
|
||||||
} else {
|
touch_pad_read_benchmark(pad, &value);
|
||||||
touch_pad_read_benchmark(pad, &value);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
child->value_ = value;
|
child->value_ = value;
|
||||||
|
|
||||||
// This is an ACTIVE event, so touched
|
// Update state if changed
|
||||||
if (!child->last_state_) {
|
if (child->last_state_ != is_touch_event) {
|
||||||
child->last_state_ = true;
|
child->last_state_ = is_touch_event;
|
||||||
child->publish_state(true);
|
child->publish_state(is_touch_event);
|
||||||
ESP_LOGD(TAG, "Touch Pad '%s' touched (value: %d, threshold: %d)", child->get_name().c_str(), value,
|
ESP_LOGD(TAG, "Touch Pad '%s' %s (value: %d, threshold: %d)", child->get_name().c_str(),
|
||||||
child->get_threshold());
|
is_touch_event ? "touched" : "released", value, child->get_threshold());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For INACTIVE events, we only process one pad
|
||||||
|
if (!is_touch_event) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -323,8 +312,7 @@ void ESP32TouchComponent::loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// In setup mode, periodically log all pad values
|
// In setup mode, periodically log all pad values
|
||||||
if (this->setup_mode_ && now - this->setup_mode_last_log_print_ > 1000) {
|
if (this->setup_mode_ && now - this->setup_mode_last_log_print_ > SETUP_MODE_LOG_INTERVAL_MS) {
|
||||||
ESP_LOGD(TAG, "=== Touch Pad Status ===");
|
|
||||||
for (auto *child : this->children_) {
|
for (auto *child : this->children_) {
|
||||||
uint32_t raw = 0;
|
uint32_t raw = 0;
|
||||||
uint32_t benchmark = 0;
|
uint32_t benchmark = 0;
|
||||||
|
Reference in New Issue
Block a user