ESPHome  2024.4.0
mpr121.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #include <vector>
8 
9 namespace esphome {
10 namespace mpr121 {
11 
12 enum {
18  MPR121_MHDR = 0x2B,
19  MPR121_NHDR = 0x2C,
20  MPR121_NCLR = 0x2D,
21  MPR121_FDLR = 0x2E,
22  MPR121_MHDF = 0x2F,
23  MPR121_NHDF = 0x30,
24  MPR121_NCLF = 0x31,
25  MPR121_FDLF = 0x32,
26  MPR121_NHDT = 0x33,
27  MPR121_NCLT = 0x34,
28  MPR121_FDLT = 0x35,
36  MPR121_ECR = 0x5E,
43  MPR121_GPIOEN = 0x77,
48 };
49 
51  friend class MPR121Component;
52 
53  public:
54  void set_channel(uint8_t channel) { channel_ = channel; }
55  void process(uint16_t data) { this->publish_state(static_cast<bool>(data & (1 << this->channel_))); }
56  void set_touch_threshold(uint8_t touch_threshold) { this->touch_threshold_ = touch_threshold; };
57  void set_release_threshold(uint8_t release_threshold) { this->release_threshold_ = release_threshold; };
58 
59  protected:
60  uint8_t channel_{0};
63 };
64 
65 class MPR121Component : public Component, public i2c::I2CDevice {
66  public:
67  void register_channel(MPR121Channel *channel) { this->channels_.push_back(channel); }
68  void set_touch_debounce(uint8_t debounce);
69  void set_release_debounce(uint8_t debounce);
70  void set_touch_threshold(uint8_t touch_threshold) { this->touch_threshold_ = touch_threshold; };
71  void set_release_threshold(uint8_t release_threshold) { this->release_threshold_ = release_threshold; };
72  uint8_t get_touch_threshold() { return this->touch_threshold_; };
73  uint8_t get_release_threshold() { return this->release_threshold_; };
74  void setup() override;
75  void dump_config() override;
76  float get_setup_priority() const override { return setup_priority::DATA; }
77  void loop() override;
78 
79  protected:
80  std::vector<MPR121Channel *> channels_{};
81  uint8_t debounce_{0};
82  uint8_t touch_threshold_{};
83  uint8_t release_threshold_{};
84  enum ErrorCode {
85  NONE = 0,
88  } error_code_{NONE};
89 };
90 
91 } // namespace mpr121
92 } // namespace esphome
void setup()
void loop()
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
void set_touch_threshold(uint8_t touch_threshold)
Definition: mpr121.h:56
optional< uint8_t > release_threshold_
Definition: mpr121.h:62
void set_release_threshold(uint8_t release_threshold)
Definition: mpr121.h:71
void set_release_threshold(uint8_t release_threshold)
Definition: mpr121.h:57
void set_touch_threshold(uint8_t touch_threshold)
Definition: mpr121.h:70
optional< uint8_t > touch_threshold_
Definition: mpr121.h:61
void set_channel(uint8_t channel)
Definition: mpr121.h:54
void process(uint16_t data)
Definition: mpr121.h:55
float get_setup_priority() const override
Definition: mpr121.h:76
void publish_state(bool state)
Publish a new state to the front-end.
void register_channel(MPR121Channel *channel)
Definition: mpr121.h:67
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
This Class provides the methods to read/write bytes from/to an i2c device.
Definition: i2c.h:133