8static const uint32_t CST328_BEFORE_RESET_TIMEOUT = 50;
9static const uint32_t CST328_TRANSITION_TIMEOUT = 300;
10static const uint16_t CST328_FW_CRC = 0xCACA;
11static const uint8_t CST328_SYNC_BYTE = 0xAB;
13static const uint8_t ZERO_BYTE = 0;
15#define I2C_WARN_ON_ERROR(x, log_tag, format, ...) \
17 i2c::ErrorCode err_rc_ = (x); \
18 if (err_rc_ != i2c::ERROR_OK) { \
19 ESP_LOGW(log_tag, "%s(%d): [error %d] " format, __FUNCTION__, __LINE__, err_rc_, ##__VA_ARGS__); \
20 this->status_set_warning(format); \
24#define I2C_FAIL_ON_ERROR(x, log_tag, format, ...) \
26 i2c::ErrorCode err_rc_ = (x); \
27 if (err_rc_ != i2c::ERROR_OK) { \
28 ESP_LOGE(log_tag, "%s(%d): [error %d] " format, __FUNCTION__, __LINE__, err_rc_, ##__VA_ARGS__); \
29 this->mark_failed(); \
35 ESP_LOGCONFIG(TAG,
"Setting up CST328 Touchscreen...");
51 ESP_LOGV(TAG,
"Continuing CST328 setup...");
56 I2C_FAIL_ON_ERROR(this->
write_register16(CST_WM_DEBUG_INFO, buf, 0), TAG,
"Failed to enter debug/info mode");
57 I2C_FAIL_ON_ERROR(this->
read_register16(CST_REG_FW_CRC_AND_BOOT_TIME, buf, 4), TAG,
58 "Failed to read FW CRC and boot time");
60 uint16_t fw_crc = buf[2] + (buf[3] << 8);
61 if (fw_crc != CST328_FW_CRC) {
62 ESP_LOGE(TAG,
"Error: Firmware CRC mismatch, expected 0x%04X but got 0x%04X", CST328_FW_CRC, fw_crc);
67 I2C_FAIL_ON_ERROR(this->
read_register16(CST_REG_CHIP_TYPE_AND_PROJECT_ID, buf, 4), TAG,
68 "Failed to read chip and project ID");
70 this->
chip_id_ = buf[2] + (buf[3] << 8);
72 ESP_LOGD(TAG,
"Chip ID %X, project ID %X", this->
chip_id_, this->project_id_);
73 I2C_FAIL_ON_ERROR(this->
read_register16(CST_REG_FW_REVISION, buf, 4), TAG,
"Failed to read FW version");
88 I2C_WARN_ON_ERROR(this->
write_register16(CST_WM_NORMAL, buf, 0), TAG,
"Failed to enter normal mode");
89 I2C_WARN_ON_ERROR(this->
read_register16(CST_REG_TOUCH_INFORMATION, &data_byte, 1), TAG,
"Failed to read sync");
90 I2C_WARN_ON_ERROR(this->
write_register16(CST_REG_TOUCH_INFORMATION, &CST328_SYNC_BYTE, 1), TAG,
91 "Failed to write sync");
99 ESP_LOGV(TAG,
"CST328 setup complete");
103 ESP_LOGCONFIG(TAG,
"CST328 Touchscreen:");
104 LOG_I2C_DEVICE(
this);
107 ESP_LOGCONFIG(TAG,
" Chip ID: 0x%04X, Project ID: 0x%04X", this->
chip_id_, this->
project_id_);
118 listener->update_button(
state);
128 uint8_t touch_data[CST328_TOUCH_DATA_SIZE];
133 ESP_LOGW(TAG,
"Failed to read touch data");
139 uint8_t touch_cnt = touch_data[CST_REG_FINGER_COUNT_IDX] & 0x0F;
140 if (touch_cnt == 0 || touch_cnt > CST328_TOUCH_MAX_POINTS) {
145 uint8_t data_idx = 0;
146 for (uint8_t i = 0; i < touch_cnt; i++) {
147 uint8_t
id = touch_data[data_idx] >> 4;
148 int16_t
x = (touch_data[data_idx + 1] << 4) | ((touch_data[data_idx + 3] >> 4) & 0x0F);
149 int16_t
y = (touch_data[data_idx + 2] << 4) | (touch_data[data_idx + 3] & 0x0F);
150 int16_t
z = touch_data[data_idx + 4];
153 data_idx += (i == 0) ? 7 : 5;
157 bool cleanup_error =
false;
162 ESP_LOGW(TAG,
"Failed to clean up touch registers");
void mark_failed()
Mark this component as failed.
void status_set_warning()
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
void status_clear_warning()
virtual void digital_write(bool value)=0
void dump_config() override
InternalGPIOPin * interrupt_pin_
void update_button_state_(bool state)
void update_touches() override
std::vector< CST328ButtonListener * > button_listeners_
int get_native_width()
Get the native (original) width of the display in pixels.
int get_native_height()
Get the native (original) height of the display in pixels.
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
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
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.
display::Display * display_
void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw=0)
ESPHOME_LOG_TAG(TAG, "cst328.binary_sensor")
@ ERROR_OK
No error found during execution of method.
ESPHOME_ALWAYS_INLINE void pulse_reset(GPIOPin *pin, ResetPulse pulse)
Set up an active-low reset pin and drive it through pulse.