ESPHome  2023.5.5
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 #include <vector>
10 
11 #if ESP_IDF_VERSION_MAJOR >= 4
12 #include <driver/touch_sensor.h>
13 #else
14 #include <driver/touch_pad.h>
15 #endif
16 
17 namespace esphome {
18 namespace esp32_touch {
19 
20 class ESP32TouchBinarySensor;
21 
23  public:
24  void register_touch_pad(ESP32TouchBinarySensor *pad) { children_.push_back(pad); }
25 
26  void set_setup_mode(bool setup_mode) { setup_mode_ = setup_mode; }
27 
28  void set_iir_filter(uint32_t iir_filter) { iir_filter_ = iir_filter; }
29 
30  void set_sleep_duration(uint16_t sleep_duration) { sleep_cycle_ = sleep_duration; }
31 
32  void set_measurement_duration(uint16_t meas_cycle) { meas_cycle_ = meas_cycle; }
33 
34  void set_low_voltage_reference(touch_low_volt_t low_voltage_reference) {
35  low_voltage_reference_ = low_voltage_reference;
36  }
37 
38  void set_high_voltage_reference(touch_high_volt_t high_voltage_reference) {
39  high_voltage_reference_ = high_voltage_reference;
40  }
41 
42  void set_voltage_attenuation(touch_volt_atten_t voltage_attenuation) { voltage_attenuation_ = voltage_attenuation; }
43 
44  void setup() override;
45  void dump_config() override;
46  void loop() override;
47  float get_setup_priority() const override { return setup_priority::DATA; }
48 
49  void on_shutdown() override;
50 
51  protected:
53  bool iir_filter_enabled_() const { return iir_filter_ > 0; }
54 
55  uint16_t sleep_cycle_{};
56  uint16_t meas_cycle_{65535};
57  touch_low_volt_t low_voltage_reference_{};
58  touch_high_volt_t high_voltage_reference_{};
59  touch_volt_atten_t voltage_attenuation_{};
60  std::vector<ESP32TouchBinarySensor *> children_;
61  bool setup_mode_{false};
63  uint32_t iir_filter_{0};
64 };
65 
68  public:
69  ESP32TouchBinarySensor(touch_pad_t touch_pad, uint16_t threshold, uint16_t wakeup_threshold);
70 
71  touch_pad_t get_touch_pad() const { return touch_pad_; }
72  uint16_t get_threshold() const { return threshold_; }
73  void set_threshold(uint16_t threshold) { threshold_ = threshold; }
74  uint16_t get_value() const { return value_; }
75  uint16_t get_wakeup_threshold() const { return wakeup_threshold_; }
76 
77  protected:
79 
80  touch_pad_t touch_pad_;
81  uint16_t threshold_;
82  uint16_t value_;
83  const uint16_t wakeup_threshold_;
84 };
85 
86 } // namespace esp32_touch
87 } // namespace esphome
88 
89 #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:53
std::vector< ESP32TouchBinarySensor * > children_
Definition: esp32_touch.h:60
float get_setup_priority() const override
Definition: esp32_touch.h:47
void set_low_voltage_reference(touch_low_volt_t low_voltage_reference)
Definition: esp32_touch.h:34
void set_measurement_duration(uint16_t meas_cycle)
Definition: esp32_touch.h:32
void set_threshold(uint16_t threshold)
Definition: esp32_touch.h:73
void register_touch_pad(ESP32TouchBinarySensor *pad)
Definition: esp32_touch.h:24
void set_iir_filter(uint32_t iir_filter)
Definition: esp32_touch.h:28
void set_sleep_duration(uint16_t sleep_duration)
Definition: esp32_touch.h:30
void set_voltage_attenuation(touch_volt_atten_t voltage_attenuation)
Definition: esp32_touch.h:42
Definition: a4988.cpp:4
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
void set_high_voltage_reference(touch_high_volt_t high_voltage_reference)
Definition: esp32_touch.h:38
Simple helper class to expose a touch pad value as a binary sensor.
Definition: esp32_touch.h:67