Skip to content

feat(t-deck): WIP t-deck support #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ set(EXTRA_COMPONENT_DIRS

set(
COMPONENTS
"main esptool_py driver lwip button display display_drivers input_drivers logger lvgl mdns socket task tt21100 wifi gui"
"main esptool_py driver lwip button display display_drivers input_drivers logger lvgl mdns socket task tt21100 gt911 wifi gui"
CACHE STRING
"List of components to include"
)
Expand Down
2 changes: 1 addition & 1 deletion components/espp
Submodule espp updated 116 files
4 changes: 4 additions & 0 deletions components/gt911/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
idf_component_register(
INCLUDE_DIRS "include"
REQUIRES "logger"
)
258 changes: 258 additions & 0 deletions components/gt911/include/gt911.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
#pragma once

#include <functional>

#include "logger.hpp"

class Gt911 {
public:
static constexpr uint8_t DEFAULT_ADDRESS_1 = 0x5D;
static constexpr uint8_t DEFAULT_ADDRESS_2 = 0x14;

typedef std::function<void(uint8_t, uint16_t, uint8_t*, size_t)> read_fn;
typedef std::function<void(uint8_t, uint8_t*, size_t)> write_fn;

struct Config {
read_fn read;
write_fn write;
uint8_t address = DEFAULT_ADDRESS_1; /**< Which address to use for this chip? */
espp::Logger::Verbosity log_level{espp::Logger::Verbosity::WARN}; /**< Log verbosity for the input driver. */
};

Gt911(const Config& config)
: read_(config.read),
write_(config.write),
address_(config.address),
logger_({.tag = "Gt911", .level = config.log_level}) {
init();
}

bool read() {
static constexpr size_t DATA_LEN = CONTACT_SIZE * MAX_CONTACTS;
static uint8_t data[DATA_LEN];
read(Registers::POINT_INFO, data, 1);
num_touch_points_ = data[0] & 0x0f;
if (num_touch_points_ > 0) {
logger_.debug("Got {} touch points", num_touch_points_);
read(Registers::POINTS, data, CONTACT_SIZE * num_touch_points_);
// convert the data pointer to a GTPoint*
GTPoint* point = (GTPoint*)&data[0];
x_ = point->x;
y_ = point->y;
logger_.debug("Touch at ({}, {})", x_, y_);
}
write(Registers::POINT_INFO, 0x00); // sync signal

return num_touch_points_ > 0;
}

uint8_t get_num_touch_points() {
return num_touch_points_;
}

void get_touch_point(uint8_t *num_touch_points, uint16_t *x, uint16_t *y) {
*num_touch_points = get_num_touch_points();
if (*num_touch_points != 0) {
*x = x_;
*y = y_;
logger_.info("Got touch ({}, {})", *x, *y);
}
}

uint8_t get_home_button_state() {
return home_button_pressed_;
}

protected:
void init() {
/* NOTE: this doesn't seem to be needed
using namespace std::chrono_literals;
// write 0x02 (software reset) to GT911 COMMAND register
write(Registers::COMMAND, 0x02);
// delay 200 ms
std::this_thread::sleep_for(200ms);
// val = read SWITCH_1
uint8_t val = read(Registers::SWITCH_1);
val &= 0xFC;
val |= 0x03;
// write back val to switch 1
write(Registers::SWITCH_1, val);
// delay 200 ms
std::this_thread::sleep_for(200ms);
*/
}

static constexpr int CONTACT_SIZE = 8;
static constexpr int MAX_CONTACTS = 5;
static constexpr int CONFIG_MAX_LEN = 240;
static constexpr int CONFIG_911_LEN = 186;
static constexpr int CONFIG_967_LEN = 228;

enum class Registers : uint16_t {
COMMAND = 0x8040,
CONFIG = 0x8047,
SWITCH_1 = 0x804D,
SWITCH_2 = 0x804E,
REFRESH_RATE = 0x8056,
DATA = 0x8140,
POINT_INFO = 0x814E,
POINTS = 0x814F,
POINT_1 = 0x814F,
POINT_2 = 0x8157,
POINT_3 = 0x815F,
POINT_4 = 0x8167,
POINT_5 = 0x816F,
};

// From Goodix library
struct GTInfo {
// 0x8140-0x814A
char productId[4];
uint16_t fwId;
uint16_t xResolution;
uint16_t yResolution;
uint8_t vendorId;
} __attribute__((packed));

struct GTPoint {
// 0x814F-0x8156, ... 0x8176 (5 points)
uint8_t trackId;
uint16_t x;
uint16_t y;
uint16_t area;
uint8_t reserved;
} __attribute__((packed));

struct GTLevelConfig {
uint8_t touch; // Threshold of touch grow out of nothing
uint8_t leave; // Threshold of touch decrease to nothing
} __attribute__((packed));

struct GTStylusConfig {
uint8_t txGain;
uint8_t rxGain;
uint8_t dumpShift;
GTLevelConfig level;
uint8_t control; //Pen mode escape time out period (Unit: Sec)
} __attribute__((packed));

struct GTFreqHoppingConfig {
uint16_t hoppingBitFreq;
uint8_t hoppingFactor;
} __attribute__((packed));

struct GTKeyConfig {
// Key position: 0-255 valid
// 0 means no touch, it means independent touch key when 4 of the keys are 8 multiples
uint8_t pos1;
uint8_t pos2;
uint8_t pos3;
uint8_t pos4;
uint8_t area;
GTLevelConfig level;
uint8_t sens12;
uint8_t sens34;
uint8_t restrain;
} __attribute__((packed));

struct GTConfig {
// start at 0x8047
uint8_t configVersion;
uint16_t xResolution;
uint16_t yResolution;
// 0x804C
uint8_t touchNumber; // 3:0 Touch No.: 1~10

// 7:6 Reserved, 5:4 Stretch rank, 3 X2Y, 2 Sito
// 1:0 INT trig method: 00-rising, 01-falling, 02-low level, 03-high level enquiry
uint8_t moduleSwitch1;
uint8_t moduleSwitch2; // bit0 TouchKey
uint8_t shakeCount; // 3:0 Finger shake count
// 0x8050
// 7:6 First filter, 5:0 Normal filter (filtering value of original coordinate window, coefficiency is 1)
uint8_t filter;
uint8_t largeTouch;
uint8_t noiseReduction;
GTLevelConfig screenLevel;

uint8_t lowPowerControl; // Time to low power consumption (0~15s)
uint8_t refreshRate; // Coordinate report rate (Cycle: 5+N ms)
uint8_t xThreshold; //res
uint8_t yThreshold; //res
uint8_t xSpeedLimit; //res
uint8_t ySpeedLimit; //res
uint8_t vSpace; // 4bit top/bottom (coefficient 32)
uint8_t hSpace; // 4bit left/right
//0x805D-0x8061
uint8_t stretchRate; //Level of weak stretch (Strtch X/16 Pitch)
uint8_t stretchR0; // Interval 1 coefficient
uint8_t stretchR1; // Interval 2 coefficient
uint8_t stretchR2; // Interval 3 coefficient
uint8_t stretchRM; // All intervals base number

uint8_t drvGroupANum;
uint8_t drvGroupBNum;
uint8_t sensorNum;
uint8_t freqAFactor;
uint8_t freqBFactor;
// 0x8067
uint16_t pannelBitFreq; //Baseband of Driver group A\B (1526HZ<baseband<14600Hz)
uint16_t pannelSensorTime; //res
uint8_t pannelTxGain;
uint8_t pannelRxGain;
uint8_t pannelDumpShift;
uint8_t drvFrameControl;
// 0x806F - 0x8071
uint8_t NC_2[3];
GTStylusConfig stylusConfig;
// 0x8078-0x8079
uint8_t NC_3[2];
uint8_t freqHoppingStart; // Frequency hopping start frequency (Unit: 2KHz, 50 means 100KHz )
uint8_t freqHoppingEnd; // Frequency hopping stop frequency (Unit: 2KHz, 150 means 300KHz )
uint8_t noiseDetectTims;
uint8_t hoppingFlag;
uint8_t hoppingThreshold;

uint8_t noiseThreshold;
uint8_t NC_4[2];
// 0x8082
GTFreqHoppingConfig hoppingSegments[5];
// 0x8091
uint8_t NC_5[2];
GTKeyConfig keys;
} __attribute__((packed));

void write(Registers reg, uint8_t val) {
write(reg, &val, 1);
}

void write(Registers reg, uint8_t* data, size_t len) {
uint16_t reg_addr = (uint16_t)reg;
size_t d_len = 2 + len;
uint8_t d[d_len];
d[0] = reg_addr >> 8;
d[1] = reg_addr & 0xFF;
memcpy(&d[2], data, len);
write_(address_, d, d_len);
}

uint8_t read(Registers reg) {
uint8_t val = 0x00;
read(reg, &val, 1);
return val;
}

void read(Registers reg, uint8_t* data, size_t len) {
uint16_t reg_addr = (uint16_t)reg;
read_(address_, reg_addr, data, len);
}

read_fn read_;
write_fn write_;
uint8_t address_;
std::atomic<bool> home_button_pressed_{false};
std::atomic<uint8_t> num_touch_points_;
std::atomic<uint16_t> x_;
std::atomic<uint16_t> y_;
espp::Logger logger_;
};
2 changes: 2 additions & 0 deletions main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ menu "Wireless Debug Display Configuration"
bool "ESP32 WROVER KIT V4"
config HARDWARE_BOX
bool "ESP BOX"
config HARDWARE_TDECK
bool "LILYGO T DECK"
endchoice

config DEBUG_SERVER_PORT
Expand Down
Loading