ESPHome 2026.11.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 if (this->reset_pin_ != nullptr) {
23 this->reset_pin_->setup();
24 this->reset_pin_->digital_write(false);
25 delay(10);
26 this->reset_pin_->digital_write(true);
27 }
28
29 if (this->x_raw_max_ == this->x_raw_min_) {
30 this->x_raw_max_ = this->display_->get_native_width();
31 }
32 if (this->y_raw_max_ == this->y_raw_min_) {
33 this->y_raw_max_ = this->display_->get_native_height();
34 }
35}
36
38 uint8_t buf[MAX_TOUCHES * POINT_SIZE] = {0};
39 uint8_t mask[1] = {0x00};
40
41 if (this->read_register16(REG_TOUCH_NUM, buf, 1) != i2c::ERROR_OK) {
42 this->status_set_warning();
43 this->skip_update_ = true;
44 ESP_LOGW(TAG, "Failed to read touch count");
45 return;
46 }
47 uint8_t touch_count = buf[0];
48
49 if (touch_count == 0x00 || touch_count > MAX_TOUCHES) { // No new touch
51 return;
52 }
53 if (this->read_register16(REG_POINT1, buf, touch_count * POINT_SIZE) != i2c::ERROR_OK) {
54 this->status_set_warning();
55 this->skip_update_ = true;
56 ESP_LOGW(TAG, "Failed to read touch points");
57 return;
58 }
59 this->write_register16(REG_TOUCH_NUM, mask, 1);
60 ESP_LOGV(TAG, "Touch count: %d", touch_count);
62
63 for (uint8_t i = 0; i < touch_count; i++) {
64 uint16_t x = ((uint16_t) buf[2 + 7 * i] << 8) + buf[1 + 7 * i];
65 uint16_t y = ((uint16_t) buf[4 + 7 * i] << 8) + buf[3 + 7 * i];
66 uint8_t pressure = buf[5 + 7 * i];
67 uint8_t touch_id = buf[6 + 7 * i];
68
69 // A zero-pressure report just means this point is no longer touched; skipping it here leaves is_touched_
70 // false (when no other point is active) so send_touches_() reports the release as normal.
71 if (pressure != 0) {
72 this->add_raw_touch_position_(touch_id, x, y, pressure);
73 }
74 }
75}
76
78 ESP_LOGCONFIG(TAG, "icnt86 Touchscreen:");
79 LOG_I2C_DEVICE(this);
80 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
81 LOG_PIN(" Reset Pin: ", this->reset_pin_);
82}
83
84} // namespace esphome::icnt86
void status_clear_warning()
Definition component.h:291
virtual void setup()=0
virtual void digital_write(bool value)=0
int get_native_width()
Get the native (original) width of the display in pixels.
Definition display.h:338
int get_native_height()
Get the native (original) height of the display in pixels.
Definition display.h:340
ErrorCode write_register16(uint16_t a_register, const uint8_t *data, size_t len) const
write an array of bytes to a specific register in the I²C device
Definition i2c.cpp:43
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:29
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:15
ESPHOME_LOG_TAG(TAG, "icnt86")
void HOT delay(uint32_t ms)
Definition hal.cpp:85
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