From 63762993ff028b097b5faf1795587dff80400868 Mon Sep 17 00:00:00 2001 From: tobozo Date: Tue, 11 Jun 2024 15:36:38 +0200 Subject: [PATCH] S3BoxV3::GT911 red circle button support (see #569) --- src/lgfx/v1/touch/Touch_GT911.cpp | 35 +++++++++++++++---- src/lgfx/v1/touch/Touch_GT911.hpp | 3 ++ .../LGFX_AutoDetect_ESP32_all.hpp | 9 ++--- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/lgfx/v1/touch/Touch_GT911.cpp b/src/lgfx/v1/touch/Touch_GT911.cpp index 1e9165c4..481ae3e6 100644 --- a/src/lgfx/v1/touch/Touch_GT911.cpp +++ b/src/lgfx/v1/touch/Touch_GT911.cpp @@ -25,7 +25,8 @@ namespace lgfx inline namespace v1 { //---------------------------------------------------------------------------- - static constexpr uint8_t gt911cmd_getdata[] = { 0x81, 0x4E, 0x00 }; + static constexpr uint8_t gt911cmd_getdata[] = { 0x81, 0x4E, 0x00 }; // read XY + static constexpr uint8_t gt911cmd_getkey[] = { 0x80, 0x93, 0x00 }; // read button static uint8_t calcChecksum(const uint8_t *buf, uint8_t len) { @@ -131,6 +132,21 @@ namespace lgfx _writeBytes(writedata, 3); } + + + void Touch_GT911::_update_keys(void) + { + if ( (_readdata[0] & 0x10) == 0x10) + { + _writeReadBytes(gt911cmd_getkey, 2, &_readdata[0], max_touch_points); + // Serial.printf("%04x: %02x %02x %02x %02x %02x\n" , 0x8093, _readdata[0], _readdata[1], _readdata[2], _readdata[3], _readdata[4]); + _writeBytes(gt911cmd_getkey, 3); // clear + _readdata[0] = 1; + _readdata[2] = _cfg.y_max+20; // assign value outside coords + } + } + + bool Touch_GT911::_update_data(void) { bool res = false; @@ -148,11 +164,18 @@ namespace lgfx _readdata[0] = buf; res = true; } + } else { + if( buf !=0 ) ESP_LOGI("[Touch_GT911]", "buf: 0x%02x", buf); } lgfx::i2c::endTransaction(_cfg.i2c_port).has_value(); + + if(_buttons>0) { + _update_keys(); + } + if (res) { - _writeBytes(gt911cmd_getdata, 3); + _writeBytes(gt911cmd_getdata, 3); // clear } } return res; @@ -161,7 +184,7 @@ namespace lgfx uint_fast8_t Touch_GT911::getTouchRaw(touch_point_t* __restrict tp, uint_fast8_t count) { if (!_inited || count == 0) return 0; - if (count > 5) { count = 5; } + if (count > max_touch_points) { count = max_touch_points; } uint32_t msec = lgfx::millis(); uint32_t diff_msec = msec - _last_update; @@ -201,13 +224,13 @@ namespace lgfx void Touch_GT911::setTouchNums(int_fast8_t nums) { - nums = std::max(1, std::min(5, nums)); + _buttons = std::max(1, std::min(max_touch_points, _buttons)); uint8_t buf[] = { 0x80, 0x4c, 0x00 }; _writeReadBytes(buf, 2, &buf[2], 1); - if (buf[2] != nums) + if (buf[2] != _buttons) { - buf[2] = nums; + buf[2] = _buttons; _writeBytes(buf, 3); _freshConfig(); diff --git a/src/lgfx/v1/touch/Touch_GT911.hpp b/src/lgfx/v1/touch/Touch_GT911.hpp index 7769bb3c..2e3620ec 100644 --- a/src/lgfx/v1/touch/Touch_GT911.hpp +++ b/src/lgfx/v1/touch/Touch_GT911.hpp @@ -62,6 +62,9 @@ namespace lgfx bool _writeBytes(const uint8_t* data, size_t len); bool _writeReadBytes(const uint8_t* write_data, size_t write_len, uint8_t* read_data, size_t read_len); bool _update_data(void); + void _update_keys(void); + + uint8_t _buttons = 0; // touch nums }; //---------------------------------------------------------------------------- diff --git a/src/lgfx/v1_autodetect/LGFX_AutoDetect_ESP32_all.hpp b/src/lgfx/v1_autodetect/LGFX_AutoDetect_ESP32_all.hpp index bd254182..ba3832a3 100644 --- a/src/lgfx/v1_autodetect/LGFX_AutoDetect_ESP32_all.hpp +++ b/src/lgfx/v1_autodetect/LGFX_AutoDetect_ESP32_all.hpp @@ -1315,21 +1315,22 @@ namespace lgfx cfg.pin_int = GPIO_NUM_3; cfg.pin_sda = GPIO_NUM_8; cfg.pin_scl = GPIO_NUM_18; - cfg.i2c_addr = 0x14; + cfg.i2c_addr = t->default_addr_1; // 0x14; cfg.i2c_port = I2C_NUM_0; cfg.x_min = 0; cfg.x_max = 319; cfg.y_min = 0; - // Max-y = 239 + 40 pixels for "red" touch point below LCD active area - cfg.y_max = 279; + cfg.y_max = 239; cfg.offset_rotation = 2; cfg.bus_shared = false; t->config(cfg); if (!t->init()) { - cfg.i2c_addr = 0x5D; // addr change (0x14 or 0x5D) + cfg.i2c_addr = t->default_addr_2; // 0x5D; // addr change (0x14 or 0x5D) t->config(cfg); + //ESP_LOGI(LIBRARY_NAME, "[TouchInit] i2c addr 0x%02x, init: %s", t->default_addr_2, t->init()?"success":"fail"); } + t->setTouchNums(5); // /!\ driver is not multitouch+button ready p->touch(t); } }