ESPHome  2024.3.1
mpr121.cpp
Go to the documentation of this file.
1 #include "mpr121.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/hal.h"
4 
5 namespace esphome {
6 namespace mpr121 {
7 
8 static const char *const TAG = "mpr121";
9 
11  ESP_LOGCONFIG(TAG, "Setting up MPR121...");
12  // soft reset device
13  this->write_byte(MPR121_SOFTRESET, 0x63);
14  delay(100); // NOLINT
15  if (!this->write_byte(MPR121_ECR, 0x0)) {
16  this->error_code_ = COMMUNICATION_FAILED;
17  this->mark_failed();
18  return;
19  }
20 
21  // set touch sensitivity for all 12 channels
22  for (auto *channel : this->channels_) {
23  this->write_byte(MPR121_TOUCHTH_0 + 2 * channel->channel_,
24  channel->touch_threshold_.value_or(this->touch_threshold_));
25  this->write_byte(MPR121_RELEASETH_0 + 2 * channel->channel_,
26  channel->release_threshold_.value_or(this->release_threshold_));
27  }
28  this->write_byte(MPR121_MHDR, 0x01);
29  this->write_byte(MPR121_NHDR, 0x01);
30  this->write_byte(MPR121_NCLR, 0x0E);
31  this->write_byte(MPR121_FDLR, 0x00);
32 
33  this->write_byte(MPR121_MHDF, 0x01);
34  this->write_byte(MPR121_NHDF, 0x05);
35  this->write_byte(MPR121_NCLF, 0x01);
36  this->write_byte(MPR121_FDLF, 0x00);
37 
38  this->write_byte(MPR121_NHDT, 0x00);
39  this->write_byte(MPR121_NCLT, 0x00);
40  this->write_byte(MPR121_FDLT, 0x00);
41 
42  this->write_byte(MPR121_DEBOUNCE, 0);
43  // default, 16uA charge current
44  this->write_byte(MPR121_CONFIG1, 0x10);
45  // 0.5uS encoding, 1ms period
46  this->write_byte(MPR121_CONFIG2, 0x20);
47  // start with first 5 bits of baseline tracking
48  this->write_byte(MPR121_ECR, 0x8F);
49 }
50 
51 void MPR121Component::set_touch_debounce(uint8_t debounce) {
52  uint8_t mask = debounce << 4;
53  this->debounce_ &= 0x0f;
54  this->debounce_ |= mask;
55 }
56 
57 void MPR121Component::set_release_debounce(uint8_t debounce) {
58  uint8_t mask = debounce & 0x0f;
59  this->debounce_ &= 0xf0;
60  this->debounce_ |= mask;
61 };
62 
64  ESP_LOGCONFIG(TAG, "MPR121:");
65  LOG_I2C_DEVICE(this);
66  switch (this->error_code_) {
68  ESP_LOGE(TAG, "Communication with MPR121 failed!");
69  break;
70  case WRONG_CHIP_STATE:
71  ESP_LOGE(TAG, "MPR121 has wrong default value for CONFIG2?");
72  break;
73  case NONE:
74  default:
75  break;
76  }
77 }
79  uint16_t val = 0;
81 
82  // Flip order
83  uint8_t lsb = val >> 8;
84  uint8_t msb = val;
85  val = (uint16_t(msb) << 8) | lsb;
86 
87  for (auto *channel : this->channels_)
88  channel->process(val);
89 }
90 
91 } // namespace mpr121
92 } // namespace esphome
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition: i2c.h:246
void set_release_debounce(uint8_t debounce)
Definition: mpr121.cpp:57
enum esphome::mpr121::MPR121Component::ErrorCode NONE
mopeka_std_values val[4]
std::vector< MPR121Channel * > channels_
Definition: mpr121.h:80
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:262
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:113
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void dump_config() override
Definition: mpr121.cpp:63
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26
void set_touch_debounce(uint8_t debounce)
Definition: mpr121.cpp:51