Files
lilygo-t-deck/examples/Touchpad/Touchpad.ino
T

106 lines
2.4 KiB
Arduino

/**
* @file Touchpad.ino
* @author Lewis He (lewishe@outlook.com)
* @license MIT
* @copyright Copyright (c) 2023 Shenzhen Xin Yuan Electronic Technology Co., Ltd
* @date 2023-04-12
*
*/
#include <Arduino.h>
#define TOUCH_MODULES_GT911
#include "TouchLib.h"
#include "utilities.h"
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");
}
void setup()
{
Serial.begin(115200);
pinMode(BOARD_TOUCH_INT, INPUT);
//! The board peripheral power control pin needs to be set to HIGH when using the peripheral
pinMode(BOARD_POWERON, OUTPUT);
digitalWrite(BOARD_POWERON, HIGH);
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();
}
void loop()
{
// You can judge the level of the touch interrupt to get whether there is a touch
if (digitalRead(BOARD_TOUCH_INT)) {
bool res = touch->read();
if (res) {
TP_Point p = touch->getPoint(0);
Serial.printf("X:%d Y:%d\n", p.x, p.y);
}
}
delay(5);
}