1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-27 13:13:50 +00:00

Add deep sleep wakeup from touch (#1238) (#2281)

This commit is contained in:
Christian Taedcke
2021-09-20 10:12:32 +02:00
committed by GitHub
parent 3052c64dd7
commit 9ebe075f9b
6 changed files with 43 additions and 4 deletions

View File

@@ -15,6 +15,7 @@ ESP_PLATFORMS = [ESP_PLATFORM_ESP32]
DEPENDENCIES = ["esp32_touch"]
CONF_ESP32_TOUCH_ID = "esp32_touch_id"
CONF_WAKEUP_THRESHOLD = "wakeup_threshold"
TOUCH_PADS = {
4: cg.global_ns.TOUCH_PAD_NUM0,
@@ -47,6 +48,7 @@ CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend(
cv.GenerateID(CONF_ESP32_TOUCH_ID): cv.use_id(ESP32TouchComponent),
cv.Required(CONF_PIN): validate_touch_pad,
cv.Required(CONF_THRESHOLD): cv.uint16_t,
cv.Optional(CONF_WAKEUP_THRESHOLD, default=0): cv.uint16_t,
}
)
@@ -58,6 +60,7 @@ async def to_code(config):
config[CONF_NAME],
TOUCH_PADS[config[CONF_PIN]],
config[CONF_THRESHOLD],
config[CONF_WAKEUP_THRESHOLD],
)
await binary_sensor.register_binary_sensor(var, config)
cg.add(hub.register_touch_pad(var))

View File

@@ -133,15 +133,34 @@ void ESP32TouchComponent::loop() {
}
void ESP32TouchComponent::on_shutdown() {
bool is_wakeup_source = false;
if (this->iir_filter_enabled_()) {
touch_pad_filter_stop();
touch_pad_filter_delete();
}
touch_pad_deinit();
for (auto *child : this->children_) {
if (child->get_wakeup_threshold() != 0) {
if (!is_wakeup_source) {
is_wakeup_source = true;
// Touch sensor FSM mode must be 'TOUCH_FSM_MODE_TIMER' to use it to wake-up.
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
}
// No filter available when using as wake-up source.
touch_pad_config(child->get_touch_pad(), child->get_wakeup_threshold());
}
}
if (!is_wakeup_source) {
touch_pad_deinit();
}
}
ESP32TouchBinarySensor::ESP32TouchBinarySensor(const std::string &name, touch_pad_t touch_pad, uint16_t threshold)
: BinarySensor(name), touch_pad_(touch_pad), threshold_(threshold) {}
ESP32TouchBinarySensor::ESP32TouchBinarySensor(const std::string &name, touch_pad_t touch_pad, uint16_t threshold,
uint16_t wakeup_threshold)
: BinarySensor(name), touch_pad_(touch_pad), threshold_(threshold), wakeup_threshold_(wakeup_threshold) {}
} // namespace esp32_touch
} // namespace esphome

View File

@@ -57,12 +57,13 @@ class ESP32TouchComponent : public Component {
/// Simple helper class to expose a touch pad value as a binary sensor.
class ESP32TouchBinarySensor : public binary_sensor::BinarySensor {
public:
ESP32TouchBinarySensor(const std::string &name, touch_pad_t touch_pad, uint16_t threshold);
ESP32TouchBinarySensor(const std::string &name, touch_pad_t touch_pad, uint16_t threshold, uint16_t wakeup_threshold);
touch_pad_t get_touch_pad() const { return touch_pad_; }
uint16_t get_threshold() const { return threshold_; }
void set_threshold(uint16_t threshold) { threshold_ = threshold; }
uint16_t get_value() const { return value_; }
uint16_t get_wakeup_threshold() const { return wakeup_threshold_; }
protected:
friend ESP32TouchComponent;
@@ -70,6 +71,7 @@ class ESP32TouchBinarySensor : public binary_sensor::BinarySensor {
touch_pad_t touch_pad_;
uint16_t threshold_;
uint16_t value_;
const uint16_t wakeup_threshold_;
};
} // namespace esp32_touch