ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
icnt86.cpp
Go to the documentation of this file.
1#include "icnt86.h"
2#include "esphome/core/log.h"
3
4namespace esphome::icnt86 {
5
6ESPHOME_LOG_TAG(TAG, "icnt86");
7static constexpr uint16_t REG_TOUCH_NUM = 0x1001;
8static constexpr uint16_t REG_POINT1 = 0x1002;
9static constexpr uint8_t MAX_TOUCHES = 5;
10static constexpr uint8_t POINT_SIZE = 7;
11
13 ESP_LOGCONFIG(TAG, "Setting up icnt86 Touchscreen...");
14
15 // Register interrupt pin
16 if (this->interrupt_pin_ != nullptr) {
17 this->interrupt_pin_->setup();
19 }
20
21 // Perform reset if necessary
22 pulse_reset(this->reset_pin_, {.low_ms = 10});
23
24 if (this->x_raw_max_ == this->x_raw_min_) {
25 this->x_raw_max_ = this->display_->get_native_width();
26 }
27 if (this->y_raw_max_ == this->y_raw_min_) {
28 this->y_raw_max_ = this->display_->get_native_height();
29 }
30}
31
33 uint8_t buf[MAX_TOUCHES * POINT_SIZE] = {0};
34
35 if (!this->read_byte_reg16(REG_TOUCH_NUM, buf)) {
36 this->status_set_warning();
37 this->skip_update_ = true;
38 ESP_LOGW(TAG, "Failed to read touch count");
39 return;
40 }
41 uint8_t touch_count = buf[0];
42
43 if (touch_count == 0x00 || touch_count > MAX_TOUCHES) { // No new touch
45 return;
46 }
47 if (this->read_register16(REG_POINT1, buf, touch_count * POINT_SIZE) != i2c::ERROR_OK) {
48 this->status_set_warning();
49 this->skip_update_ = true;
50 ESP_LOGW(TAG, "Failed to read touch points");
51 return;
52 }
53 this->write_byte_reg16(REG_TOUCH_NUM, 0x00);
54 ESP_LOGV(TAG, "Touch count: %d", touch_count);
56
57 for (uint8_t i = 0; i < touch_count; i++) {
58 uint16_t x = ((uint16_t) buf[2 + 7 * i] << 8) + buf[1 + 7 * i];
59 uint16_t y = ((uint16_t) buf[4 + 7 * i] << 8) + buf[3 + 7 * i];
60 uint8_t pressure = buf[5 + 7 * i];
61 uint8_t touch_id = buf[6 + 7 * i];
62
63 // A zero-pressure report just means this point is no longer touched; skipping it here leaves is_touched_
64 // false (when no other point is active) so send_touches_() reports the release as normal.
65 if (pressure != 0) {
66 this->add_raw_touch_position_(touch_id, x, y, pressure);
67 }
68 }
69}
70
72 ESP_LOGCONFIG(TAG, "icnt86 Touchscreen:");
73 LOG_I2C_DEVICE(this);
74 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
75 LOG_PIN(" Reset Pin: ", this->reset_pin_);
76}
77
78} // namespace esphome::icnt86
void status_clear_warning()
Definition component.h:318
int get_native_width()
Get the native (original) width of the display in pixels.
Definition display.h:343
int get_native_height()
Get the native (original) height of the display in pixels.
Definition display.h:345
bool write_byte_reg16(uint16_t a_register, uint8_t data) const
Write one byte to a register with a 16-bit register address.
Definition i2c.h:330
bool read_byte_reg16(uint16_t a_register, uint8_t *data)
Read one byte from a register with a 16-bit register address.
Definition i2c.h:325
ErrorCode read_register16(uint16_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:62
InternalGPIOPin * interrupt_pin_
Definition icnt86.h:20
void attach_interrupt_(InternalGPIOPin *irq_pin, esphome::gpio::InterruptType type)
Call this function to send touch points to the on_touch listener and the binary_sensors.
void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw=0)
@ INTERRUPT_FALLING_EDGE
Definition gpio_pin.h:53
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
ESPHOME_LOG_TAG(TAG, "icnt86")
ESPHOME_ALWAYS_INLINE void pulse_reset(GPIOPin *pin, ResetPulse pulse)
Set up an active-low reset pin and drive it through pulse.
Definition gpio_pin.h:214
uint16_t x
Definition tt21100.cpp:5
uint8_t touch_id
Definition tt21100.cpp:4
uint16_t y
Definition tt21100.cpp:6
uint8_t pressure
Definition tt21100.cpp:7