ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
cst328_touchscreen.cpp
Go to the documentation of this file.
3#include "esphome/core/log.h"
4
5namespace esphome::cst328 {
6
7ESPHOME_LOG_TAG(TAG, "cst328.touchscreen");
8
9static constexpr uint32_t CST328_BEFORE_RESET_TIMEOUT = 50; // 50 ms from datasheet
10static constexpr uint32_t CST328_TRANSITION_TIMEOUT = 300; // 200 ms from datasheet, but typically much less
11
13 ESP_LOGCONFIG(TAG, "Setting up CST328 Touchscreen...");
14#ifdef CST328_BUTTON_COUNT
15 for (auto *button : this->buttons_)
16 button->publish_initial_state(false);
17#endif
18 if (this->reset_pin_ != nullptr) {
19 setup_output_pin(this->reset_pin_, true);
20 this->set_timeout(CST328_BEFORE_RESET_TIMEOUT, [this] { this->reset_device_(); });
21 } else {
22 this->continue_setup_();
23 }
24}
25
27 pulse_reset(this->reset_pin_, {.low_ms = 5});
28 this->set_timeout(CST328_TRANSITION_TIMEOUT, [this] { this->continue_setup_(); });
29}
30
32 ESP_LOGV(TAG, "Continuing CST328 setup...");
33
34 if (!this->enter_command_mode_() || !this->check_firmware_())
35 return;
36 if (!this->setup_check(this->read_words_(cstxxx::REG_CHIP_INFO, this->project_id_, this->chip_id_),
37 LOG_STR("Failed to read chip and project ID")))
38 return;
39 ESP_LOGD(TAG, "Chip ID %X, project ID %X", this->chip_id_, this->project_id_);
40 uint16_t fw_version;
41 if (!this->setup_check(this->read_words_(cstxxx::REG_FW_VERSION, this->fw_build_, fw_version),
42 LOG_STR("Failed to read FW version")))
43 return;
44 this->fw_ver_major_ = fw_version >> 8;
45 this->fw_ver_minor_ = fw_version & 0xFF;
46 ESP_LOGV(TAG, "FW version %d.%d.%d", this->fw_ver_major_, this->fw_ver_minor_, this->fw_build_);
47
48 uint16_t x_max, y_max;
49 if (this->read_words_(cstxxx::REG_RESOLUTION, x_max, y_max)) {
50 this->x_raw_max_ = x_max;
51 this->y_raw_max_ = y_max;
52 } else {
53 this->x_raw_max_ = this->display_->get_native_width();
54 this->y_raw_max_ = this->display_->get_native_height();
55 }
56
57 uint8_t sync;
58 bool ok = this->write_register16(cstxxx::REG_NORMAL_MODE, nullptr, 0) == i2c::ERROR_OK;
59 ok &= this->read_byte_reg16(cstxxx::REG_TOUCH_DATA, &sync);
60 ok &= this->write_byte_reg16(cstxxx::REG_TOUCH_DATA, cstxxx::SYNC_BYTE);
61 this->status_comm_check(ok);
62
64
65 this->setup_complete_ = true;
66 ESP_LOGV(TAG, "CST328 setup complete");
67}
68
70 ESP_LOGCONFIG(TAG, "CST328 Touchscreen:");
71 LOG_I2C_DEVICE(this);
72 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
73 LOG_PIN(" Reset Pin: ", this->reset_pin_);
74 ESP_LOGCONFIG(TAG, " Chip ID: 0x%04X, Project ID: 0x%04X", this->chip_id_, this->project_id_);
75 ESP_LOGCONFIG(TAG, " FW version: %d.%d.%d", this->fw_ver_major_, this->fw_ver_minor_, this->fw_build_);
76 ESP_LOGCONFIG(TAG, " X/Y resolution: %d/%d", this->x_raw_max_, this->y_raw_max_);
77#ifdef CST328_BUTTON_COUNT
78 for (auto *button : this->buttons_)
79 LOG_BINARY_SENSOR(" ", "Button", button);
80#endif
81}
82
84#ifdef CST328_BUTTON_COUNT
85 if (this->button_touched_ == state) {
86 return;
87 }
88 this->button_touched_ = state;
89 for (auto *button : this->buttons_) {
90 button->publish_state(state);
91 }
92#endif
93}
94
96 if (!this->setup_complete_) {
97 this->skip_update_ = true;
98 return;
99 }
100
101 uint8_t touch_data[CST328_TOUCH_DATA_SIZE];
102
103 this->status_clear_warning();
104
105 if (i2c::ERROR_OK != this->read_register16(CST_REG_TOUCH_INFORMATION, touch_data, CST328_TOUCH_DATA_SIZE)) {
106 ESP_LOGW(TAG, "Failed to read touch data");
107 this->status_set_warning();
108 this->skip_update_ = true;
109 return;
110 }
111
112 uint8_t touch_cnt = low_nibble(touch_data[CST_REG_FINGER_COUNT_IDX]);
113 if (touch_cnt == 0 || touch_cnt > CST328_TOUCH_MAX_POINTS) {
114 this->update_button_state_(false);
115 } else {
116 this->update_button_state_(true);
117
118 for (uint8_t i = 0; i < touch_cnt; i++) {
119 auto touch = cstxxx::decode_touch(touch_data, i);
120 this->add_raw_touch_position_(touch.id, touch.x, touch.y, touch.z);
121 }
122 }
123
124 bool cleanup_error = false;
125 cleanup_error |= !this->write_byte_reg16(cstxxx::REG_FINGER_COUNT, 0);
126 cleanup_error |= !this->write_byte_reg16(cstxxx::REG_TOUCH_DATA, cstxxx::SYNC_BYTE);
127
128 if (cleanup_error) {
129 ESP_LOGW(TAG, "Failed to clean up touch registers");
130 }
131}
132
133} // namespace esphome::cst328
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 status_clear_warning()
Definition component.h:318
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
StaticVector< binary_sensor::BinarySensor *, CST328_BUTTON_COUNT > buttons_
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 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
bool read_byte_reg16(uint16_t a_register, uint8_t *data)
Read one byte from a register with a 16-bit register address.
Definition i2c.h:325
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)
bool state
Definition fan.h:2
ESPHOME_LOG_TAG(TAG, "cst328.touchscreen")
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 setup_output_pin(GPIOPin *pin, bool level)
Set up pin and drive it to level. Does nothing when pin is null, so an optional pin needs no check.
Definition gpio.cpp:44
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
static void uint32_t
uint16_t sync
Definition sun_gtil2.cpp:0