ESPHome 2026.11.0b6
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 // The threshold replaces the upper nibble and is ORed into the lower one, as before
47
48 // Allow multiple touches
50
51 // Have LEDs follow touches
52 this->write_byte(CAP1188_LED_LINK, 0xFF);
53
54 // Speed up a bit
56
57 // Setup successful, so enable loop
58 this->enable_loop();
59}
60
62 ESP_LOGCONFIG(TAG,
63 "CAP1188:\n"
64 " Product ID: 0x%x\n"
65 " Manufacture ID: 0x%x\n"
66 " Revision ID: 0x%x",
68 LOG_I2C_DEVICE(this);
69 LOG_PIN(" Reset Pin: ", this->reset_pin_);
70}
71
73 uint8_t touched = 0;
74
75 // On a failed read the channels keep their state instead of reporting a release
76 if (!this->status_comm_result(this->read_byte(CAP1188_SENSOR_INPUT_STATUS, &touched)))
77 return;
78
79 if (touched)
81
82 for (auto *channel : this->channels_) {
83 channel->process(touched);
84 }
85}
86
87} // namespace esphome::cap1188
void mark_failed()
Mark this component as failed.
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void enable_loop()
Enable this component's loop.
Definition component.h:261
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
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
bool update_register(uint8_t a_register, uint8_t mask, uint8_t value)
Read a register, replace the bits in mask with those of value and write it back.
Definition i2c.cpp:95
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