ESPHome  2024.11.0
cap1188.cpp
Go to the documentation of this file.
1 #include "cap1188.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/hal.h"
4 
5 namespace esphome {
6 namespace cap1188 {
7 
8 static const char *const TAG = "cap1188";
9 
11  ESP_LOGCONFIG(TAG, "Setting up CAP1188...");
12 
13  // Reset device using the reset pin
14  if (this->reset_pin_ != nullptr) {
15  this->reset_pin_->setup();
16  this->reset_pin_->digital_write(false);
17  delay(100); // NOLINT
18  this->reset_pin_->digital_write(true);
19  delay(100); // NOLINT
20  this->reset_pin_->digital_write(false);
21  delay(100); // NOLINT
22  }
23 
24  // Check if CAP1188 is actually connected
28 
29  if ((this->cap1188_product_id_ != 0x50) || (this->cap1188_manufacture_id_ != 0x5D)) {
30  this->error_code_ = COMMUNICATION_FAILED;
31  this->mark_failed();
32  return;
33  }
34 
35  // Set sensitivity
36  uint8_t sensitivity = 0;
37  this->read_byte(CAP1188_SENSITVITY, &sensitivity);
38  sensitivity = sensitivity & 0x0f;
39  this->write_byte(CAP1188_SENSITVITY, sensitivity | this->touch_threshold_);
40 
41  // Allow multiple touches
43 
44  // Have LEDs follow touches
45  this->write_byte(CAP1188_LED_LINK, 0xFF);
46 
47  // Speed up a bit
49 }
50 
52  ESP_LOGCONFIG(TAG, "CAP1188:");
53  LOG_I2C_DEVICE(this);
54  LOG_PIN(" Reset Pin: ", this->reset_pin_);
55  ESP_LOGCONFIG(TAG, " Product ID: 0x%x", this->cap1188_product_id_);
56  ESP_LOGCONFIG(TAG, " Manufacture ID: 0x%x", this->cap1188_manufacture_id_);
57  ESP_LOGCONFIG(TAG, " Revision ID: 0x%x", this->cap1188_revision_);
58 
59  switch (this->error_code_) {
61  ESP_LOGE(TAG, "Product ID or Manufacture ID of the connected device does not match a known CAP1188.");
62  break;
63  case NONE:
64  default:
65  break;
66  }
67 }
68 
70  uint8_t touched = 0;
71 
72  this->read_register(CAP1188_SENSOR_INPUT_STATUS, &touched, 1);
73 
74  if (touched) {
75  uint8_t data = 0;
76  this->read_register(CAP1188_MAIN, &data, 1);
77  data = data & ~CAP1188_MAIN_INT;
78 
79  this->write_register(CAP1188_MAIN, &data, 2);
80  }
81 
82  for (auto *channel : this->channels_) {
83  channel->process(touched);
84  }
85 }
86 
87 } // namespace cap1188
88 } // namespace esphome
virtual void digital_write(bool value)=0
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition: i2c.h:235
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop=true)
reads an array of bytes from a specific register in the I²C device
Definition: i2c.cpp:10
virtual void setup()=0
enum esphome::cap1188::CAP1188Component::ErrorCode NONE
std::vector< CAP1188Channel * > channels_
Definition: cap1188.h:53
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:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a specific register in the I²C device
Definition: i2c.cpp:25
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26