ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
st7123_touchscreen.cpp
Go to the documentation of this file.
2
4#include "esphome/core/log.h"
5
6namespace esphome::st7123 {
7
8ESPHOME_LOG_TAG(TAG, "st7123.touchscreen");
9
11 if (this->reset_pin_ != nullptr) {
12 // TP_RESX is active low, assert for at least tRSTW (2ms)
13 pulse_reset(this->reset_pin_, {.high_before_ms = 5, .low_ms = 5});
14 // The controller needs up to 20ms to initialize after reset before it can be accessed.
15 this->setup_time_ = millis() + 30;
16 }
17}
18
20 // check if setup is complete
21 if (this->setup_time_ != 0) {
22 if (this->setup_time_ > millis())
23 return;
24
25 uint8_t status;
26 if (!this->read_byte_reg16(ST7123_REG_STATUS, &status)) {
27 this->mark_failed(LOG_STR("Failed to read status register")); // will stop updates
28 return;
29 }
30 if ((status & 0x0F) == ST7123_STATUS_INIT) {
31 ESP_LOGD(TAG, "Controller still initializing");
32 return;
33 }
34 if (this->interrupt_pin_ != nullptr) {
35 this->interrupt_pin_->setup();
36 // INT is held high when idle and pulses low when touch data is ready.
38 }
39
40 ESP_LOGD(TAG, "Status is %X", status);
41
42 uint8_t data;
43 if (this->read_byte_reg16(ST7123_REG_MAX_TOUCHES, &data) && data != 0 && data <= ST7123_MAX_TOUCHES) {
44 this->max_touches_ = data;
45 }
46
47 // If no calibration was supplied, read the native coordinate resolution from the controller.
48 if (this->x_raw_max_ == this->x_raw_min_ || this->y_raw_max_ == this->y_raw_min_) {
49 uint8_t res[4];
50 if (this->read_register16(ST7123_REG_MAX_X, res, sizeof(res)) == i2c::ERROR_OK) {
51 this->x_raw_max_ = encode_uint16(res[0] & ST7123_COORD_HIGH_MASK, res[1]);
52 this->y_raw_max_ = encode_uint16(res[2] & ST7123_COORD_HIGH_MASK, res[3]);
53 if (this->swap_x_y_)
54 std::swap(this->x_raw_max_, this->y_raw_max_);
55 } else {
56 this->mark_failed(LOG_STR("Failed to read calibration"));
57 return;
58 }
59 ESP_LOGD(TAG, "Read dimensions %d/%d", this->x_raw_max_, this->y_raw_max_);
60 }
61 this->setup_time_ = 0; // flag setup complete
62 }
63 Touchscreen::update();
64}
65
67 // Read the reporting table from the advanced touch info register through the last touch point.
68 // Reading from this register also clears the INT pin so the controller can report the next frame.
69 uint8_t data[(ST7123_REG_TOUCH_DATA - ST7123_REG_ADV_TOUCH_INFO) + ST7123_MAX_TOUCHES * ST7123_TOUCH_STRIDE];
70 const size_t len = (ST7123_REG_TOUCH_DATA - ST7123_REG_ADV_TOUCH_INFO) + this->max_touches_ * ST7123_TOUCH_STRIDE;
71 if (!this->status_comm_result(this->read_register16(ST7123_REG_ADV_TOUCH_INFO, data, len) == i2c::ERROR_OK)) {
72 this->skip_update_ = true;
73 return;
74 }
75
76 const uint8_t *points = data + (ST7123_REG_TOUCH_DATA - ST7123_REG_ADV_TOUCH_INFO);
77 for (uint8_t i = 0; i != this->max_touches_; i++) {
78 const uint8_t *p = points + i * ST7123_TOUCH_STRIDE;
79 if ((p[0] & ST7123_TOUCH_VALID) == 0)
80 continue;
81 uint16_t x = encode_uint16(p[0] & ST7123_COORD_HIGH_MASK, p[1]);
82 uint16_t y = encode_uint16(p[2] & ST7123_COORD_HIGH_MASK, p[3]);
83 uint8_t intensity = p[5];
84 ESP_LOGV(TAG, "Touch %u: x=%u, y=%u, intensity=%u", i, x, y, intensity);
85 this->add_raw_touch_position_(i, x, y, intensity);
86 }
87}
88
90 ESP_LOGCONFIG(TAG,
91 "ST7123 Touchscreen:\n"
92 " Max touches: %u\n"
93 " X Raw Min: %d, X Raw Max: %d\n"
94 " Y Raw Min: %d, Y Raw Max: %d",
95 this->max_touches_, this->x_raw_min_, this->x_raw_max_, this->y_raw_min_, this->y_raw_max_);
96 LOG_I2C_DEVICE(this);
97 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
98 LOG_PIN(" Reset Pin: ", this->reset_pin_);
99}
100
101} // namespace esphome::st7123
uint8_t status
Definition bl0942.h:8
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 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 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.
void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw=0)
@ INTERRUPT_FALLING_EDGE
Definition gpio_pin.h:53
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
ESPHOME_LOG_TAG(TAG, "st7123.touchscreen")
const void size_t len
Definition hal.h:68
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1227
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
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 x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6