ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
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
5namespace esphome {
6namespace cap1188 {
7
8static const char *const TAG = "cap1188";
9
11 this->disable_loop();
12
13 // no reset pin
14 if (this->reset_pin_ == nullptr) {
15 this->finish_setup_();
16 return;
17 }
18
19 // reset pin configured so reset before finishing setup
20 this->reset_pin_->setup();
21 this->reset_pin_->digital_write(false);
22 // delay after reset pin write
23 this->set_timeout(100, [this]() {
24 this->reset_pin_->digital_write(true);
25 // delay after reset pin write
26 this->set_timeout(100, [this]() {
27 this->reset_pin_->digital_write(false);
28 // delay after reset pin write
29 this->set_timeout(100, [this]() { this->finish_setup_(); });
30 });
31 });
32}
33
35 // Check if CAP1188 is actually connected
39
40 if ((this->cap1188_product_id_ != 0x50) || (this->cap1188_manufacture_id_ != 0x5D)) {
41 this->error_code_ = COMMUNICATION_FAILED;
42 this->mark_failed();
43 return;
44 }
45
46 // Set sensitivity
47 uint8_t sensitivity = 0;
48 this->read_byte(CAP1188_SENSITVITY, &sensitivity);
49 sensitivity = sensitivity & 0x0f;
50 this->write_byte(CAP1188_SENSITVITY, sensitivity | this->touch_threshold_);
51
52 // Allow multiple touches
54
55 // Have LEDs follow touches
56 this->write_byte(CAP1188_LED_LINK, 0xFF);
57
58 // Speed up a bit
60
61 // Setup successful, so enable loop
62 this->enable_loop();
63}
64
66 ESP_LOGCONFIG(TAG, "CAP1188:");
67 LOG_I2C_DEVICE(this);
68 LOG_PIN(" Reset Pin: ", this->reset_pin_);
69 ESP_LOGCONFIG(TAG,
70 " Product ID: 0x%x\n"
71 " Manufacture ID: 0x%x\n"
72 " Revision ID: 0x%x",
74
75 switch (this->error_code_) {
77 ESP_LOGE(TAG, "Product ID or Manufacture ID of the connected device does not match a known CAP1188.");
78 break;
79 case NONE:
80 default:
81 break;
82 }
83}
84
86 uint8_t touched = 0;
87
89
90 if (touched) {
91 uint8_t data = 0;
92 this->read_register(CAP1188_MAIN, &data, 1);
93 data = data & ~CAP1188_MAIN_INT;
94
95 this->write_register(CAP1188_MAIN, &data, 2);
96 }
97
98 for (auto *channel : this->channels_) {
99 channel->process(touched);
100 }
101}
102
103} // namespace cap1188
104} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
void enable_loop()
Enable this component's loop.
void disable_loop()
Disable this component's loop.
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
virtual void setup()=0
virtual void digital_write(bool value)=0
enum esphome::cap1188::CAP1188Component::ErrorCode NONE
std::vector< CAP1188Channel * > channels_
Definition cap1188.h:54
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:44
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:266
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:241
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:35
@ CAP1188_STAND_BY_CONFIGURATION
Definition cap1188.h:21
@ CAP1188_MANUFACTURE_ID
Definition cap1188.h:20
@ CAP1188_SENSOR_INPUT_STATUS
Definition cap1188.h:16
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7