ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
gsl3670_touchscreen.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
5
6namespace esphome::gsl3670 {
7
8ESPHOME_LOG_TAG(TAG, "gsl3670.touchscreen");
9static const size_t MAX_TOUCHES = 3;
10// ---------------------------------------------------------------------------
11// setup() – mirrors esp_lcd_touch_gsl3670_init() in the Seeed BSP:
12// clear_reg → reset → load_fw → startup_chip → reset → startup_chip
13// ---------------------------------------------------------------------------
15 ESP_LOGCONFIG(TAG, "Setting up GSL3670 touchscreen...");
16
17 setup_output_pin(this->reset_pin_, true);
18
19 if (this->interrupt_pin_ != nullptr) {
20 this->interrupt_pin_->setup();
21 this->attach_interrupt_(this->interrupt_pin_, gpio::INTERRUPT_FALLING_EDGE);
22 }
23
24 if (this->x_raw_max_ == this->x_raw_min_) {
25 this->x_raw_max_ = this->display_->get_native_width();
26 }
27 if (this->y_raw_max_ == this->y_raw_min_) {
28 this->y_raw_max_ = this->display_->get_native_height();
29 }
30
31 this->clear_reg_();
32 this->reset_();
33 this->load_firmware_();
34 this->startup_chip_();
35 this->reset_();
36 this->startup_chip_();
37
38 ESP_LOGCONFIG(TAG, "GSL3670 initialised OK");
39}
40
42 ESP_LOGCONFIG(TAG,
43 "GSL3670 Touchscreen:\n"
44 " X-raw-max: %d\n"
45 " Y-raw-max: %d\n",
46 this->x_raw_max_, this->y_raw_max_);
47 LOG_I2C_DEVICE(this);
48 LOG_PIN(" Reset Pin: ", this->reset_pin_);
49 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
50 ESP_LOGCONFIG(TAG, " Firmware records: %zu", this->firmware_len_);
51}
52
53// ---------------------------------------------------------------------------
54// update_touches() – mirrors esp_lcd_touch_gsl3670_read_data() in Seeed BSP
55// ---------------------------------------------------------------------------
57 uint8_t buf[44] = {};
58 auto err = this->read_register(0x80, buf, sizeof(buf));
59 if (err != i2c::ERROR_OK) {
60 ESP_LOGW(TAG, "I2C read failed (%d)", err);
61 return;
62 }
63 uint8_t finger_num = clamp_at_most(buf[0], MAX_TOUCHES);
64
65 // Build gsl_touch_info exactly as the Seeed driver does
66 for (uint8_t j = 0; j != finger_num; j++) {
67 // Record: y_lo, y_hi, x_lo, then the id (high nibble) and the top 4 bits of X (low nibble)
68 const uint8_t *record = &buf[(j + 1) * 4];
69 auto x = encode_uint16(low_nibble(record[3]), record[2]);
70 auto y = encode_uint16(record[1], record[0]);
71 auto id = high_nibble(record[3]);
72 ESP_LOGV(TAG, "Touch id=%u, x=%u y=%u", id, x, y);
73 if (x <= 8192 && y <= 8192)
74 this->add_raw_touch_position_(id, x, y);
75 }
76}
77
78// ---------------------------------------------------------------------------
79// clear_reg_() – mirrors esp_lcd_touch_gsl3670_clear_reg()
80// GPIO reset → write 0x01 to 0x88 → write 0x04 to 0xe4 → write 0x00 to 0xe0
81// ---------------------------------------------------------------------------
82void GSL3670Touchscreen::clear_reg_() {
83 ESP_LOGD(TAG, "clear_reg");
84
85 pulse_reset(this->reset_pin_, {.low_ms = 1, .high_after_ms = 5});
86
87 this->write_reg8_(0x88, 0x01);
88 // delay(5);
89 this->write_reg8_(0xe4, 0x04);
90 // delay(5);
91 this->write_reg8_(0xe0, 0x00);
92 // delay(5);
93}
94
95// ---------------------------------------------------------------------------
96// reset_() – mirrors touch_gsl3670_reset()
97// GPIO reset → write 0x04 to 0xe4 → write 4×0x00 to 0xbc
98// ---------------------------------------------------------------------------
99void GSL3670Touchscreen::reset_() {
100 ESP_LOGD(TAG, "reset");
101
102 pulse_reset(this->reset_pin_, {.low_ms = 1, .high_after_ms = 5});
103
104 this->write_reg8_(0xe4, 0x04);
105
106 uint8_t zeros[4] = {0, 0, 0, 0};
107 this->write_reg_(0xbc, zeros, 4);
108}
109
110void GSL3670Touchscreen::load_firmware_() {
111 if (firmware_ == nullptr || firmware_len_ == 0) {
112 ESP_LOGW(TAG, "No firmware supplied – skipping");
113 return;
114 }
115
116 ESP_LOGD(TAG, "Loading firmware (%zu blocks)...", firmware_len_);
117
118 static constexpr size_t FW_BLK_SIZE = 128 + 4;
119
120 for (size_t i = 0; i != this->firmware_len_; i++) {
121 auto offset = i * FW_BLK_SIZE;
122 uint8_t val = this->firmware_[offset + 0];
123 ESP_LOGV(TAG, "Firmware address 0x%02X", val);
124 this->write_reg_(0xf0, &val, 1);
125 this->write_reg_(0, this->firmware_ + offset + 4, 128);
126 }
127 ESP_LOGD(TAG, "Firmware load complete");
128}
129
130// ---------------------------------------------------------------------------
131// startup_chip_() – mirrors esp_lcd_touch_gsl3670_startup_chip()
132// write 0x00 to 0xe0
133// ---------------------------------------------------------------------------
134void GSL3670Touchscreen::startup_chip_() {
135 ESP_LOGD(TAG, "startup_chip");
136 this->write_reg8_(0xe0, 0x00);
137 delay(5);
138}
139
140// ---------------------------------------------------------------------------
141// I2C helpers
142// ---------------------------------------------------------------------------
143
144bool GSL3670Touchscreen::write_reg_(uint8_t reg, const uint8_t *data, size_t len) {
145 auto err = this->write_register(reg, data, len);
146 if (err != i2c::ERROR_OK) {
147 ESP_LOGW(TAG, "I2C write reg 0x%02X len %zu failed (%d)", reg, len, err);
148 return false;
149 }
150 return true;
151}
152
153bool GSL3670Touchscreen::write_reg8_(uint8_t reg, uint8_t val) { return write_reg_(reg, &val, 1); }
154
155} // namespace esphome::gsl3670
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_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:79
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
ErrorCode read_register(uint8_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:58
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)
mopeka_std_values val[3]
@ INTERRUPT_FALLING_EDGE
Definition gpio_pin.h:53
ESPHOME_LOG_TAG(TAG, "gsl3670.touchscreen")
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
T clamp_at_most(T value, U max)
Definition helpers.h:2859
const void size_t len
Definition hal.h:68
constexpr uint8_t high_nibble(uint8_t byte)
The high 4 bits of a byte, e.g. high_nibble(0xA5) == 0xA.
Definition helpers.h:1217
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 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
void HOT delay(uint32_t ms)
Definition hal.cpp:78
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
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6