ESPHome 2026.11.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
7ESPHOME_LOG_TAG(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->mark_failed(LOG_STR("Product ID or Manufacture ID of the connected device does not match a known CAP1188"));
41 return;
42 }
43
44 // Set sensitivity
45 uint8_t sensitivity = 0;
46 this->read_byte(CAP1188_SENSITVITY, &sensitivity);
47 sensitivity = sensitivity & 0x0f;
48 this->write_byte(CAP1188_SENSITVITY, sensitivity | this->touch_threshold_);
49
50 // Allow multiple touches
52
53 // Have LEDs follow touches
54 this->write_byte(CAP1188_LED_LINK, 0xFF);
55
56 // Speed up a bit
58
59 // Setup successful, so enable loop
60 this->enable_loop();
61}
62
64 ESP_LOGCONFIG(TAG,
65 "CAP1188:\n"
66 " Product ID: 0x%x\n"
67 " Manufacture ID: 0x%x\n"
68 " Revision ID: 0x%x",
70 LOG_I2C_DEVICE(this);
71 LOG_PIN(" Reset Pin: ", this->reset_pin_);
72}
73
75 uint8_t touched = 0;
76
78
79 if (touched) {
80 uint8_t data = 0;
81 this->read_register(CAP1188_MAIN, &data, 1);
82 data = data & ~CAP1188_MAIN_INT;
83
84 this->write_register(CAP1188_MAIN, &data, 1);
85 }
86
87 for (auto *channel : this->channels_) {
88 channel->process(touched);
89 }
90}
91
92} // namespace esphome::cap1188
void mark_failed()
Mark this component as failed.
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.
virtual void setup()=0
virtual void digital_write(bool value)=0
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:37
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
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:28
ESPHOME_LOG_TAG(TAG, "cap1188")
@ CAP1188_STAND_BY_CONFIGURATION
Definition cap1188.h:20
@ CAP1188_MANUFACTURE_ID
Definition cap1188.h:19
@ CAP1188_SENSOR_INPUT_STATUS
Definition cap1188.h:15