ESPHome 2026.11.0b6
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 uint8_t mask[1] = {0x00};
35
36 if (this->read_register16(REG_TOUCH_NUM, buf, 1) != i2c::ERROR_OK) {
37 this->status_set_warning();
38 this->skip_update_ = true;
39 ESP_LOGW(TAG, "Failed to read touch count");
40 return;
41 }
42 uint8_t touch_count = buf[0];
43
44 if (touch_count == 0x00 || touch_count > MAX_TOUCHES) { // No new touch
46 return;
47 }
48 if (this->read_register16(REG_POINT1, buf, touch_count * POINT_SIZE) != i2c::ERROR_OK) {
49 this->status_set_warning();
50 this->skip_update_ = true;
51 ESP_LOGW(TAG, "Failed to read touch points");
52 return;
53 }
54 this->write_register16(REG_TOUCH_NUM, mask, 1);
55 ESP_LOGV(TAG, "Touch count: %d", touch_count);
57
58 for (uint8_t i = 0; i < touch_count; i++) {
59 uint16_t x = ((uint16_t) buf[2 + 7 * i] << 8) + buf[1 + 7 * i];
60 uint16_t y = ((uint16_t) buf[4 + 7 * i] << 8) + buf[3 + 7 * i];
61 uint8_t pressure = buf[5 + 7 * i];
62 uint8_t touch_id = buf[6 + 7 * i];
63
64 // A zero-pressure report just means this point is no longer touched; skipping it here leaves is_touched_
65 // false (when no other point is active) so send_touches_() reports the release as normal.
66 if (pressure != 0) {
67 this->add_raw_touch_position_(touch_id, x, y, pressure);
68 }
69 }
70}
71
73 ESP_LOGCONFIG(TAG, "icnt86 Touchscreen:");
74 LOG_I2C_DEVICE(this);
75 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
76 LOG_PIN(" Reset Pin: ", this->reset_pin_);
77}
78
79} // namespace esphome::icnt86
void status_clear_warning()
Definition component.h:309
int get_native_width()
Get the native (original) width of the display in pixels.
Definition display.h:341
int get_native_height()
Get the native (original) height of the display in pixels.
Definition display.h:343
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:63
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:49
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")
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:163
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