ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
cst3240_touchscreen.cpp
Go to the documentation of this file.
3#include "esphome/core/log.h"
4
5#include <utility>
6
7namespace esphome::cst3240 {
8
9ESPHOME_LOG_TAG(TAG, "cst3240.touchscreen");
10
12 if (this->reset_pin_ == nullptr) {
13 this->continue_setup_();
14 return;
15 }
16 pulse_reset(this->reset_pin_, {.high_before_ms = 5, .low_ms = 5});
17 this->set_timeout(30, [this] { this->continue_setup_(); });
18}
19
21 if (!this->enter_command_mode_())
22 return;
23 if (!this->setup_check(this->read_words_(cstxxx::REG_CHIP_INFO, this->project_id_, this->chip_id_),
24 LOG_STR("Failed to read chip ID")))
25 return;
26 // Only warn: the chip ID has not been confirmed on enough boards to fail setup on it
27 if (this->chip_id_ != CST3240_CHIP_ID) {
28 ESP_LOGW(TAG, "Unexpected chip ID 0x%04X", this->chip_id_);
29 }
30 this->read_words_(cstxxx::REG_FW_VERSION, this->fw_build_, this->fw_version_);
31
32 if (this->x_raw_max_ == this->x_raw_min_ || this->y_raw_max_ == this->y_raw_min_) {
33 uint16_t x_max, y_max;
34 if (this->read_words_(cstxxx::REG_RESOLUTION, x_max, y_max)) {
35 this->x_raw_max_ = x_max;
36 this->y_raw_max_ = y_max;
37 if (this->swap_x_y_)
38 std::swap(this->x_raw_max_, this->y_raw_max_);
39 } else {
40 this->x_raw_max_ = this->display_->get_native_width();
41 this->y_raw_max_ = this->display_->get_native_height();
42 }
43 }
44
45 this->status_comm_check(this->write_register16(cstxxx::REG_NORMAL_MODE, nullptr, 0) == i2c::ERROR_OK);
47 this->setup_complete_ = true;
48}
49
51 if (!this->setup_complete_) {
52 this->skip_update_ = true;
53 return;
54 }
55 uint8_t data[CST3240_REPORT_SIZE];
56 if (!this->status_comm_result(this->read_register16(cstxxx::REG_TOUCH_DATA, data, sizeof(data)) == i2c::ERROR_OK)) {
57 this->skip_update_ = true;
58 return;
59 }
60
61 const uint8_t sync = data[cstxxx::SYNC_IDX];
62 if (sync != cstxxx::SYNC_BYTE) {
63 this->skip_update_ = true;
64 if (sync != 0) {
65 ESP_LOGV(TAG, "Skipping report, sync byte 0x%02X", sync);
66 this->clear_report_();
67 }
68 return;
69 }
70
71 const uint8_t count = low_nibble(data[cstxxx::FINGER_COUNT_IDX]);
72 if (count <= CST3240_MAX_TOUCHES) {
73 for (uint8_t i = 0; i < count; i++) {
74 auto touch = cstxxx::decode_touch(data, i);
75 // A released finger is still listed once, with both press bits clear
76 if ((touch.event & TOUCH_EVENT_PRESSED) == 0)
77 continue;
78 this->add_raw_touch_position_(touch.id, touch.x, touch.y, touch.z);
79 }
80 }
81
82 if (this->interrupt_pin_ == nullptr) {
83 this->clear_report_();
84 } else if (!this->write_byte_reg16(cstxxx::REG_FINGER_COUNT, 0) ||
85 !this->write_byte_reg16(cstxxx::REG_TOUCH_DATA, cstxxx::SYNC_BYTE)) {
86 ESP_LOGW(TAG, "Failed to acknowledge the report");
87 }
88}
89
91 const uint8_t zeros[CST3240_REPORT_SIZE]{};
92 if (this->write_register16(cstxxx::REG_TOUCH_DATA, zeros, sizeof(zeros)) != i2c::ERROR_OK) {
93 ESP_LOGW(TAG, "Failed to clear the report");
94 }
95}
96
98 ESP_LOGCONFIG(TAG,
99 "CST3240 Touchscreen:\n"
100 " Chip ID: 0x%04X, Project ID: 0x%04X\n"
101 " FW version: %d.%d.%d\n"
102 " X/Y resolution: %d/%d",
103 this->chip_id_, this->project_id_, this->fw_version_ >> 8, this->fw_version_ & 0xFF, this->fw_build_,
104 this->x_raw_max_, this->y_raw_max_);
105 LOG_I2C_DEVICE(this);
106 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
107 LOG_PIN(" Reset Pin: ", this->reset_pin_);
108}
109
110} // namespace esphome::cst3240
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
bool status_comm_check(bool ok)
Sets the warning ("Communication failed") when not ok but never clears it; returns ok.
Definition component.h:309
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void clear_report_()
Zeroes the touch report block, which polling relies on to tell new reports from old.
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 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 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:83
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
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
void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw=0)
unsigned count
Definition gif.cpp:51
ESPHOME_LOG_TAG(TAG, "cst3240.touchscreen")
TouchRecord decode_touch(const uint8_t *report, uint8_t index)
Decodes record index of a touch report.
Definition cstxxx.h:43
@ INTERRUPT_RISING_EDGE
Definition gpio_pin.h:52
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
constexpr uint8_t low_nibble(uint8_t byte)
The low 4 bits of a byte, e.g. low_nibble(0xA5) == 0x5.
Definition helpers.h:1219
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 sync
Definition sun_gtil2.cpp:0