ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
cst9220_touchscreen.cpp
Go to the documentation of this file.
2
3namespace esphome::cst9220 {
4
5// Id 0 is the base Touchscreen class touch timeout.
6static constexpr uint32_t REREAD_TIMEOUT_ID = 1;
7static constexpr uint32_t REREAD_DELAY_MS = 20;
8
10 pulse_reset(this->reset_pin_, {.high_before_ms = 5, .low_ms = 10});
11 // Wait for the controller to leave its bootloader before talking to it.
12 this->set_timeout(30, [this] { this->continue_setup_(); });
13}
14
17
18 if (!this->enter_command_mode_())
19 return;
20 delay(10);
21 if (!this->check_firmware_())
22 return;
23
24 // Read the panel resolution unless the user supplied calibration values.
25 uint16_t x_max, y_max;
26 if (this->read_words_(cstxxx::REG_RESOLUTION, x_max, y_max)) {
27 if (this->x_raw_max_ == this->x_raw_min_)
28 this->x_raw_max_ = x_max;
29 if (this->y_raw_max_ == this->y_raw_min_)
30 this->y_raw_max_ = y_max;
31 }
32
33 if (!this->setup_check(this->read_words_(cstxxx::REG_CHIP_INFO, this->project_id_, this->chip_id_),
34 LOG_STR("Failed to read chip ID")))
35 return;
36 if (this->chip_id_ != CST9220_CHIP_ID && this->chip_id_ != CST9217_CHIP_ID) {
37 ESP_LOGE(TAG, "Unknown chip ID: 0x%04X", this->chip_id_);
38 this->mark_failed(LOG_STR("Unknown chip ID"));
39 return;
40 }
41
42 // Fall back to the display dimensions if the resolution read failed.
43 if (this->x_raw_max_ == this->x_raw_min_)
44 this->x_raw_max_ = this->display_->get_native_width();
45 if (this->y_raw_max_ == this->y_raw_min_)
46 this->y_raw_max_ = this->display_->get_native_height();
47
48 this->setup_complete_ = true;
49}
50
52 if (!this->setup_complete_)
53 return;
54 uint8_t data[CST9220_DATA_LENGTH];
55 // Only an actual I2C failure should skip the update; a successful read with no
56 // touches is a real "all fingers lifted" state that must flow through so the
57 // base class can generate the release event.
58 if (!this->status_comm_result(this->read_register16(cstxxx::REG_TOUCH_DATA, data, sizeof(data)) == i2c::ERROR_OK)) {
59 this->skip_update_ = true;
60 return;
61 }
62
63 // A valid report carries the marker at offset 6. Writing an acknowledge back makes the controller return
64 // invalid frames during a long press, so none is written. An invalid frame keeps the previous points; while
65 // a finger is down, read again shortly so a lost release frame does not leave the touch held.
66 if (data[cstxxx::SYNC_IDX] != cstxxx::SYNC_BYTE) {
67 this->skip_update_ = true;
68 if (!this->touches_.empty())
69 this->set_timeout(REREAD_TIMEOUT_ID, REREAD_DELAY_MS, [this] { this->store_.touched = true; });
70 return;
71 }
72
73 uint8_t num_touches = data[cstxxx::FINGER_COUNT_IDX] & 0x7F;
74 if (num_touches > CST9220_MAX_TOUCHES)
75 num_touches = CST9220_MAX_TOUCHES;
76
77 for (uint8_t i = 0; i < num_touches; i++) {
78 auto touch = cstxxx::decode_touch(data, i);
79 if (touch.event != TOUCH_EVENT_DOWN)
80 continue;
81 ESP_LOGV(TAG, "Read touch %d: %d/%d", touch.id, touch.x, touch.y);
82 this->add_raw_touch_position_(touch.id, touch.x, touch.y);
83 }
84}
85
87 ESP_LOGCONFIG(TAG,
88 "CST9220 Touchscreen:\n"
89 " Chip ID: 0x%04X\n"
90 " Project ID: 0x%04X\n"
91 " X Raw Min: %d, X Raw Max: %d\n"
92 " Y Raw Min: %d, Y Raw Max: %d",
93 this->chip_id_, this->project_id_, this->x_raw_min_, this->x_raw_max_, this->y_raw_min_,
94 this->y_raw_max_);
95 LOG_I2C_DEVICE(this);
96 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
97 LOG_PIN(" Reset Pin: ", this->reset_pin_);
98}
99
100} // 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.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:236
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
bool ESPHOME_ALWAYS_INLINE read_words_(uint16_t reg, uint16_t &low, uint16_t &high)
Reads two little-endian words starting at reg.
Definition cstxxx.h:68
bool check_firmware_()
Checks the firmware check code, marking the component failed if it is unreadable or wrong.
Definition cstxxx.h:82
bool enter_command_mode_()
Enters command mode, marking the component failed if the write fails.
Definition cstxxx.h:77
void setup_interrupt_pin_(gpio::InterruptType type)
Sets up the interrupt pin, if configured, to fire on type.
Definition cstxxx.h:61
InternalGPIOPin * interrupt_pin_
Definition cstxxx.h:94
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
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
std::map< uint8_t, TouchPoint > touches_
void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw=0)
TouchRecord decode_touch(const uint8_t *report, uint8_t index)
Decodes record index of a touch report.
Definition cstxxx.h:43
@ INTERRUPT_FALLING_EDGE
Definition gpio_pin.h:53
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
void HOT delay(uint32_t ms)
Definition hal.cpp:78
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
static void uint32_t