ESPHome 2026.11.0b6
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_register16(ST7123_REG_STATUS, &status, 1) != i2c::ERROR_OK) {
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_register16(ST7123_REG_MAX_TOUCHES, &data, 1) == i2c::ERROR_OK && data != 0 &&
44 data <= ST7123_MAX_TOUCHES) {
45 this->max_touches_ = data;
46 }
47
48 // If no calibration was supplied, read the native coordinate resolution from the controller.
49 if (this->x_raw_max_ == this->x_raw_min_ || this->y_raw_max_ == this->y_raw_min_) {
50 uint8_t res[4];
51 if (this->read_register16(ST7123_REG_MAX_X, res, sizeof(res)) == i2c::ERROR_OK) {
52 this->x_raw_max_ = encode_uint16(res[0] & ST7123_COORD_HIGH_MASK, res[1]);
53 this->y_raw_max_ = encode_uint16(res[2] & ST7123_COORD_HIGH_MASK, res[3]);
54 if (this->swap_x_y_)
55 std::swap(this->x_raw_max_, this->y_raw_max_);
56 } else {
57 this->mark_failed(LOG_STR("Failed to read calibration"));
58 return;
59 }
60 ESP_LOGD(TAG, "Read dimensions %d/%d", this->x_raw_max_, this->y_raw_max_);
61 }
62 this->setup_time_ = 0; // flag setup complete
63 }
64 Touchscreen::update();
65}
66
68 // Read the reporting table from the advanced touch info register through the last touch point.
69 // Reading from this register also clears the INT pin so the controller can report the next frame.
70 uint8_t data[(ST7123_REG_TOUCH_DATA - ST7123_REG_ADV_TOUCH_INFO) + ST7123_MAX_TOUCHES * ST7123_TOUCH_STRIDE];
71 const size_t len = (ST7123_REG_TOUCH_DATA - ST7123_REG_ADV_TOUCH_INFO) + this->max_touches_ * ST7123_TOUCH_STRIDE;
72 if (!this->status_comm_result(this->read_register16(ST7123_REG_ADV_TOUCH_INFO, data, len) == i2c::ERROR_OK)) {
73 this->skip_update_ = true;
74 return;
75 }
76
77 const uint8_t *points = data + (ST7123_REG_TOUCH_DATA - ST7123_REG_ADV_TOUCH_INFO);
78 for (uint8_t i = 0; i != this->max_touches_; i++) {
79 const uint8_t *p = points + i * ST7123_TOUCH_STRIDE;
80 if ((p[0] & ST7123_TOUCH_VALID) == 0)
81 continue;
82 uint16_t x = encode_uint16(p[0] & ST7123_COORD_HIGH_MASK, p[1]);
83 uint16_t y = encode_uint16(p[2] & ST7123_COORD_HIGH_MASK, p[3]);
84 uint8_t intensity = p[5];
85 ESP_LOGV(TAG, "Touch %u: x=%u, y=%u, intensity=%u", i, x, y, intensity);
86 this->add_raw_touch_position_(i, x, y, intensity);
87 }
88}
89
91 ESP_LOGCONFIG(TAG,
92 "ST7123 Touchscreen:\n"
93 " Max touches: %u\n"
94 " X Raw Min: %d, X Raw Max: %d\n"
95 " Y Raw Min: %d, Y Raw Max: %d",
96 this->max_touches_, this->x_raw_min_, this->x_raw_max_, this->y_raw_min_, this->y_raw_max_);
97 LOG_I2C_DEVICE(this);
98 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
99 LOG_PIN(" Reset Pin: ", this->reset_pin_);
100}
101
102} // 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.
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:49
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:15
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:1043
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:163
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6