From 67a682080d56ed460246d688c3c2a8ff039bffcb Mon Sep 17 00:00:00 2001 From: Scotty Date: Sun, 22 Mar 2026 09:05:41 +0100 Subject: [PATCH 1/2] Store and use I2C address at which GT911 was found --- src/config/BoardConfig.h | 3 ++- src/hal/TouchInput.cpp | 22 ++++++++++++---------- src/hal/TouchInput.h | 2 ++ src/input/InputManager.cpp | 1 + 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/config/BoardConfig.h b/src/config/BoardConfig.h index 7fcbaa6..69db942 100644 --- a/src/config/BoardConfig.h +++ b/src/config/BoardConfig.h @@ -52,7 +52,8 @@ // --- Touchscreen (GT911 capacitive) --- #define TOUCH_INT 16 // GT911 I2C address: typically 0x5D or 0x14 (depends on INT state at boot) -#define TOUCH_I2C_ADDR 0x5D +#define TOUCH_I2C_ADDR_1 0x5D +#define TOUCH_I2C_ADDR_2 0x14 // --- Trackball --- #define TBALL_UP 3 diff --git a/src/hal/TouchInput.cpp b/src/hal/TouchInput.cpp index 3c7d372..d1480bf 100644 --- a/src/hal/TouchInput.cpp +++ b/src/hal/TouchInput.cpp @@ -9,19 +9,21 @@ bool TouchInput::begin() { pinMode(TOUCH_INT, INPUT); // Try GT911 at both possible addresses - Wire.beginTransmission(TOUCH_I2C_ADDR); + Wire.beginTransmission(TOUCH_I2C_ADDR_1); uint8_t err = Wire.endTransmission(); if (err != 0) { - Wire.beginTransmission(0x14); + Wire.beginTransmission(TOUCH_I2C_ADDR_2); err = Wire.endTransmission(); if (err != 0) { Serial.println("[TOUCH] GT911 not found at 0x5D or 0x14"); return false; } - Serial.println("[TOUCH] GT911 found at 0x14"); + _i2cAddress = TOUCH_I2C_ADDR_2; } else { - Serial.println("[TOUCH] GT911 found at 0x5D"); + _i2cAddress = TOUCH_I2C_ADDR_1; } + Serial.print("[TOUCH] GT911 found at 0x"); + Serial.println(_i2cAddress, HEX); Serial.println("[TOUCH] Touch input registered"); return true; @@ -33,7 +35,7 @@ void TouchInput::update() { bool TouchInput::readGT911() { // Read touch status register (0x814E) - Wire.beginTransmission(TOUCH_I2C_ADDR); + Wire.beginTransmission(_i2cAddress); Wire.write(0x81); Wire.write(0x4E); if (Wire.endTransmission() != 0) { @@ -41,7 +43,7 @@ bool TouchInput::readGT911() { return false; } - Wire.requestFrom((uint8_t)TOUCH_I2C_ADDR, (uint8_t)1); + Wire.requestFrom(_i2cAddress, (uint8_t)1); if (!Wire.available()) { _touched = false; return false; @@ -53,7 +55,7 @@ bool TouchInput::readGT911() { if (!bufferReady || touchCount == 0) { _touched = false; - Wire.beginTransmission(TOUCH_I2C_ADDR); + Wire.beginTransmission(_i2cAddress); Wire.write(0x81); Wire.write(0x4E); Wire.write(0x00); @@ -62,7 +64,7 @@ bool TouchInput::readGT911() { } // Read first touch point (0x8150-0x8157) - Wire.beginTransmission(TOUCH_I2C_ADDR); + Wire.beginTransmission(_i2cAddress); Wire.write(0x81); Wire.write(0x50); if (Wire.endTransmission() != 0) { @@ -70,7 +72,7 @@ bool TouchInput::readGT911() { return false; } - Wire.requestFrom((uint8_t)TOUCH_I2C_ADDR, (uint8_t)6); + Wire.requestFrom(_i2cAddress, (uint8_t)6); if (Wire.available() < 6) { _touched = false; return false; @@ -84,7 +86,7 @@ bool TouchInput::readGT911() { _touched = true; // Clear buffer status - Wire.beginTransmission(TOUCH_I2C_ADDR); + Wire.beginTransmission(_i2cAddress); Wire.write(0x81); Wire.write(0x4E); Wire.write(0x00); diff --git a/src/hal/TouchInput.h b/src/hal/TouchInput.h index dce311b..dd9b836 100644 --- a/src/hal/TouchInput.h +++ b/src/hal/TouchInput.h @@ -18,6 +18,8 @@ public: private: bool readGT911(); + uint8_t _i2cAddress = 0; + bool _touched = false; int16_t _x = 0; int16_t _y = 0; diff --git a/src/input/InputManager.cpp b/src/input/InputManager.cpp index 81405a4..378c1be 100644 --- a/src/input/InputManager.cpp +++ b/src/input/InputManager.cpp @@ -121,6 +121,7 @@ void InputManager::update() { unsigned long now = millis(); if (now - _lastTouchPoll >= TOUCH_POLL_MS) { _lastTouchPoll = now; + // _touch->update(); if (_touch->isTouched()) { _activity = true; _strongActivity = true; From 3b1772147b3257afc63c7ac4fbcfae6c7a15a72a Mon Sep 17 00:00:00 2001 From: Scotty Date: Sun, 22 Mar 2026 09:20:43 +0100 Subject: [PATCH 2/2] Fix register address and size of GT911 point data --- src/hal/TouchInput.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/hal/TouchInput.cpp b/src/hal/TouchInput.cpp index d1480bf..52fd561 100644 --- a/src/hal/TouchInput.cpp +++ b/src/hal/TouchInput.cpp @@ -63,25 +63,25 @@ bool TouchInput::readGT911() { return false; } - // Read first touch point (0x8150-0x8157) + // Read first touch point (0x814F-0x8155) Wire.beginTransmission(_i2cAddress); Wire.write(0x81); - Wire.write(0x50); + Wire.write(0x4F); if (Wire.endTransmission() != 0) { _touched = false; return false; } - Wire.requestFrom(_i2cAddress, (uint8_t)6); - if (Wire.available() < 6) { + Wire.requestFrom(_i2cAddress, (uint8_t)7); + if (Wire.available() < 7) { _touched = false; return false; } - Wire.read(); // track ID + uint8_t trackId = Wire.read(); _x = Wire.read() | (Wire.read() << 8); _y = Wire.read() | (Wire.read() << 8); - Wire.read(); // size (unused) + Wire.read() | (Wire.read() << 8); // size (unused) _touched = true; @@ -92,5 +92,9 @@ bool TouchInput::readGT911() { Wire.write(0x00); Wire.endTransmission(); + // TODO: remove + Serial.write("[TOUCH] id "); Serial.print(trackId); Serial.write(" @ "); + Serial.print(_x); Serial.write(' '); Serial.println(_y); + return true; }