From e11e4eaaeee4d42b0bd535a8f71cd38cc466d3cb Mon Sep 17 00:00:00 2001 From: olg Date: Sun, 9 Jun 2019 18:10:42 +0200 Subject: [PATCH] Add set_threshold and get_value methods to ESP32TouchBinarySensor and add a test. --- esphome/components/esp32_touch/esp32_touch.cpp | 8 +++++++- esphome/components/esp32_touch/esp32_touch.h | 3 +++ tests/test1.yaml | 11 +++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/esphome/components/esp32_touch/esp32_touch.cpp b/esphome/components/esp32_touch/esp32_touch.cpp index e85d0ef5c2..8be9ab943c 100644 --- a/esphome/components/esp32_touch/esp32_touch.cpp +++ b/esphome/components/esp32_touch/esp32_touch.cpp @@ -116,6 +116,7 @@ void ESP32TouchComponent::loop() { touch_pad_read(child->get_touch_pad(), &value); } + child->value_ = value; child->publish_state(value < child->get_threshold()); if (this->setup_mode_) { @@ -157,7 +158,12 @@ ESP32TouchBinarySensor::ESP32TouchBinarySensor(const std::string &name, touch_pa : BinarySensor(name), touch_pad_(touch_pad), threshold_(threshold) {} touch_pad_t ESP32TouchBinarySensor::get_touch_pad() const { return this->touch_pad_; } uint16_t ESP32TouchBinarySensor::get_threshold() const { return this->threshold_; } - +void ESP32TouchBinarySensor::set_threshold(uint16_t threshold) { + this->threshold_ = threshold; +} +uint16_t ESP32TouchBinarySensor::get_value () const { + return this->value_; +} } // namespace esp32_touch } // namespace esphome diff --git a/esphome/components/esp32_touch/esp32_touch.h b/esphome/components/esp32_touch/esp32_touch.h index b68876c33e..13f14ef968 100644 --- a/esphome/components/esp32_touch/esp32_touch.h +++ b/esphome/components/esp32_touch/esp32_touch.h @@ -56,12 +56,15 @@ class ESP32TouchBinarySensor : public binary_sensor::BinarySensor { touch_pad_t get_touch_pad() const; uint16_t get_threshold() const; + void set_threshold(uint16_t); + uint16_t get_value() const; protected: friend ESP32TouchComponent; touch_pad_t touch_pad_; uint16_t threshold_; + uint16_t value_; }; } // namespace esp32_touch diff --git a/tests/test1.yaml b/tests/test1.yaml index 2e92f817f8..7708281109 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -618,6 +618,7 @@ binary_sensor: name: "ESP32 Touch Pad GPIO27" pin: GPIO27 threshold: 1000 + id: btn_left - platform: nextion page_id: 0 component_id: 2 @@ -1118,6 +1119,16 @@ interval: if (true) return id(page1); else return id(page2); - display.page.show_next: display1 - display.page.show_previous: display1 + - interval: 2s + then: + - lambda: |- + static uint16_t btn_left_state = id(btn_left)->get_value(); + + ESP_LOGD("adaptive touch", "___ Touch Pad '%s' (T%u): val: %u state: %u tres:%u", id(btn_left)->get_name().c_str(), id(btn_left)->get_touch_pad(), id(btn_left)->get_value(), btn_left_state, id(btn_left)->get_threshold()); + + btn_left_state = ((uint32_t) id(btn_left)->get_value() + 63 * (uint32_t)btn_left_state) >> 6; + + id(btn_left)->set_threshold(btn_left_state * 0.9); display: - platform: lcd_gpio