9 static const char *
const TAG =
"gt911.touchscreen";
11 static const uint8_t GET_TOUCH_STATE[2] = {0x81, 0x4E};
12 static const uint8_t CLEAR_TOUCH_STATE[3] = {0x81, 0x4E, 0x00};
13 static const uint8_t GET_TOUCHES[2] = {0x81, 0x4F};
14 static const uint8_t GET_SWITCHES[2] = {0x80, 0x4D};
15 static const uint8_t GET_MAX_VALUES[2] = {0x80, 0x48};
16 static const size_t MAX_TOUCHES = 5;
17 static const size_t MAX_BUTTONS = 4;
19 #define ERROR_CHECK(err) \ 20 if ((err) != i2c::ERROR_OK) { \ 21 ESP_LOGE(TAG, "Failed to communicate!"); \ 22 this->status_set_warning(); \ 28 ESP_LOGCONFIG(TAG,
"Setting up GT911 Touchscreen...");
49 err = this->
write(GET_SWITCHES, 2);
51 err = this->
read(data, 1);
53 ESP_LOGD(TAG,
"Read from switches: 0x%02X", data[0]);
64 err = this->
write(GET_MAX_VALUES, 2);
66 err = this->
read(data,
sizeof(data));
79 ESP_LOGE(TAG,
"Failed to communicate!");
84 ESP_LOGCONFIG(TAG,
"GT911 Touchscreen setup complete");
89 uint8_t touch_state = 0;
90 uint8_t data[MAX_TOUCHES + 1][8];
92 err = this->
write(GET_TOUCH_STATE,
sizeof(GET_TOUCH_STATE),
false);
94 err = this->
read(&touch_state, 1);
96 this->
write(CLEAR_TOUCH_STATE,
sizeof(CLEAR_TOUCH_STATE));
97 uint8_t num_of_touches = touch_state & 0x07;
99 if ((touch_state & 0x80) == 0 || num_of_touches > MAX_TOUCHES) {
104 err = this->
write(GET_TOUCHES,
sizeof(GET_TOUCHES),
false);
107 err = this->
read(data[0],
sizeof(data[0]) * num_of_touches + 1);
110 for (uint8_t i = 0; i != num_of_touches; i++) {
111 uint16_t
id = data[i][0];
116 auto keys = data[num_of_touches][0] & ((1 << MAX_BUTTONS) - 1);
119 for (
size_t i = 0; i != MAX_BUTTONS; i++) {
121 listener->update_button(i, (keys & (1 << i)) != 0);
127 ESP_LOGCONFIG(TAG,
"GT911 Touchscreen:");
128 LOG_I2C_DEVICE(
this);
virtual void digital_write(bool value)=0
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
virtual void pin_mode(gpio::Flags flags)=0
void update_touches() override
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
InternalGPIOPin * interrupt_pin_
std::vector< GT911ButtonListener * > button_listeners_
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.
No error found during execution of method.
void dump_config() override
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
virtual void mark_failed()
Mark this component as failed.
void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw=0)
Implementation of SPI Controller mode.
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
void IRAM_ATTR HOT delay(uint32_t ms)