9static const char *
const TAG =
"gt911.touchscreen";
11static const uint8_t PRIMARY_ADDRESS = 0x5D;
12static const uint8_t SECONDARY_ADDRESS = 0x14;
13static const uint8_t GET_TOUCH_STATE[2] = {0x81, 0x4E};
14static const uint8_t CLEAR_TOUCH_STATE[3] = {0x81, 0x4E, 0x00};
15static const uint8_t GET_TOUCHES[2] = {0x81, 0x4F};
16static const uint8_t GET_SWITCHES[2] = {0x80, 0x4D};
17static const uint8_t GET_MAX_VALUES[2] = {0x80, 0x48};
18static const size_t MAX_TOUCHES = 5;
19static const size_t MAX_BUTTONS = 4;
21#define ERROR_CHECK(err) \
22 if ((err) != i2c::ERROR_OK) { \
23 this->status_set_warning(ESP_LOG_MSG_COMM_FAIL); \
55 err = this->
write(GET_SWITCHES,
sizeof(GET_SWITCHES));
58 err = this->
read(data, 1);
60 ESP_LOGD(TAG,
"Switches ADDR: 0x%02X DATA: 0x%02X", this->
address_, data[0]);
70 err = this->
write(GET_MAX_VALUES,
sizeof(GET_MAX_VALUES));
72 err = this->
read(data,
sizeof(data));
101 uint8_t touch_state = 0;
102 uint8_t data[MAX_TOUCHES + 1][8];
104 err = this->
write(GET_TOUCH_STATE,
sizeof(GET_TOUCH_STATE));
106 err = this->
read(&touch_state, 1);
108 this->
write(CLEAR_TOUCH_STATE,
sizeof(CLEAR_TOUCH_STATE));
109 uint8_t num_of_touches = touch_state & 0x07;
111 if ((touch_state & 0x80) == 0 || num_of_touches > MAX_TOUCHES) {
115 err = this->
write(GET_TOUCHES,
sizeof(GET_TOUCHES));
118 err = this->
read(data[0],
sizeof(data[0]) * num_of_touches + 1);
122 for (uint8_t i = 0; i != num_of_touches; i++) {
123 uint16_t
id = data[i][0];
128 auto keys = data[num_of_touches][0] & ((1 << MAX_BUTTONS) - 1);
131 for (
size_t i = 0; i != MAX_BUTTONS; i++) {
133 listener->update_button(i, (keys & (1 << i)) != 0);
139 ESP_LOGCONFIG(TAG,
"GT911 Touchscreen:");
140 LOG_I2C_DEVICE(
this);
virtual void mark_failed()
Mark this component as failed.
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
virtual void pin_mode(gpio::Flags flags)=0
virtual void digital_write(bool value)=0
void dump_config() override
InternalGPIOPin * interrupt_pin_
void setup_internal_()
Perform the internal setup routine for the GT911 touchscreen.
void update_touches() override
void setup() override
Initialize the GT911 touchscreen.
std::vector< GT911ButtonListener * > button_listeners_
bool setup_done_
True if the touchscreen setup has completed successfully.
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
uint8_t address_
store the address of the device on the bus
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
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)
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
@ ERROR_OK
No error found during execution of method.
Providing packet encoding functions for exchanging data with a remote host.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
void IRAM_ATTR HOT delay(uint32_t ms)