ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
cst9220_touchscreen.cpp
Go to the documentation of this file.
3
4#include <cinttypes>
5
6namespace esphome::cst9220 {
7
8// Id 0 is the base Touchscreen class touch timeout.
9static constexpr uint32_t REREAD_TIMEOUT_ID = 1;
10static constexpr uint32_t REREAD_DELAY_MS = 20;
11
13 pulse_reset(this->reset_pin_, {.high_before_ms = 5, .low_ms = 10});
14 // Wait for the controller to leave its bootloader before talking to it.
15 this->set_timeout(30, [this] { this->continue_setup_(); });
16}
17
19 uint8_t buffer[4];
20
21 if (this->interrupt_pin_ != nullptr) {
22 this->interrupt_pin_->setup();
24 }
25
26 // Enter command mode so the configuration registers can be read.
27 if (this->write_register16(REG_CMD_MODE, buffer, 0) != i2c::ERROR_OK) {
28 this->status_set_error(LOG_STR("Failed to enter command mode"));
29 this->mark_failed();
30 return;
31 }
32 delay(10);
33
34 // The firmware check code confirms that valid firmware is loaded.
35 if (this->read_register16(REG_CHECKCODE, buffer, 4) != i2c::ERROR_OK) {
36 this->status_set_error(LOG_STR("Failed to read check code"));
37 this->mark_failed();
38 return;
39 }
40 uint32_t checkcode = encode_uint32(buffer[3], buffer[2], buffer[1], buffer[0]);
41 if ((checkcode & 0xFFFF0000) != 0xCACA0000) {
42 ESP_LOGE(TAG, "Invalid firmware check code: 0x%08" PRIX32, checkcode);
43 this->status_set_error(LOG_STR("Invalid firmware check code"));
44 this->mark_failed();
45 return;
46 }
47
48 // Read the panel resolution unless the user supplied calibration values.
49 if (this->read_register16(REG_RESOLUTION, buffer, 4) == i2c::ERROR_OK) {
50 if (this->x_raw_max_ == this->x_raw_min_)
52 if (this->y_raw_max_ == this->y_raw_min_)
53 this->y_raw_max_ = encode_value_le<uint16_t>(&buffer[2]);
54 }
55
56 // Read the chip type and project id and validate the controller.
57 if (this->read_register16(REG_CHIP_INFO, buffer, 4) != i2c::ERROR_OK) {
58 this->status_set_error(LOG_STR("Failed to read chip ID"));
59 this->mark_failed();
60 return;
61 }
62 this->chip_id_ = encode_value_le<uint16_t>(&buffer[2]);
64 if (this->chip_id_ != CST9220_CHIP_ID && this->chip_id_ != CST9217_CHIP_ID) {
65 ESP_LOGE(TAG, "Unknown chip ID: 0x%04X", this->chip_id_);
66 this->status_set_error(LOG_STR("Unknown chip ID"));
67 this->mark_failed();
68 return;
69 }
70
71 // Fall back to the display dimensions if the resolution read failed.
72 if (this->x_raw_max_ == this->x_raw_min_)
73 this->x_raw_max_ = this->display_->get_native_width();
74 if (this->y_raw_max_ == this->y_raw_min_)
75 this->y_raw_max_ = this->display_->get_native_height();
76
77 this->setup_complete_ = true;
78}
79
81 if (!this->setup_complete_)
82 return;
83 uint8_t data[CST9220_DATA_LENGTH];
84 // Only an actual I2C failure should skip the update; a successful read with no
85 // touches is a real "all fingers lifted" state that must flow through so the
86 // base class can generate the release event.
87 if (!this->status_comm_result(this->read_register16(REG_TOUCH_DATA, data, sizeof(data)) == i2c::ERROR_OK)) {
88 this->skip_update_ = true;
89 return;
90 }
91
92 // A valid report carries the marker at offset 6. Writing an acknowledge back makes the controller return
93 // invalid frames during a long press, so none is written. An invalid frame keeps the previous points; while
94 // a finger is down, read again shortly so a lost release frame does not leave the touch held.
95 if (data[6] != TOUCH_ACK) {
96 this->skip_update_ = true;
97 if (!this->touches_.empty())
98 this->set_timeout(REREAD_TIMEOUT_ID, REREAD_DELAY_MS, [this] { this->store_.touched = true; });
99 return;
100 }
101
102 uint8_t num_touches = data[5] & 0x7F;
103 if (num_touches > CST9220_MAX_TOUCHES)
104 num_touches = CST9220_MAX_TOUCHES;
105
106 for (uint8_t i = 0; i < num_touches; i++) {
107 // The first point starts at offset 0; subsequent points are offset by the
108 // two status bytes that follow it.
109 const uint8_t *p = data + i * 5 + (i == 0 ? 0 : 2);
110 uint8_t id = p[0] >> 4;
111 uint8_t event = p[0] & 0x0F;
112 if (event != TOUCH_EVENT_DOWN)
113 continue;
114 // p[3] is shared: high nibble holds the X LSBs, low nibble the Y LSBs.
115 uint16_t x = (p[1] << 4) | (p[3] >> 4);
116 uint16_t y = (p[2] << 4) | (p[3] & 0x0F);
117 ESP_LOGV(TAG, "Read touch %d: %d/%d", id, x, y);
118 this->add_raw_touch_position_(id, x, y);
119 }
120}
121
123 ESP_LOGCONFIG(TAG,
124 "CST9220 Touchscreen:\n"
125 " Chip ID: 0x%04X\n"
126 " Project ID: 0x%04X\n"
127 " X Raw Min: %d, X Raw Max: %d\n"
128 " Y Raw Min: %d, Y Raw Max: %d",
129 this->chip_id_, this->project_id_, this->x_raw_min_, this->x_raw_max_, this->y_raw_min_,
130 this->y_raw_max_);
131 LOG_I2C_DEVICE(this);
132 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
133 LOG_PIN(" Reset Pin: ", this->reset_pin_);
134}
135
136} // namespace esphome::cst9220
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 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
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
std::map< uint8_t, TouchPoint > touches_
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)
uint32_t buffer
Definition gif.cpp:50
@ INTERRUPT_FALLING_EDGE
Definition gpio_pin.h:53
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Definition helpers.h:1084
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:1051
void HOT delay(uint32_t ms)
Definition hal.cpp:85
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
static void uint32_t
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6