ESPHome 2026.6.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::cap1188 {
6
7static const char *const TAG = "cap1188";
8
10 this->disable_loop();
11
12 // no reset pin
13 if (this->reset_pin_ == nullptr) {
14 this->finish_setup_();
15 return;
16 }
17
18 // reset pin configured so reset before finishing setup
19 this->reset_pin_->setup();
20 this->reset_pin_->digital_write(false);
21 // delay after reset pin write
22 this->set_timeout(100, [this]() {
23 this->reset_pin_->digital_write(true);
24 // delay after reset pin write
25 this->set_timeout(100, [this]() {
26 this->reset_pin_->digital_write(false);
27 // delay after reset pin write
28 this->set_timeout(100, [this]() { this->finish_setup_(); });
29 });
30 });
31}
32
34 // Check if CAP1188 is actually connected
38
39 if ((this->cap1188_product_id_ != 0x50) || (this->cap1188_manufacture_id_ != 0x5D)) {
40 this->error_code_ = COMMUNICATION_FAILED;
41 this->mark_failed();
42 return;
43 }
44
45 // Set sensitivity
46 uint8_t sensitivity = 0;
47 this->read_byte(CAP1188_SENSITVITY, &sensitivity);
48 sensitivity = sensitivity & 0x0f;
49 this->write_byte(CAP1188_SENSITVITY, sensitivity | this->touch_threshold_);
50
51 // Allow multiple touches
53
54 // Have LEDs follow touches
55 this->write_byte(CAP1188_LED_LINK, 0xFF);
56
57 // Speed up a bit
59
60 // Setup successful, so enable loop
61 this->enable_loop();
62}
63
65 ESP_LOGCONFIG(TAG,
66 "CAP1188:\n"
67 " Product ID: 0x%x\n"
68 " Manufacture ID: 0x%x\n"
69 " Revision ID: 0x%x",
71 LOG_I2C_DEVICE(this);
72 LOG_PIN(" Reset Pin: ", this->reset_pin_);
73
74 switch (this->error_code_) {
76 ESP_LOGE(TAG, "Product ID or Manufacture ID of the connected device does not match a known CAP1188.");
77 break;
78 case NONE:
79 default:
80 break;
81 }
82}
83
85 uint8_t touched = 0;
86
88
89 if (touched) {
90 uint8_t data = 0;
91 this->read_register(CAP1188_MAIN, &data, 1);
92 data = data & ~CAP1188_MAIN_INT;
93
94 this->write_register(CAP1188_MAIN, &data, 1);
95 }
96
97 for (auto *channel : this->channels_) {
98 channel->process(touched);
99 }
100}
101
102} // namespace esphome::cap1188
void mark_failed()
Mark this component as failed.
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:493
void enable_loop()
Enable this component's loop.
Definition component.h:246
void disable_loop()
Disable this component's loop.
virtual void setup()=0
virtual void digital_write(bool value)=0
enum esphome::cap1188::CAP1188Component::ErrorCode NONE
std::vector< CAP1188Channel * > channels_
Definition cap1188.h:53
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:34
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:240
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:25
@ CAP1188_STAND_BY_CONFIGURATION
Definition cap1188.h:20
@ CAP1188_MANUFACTURE_ID
Definition cap1188.h:19
@ CAP1188_SENSOR_INPUT_STATUS
Definition cap1188.h:15