ESPHome  2024.11.0
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 #include <driver/touch_sensor.h>
12 
13 namespace esphome {
14 namespace esp32_touch {
15 
16 class ESP32TouchBinarySensor;
17 
19  public:
20  void register_touch_pad(ESP32TouchBinarySensor *pad) { this->children_.push_back(pad); }
21 
22  void set_setup_mode(bool setup_mode) { this->setup_mode_ = setup_mode; }
23  void set_sleep_duration(uint16_t sleep_duration) { this->sleep_cycle_ = sleep_duration; }
24  void set_measurement_duration(uint16_t meas_cycle) { this->meas_cycle_ = meas_cycle; }
25  void set_low_voltage_reference(touch_low_volt_t low_voltage_reference) {
26  this->low_voltage_reference_ = low_voltage_reference;
27  }
28  void set_high_voltage_reference(touch_high_volt_t high_voltage_reference) {
29  this->high_voltage_reference_ = high_voltage_reference;
30  }
31  void set_voltage_attenuation(touch_volt_atten_t voltage_attenuation) {
32  this->voltage_attenuation_ = voltage_attenuation;
33  }
34 #if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
35  void set_filter_mode(touch_filter_mode_t filter_mode) { this->filter_mode_ = filter_mode; }
36  void set_debounce_count(uint32_t debounce_count) { this->debounce_count_ = debounce_count; }
37  void set_noise_threshold(uint32_t noise_threshold) { this->noise_threshold_ = noise_threshold; }
38  void set_jitter_step(uint32_t jitter_step) { this->jitter_step_ = jitter_step; }
39  void set_smooth_level(touch_smooth_mode_t smooth_level) { this->smooth_level_ = smooth_level; }
40  void set_denoise_grade(touch_pad_denoise_grade_t denoise_grade) { this->grade_ = denoise_grade; }
41  void set_denoise_cap(touch_pad_denoise_cap_t cap_level) { this->cap_level_ = cap_level; }
42  void set_waterproof_guard_ring_pad(touch_pad_t pad) { this->waterproof_guard_ring_pad_ = pad; }
43  void set_waterproof_shield_driver(touch_pad_shield_driver_t drive_capability) {
44  this->waterproof_shield_driver_ = drive_capability;
45  }
46 #else
47  void set_iir_filter(uint32_t iir_filter) { this->iir_filter_ = iir_filter; }
48 #endif
49 
50  uint32_t component_touch_pad_read(touch_pad_t tp);
51 
52  void setup() override;
53  void dump_config() override;
54  void loop() override;
55  float get_setup_priority() const override { return setup_priority::DATA; }
56 
57  void on_shutdown() override;
58 
59  protected:
60 #if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
61  bool filter_configured_() const {
62  return (this->filter_mode_ != TOUCH_PAD_FILTER_MAX) && (this->smooth_level_ != TOUCH_PAD_SMOOTH_MAX);
63  }
64  bool denoise_configured_() const {
65  return (this->grade_ != TOUCH_PAD_DENOISE_MAX) && (this->cap_level_ != TOUCH_PAD_DENOISE_CAP_MAX);
66  }
67  bool waterproof_configured_() const {
68  return (this->waterproof_guard_ring_pad_ != TOUCH_PAD_MAX) &&
69  (this->waterproof_shield_driver_ != TOUCH_PAD_SHIELD_DRV_MAX);
70  }
71 #else
72  bool iir_filter_enabled_() const { return this->iir_filter_ > 0; }
73 #endif
74 
75  std::vector<ESP32TouchBinarySensor *> children_;
76  bool setup_mode_{false};
78  // common parameters
79  uint16_t sleep_cycle_{4095};
80  uint16_t meas_cycle_{65535};
81  touch_low_volt_t low_voltage_reference_{TOUCH_LVOLT_0V5};
82  touch_high_volt_t high_voltage_reference_{TOUCH_HVOLT_2V7};
83  touch_volt_atten_t voltage_attenuation_{TOUCH_HVOLT_ATTEN_0V};
84 #if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
85  touch_filter_mode_t filter_mode_{TOUCH_PAD_FILTER_MAX};
86  uint32_t debounce_count_{0};
87  uint32_t noise_threshold_{0};
88  uint32_t jitter_step_{0};
89  touch_smooth_mode_t smooth_level_{TOUCH_PAD_SMOOTH_MAX};
90  touch_pad_denoise_grade_t grade_{TOUCH_PAD_DENOISE_MAX};
91  touch_pad_denoise_cap_t cap_level_{TOUCH_PAD_DENOISE_CAP_MAX};
92  touch_pad_t waterproof_guard_ring_pad_{TOUCH_PAD_MAX};
93  touch_pad_shield_driver_t waterproof_shield_driver_{TOUCH_PAD_SHIELD_DRV_MAX};
94 #else
95  uint32_t iir_filter_{0};
96 #endif
97 };
98 
101  public:
102  ESP32TouchBinarySensor(touch_pad_t touch_pad, uint32_t threshold, uint32_t wakeup_threshold);
103 
104  touch_pad_t get_touch_pad() const { return this->touch_pad_; }
105  uint32_t get_threshold() const { return this->threshold_; }
106  void set_threshold(uint32_t threshold) { this->threshold_ = threshold; }
107  uint32_t get_value() const { return this->value_; }
108  uint32_t get_wakeup_threshold() const { return this->wakeup_threshold_; }
109 
110  protected:
112 
113  touch_pad_t touch_pad_{TOUCH_PAD_MAX};
114  uint32_t threshold_{0};
115  uint32_t value_{0};
116  const uint32_t wakeup_threshold_{0};
117 };
118 
119 } // namespace esp32_touch
120 } // namespace esphome
121 
122 #endif
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
void set_denoise_grade(touch_pad_denoise_grade_t denoise_grade)
Definition: esp32_touch.h:40
std::vector< ESP32TouchBinarySensor * > children_
Definition: esp32_touch.h:75
touch_pad_denoise_cap_t cap_level_
Definition: esp32_touch.h:91
void set_debounce_count(uint32_t debounce_count)
Definition: esp32_touch.h:36
uint32_t component_touch_pad_read(touch_pad_t tp)
float get_setup_priority() const override
Definition: esp32_touch.h:55
void set_low_voltage_reference(touch_low_volt_t low_voltage_reference)
Definition: esp32_touch.h:25
void set_measurement_duration(uint16_t meas_cycle)
Definition: esp32_touch.h:24
void set_denoise_cap(touch_pad_denoise_cap_t cap_level)
Definition: esp32_touch.h:41
void register_touch_pad(ESP32TouchBinarySensor *pad)
Definition: esp32_touch.h:20
void set_noise_threshold(uint32_t noise_threshold)
Definition: esp32_touch.h:37
touch_pad_shield_driver_t waterproof_shield_driver_
Definition: esp32_touch.h:93
void set_iir_filter(uint32_t iir_filter)
Definition: esp32_touch.h:47
void set_waterproof_shield_driver(touch_pad_shield_driver_t drive_capability)
Definition: esp32_touch.h:43
void set_filter_mode(touch_filter_mode_t filter_mode)
Definition: esp32_touch.h:35
void set_jitter_step(uint32_t jitter_step)
Definition: esp32_touch.h:38
void set_sleep_duration(uint16_t sleep_duration)
Definition: esp32_touch.h:23
void set_voltage_attenuation(touch_volt_atten_t voltage_attenuation)
Definition: esp32_touch.h:31
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
void set_waterproof_guard_ring_pad(touch_pad_t pad)
Definition: esp32_touch.h:42
void set_high_voltage_reference(touch_high_volt_t high_voltage_reference)
Definition: esp32_touch.h:28
Simple helper class to expose a touch pad value as a binary sensor.
Definition: esp32_touch.h:100
touch_pad_denoise_grade_t grade_
Definition: esp32_touch.h:90
void set_smooth_level(touch_smooth_mode_t smooth_level)
Definition: esp32_touch.h:39