ESPHome  2024.10.2
mpr121.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/defines.h"
5 #include "esphome/core/hal.h"
6 
8 
9 #include <vector>
10 
11 namespace esphome {
12 namespace mpr121 {
13 
14 enum {
20  MPR121_MHDR = 0x2B,
21  MPR121_NHDR = 0x2C,
22  MPR121_NCLR = 0x2D,
23  MPR121_FDLR = 0x2E,
24  MPR121_MHDF = 0x2F,
25  MPR121_NHDF = 0x30,
26  MPR121_NCLF = 0x31,
27  MPR121_FDLF = 0x32,
28  MPR121_NHDT = 0x33,
29  MPR121_NCLT = 0x34,
30  MPR121_FDLT = 0x35,
38  MPR121_ECR = 0x5E,
48  MPR121_GPIOEN = 0x77,
53 };
54 
56  public:
57  virtual void setup() = 0;
58  virtual void process(uint16_t data) = 0;
59 };
60 
61 class MPR121Component : public Component, public i2c::I2CDevice {
62  public:
63  void register_channel(MPR121Channel *channel) { this->channels_.push_back(channel); }
64  void set_touch_debounce(uint8_t debounce);
65  void set_release_debounce(uint8_t debounce);
66  void set_touch_threshold(uint8_t touch_threshold) { this->touch_threshold_ = touch_threshold; };
67  void set_release_threshold(uint8_t release_threshold) { this->release_threshold_ = release_threshold; };
68  uint8_t get_touch_threshold() const { return this->touch_threshold_; };
69  uint8_t get_release_threshold() const { return this->release_threshold_; };
70  void setup() override;
71  void dump_config() override;
72  float get_setup_priority() const override { return setup_priority::IO; }
73  void loop() override;
74 
75  void set_max_touch_channel(uint8_t max_touch_channel) { this->max_touch_channel_ = max_touch_channel; }
76 
77  // GPIO helper functions.
78  bool digital_read(uint8_t ionum);
79  void digital_write(uint8_t ionum, bool value);
80  void pin_mode(uint8_t ionum, gpio::Flags flags);
81 
82  protected:
83  std::vector<MPR121Channel *> channels_{};
84  uint8_t debounce_{0};
85  uint8_t touch_threshold_{};
86  uint8_t release_threshold_{};
87  uint8_t max_touch_channel_{3};
88  enum ErrorCode {
89  NONE = 0,
92  } error_code_{NONE};
93 
94  bool flush_gpio_();
95 
97  uint8_t gpio_enable_{0x00};
99  uint8_t gpio_direction_{0x00};
101  uint8_t gpio_output_{0x00};
103  uint8_t gpio_input_{0x00};
104 };
105 
107 class MPR121GPIOPin : public GPIOPin {
108  public:
109  void setup() override;
110  void pin_mode(gpio::Flags flags) override;
111  bool digital_read() override;
112  void digital_write(bool value) override;
113  std::string dump_summary() const override;
114 
115  void set_parent(MPR121Component *parent) { this->parent_ = parent; }
116  void set_pin(uint8_t pin) { this->pin_ = pin; }
117  void set_inverted(bool inverted) { this->inverted_ = inverted; }
118  void set_flags(gpio::Flags flags) { this->flags_ = flags; }
119 
120  protected:
122  uint8_t pin_;
123  bool inverted_;
125 };
126 
127 } // namespace mpr121
128 } // namespace esphome
void loop()
MPR121Component * parent_
Definition: mpr121.h:121
void set_release_threshold(uint8_t release_threshold)
Definition: mpr121.h:67
void set_touch_threshold(uint8_t touch_threshold)
Definition: mpr121.h:66
void set_flags(gpio::Flags flags)
Definition: mpr121.h:118
virtual void process(uint16_t data)=0
float get_setup_priority() const override
Definition: mpr121.h:72
void set_max_touch_channel(uint8_t max_touch_channel)
Definition: mpr121.h:75
const uint32_t flags
Definition: stm32flash.h:85
void register_channel(MPR121Channel *channel)
Definition: mpr121.h:63
uint8_t get_release_threshold() const
Definition: mpr121.h:69
const float IO
For components that represent GPIO pins like PCF8573.
Definition: component.cpp:17
uint8_t get_touch_threshold() const
Definition: mpr121.h:68
void set_pin(uint8_t pin)
Definition: mpr121.h:116
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Helper class to expose a MPR121 pin as an internal input GPIO pin.
Definition: mpr121.h:107
void set_parent(MPR121Component *parent)
Definition: mpr121.h:115
This Class provides the methods to read/write bytes from/to an i2c device.
Definition: i2c.h:133
void set_inverted(bool inverted)
Definition: mpr121.h:117