ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
mpr121.cpp
Go to the documentation of this file.
1#include "mpr121.h"
2
3#include <cstdint>
4
5#include "esphome/core/hal.h"
6#include "esphome/core/log.h"
7
8namespace esphome::mpr121 {
9
10ESPHOME_LOG_TAG(TAG, "mpr121");
11
13 this->disable_loop();
14 // soft reset device
15 this->write_byte(MPR121_SOFTRESET, 0x63);
16 this->set_timeout(100, [this]() {
17 if (!this->write_byte(MPR121_ECR, 0x0)) {
18 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
19 return;
20 }
21 // set touch sensitivity for all 12 channels
22 for (auto *channel : this->channels_) {
23 channel->setup();
24 }
25 this->write_byte(MPR121_MHDR, 0x01);
26 this->write_byte(MPR121_NHDR, 0x01);
27 this->write_byte(MPR121_NCLR, 0x0E);
28 this->write_byte(MPR121_FDLR, 0x00);
29
30 this->write_byte(MPR121_MHDF, 0x01);
31 this->write_byte(MPR121_NHDF, 0x05);
32 this->write_byte(MPR121_NCLF, 0x01);
33 this->write_byte(MPR121_FDLF, 0x00);
34
35 this->write_byte(MPR121_NHDT, 0x00);
36 this->write_byte(MPR121_NCLT, 0x00);
37 this->write_byte(MPR121_FDLT, 0x00);
38
40 // default, 16uA charge current
41 this->write_byte(MPR121_CONFIG1, 0x10);
42 // 0.5uS encoding, 1ms period
43 this->write_byte(MPR121_CONFIG2, 0x20);
44
45 // Write the Electrode Configuration Register
46 // * Highest 2 bits is "Calibration Lock", which we set to a value corresponding to 5 bits.
47 // * The 2 bits below is "Proximity Enable" and are left at 0.
48 // * The 4 least significant bits control how many electrodes are enabled. Electrodes are enabled
49 // as a range, starting at 0 up to the highest channel index used.
50 this->write_byte(MPR121_ECR, 0x80 | (this->max_touch_channel_ + 1));
51
52 this->flush_gpio_();
53 this->enable_loop();
54 });
55}
56
58 uint8_t mask = debounce << 4;
59 this->debounce_ &= 0x0f;
60 this->debounce_ |= mask;
61}
62
64 uint8_t mask = debounce & 0x0f;
65 this->debounce_ &= 0xf0;
66 this->debounce_ |= mask;
67};
68
70 ESP_LOGCONFIG(TAG, "MPR121:");
71 LOG_I2C_DEVICE(this);
72}
74 uint16_t val = 0;
76
77 for (auto *channel : this->channels_)
78 channel->process(val);
79
81}
82
83bool MPR121Component::digital_read(uint8_t pin) { return (this->gpio_input_ & (1 << (pin - 4))) != 0; }
84
85void MPR121Component::digital_write(uint8_t pin, bool value) {
86 const uint8_t ionum = pin - 4;
87 if (value) {
88 this->gpio_output_ |= (1 << ionum);
89 } else {
90 this->gpio_output_ &= ~(1 << ionum);
91 }
92 this->flush_gpio_();
93}
94
96 const uint8_t ionum = pin - 4;
97 this->gpio_enable_ |= (1 << ionum);
98 if (flags & gpio::FLAG_INPUT) {
99 this->gpio_direction_ &= ~(1 << ionum);
100 } else if (flags & gpio::FLAG_OUTPUT) {
101 this->gpio_direction_ |= 1 << ionum;
102 }
103 this->flush_gpio_();
104}
105
107 if (this->is_failed()) {
108 return false;
109 }
110
111 // TODO: The CTL registers can configure internal pullup/pulldown resistors.
112 this->write_byte(MPR121_GPIOCTL0, 0x00);
113 this->write_byte(MPR121_GPIOCTL1, 0x00);
116
117 return this->status_comm_result(this->write_byte(MPR121_GPIODATA, this->gpio_output_));
118}
119
120} // namespace esphome::mpr121
121
122ESPHOME_EXPANDER_PIN_SUMMARY(esphome::mpr121::MPR121Component, "ELE%u on MPR121")
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:274
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void enable_loop()
Enable this component's loop.
Definition component.h:248
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void disable_loop()
Disable this component's loop.
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
bool read_byte_16_le(uint8_t a_register, uint16_t *data)
Read a 16-bit value stored least significant byte first.
Definition i2c.h:296
uint8_t gpio_enable_
The enable mask - zero means high Z, 1 means GPIO usage.
Definition mpr121.h:92
void set_release_debounce(uint8_t debounce)
Definition mpr121.cpp:63
uint8_t gpio_direction_
Mask for the pin mode - 1 means output, 0 means input.
Definition mpr121.h:94
std::vector< MPR121Channel * > channels_
Definition mpr121.h:83
uint8_t gpio_output_
The mask to write as output state - 1 means HIGH, 0 means LOW.
Definition mpr121.h:96
bool digital_read(uint8_t pin)
Definition mpr121.cpp:83
void pin_mode(uint8_t pin, gpio::Flags flags)
Definition mpr121.cpp:95
uint8_t gpio_input_
The mask to read as input state - 1 means HIGH, 0 means LOW.
Definition mpr121.h:98
void digital_write(uint8_t pin, bool value)
Definition mpr121.cpp:85
void set_touch_debounce(uint8_t debounce)
Definition mpr121.cpp:57
uint16_t flags
mopeka_std_values val[3]
@ MPR121_TOUCHSTATUS_L
Definition mpr121.h:15
ESPHOME_LOG_TAG(TAG, "mpr121")