Added Touch support for the 4inchAP

This commit is contained in:
atc1441
2025-03-22 23:28:53 +01:00
parent 3810fbf68c
commit ac0c3ccfc9
4 changed files with 283 additions and 2 deletions

View File

@@ -0,0 +1,114 @@
#include "Arduino.h"
#include <Touch_GT911.h>
#include <Wire.h>
Touch_GT911::Touch_GT911(uint8_t _sda, uint8_t _scl, uint16_t _width, uint16_t _height) :
pinSda(_sda), pinScl(_scl), width(_width), height(_height) {
}
TPoint::TPoint(void) {
id = x = y = size = 0;
}
TPoint::TPoint(uint8_t _id, uint16_t _x, uint16_t _y, uint16_t _size) {
id = _id;
x = _x;
y = _y;
size = _size;
}
bool TPoint::operator==(TPoint point) {
return ((point.x == x) && (point.y == y) && (point.size == size));
}
bool TPoint::operator!=(TPoint point) {
return ((point.x != x) || (point.y != y) || (point.size != size));
}
void Touch_GT911::begin(uint8_t _addr) {
addr = _addr;
Wire.begin(pinSda, pinScl);
}
void Touch_GT911::calculateChecksum() {
uint8_t checksum;
for (uint8_t i=0; i<GT911_CONFIG_SIZE; i++) {
checksum += configBuf[i];
}
checksum = (~checksum) + 1;
configBuf[GT911_CONFIG_CHKSUM - GT911_CONFIG_START] = checksum;
}
void Touch_GT911::reConfig() {
calculateChecksum();
writeByteData(GT911_CONFIG_CHKSUM, configBuf[GT911_CONFIG_CHKSUM-GT911_CONFIG_START]);
writeByteData(GT911_CONFIG_FRESH, 1);
}
void Touch_GT911::setResolution(uint16_t _width, uint16_t _height) {
configBuf[GT911_X_OUTPUT_MAX_LOW - GT911_CONFIG_START] = lowByte(_width);
configBuf[GT911_X_OUTPUT_MAX_HIGH - GT911_CONFIG_START] = highByte(_width);
configBuf[GT911_Y_OUTPUT_MAX_LOW - GT911_CONFIG_START] = lowByte(_height);
configBuf[GT911_Y_OUTPUT_MAX_HIGH - GT911_CONFIG_START] = highByte(_height);
reConfig();
}
void Touch_GT911::read(void) {
uint8_t data[7];
uint8_t id;
uint16_t x, y, size;
uint8_t pointInfo = readByteData(GT911_POINT_INFO);
uint8_t bufferStatus = pointInfo >> 7 & 1;
uint8_t proximityValid = pointInfo >> 5 & 1;
uint8_t haveKey = pointInfo >> 4 & 1;
isLargeDetect = pointInfo >> 6 & 1;
touches = pointInfo & 0xF;
isTouched = touches > 0;
if (bufferStatus == 1 && isTouched) {
for (uint8_t i=0; i<touches; i++) {
readBlockData(data, GT911_POINT_1 + i * 8, 7);
points[i] = readPoint(data);
}
}
writeByteData(GT911_POINT_INFO, 0);
}
TPoint Touch_GT911::readPoint(uint8_t *data) {
uint16_t temp;
uint8_t id = data[0];
uint16_t x = data[1] + (data[2] << 8);
uint16_t y = data[3] + (data[4] << 8);
uint16_t size = data[5] + (data[6] << 8);
x = width - x;
y = height - y;
return TPoint(id, x, y, size);
}
void Touch_GT911::writeByteData(uint16_t reg, uint8_t val) {
Wire.beginTransmission(addr);
Wire.write(highByte(reg));
Wire.write(lowByte(reg));
Wire.write(val);
Wire.endTransmission();
}
uint8_t Touch_GT911::readByteData(uint16_t reg) {
uint8_t x;
Wire.beginTransmission(addr);
Wire.write(highByte(reg));
Wire.write(lowByte(reg));
Wire.endTransmission();
Wire.requestFrom(addr, (uint8_t)1);
x = Wire.read();
return x;
}
void Touch_GT911::writeBlockData(uint16_t reg, uint8_t *val, uint8_t size) {
Wire.beginTransmission(addr);
Wire.write(highByte(reg));
Wire.write(lowByte(reg));
// Wire.write(val, size);
for (uint8_t i=0; i<size; i++) {
Wire.write(val[i]);
}
Wire.endTransmission();
}
void Touch_GT911::readBlockData(uint8_t *buf, uint16_t reg, uint8_t size) {
Wire.beginTransmission(addr);
Wire.write(highByte(reg));
Wire.write(lowByte(reg));
Wire.endTransmission();
Wire.requestFrom(addr, size);
for (uint8_t i=0; i<size; i++) {
buf[i] = Wire.read();
}
}

View File

@@ -0,0 +1,116 @@
#ifndef Touch_GT911_H
#define Touch_GT911_H
#include "Arduino.h"
#include <Wire.h>
#define GT911_ADDR1 (uint8_t)0x5D
#define GT911_ADDR2 (uint8_t)0x14
// Real-time command (Write only)
#define GT911_COMMAND (uint16_t)0x8040
#define GT911_ESD_CHECK (uint16_t)0x8041
#define GT911_COMMAND_CHECK (uint16_t)0x8046
#define GT911_STRETCH_R0 (uint16_t)0x805E
#define GT911_STRETCH_R1 (uint16_t)0x805F
#define GT911_STRETCH_R2 (uint16_t)0x8060
#define GT911_STRETCH_RM (uint16_t)0x8061
#define GT911_DRV_GROUPA_NUM (uint16_t)0x8062
#define GT911_CONFIG_START (uint16_t)0x8047
#define GT911_CONFIG_VERSION (uint16_t)0x8047
#define GT911_X_OUTPUT_MAX_LOW (uint16_t)0x8048
#define GT911_X_OUTPUT_MAX_HIGH (uint16_t)0x8049
#define GT911_Y_OUTPUT_MAX_LOW (uint16_t)0x804A
#define GT911_Y_OUTPUT_MAX_HIGH (uint16_t)0x804B
#define GT911_TOUCH_NUMBER (uint16_t)0x804C
#define GT911_MODULE_SWITCH_1 (uint16_t)0x804D
#define GT911_MODULE_SWITCH_2 (uint16_t)0x804E
#define GT911_SHAKE_COUNT (uint16_t)0x804F
#define GT911_FILTER (uint16_t)0x8050
#define GT911_LARGE_TOUCH (uint16_t)0x8051
#define GT911_NOISE_REDUCTION (uint16_t)0x8052
#define GT911_SCREEN_TOUCH_LEVEL (uint16_t)0x8053
#define GT911_SCREEN_RELEASE_LEVEL (uint16_t)0x8054
#define GT911_LOW_POWER_CONTROL (uint16_t)0x8055
#define GT911_REFRESH_RATE (uint16_t)0x8056
#define GT911_X_THRESHOLD (uint16_t)0x8057
#define GT911_Y_THRESHOLD (uint16_t)0x8058
#define GT911_SPACE_TOP_BOTTOM (uint16_t)0x805B
#define GT911_PANEL_TX_GAIN (uint16_t)0x806B
#define GT911_PANEL_RX_GAIN (uint16_t)0x806C
#define GT911_PANEL_DUMP_SHIFT (uint16_t)0x806D
#define GT911_DRV_FRAME_CONTROL (uint16_t)0x806E
#define GT911_CHARGING_LEVEL_UP (uint16_t)0x806F
#define GT911_MODULE_SWITCH3 (uint16_t)0x8070
#define GT911_GESTURE_DIS (uint16_t)0X8071
#define GT911_GESTURE_LONG_PRESS_TIME (uint16_t)0x8072
#define GT911_X_Y_SLOPE_ADJUST (uint16_t)0X8073
#define GT911_GESTURE_CONTROL (uint16_t)0X8074
#define GT911_GESTURE_SWITCH1 (uint16_t)0X8075
#define GT911_GESTURE_SWITCH2 (uint16_t)0X8076
#define GT911_GESTURE_REFRESH_RATE (uint16_t)0x8077
#define GT911_GESTURE_TOUCH_LEVEL (uint16_t)0x8078
#define GT911_NEWGREENWAKEUPLEVEL (uint16_t)0x8079
#define GT911_FREQ_HOPPING_START (uint16_t)0x807A
#define GT911_CONFIG_CHKSUM (uint16_t)0X80FF
#define GT911_CONFIG_FRESH (uint16_t)0X8100
#define GT911_CONFIG_SIZE (uint16_t)0xFF-0x46
// Coordinate information
#define GT911_PRODUCT_ID (uint16_t)0X8140
#define GT911_FIRMWARE_VERSION (uint16_t)0X8140
#define GT911_RESOLUTION (uint16_t)0X8140
#define GT911_VENDOR_ID (uint16_t)0X8140
#define GT911_IMFORMATION (uint16_t)0X8140
#define GT911_POINT_INFO (uint16_t)0X814E
#define GT911_POINT_1 (uint16_t)0X814F
#define GT911_POINT_2 (uint16_t)0X8157
#define GT911_POINT_3 (uint16_t)0X815F
#define GT911_POINT_4 (uint16_t)0X8167
#define GT911_POINT_5 (uint16_t)0X816F
#define GT911_POINTS_REG {GT911_POINT_1, GT911_POINT_2, GT911_POINT_3, GT911_POINT_4, GT911_POINT_5}
class TPoint {
public:
TPoint(void);
TPoint(uint8_t id, uint16_t x, uint16_t y, uint16_t size);
bool operator==(TPoint);
bool operator!=(TPoint);
uint8_t id;
uint16_t x;
uint16_t y;
uint8_t size;
};
class Touch_GT911 {
public:
Touch_GT911(uint8_t _sda, uint8_t _scl,uint16_t _width, uint16_t _height);
void begin(uint8_t _addr=GT911_ADDR1);
void setRotation(uint8_t rot);
void setResolution(uint16_t _width, uint16_t _height);
uint8_t getGesture(void);
void read(void);
uint8_t isLargeDetect;
uint8_t touches = 0;
bool isTouched = false;
TPoint points[5];
private:
void calculateChecksum();
void reConfig();
TPoint readPoint(uint8_t *data);
void writeByteData(uint16_t reg, uint8_t val);
uint8_t readByteData(uint16_t reg);
void writeBlockData(uint16_t reg, uint8_t *val, uint8_t size);
void readBlockData(uint8_t *buf, uint16_t reg, uint8_t size);
uint8_t addr;
uint8_t pinSda;
uint8_t pinScl;
uint16_t width;
uint16_t height;
uint8_t configBuf[GT911_CONFIG_SIZE];
};
#endif // Touch_GT911_H

View File

@@ -235,11 +235,13 @@ build_unflags =
-D CONFIG_MBEDTLS_INTERNAL_MEM_ALLOC=y
lib_deps = ${env.lib_deps}
lib_extra_dirs = lib2/Arduino_GFX-1.3.7
lib2/gt911-touch
build_flags =
-std=gnu++17
${env.build_flags}
-D HAS_TFT
-D HAS_4inch_TPANEL
-D HAS_GT911_TOUCH
-D CORE_DEBUG_LEVEL=1
-D CONFIG_ESP32S3_SPIRAM_SUPPORT=1
-D CONFIG_SPIRAM_USE_MALLOC=1
@@ -256,8 +258,8 @@ build_flags =
-D FLASHER_AP_TEST=-1
-D FLASHER_AP_TXD=40
-D FLASHER_AP_RXD=44
-D FLASHER_DEBUG_TXD=19
-D FLASHER_DEBUG_RXD=45
-D FLASHER_DEBUG_TXD=42
-D FLASHER_DEBUG_RXD=42
-D FLASHER_DEBUG_PROG=2
-D FLASHER_LED=-1
-D TFT_HEIGHT=480
@@ -267,6 +269,7 @@ build_flags =
-D SERIAL_FLASHER_INTERFACE_UART=1
-D SERIAL_FLASHER_BOOT_HOLD_TIME_MS=200
-D SERIAL_FLASHER_RESET_HOLD_TIME_MS=200
-D C6_OTA_FLASHING
-D HAS_SUBGHZ
build_src_filter =
+<*>-<usbflasher.cpp>-<swd.cpp>-<webflasher.cpp>

View File

@@ -285,6 +285,52 @@ Arduino_RGB_Display* gfx = new Arduino_RGB_Display(
#endif
#endif
#if defined HAS_GT911_TOUCH
#include <Wire.h>
#include <Touch_GT911.h>
#define TOUCH_GT911_SCL 45
#define TOUCH_GT911_SDA 19
int touch_last_x = 0, touch_last_y = 0;
uint32_t last_touch_read = 0;
uint8_t is_new_touch_checked = false;
Touch_GT911 ts = Touch_GT911(TOUCH_GT911_SDA, TOUCH_GT911_SCL, max(480, 0), max(480, 0));
void touch_init()
{
ts.begin();
}
void touch_loop()
{
if (millis() - last_touch_read >= 50) {
last_touch_read = millis();
ts.read();
if (ts.isTouched)
{
touch_last_x = map(ts.points[0].x, 480, 0, 0, 480 - 1);
touch_last_y = map(ts.points[0].y, 480, 0, 0, 480 - 1);
Serial.printf("Touch position X: %i Y: %i\r\n", touch_last_x, touch_last_y);
if(is_new_touch_checked == false)
{
is_new_touch_checked = true;
if(touch_last_x <= 240)
sendAvail(WAKEUP_REASON_BUTTON1);
else
sendAvail(WAKEUP_REASON_BUTTON2);
}
}else{
is_new_touch_checked = false;
}
}
}
#else
void touch_init()
{
}
void touch_loop()
{
}
#endif
void TFTLog(String text) {
#if defined HAS_LILYGO_TPANEL || defined HAS_4inch_TPANEL
@@ -421,6 +467,7 @@ void yellow_ap_display_init(void) {
if (tft2.width() == 160) ledcOutputInvert(TFT_BACKLIGHT, true);
#endif
#endif
touch_init();
}
void yellow_ap_display_loop(void) {
@@ -483,6 +530,7 @@ void yellow_ap_display_loop(void) {
}
last_update = millis();
}
touch_loop();
}
#endif