-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPD2010Touch.cpp
More file actions
370 lines (301 loc) · 10.9 KB
/
Copy pathSPD2010Touch.cpp
File metadata and controls
370 lines (301 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "SPD2010Touch.h"
// Static instance for interrupt handling
SPD2010Touch* SPD2010Touch::_instance = nullptr;
SPD2010Touch::SPD2010Touch(TwoWire& wire, int reset_pin, int interrupt_pin, Adafruit_XCA9554* expander)
: _wire(&wire), _reset_pin(reset_pin), _interrupt_pin(interrupt_pin), _expander(expander), _interrupt_flag(false) {
memset(&_touch_data, 0, sizeof(_touch_data));
_instance = this;
}
bool SPD2010Touch::begin() {
// Initialize I2C if not already done
if (!_wire) {
return false;
}
// Setup reset pin
_expander->pinMode(_reset_pin, OUTPUT);
_expander->digitalWrite(_reset_pin, HIGH);
// Setup interrupt pin
if (_interrupt_pin >= 0) {
pinMode(_interrupt_pin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(_interrupt_pin), interruptHandler, FALLING);
}
// Reset the controller
reset();
// Read firmware version to verify communication
delay(100);
return readFirmwareVersion();
}
void SPD2010Touch::reset() {
_expander->digitalWrite(_reset_pin, LOW);
delay(50);
_expander->digitalWrite(_reset_pin, HIGH);
delay(50);
}
bool SPD2010Touch::available() {
return _interrupt_flag;
}
bool SPD2010Touch::read(TouchData& data) {
if (readTouchData(data)) {
_interrupt_flag = false;
return true;
}
return false;
}
bool SPD2010Touch::getTouch(uint16_t& x, uint16_t& y, uint8_t& weight) {
TouchData data;
if (readTouchData(data) && data.touch_count > 0) {
x = data.points[0].x;
y = data.points[0].y;
weight = data.points[0].weight;
_interrupt_flag = false;
return true;
}
return false;
}
uint8_t SPD2010Touch::getTouchPoints(TouchPoint* points, uint8_t max_points) {
TouchData data;
if (readTouchData(data)) {
uint8_t count = min(data.touch_count, max_points);
for (uint8_t i = 0; i < count; i++) {
points[i] = data.points[i];
}
_interrupt_flag = false;
return count;
}
return 0;
}
bool SPD2010Touch::isTouched() {
TouchData data;
if (readTouchData(data)) {
_interrupt_flag = false;
return data.touch_count > 0;
}
return false;
}
uint8_t SPD2010Touch::getGesture() {
TouchData data;
if (readTouchData(data)) {
_interrupt_flag = false;
return data.gesture;
}
return 0;
}
void SPD2010Touch::setInterruptCallback(void (*callback)()) {
// This would be called from the interrupt handler if needed
// For now, users can check available() in their main loop
}
bool SPD2010Touch::writeCommand(uint16_t reg, const uint8_t* data, uint8_t length) {
_wire->beginTransmission(SPD2010_I2C_ADDRESS);
_wire->write((uint8_t)reg);
_wire->write((uint8_t)(reg >> 8));
for (uint8_t i = 0; i < length; i++) {
_wire->write(data[i]);
}
return _wire->endTransmission() == 0;
}
bool SPD2010Touch::readRegister(uint16_t reg, uint8_t* data, uint8_t length) {
_wire->beginTransmission(SPD2010_I2C_ADDRESS);
_wire->write((uint8_t)reg);
_wire->write((uint8_t)(reg >> 8));
if (_wire->endTransmission() != 0) {
return false;
}
uint8_t received = _wire->requestFrom(SPD2010_I2C_ADDRESS, length);
if (received != length) {
return false;
}
for (uint8_t i = 0; i < length; i++) {
data[i] = _wire->read();
}
return true;
}
bool SPD2010Touch::writePointModeCommand() {
uint8_t data[2] = {0x00, 0x00};
return writeCommand(0x0050, data, 2);
}
bool SPD2010Touch::writeStartCommand() {
uint8_t data[2] = {0x00, 0x00};
return writeCommand(0x0046, data, 2);
}
bool SPD2010Touch::writeCpuStartCommand() {
uint8_t data[2] = {0x01, 0x00};
return writeCommand(0x0004, data, 2);
}
bool SPD2010Touch::writeClearIntCommand() {
/* uint8_t data[2] = {0x01, 0x00};
return writeCommand(0x0002, data, 2); */ //Old method.
static const uint8_t ack[2] = { 0x01, 0x00 }; // step 1: ACK
static const uint8_t rear[2] = { 0x00, 0x00 }; // step 2: re-arm
if (!writeCommand(0x0002, ack, 2)) return false;
delayMicroseconds(200); // controller timing
if (! writeCommand(0x0002, rear, 2)) return false; // re-arm
uint32_t t0 = millis();
while (digitalRead(_interrupt_pin) == LOW) {
if (millis() - t0 > 2) { // >2 ms → retry sequence once
if (!writeCommand(0x0002, ack, 2)) return false;
delayMicroseconds(200);
if (!writeCommand(0x0002, rear, 2)) return false;
t0 = millis();
}
if (millis() - t0 > 10) { // safety timeout
return false; // controller never released INT
}
}
return true;
}
bool SPD2010Touch::readStatusLength(TouchStatus& status) {
uint8_t data[4];
if (!readRegister(0x0020, data, 4)) {
return false;
}
/* little-endian length */
uint16_t len = (data[3] << 8) | data[2];
/* ---- sanity clamp ----------------------------------- */
if (len < 4 || len > 64) len = 0; // 0 means “no HDP”
status.read_len = len;
delayMicroseconds(200);
status.status_low.pt_exist = (data[0] & 0x01);
status.status_low.gesture = (data[0] & 0x02);
status.status_low.aux = (data[0] & 0x08);
status.status_high.tic_busy = (data[1] & 0x80) >> 7;
status.status_high.tic_in_bios = (data[1] & 0x40) >> 6;
status.status_high.tic_in_cpu = (data[1] & 0x20) >> 5;
status.status_high.tint_low = (data[1] & 0x10) >> 4;
status.status_high.cpu_run = (data[1] & 0x08) >> 3;
// status.read_len = (data[3] << 8) | data[2];
return true;
}
bool SPD2010Touch::readHDP(const TouchStatus& status, TouchData& touch) {
uint8_t data[64]; // Maximum expected data size
if (!readRegister(0x0300, data, status.read_len)) {
return false;
}
uint8_t check_id = data[4];
if (check_id <= 0x0A && status.status_low.pt_exist) {
touch.touch_count = (status.read_len - 4) / 6;
touch.gesture = 0x00;
// Limit to maximum supported points
if (touch.touch_count > SPD2010_MAX_TOUCH_POINTS) {
touch.touch_count = SPD2010_MAX_TOUCH_POINTS;
}
for (uint8_t i = 0; i < touch.touch_count; i++) {
uint8_t offset = i * 6;
touch.points[i].id = data[4 + offset];
touch.points[i].x = ((data[7 + offset] & 0xF0) << 4) | data[5 + offset];
touch.points[i].y = ((data[7 + offset] & 0x0F) << 8) | data[6 + offset];
touch.points[i].weight = data[8 + offset];
}
// For slide gesture recognition
if (touch.points[0].weight != 0 && !touch.down) {
touch.down = true;
touch.up = false;
touch.down_x = touch.points[0].x;
touch.down_y = touch.points[0].y;
} else if (touch.points[0].weight == 0 && touch.down) {
touch.up = true;
touch.down = false;
touch.up_x = touch.points[0].x;
touch.up_y = touch.points[0].y;
}
} else if (check_id == 0xF6 && status.status_low.gesture) {
touch.touch_count = 0;
touch.up = false;
touch.down = false;
touch.gesture = data[6] & 0x07;
} else {
touch.touch_count = 0;
touch.gesture = 0;
}
return true;
}
bool SPD2010Touch::readHDPStatus(HDPStatus& hdp_status) {
uint8_t data[8];
if (!readRegister(0xFC02, data, 8)) {
return false;
}
hdp_status.status = data[5]; // Was 5 but chatGPT thinks it should be 0
hdp_status.next_packet_len = data[2] | (data[3] << 8);
return true;
}
bool SPD2010Touch::readHDPRemainData(const HDPStatus& hdp_status) {
uint8_t data[32];
return readRegister(0x0300, data, hdp_status.next_packet_len);
}
bool SPD2010Touch::readFirmwareVersion() {
uint8_t data[18];
if (!readRegister(0x2600, data, 18)) {
return false;
}
uint32_t dummy = (data[0] << 24) | (data[1] << 16) | (data[3] << 8) | data[0];
uint16_t dver = (data[5] << 8) | data[4];
uint32_t pid = (data[9] << 24) | (data[8] << 16) | (data[7] << 8) | data[6];
uint32_t ic_name_l = (data[13] << 24) | (data[12] << 16) | (data[11] << 8) | data[10];
uint32_t ic_name_h = (data[17] << 24) | (data[16] << 16) | (data[15] << 8) | data[14];
Serial.print("SPD2010 - Dummy: ");
Serial.print(dummy);
Serial.print(", Version: ");
Serial.print(dver);
Serial.print(", PID: ");
Serial.print(pid);
Serial.print(", IC Name: ");
Serial.print(ic_name_h);
Serial.print("-");
Serial.println(ic_name_l);
return true;
}
bool SPD2010Touch::readTouchData(TouchData& touch)
{
/* -----------------------------------------------------------
* 1. CONSUME the flag set by ISR so subsequent polls wait
* --------------------------------------------------------- */
_interrupt_flag = false; // ← add this line
TouchStatus tp_status;
HDPStatus hdp_status;
memset(&touch, 0, sizeof(touch));
if (!readStatusLength(tp_status)) { // 0x0020
return false;
}
/* ------------- BIOS / CPU housekeeping ------------------- */
if (tp_status.status_high.tic_in_bios) {
writeClearIntCommand(); // ACK+re-arm+verify
writeCpuStartCommand();
return false;
}
if (tp_status.status_high.tic_in_cpu) {
writePointModeCommand();
writeStartCommand();
writeClearIntCommand();
return false;
}
if (tp_status.status_high.cpu_run && tp_status.read_len == 0) {
writeClearIntCommand();
return false;
}
/* ------------- Touch / Gesture present ------------------- */
if (tp_status.status_low.pt_exist || tp_status.status_low.gesture) {
readHDP(tp_status, touch); // parse packet(s)
writeClearIntCommand(); // ACK now, no while-loop needed
/* HDP done-check loop */
while (true) {
if (!readHDPStatus(hdp_status)) break;
if (hdp_status.status == 0x82) { // all done
writeClearIntCommand(); // ACK+re-arm
break;
} else if (hdp_status.status == 0x00) { // remain data
readHDPRemainData(hdp_status);
continue;
} else break;
}
return true; // new data available
}
/* ------------- AUX only ---------------------------------- */
if (tp_status.status_high.cpu_run && tp_status.status_low.aux) {
writeClearIntCommand(); // clear & re-arm
}
return false; // no touch data
}
void IRAM_ATTR SPD2010Touch::interruptHandler() {
if (_instance) {
_instance->_interrupt_flag = true;
}
}