|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <functional> |
| 4 | + |
| 5 | +#include "logger.hpp" |
| 6 | + |
| 7 | +class Gt911 { |
| 8 | +public: |
| 9 | + static constexpr uint8_t DEFAULT_ADDRESS_1 = 0x5D; |
| 10 | + static constexpr uint8_t DEFAULT_ADDRESS_2 = 0x14; |
| 11 | + |
| 12 | + typedef std::function<void(uint8_t, uint16_t, uint8_t*, size_t)> read_fn; |
| 13 | + typedef std::function<void(uint8_t, uint8_t*, size_t)> write_fn; |
| 14 | + |
| 15 | + struct Config { |
| 16 | + read_fn read; |
| 17 | + write_fn write; |
| 18 | + uint8_t address = DEFAULT_ADDRESS_1; /**< Which address to use for this chip? */ |
| 19 | + espp::Logger::Verbosity log_level{espp::Logger::Verbosity::WARN}; /**< Log verbosity for the input driver. */ |
| 20 | + }; |
| 21 | + |
| 22 | + Gt911(const Config& config) |
| 23 | + : read_(config.read), |
| 24 | + write_(config.write), |
| 25 | + address_(config.address), |
| 26 | + logger_({.tag = "Gt911", .level = config.log_level}) { |
| 27 | + init(); |
| 28 | + } |
| 29 | + |
| 30 | + bool read() { |
| 31 | + static constexpr size_t DATA_LEN = CONTACT_SIZE * MAX_CONTACTS; |
| 32 | + static uint8_t data[DATA_LEN]; |
| 33 | + read(Registers::POINT_INFO, data, 1); |
| 34 | + num_touch_points_ = data[0] & 0x0f; |
| 35 | + if (num_touch_points_ > 0) { |
| 36 | + logger_.debug("Got {} touch points", num_touch_points_); |
| 37 | + read(Registers::POINTS, data, CONTACT_SIZE * num_touch_points_); |
| 38 | + // convert the data pointer to a GTPoint* |
| 39 | + GTPoint* point = (GTPoint*)&data[0]; |
| 40 | + x_ = point->x; |
| 41 | + y_ = point->y; |
| 42 | + logger_.debug("Touch at ({}, {})", x_, y_); |
| 43 | + } |
| 44 | + write(Registers::POINT_INFO, 0x00); // sync signal |
| 45 | + |
| 46 | + return num_touch_points_ > 0; |
| 47 | + } |
| 48 | + |
| 49 | + uint8_t get_num_touch_points() { |
| 50 | + return num_touch_points_; |
| 51 | + } |
| 52 | + |
| 53 | + void get_touch_point(uint8_t *num_touch_points, uint16_t *x, uint16_t *y) { |
| 54 | + *num_touch_points = get_num_touch_points(); |
| 55 | + if (*num_touch_points != 0) { |
| 56 | + *x = x_; |
| 57 | + *y = y_; |
| 58 | + logger_.info("Got touch ({}, {})", *x, *y); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + uint8_t get_home_button_state() { |
| 63 | + return home_button_pressed_; |
| 64 | + } |
| 65 | + |
| 66 | +protected: |
| 67 | + void init() { |
| 68 | + /* NOTE: this doesn't seem to be needed |
| 69 | + using namespace std::chrono_literals; |
| 70 | + // write 0x02 (software reset) to GT911 COMMAND register |
| 71 | + write(Registers::COMMAND, 0x02); |
| 72 | + // delay 200 ms |
| 73 | + std::this_thread::sleep_for(200ms); |
| 74 | + // val = read SWITCH_1 |
| 75 | + uint8_t val = read(Registers::SWITCH_1); |
| 76 | + val &= 0xFC; |
| 77 | + val |= 0x03; |
| 78 | + // write back val to switch 1 |
| 79 | + write(Registers::SWITCH_1, val); |
| 80 | + // delay 200 ms |
| 81 | + std::this_thread::sleep_for(200ms); |
| 82 | + */ |
| 83 | + } |
| 84 | + |
| 85 | + static constexpr int CONTACT_SIZE = 8; |
| 86 | + static constexpr int MAX_CONTACTS = 5; |
| 87 | + static constexpr int CONFIG_MAX_LEN = 240; |
| 88 | + static constexpr int CONFIG_911_LEN = 186; |
| 89 | + static constexpr int CONFIG_967_LEN = 228; |
| 90 | + |
| 91 | + enum class Registers : uint16_t { |
| 92 | + COMMAND = 0x8040, |
| 93 | + CONFIG = 0x8047, |
| 94 | + SWITCH_1 = 0x804D, |
| 95 | + SWITCH_2 = 0x804E, |
| 96 | + REFRESH_RATE = 0x8056, |
| 97 | + DATA = 0x8140, |
| 98 | + POINT_INFO = 0x814E, |
| 99 | + POINTS = 0x814F, |
| 100 | + POINT_1 = 0x814F, |
| 101 | + POINT_2 = 0x8157, |
| 102 | + POINT_3 = 0x815F, |
| 103 | + POINT_4 = 0x8167, |
| 104 | + POINT_5 = 0x816F, |
| 105 | + }; |
| 106 | + |
| 107 | + // From Goodix library |
| 108 | + struct GTInfo { |
| 109 | + // 0x8140-0x814A |
| 110 | + char productId[4]; |
| 111 | + uint16_t fwId; |
| 112 | + uint16_t xResolution; |
| 113 | + uint16_t yResolution; |
| 114 | + uint8_t vendorId; |
| 115 | + } __attribute__((packed)); |
| 116 | + |
| 117 | + struct GTPoint { |
| 118 | + // 0x814F-0x8156, ... 0x8176 (5 points) |
| 119 | + uint8_t trackId; |
| 120 | + uint16_t x; |
| 121 | + uint16_t y; |
| 122 | + uint16_t area; |
| 123 | + uint8_t reserved; |
| 124 | + } __attribute__((packed)); |
| 125 | + |
| 126 | + struct GTLevelConfig { |
| 127 | + uint8_t touch; // Threshold of touch grow out of nothing |
| 128 | + uint8_t leave; // Threshold of touch decrease to nothing |
| 129 | + } __attribute__((packed)); |
| 130 | + |
| 131 | + struct GTStylusConfig { |
| 132 | + uint8_t txGain; |
| 133 | + uint8_t rxGain; |
| 134 | + uint8_t dumpShift; |
| 135 | + GTLevelConfig level; |
| 136 | + uint8_t control; //Pen mode escape time out period (Unit: Sec) |
| 137 | + } __attribute__((packed)); |
| 138 | + |
| 139 | + struct GTFreqHoppingConfig { |
| 140 | + uint16_t hoppingBitFreq; |
| 141 | + uint8_t hoppingFactor; |
| 142 | + } __attribute__((packed)); |
| 143 | + |
| 144 | + struct GTKeyConfig { |
| 145 | + // Key position: 0-255 valid |
| 146 | + // 0 means no touch, it means independent touch key when 4 of the keys are 8 multiples |
| 147 | + uint8_t pos1; |
| 148 | + uint8_t pos2; |
| 149 | + uint8_t pos3; |
| 150 | + uint8_t pos4; |
| 151 | + uint8_t area; |
| 152 | + GTLevelConfig level; |
| 153 | + uint8_t sens12; |
| 154 | + uint8_t sens34; |
| 155 | + uint8_t restrain; |
| 156 | + } __attribute__((packed)); |
| 157 | + |
| 158 | + struct GTConfig { |
| 159 | + // start at 0x8047 |
| 160 | + uint8_t configVersion; |
| 161 | + uint16_t xResolution; |
| 162 | + uint16_t yResolution; |
| 163 | + // 0x804C |
| 164 | + uint8_t touchNumber; // 3:0 Touch No.: 1~10 |
| 165 | + |
| 166 | + // 7:6 Reserved, 5:4 Stretch rank, 3 X2Y, 2 Sito |
| 167 | + // 1:0 INT trig method: 00-rising, 01-falling, 02-low level, 03-high level enquiry |
| 168 | + uint8_t moduleSwitch1; |
| 169 | + uint8_t moduleSwitch2; // bit0 TouchKey |
| 170 | + uint8_t shakeCount; // 3:0 Finger shake count |
| 171 | + // 0x8050 |
| 172 | + // 7:6 First filter, 5:0 Normal filter (filtering value of original coordinate window, coefficiency is 1) |
| 173 | + uint8_t filter; |
| 174 | + uint8_t largeTouch; |
| 175 | + uint8_t noiseReduction; |
| 176 | + GTLevelConfig screenLevel; |
| 177 | + |
| 178 | + uint8_t lowPowerControl; // Time to low power consumption (0~15s) |
| 179 | + uint8_t refreshRate; // Coordinate report rate (Cycle: 5+N ms) |
| 180 | + uint8_t xThreshold; //res |
| 181 | + uint8_t yThreshold; //res |
| 182 | + uint8_t xSpeedLimit; //res |
| 183 | + uint8_t ySpeedLimit; //res |
| 184 | + uint8_t vSpace; // 4bit top/bottom (coefficient 32) |
| 185 | + uint8_t hSpace; // 4bit left/right |
| 186 | + //0x805D-0x8061 |
| 187 | + uint8_t stretchRate; //Level of weak stretch (Strtch X/16 Pitch) |
| 188 | + uint8_t stretchR0; // Interval 1 coefficient |
| 189 | + uint8_t stretchR1; // Interval 2 coefficient |
| 190 | + uint8_t stretchR2; // Interval 3 coefficient |
| 191 | + uint8_t stretchRM; // All intervals base number |
| 192 | + |
| 193 | + uint8_t drvGroupANum; |
| 194 | + uint8_t drvGroupBNum; |
| 195 | + uint8_t sensorNum; |
| 196 | + uint8_t freqAFactor; |
| 197 | + uint8_t freqBFactor; |
| 198 | + // 0x8067 |
| 199 | + uint16_t pannelBitFreq; //Baseband of Driver group A\B (1526HZ<baseband<14600Hz) |
| 200 | + uint16_t pannelSensorTime; //res |
| 201 | + uint8_t pannelTxGain; |
| 202 | + uint8_t pannelRxGain; |
| 203 | + uint8_t pannelDumpShift; |
| 204 | + uint8_t drvFrameControl; |
| 205 | + // 0x806F - 0x8071 |
| 206 | + uint8_t NC_2[3]; |
| 207 | + GTStylusConfig stylusConfig; |
| 208 | + // 0x8078-0x8079 |
| 209 | + uint8_t NC_3[2]; |
| 210 | + uint8_t freqHoppingStart; // Frequency hopping start frequency (Unit: 2KHz, 50 means 100KHz ) |
| 211 | + uint8_t freqHoppingEnd; // Frequency hopping stop frequency (Unit: 2KHz, 150 means 300KHz ) |
| 212 | + uint8_t noiseDetectTims; |
| 213 | + uint8_t hoppingFlag; |
| 214 | + uint8_t hoppingThreshold; |
| 215 | + |
| 216 | + uint8_t noiseThreshold; |
| 217 | + uint8_t NC_4[2]; |
| 218 | + // 0x8082 |
| 219 | + GTFreqHoppingConfig hoppingSegments[5]; |
| 220 | + // 0x8091 |
| 221 | + uint8_t NC_5[2]; |
| 222 | + GTKeyConfig keys; |
| 223 | + } __attribute__((packed)); |
| 224 | + |
| 225 | + void write(Registers reg, uint8_t val) { |
| 226 | + write(reg, &val, 1); |
| 227 | + } |
| 228 | + |
| 229 | + void write(Registers reg, uint8_t* data, size_t len) { |
| 230 | + uint16_t reg_addr = (uint16_t)reg; |
| 231 | + size_t d_len = 2 + len; |
| 232 | + uint8_t d[d_len]; |
| 233 | + d[0] = reg_addr >> 8; |
| 234 | + d[1] = reg_addr & 0xFF; |
| 235 | + memcpy(&d[2], data, len); |
| 236 | + write_(address_, d, d_len); |
| 237 | + } |
| 238 | + |
| 239 | + uint8_t read(Registers reg) { |
| 240 | + uint8_t val = 0x00; |
| 241 | + read(reg, &val, 1); |
| 242 | + return val; |
| 243 | + } |
| 244 | + |
| 245 | + void read(Registers reg, uint8_t* data, size_t len) { |
| 246 | + uint16_t reg_addr = (uint16_t)reg; |
| 247 | + read_(address_, reg_addr, data, len); |
| 248 | + } |
| 249 | + |
| 250 | + read_fn read_; |
| 251 | + write_fn write_; |
| 252 | + uint8_t address_; |
| 253 | + std::atomic<bool> home_button_pressed_{false}; |
| 254 | + std::atomic<uint8_t> num_touch_points_; |
| 255 | + std::atomic<uint16_t> x_; |
| 256 | + std::atomic<uint16_t> y_; |
| 257 | + espp::Logger logger_; |
| 258 | +}; |
0 commit comments