ESPHome  2022.6.3
esp32_touch.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ESP32
4 
7 #include <esp_idf_version.h>
8 
9 #if ESP_IDF_VERSION_MAJOR >= 4
10 #include <driver/touch_sensor.h>
11 #else
12 #include <driver/touch_pad.h>
13 #endif
14 
15 namespace esphome {
16 namespace esp32_touch {
17 
18 class ESP32TouchBinarySensor;
19 
21  public:
22  void register_touch_pad(ESP32TouchBinarySensor *pad) { children_.push_back(pad); }
23 
24  void set_setup_mode(bool setup_mode) { setup_mode_ = setup_mode; }
25 
26  void set_iir_filter(uint32_t iir_filter) { iir_filter_ = iir_filter; }
27 
28  void set_sleep_duration(uint16_t sleep_duration) { sleep_cycle_ = sleep_duration; }
29 
30  void set_measurement_duration(uint16_t meas_cycle) { meas_cycle_ = meas_cycle; }
31 
32  void set_low_voltage_reference(touch_low_volt_t low_voltage_reference) {
33  low_voltage_reference_ = low_voltage_reference;
34  }
35 
36  void set_high_voltage_reference(touch_high_volt_t high_voltage_reference) {
37  high_voltage_reference_ = high_voltage_reference;
38  }
39 
40  void set_voltage_attenuation(touch_volt_atten_t voltage_attenuation) { voltage_attenuation_ = voltage_attenuation; }
41 
42  void setup() override;
43  void dump_config() override;
44  void loop() override;
45  float get_setup_priority() const override { return setup_priority::DATA; }
46 
47  void on_shutdown() override;
48 
49  protected:
51  bool iir_filter_enabled_() const { return iir_filter_ > 0; }
52 
53  uint16_t sleep_cycle_{};
54  uint16_t meas_cycle_{65535};
55  touch_low_volt_t low_voltage_reference_{};
56  touch_high_volt_t high_voltage_reference_{};
57  touch_volt_atten_t voltage_attenuation_{};
58  std::vector<ESP32TouchBinarySensor *> children_;
59  bool setup_mode_{false};
61  uint32_t iir_filter_{0};
62 };
63 
66  public:
67  ESP32TouchBinarySensor(touch_pad_t touch_pad, uint16_t threshold, uint16_t wakeup_threshold);
68 
69  touch_pad_t get_touch_pad() const { return touch_pad_; }
70  uint16_t get_threshold() const { return threshold_; }
71  void set_threshold(uint16_t threshold) { threshold_ = threshold; }
72  uint16_t get_value() const { return value_; }
73  uint16_t get_wakeup_threshold() const { return wakeup_threshold_; }
74 
75  protected:
77 
78  touch_pad_t touch_pad_;
79  uint16_t threshold_;
80  uint16_t value_;
81  const uint16_t wakeup_threshold_;
82 };
83 
84 } // namespace esp32_touch
85 } // namespace esphome
86 
87 #endif
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
bool iir_filter_enabled_() const
Is the IIR filter enabled?
Definition: esp32_touch.h:51
std::vector< ESP32TouchBinarySensor * > children_
Definition: esp32_touch.h:58
float get_setup_priority() const override
Definition: esp32_touch.h:45
void set_low_voltage_reference(touch_low_volt_t low_voltage_reference)
Definition: esp32_touch.h:32
void set_measurement_duration(uint16_t meas_cycle)
Definition: esp32_touch.h:30
void set_threshold(uint16_t threshold)
Definition: esp32_touch.h:71
void register_touch_pad(ESP32TouchBinarySensor *pad)
Definition: esp32_touch.h:22
void set_iir_filter(uint32_t iir_filter)
Definition: esp32_touch.h:26
void set_sleep_duration(uint16_t sleep_duration)
Definition: esp32_touch.h:28
void set_voltage_attenuation(touch_volt_atten_t voltage_attenuation)
Definition: esp32_touch.h:40
Definition: a4988.cpp:4
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:26
void set_high_voltage_reference(touch_high_volt_t high_voltage_reference)
Definition: esp32_touch.h:36
Simple helper class to expose a touch pad value as a binary sensor.
Definition: esp32_touch.h:65