ESPHome 2025.6.3
Loading...
Searching...
No Matches
gt911_touchscreen.cpp
Go to the documentation of this file.
1#include "gt911_touchscreen.h"
2
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace gt911 {
8
9static const char *const TAG = "gt911.touchscreen";
10
11static const uint8_t GET_TOUCH_STATE[2] = {0x81, 0x4E};
12static const uint8_t CLEAR_TOUCH_STATE[3] = {0x81, 0x4E, 0x00};
13static const uint8_t GET_TOUCHES[2] = {0x81, 0x4F};
14static const uint8_t GET_SWITCHES[2] = {0x80, 0x4D};
15static const uint8_t GET_MAX_VALUES[2] = {0x80, 0x48};
16static const size_t MAX_TOUCHES = 5; // max number of possible touches reported
17static const size_t MAX_BUTTONS = 4; // max number of buttons scanned
18
19#define ERROR_CHECK(err) \
20 if ((err) != i2c::ERROR_OK) { \
21 ESP_LOGE(TAG, "Failed to communicate!"); \
22 this->status_set_warning(); \
23 return; \
24 }
25
28 ESP_LOGCONFIG(TAG, "Running setup");
29 if (this->reset_pin_ != nullptr) {
30 this->reset_pin_->setup();
31 this->reset_pin_->digital_write(false);
32 if (this->interrupt_pin_ != nullptr) {
33 // The interrupt pin is used as an input during reset to select the I2C address.
35 this->interrupt_pin_->setup();
36 this->interrupt_pin_->digital_write(false);
37 }
38 delay(2);
39 this->reset_pin_->digital_write(true);
40 delay(50); // NOLINT
41 if (this->interrupt_pin_ != nullptr) {
43 this->interrupt_pin_->setup();
44 }
45 }
46
47 // check the configuration of the int line.
48 uint8_t data[4];
49 err = this->write(GET_SWITCHES, 2);
50 if (err == i2c::ERROR_OK) {
51 err = this->read(data, 1);
52 if (err == i2c::ERROR_OK) {
53 ESP_LOGD(TAG, "Read from switches: 0x%02X", data[0]);
54 if (this->interrupt_pin_ != nullptr) {
55 // datasheet says NOT to use pullup/down on the int line.
57 this->interrupt_pin_->setup();
60 }
61 }
62 }
63 if (this->x_raw_max_ == 0 || this->y_raw_max_ == 0) {
64 // no calibration? Attempt to read the max values from the touchscreen.
65 if (err == i2c::ERROR_OK) {
66 err = this->write(GET_MAX_VALUES, 2);
67 if (err == i2c::ERROR_OK) {
68 err = this->read(data, sizeof(data));
69 if (err == i2c::ERROR_OK) {
70 this->x_raw_max_ = encode_uint16(data[1], data[0]);
71 this->y_raw_max_ = encode_uint16(data[3], data[2]);
72 if (this->swap_x_y_)
73 std::swap(this->x_raw_max_, this->y_raw_max_);
74 }
75 }
76 }
77 if (err != i2c::ERROR_OK) {
78 ESP_LOGE(TAG, "Failed to read calibration values from touchscreen!");
79 this->mark_failed();
80 return;
81 }
82 }
83 if (err != i2c::ERROR_OK) {
84 ESP_LOGE(TAG, "Failed to communicate!");
85 this->mark_failed();
86 return;
87 }
88
89 ESP_LOGCONFIG(TAG, "GT911 Touchscreen setup complete");
90}
91
94 uint8_t touch_state = 0;
95 uint8_t data[MAX_TOUCHES + 1][8]; // 8 bytes each for each point, plus extra space for the key byte
96
97 err = this->write(GET_TOUCH_STATE, sizeof(GET_TOUCH_STATE), false);
98 ERROR_CHECK(err);
99 err = this->read(&touch_state, 1);
100 ERROR_CHECK(err);
101 this->write(CLEAR_TOUCH_STATE, sizeof(CLEAR_TOUCH_STATE));
102 uint8_t num_of_touches = touch_state & 0x07;
103
104 if ((touch_state & 0x80) == 0 || num_of_touches > MAX_TOUCHES) {
105 this->skip_update_ = true; // skip send touch events, touchscreen is not ready yet.
106 return;
107 }
108
109 err = this->write(GET_TOUCHES, sizeof(GET_TOUCHES), false);
110 ERROR_CHECK(err);
111 // num_of_touches is guaranteed to be 0..5. Also read the key data
112 err = this->read(data[0], sizeof(data[0]) * num_of_touches + 1);
113 ERROR_CHECK(err);
114
115 for (uint8_t i = 0; i != num_of_touches; i++) {
116 uint16_t id = data[i][0];
117 uint16_t x = encode_uint16(data[i][2], data[i][1]);
118 uint16_t y = encode_uint16(data[i][4], data[i][3]);
119 this->add_raw_touch_position_(id, x, y);
120 }
121 auto keys = data[num_of_touches][0] & ((1 << MAX_BUTTONS) - 1);
122 if (keys != this->button_state_) {
123 this->button_state_ = keys;
124 for (size_t i = 0; i != MAX_BUTTONS; i++) {
125 for (auto *listener : this->button_listeners_)
126 listener->update_button(i, (keys & (1 << i)) != 0);
127 }
128 }
129}
130
132 ESP_LOGCONFIG(TAG, "GT911 Touchscreen:");
133 LOG_I2C_DEVICE(this);
134 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
135}
136
137} // namespace gt911
138} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
virtual void pin_mode(gpio::Flags flags)=0
virtual void setup()=0
virtual void digital_write(bool value)=0
std::vector< GT911ButtonListener * > button_listeners_
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition i2c.h:190
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
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.h:42
@ INTERRUPT_RISING_EDGE
Definition gpio.h:41
@ FLAG_OUTPUT
Definition gpio.h:19
@ FLAG_INPUT
Definition gpio.h:18
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:11
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:192
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6