9static constexpr uint32_t CST328_BEFORE_RESET_TIMEOUT = 50;
10static constexpr uint32_t CST328_TRANSITION_TIMEOUT = 300;
13 ESP_LOGCONFIG(TAG,
"Setting up CST328 Touchscreen...");
14#ifdef CST328_BUTTON_COUNT
16 button->publish_initial_state(
false);
32 ESP_LOGV(TAG,
"Continuing CST328 setup...");
37 LOG_STR(
"Failed to read chip and project ID")))
42 LOG_STR(
"Failed to read FW version")))
48 uint16_t x_max, y_max;
49 if (this->
read_words_(cstxxx::REG_RESOLUTION, x_max, y_max)) {
66 ESP_LOGV(TAG,
"CST328 setup complete");
70 ESP_LOGCONFIG(TAG,
"CST328 Touchscreen:");
74 ESP_LOGCONFIG(TAG,
" Chip ID: 0x%04X, Project ID: 0x%04X", this->
chip_id_, this->
project_id_);
77#ifdef CST328_BUTTON_COUNT
79 LOG_BINARY_SENSOR(
" ",
"Button", button);
84#ifdef CST328_BUTTON_COUNT
89 for (
auto *button : this->
buttons_) {
90 button->publish_state(
state);
101 uint8_t touch_data[CST328_TOUCH_DATA_SIZE];
106 ESP_LOGW(TAG,
"Failed to read touch data");
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) {
118 for (uint8_t i = 0; i < touch_cnt; i++) {
124 bool cleanup_error =
false;
126 cleanup_error |= !this->
write_byte_reg16(cstxxx::REG_TOUCH_DATA, cstxxx::SYNC_BYTE);
129 ESP_LOGW(TAG,
"Failed to clean up touch registers");
void status_set_warning()
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
bool status_comm_check(bool ok)
Sets the warning ("Communication failed") when not ok but never clears it; returns ok.
void status_clear_warning()
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
void dump_config() override
StaticVector< binary_sensor::BinarySensor *, CST328_BUTTON_COUNT > buttons_
void update_button_state_(bool state)
void update_touches() override
bool ESPHOME_ALWAYS_INLINE read_words_(uint16_t reg, uint16_t &low, uint16_t &high)
Reads two little-endian words starting at reg.
bool check_firmware_()
Checks the firmware check code, marking the component failed if it is unreadable or wrong.
bool enter_command_mode_()
Enters command mode, marking the component failed if the write fails.
void setup_interrupt_pin_(gpio::InterruptType type)
Sets up the interrupt pin, if configured, to fire on type.
InternalGPIOPin * interrupt_pin_
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
bool write_byte_reg16(uint16_t a_register, uint8_t data) const
Write one byte to a register with a 16-bit register address.
bool read_byte_reg16(uint16_t a_register, uint8_t *data)
Read one byte from a register with a 16-bit register address.
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
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.touchscreen")
TouchRecord decode_touch(const uint8_t *report, uint8_t index)
Decodes record index of a touch report.
@ ERROR_OK
No error found during execution of method.
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.
constexpr uint8_t low_nibble(uint8_t byte)
The low 4 bits of a byte, e.g. low_nibble(0xA5) == 0x5.
ESPHOME_ALWAYS_INLINE void pulse_reset(GPIOPin *pin, ResetPulse pulse)
Set up an active-low reset pin and drive it through pulse.