From 12f68646e81d787f86f00af4843a2015f08eb1fc Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Wed, 4 Sep 2024 09:33:21 +0800 Subject: [PATCH] lvgl_example Touch interrupt method https://github.com/Xinyuan-LilyGO/T-Deck/issues/42 --- examples/lvgl_example/lvgl_example.ino | 60 +++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/examples/lvgl_example/lvgl_example.ino b/examples/lvgl_example/lvgl_example.ino index 93b3fb8..3f3eb6d 100644 --- a/examples/lvgl_example/lvgl_example.ino +++ b/examples/lvgl_example/lvgl_example.ino @@ -8,8 +8,46 @@ static void slider_event_cb(lv_event_t *e); static lv_obj_t *slider_label; -TouchLib touch = TouchLib(Wire, BOARD_I2C_SDA, BOARD_I2C_SCL, GT911_SLAVE_ADDRESS1); TFT_eSPI tft; +TouchLib *touch = NULL; +uint8_t touchAddress = GT911_SLAVE_ADDRESS2; + +void scanDevices(TwoWire *w) +{ + uint8_t err, addr; + int nDevices = 0; + uint32_t start = 0; + for (addr = 1; addr < 127; addr++) { + start = millis(); + w->beginTransmission(addr); delay(2); + err = w->endTransmission(); + if (err == 0) { + nDevices++; + Serial.print("I2C device found at address 0x"); + if (addr < 16) { + Serial.print("0"); + } + Serial.print(addr, HEX); + Serial.println(" !"); + + if (addr == GT911_SLAVE_ADDRESS2) { + touchAddress = GT911_SLAVE_ADDRESS2; + Serial.println("Find GT911 Drv Slave address: 0x14"); + } else if (addr == GT911_SLAVE_ADDRESS1) { + touchAddress = GT911_SLAVE_ADDRESS1; + Serial.println("Find GT911 Drv Slave address: 0x5D"); + } + } else if (err == 4) { + Serial.print("Unknow error at address 0x"); + if (addr < 16) { + Serial.print("0"); + } + Serial.println(addr, HEX); + } + } + if (nDevices == 0) + Serial.println("No I2C devices found\n"); +} // LilyGo T-Deck control backlight chip has 16 levels of adjustment range @@ -54,15 +92,15 @@ static void disp_flush( lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t * lv_disp_flush_ready( disp ); } -static volatile bool touch_irq = false; static bool getTouch(int16_t &x, int16_t &y) { + if (! digitalRead(BOARD_TOUCH_INT))return false; uint8_t rotation = tft.getRotation(); - if (!touch.read()) { + if (!touch->read()) { return false; } - TP_Point t = touch.getPoint(0); + TP_Point t = touch->getPoint(0); switch (rotation) { case 1: x = t.y; @@ -191,9 +229,19 @@ void setup() tft.fillScreen(TFT_BLACK); // Set touch int input - pinMode(BOARD_TOUCH_INT, INPUT); delay(20); + pinMode(BOARD_TOUCH_INT, INPUT); + delay(20); + + Wire.begin(BOARD_I2C_SDA, BOARD_I2C_SCL); + + + // Two touch screens, the difference between them is the device address, + // use ScanDevices to get the existing I2C address + scanDevices(&Wire); + + touch = new TouchLib(Wire, BOARD_I2C_SDA, BOARD_I2C_SCL, touchAddress); - touch.init(); + touch->init(); setupLvgl();