-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathTouch.c
More file actions
711 lines (619 loc) · 18 KB
/
Touch.c
File metadata and controls
711 lines (619 loc) · 18 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/***********************************************************************************************************************
PicoMite MMBasic - Touch.c
<COPYRIGHT HOLDERS> Geoff Graham, Peter Mather
Copyright (c) 2021, <COPYRIGHT HOLDERS> All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided with the distribution.
3. The name MMBasic be used when referring to the interpreter in any documentation and promotional material and the
original copyright message be displayed on the console at startup (additional copyright messages may be added).
4. All advertising materials mentioning features or use of this software must display the following acknowledgement:
This product includes software developed by the <copyright holder>.
5. Neither the name of the <copyright holder> nor the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDERS> AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL <COPYRIGHT HOLDERS> BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
************************************************************************************************************************/
/**
* @file Touch.c
* @author Geoff Graham, Peter Mather
* @brief Source for the MMBasic Touch function
*/
#include "MMBasic_Includes.h"
#include "Hardware_Includes.h"
#include "hardware/structs/systick.h"
#include "hardware/i2c.h"
#ifdef PICOMITEWEB
#include "pico/cyw43_arch.h"
#endif
#ifndef PICOMITEWEB
#include "pico/multicore.h"
extern mutex_t frameBufferMutex;
#endif
// ========================================
// Function Prototypes
// ========================================
int GetTouchValue(int cmd);
void TDelay(void);
// ========================================
// Global Variables
// ========================================
int TouchIrqPortBit;
int TOUCH_IRQ_PIN;
int TOUCH_CS_PIN;
int TOUCH_Click_PIN;
int TOUCH_GETIRQTRIS = 0;
static int gt911_addr = GT911_ADDR;
// ========================================
// GT911 Device Mode Functions
// ========================================
int gt911_dev_mode_w(uint8_t value)
{
uint8_t tmp = read8Register16(gt911_addr, GT911_DEV_MODE_REG);
if (mmI2Cvalue == 0L)
{
tmp &= ~GT911_DEV_MODE_BIT_MASK;
tmp |= value << GT911_DEV_MODE_BIT_POSITION;
Write8Register16(gt911_addr, GT911_DEV_MODE_REG, tmp);
}
return mmI2Cvalue;
}
int32_t gt911_dev_mode_r(uint8_t *pValue)
{
*pValue = read8Register16(gt911_addr, GT911_DEV_MODE_REG);
if (mmI2Cvalue == 0L)
{
*pValue &= GT911_DEV_MODE_BIT_MASK;
*pValue = *pValue >> GT911_DEV_MODE_BIT_POSITION;
}
return mmI2Cvalue;
}
// ========================================
// Touch Configuration
// ========================================
/**
* Configure the touch parameters (chip select pin and the IRQ pin)
* Called by the OPTION TOUCH command
*/
void MIPS16 ConfigTouch(unsigned char *p)
{
int pin1, pin2 = 0, pin3 = 0;
uint8_t TOUCH_CAP = 0;
int threshold = 50;
unsigned char *tp = NULL;
// Check for FT6336 touch controller
tp = checkstring(p, (unsigned char *)"FT6336");
if (tp)
{
TOUCH_CAP = 1;
p = tp;
if (!Option.SYSTEM_I2C_SDA)
{
error("System I2C not set");
}
if (!TOUCH_CAP)
{
TOUCH_CAP = 2;
}
}
getcsargs(&p, 7);
if (!(Option.SYSTEM_CLK || TOUCH_CAP))
{
StandardError(45);
}
// Validate argument count
if (!TOUCH_CAP)
{
if (!(argc == 3 || argc == 5))
{
StandardError(2);
}
}
else if (argc < 3)
{
StandardError(2);
}
// Parse and validate pin1
pin1 = getpinarg(argv[0]);
// Parse and validate pin2
pin2 = getpinarg(argv[2]);
// Parse and validate pin3 (optional)
if (argc >= 5 && *argv[4])
{
pin3 = getpinarg(argv[4]);
}
// Parse threshold for capacitive touch
if (TOUCH_CAP && argc == 7)
{
threshold = getint(argv[6], 0, 255);
}
// Check if pins are already configured
if (ExtCurrentConfig[pin1] != EXT_NOT_CONFIG)
{
StandardErrorParam2(27, pin1, pin1);
}
if (pin2 && ExtCurrentConfig[pin2] != EXT_NOT_CONFIG)
{
StandardErrorParam2(27, pin2, pin2);
}
if (pin3 && ExtCurrentConfig[pin3] != EXT_NOT_CONFIG)
{
StandardErrorParam2(27, pin3, pin3);
}
// Save configuration
Option.TOUCH_CS = (TOUCH_CAP ? pin2 : pin1);
Option.TOUCH_IRQ = (TOUCH_CAP ? pin1 : pin2);
Option.TOUCH_Click = pin3;
Option.TOUCH_XZERO = Option.TOUCH_YZERO = 0;
Option.TOUCH_CAP = TOUCH_CAP;
Option.THRESHOLD_CAP = threshold;
}
// ========================================
// Touch Initialization
// ========================================
/**
* Setup touch based on the settings saved in flash
*/
void MIPS16 InitTouch(void)
{
if (Option.TOUCH_CAP == 1)
{
if (!Option.TOUCH_IRQ || !Option.SYSTEM_I2C_SCL)
{
return;
}
// Reset capacitive touch controller
PinSetBit(CAP_RESET, LATCLR);
uSec(1000);
PinSetBit(CAP_RESET, LATSET);
uSec(500000);
// Verify panel ID
if (readRegister8(FT6X36_ADDR, FT6X36_REG_PANEL_ID) != FT6X36_VENDID)
{
MMPrintString("Touch panel ID not found\r\n");
}
// Verify chip ID
uint8_t id = readRegister8(FT6X36_ADDR, FT6X36_REG_CHIPID);
if (!(id == FT6206_CHIPID || id == FT6236_CHIPID || id == FT6336_CHIPID))
{
PIntH(id);
MMPrintString(" Touch panel not found\r\n");
}
// Configure touch controller
WriteRegister8(FT6X36_ADDR, FT6X36_REG_DEVICE_MODE, 0x00);
WriteRegister8(FT6X36_ADDR, FT6X36_REG_INTERRUPT_MODE, 0x00);
WriteRegister8(FT6X36_ADDR, FT6X36_REG_CTRL, 0x00);
WriteRegister8(FT6X36_ADDR, FT6X36_REG_THRESHHOLD, Option.THRESHOLD_CAP);
WriteRegister8(FT6X36_ADDR, FT6X36_REG_TOUCHRATE_ACTIVE, 0x01);
TOUCH_GETIRQTRIS = 1;
}
else
{
if (!Option.TOUCH_CS)
{
return;
}
GetTouchValue(CMD_PENIRQ_ON);
TOUCH_GETIRQTRIS = 1;
GetTouchAxis(CMD_MEASURE_X);
}
}
// ========================================
// Touch Calibration
// ========================================
/**
* Draws the target, waits for touch to stabilize and returns raw x and y values
* Used only during calibration
*/
void MIPS16 GetCalibration(int x, int y, int *xval, int *yval)
{
int i, j;
#define TCAL_FONT 0x02
// Draw calibration screen
ClearScreen(BLACK);
GUIPrintString(HRes / 2, VRes / 2 - GetFontHeight(TCAL_FONT) / 2,
TCAL_FONT, JUSTIFY_CENTER, JUSTIFY_MIDDLE, 0, WHITE, BLACK,
"Touch Target");
GUIPrintString(HRes / 2, VRes / 2 + GetFontHeight(TCAL_FONT) / 2,
TCAL_FONT, JUSTIFY_CENTER, JUSTIFY_MIDDLE, 0, WHITE, BLACK,
"and Hold");
// Draw target
DrawLine(x - (TARGET_OFFSET * 3) / 4, y, x + (TARGET_OFFSET * 3) / 4, y, 1, WHITE);
DrawLine(x, y - (TARGET_OFFSET * 3) / 4, x, y + (TARGET_OFFSET * 3) / 4, 1, WHITE);
DrawCircle(x, y, TARGET_OFFSET / 2, 1, WHITE, -1, 1);
if (!Option.TOUCH_CAP)
{
// Resistive touch calibration
while (!TOUCH_DOWN)
{
CheckAbort();
}
// Discard initial readings
for (i = 0; i < 50; i++)
{
GetTouchAxis(CMD_MEASURE_X);
GetTouchAxis(CMD_MEASURE_Y);
}
// Average multiple readings for X
for (i = j = 0; i < 50; i++)
{
j += GetTouchAxis(CMD_MEASURE_X);
}
*xval = j / 50;
// Average multiple readings for Y
for (i = j = 0; i < 50; i++)
{
j += GetTouchAxis(CMD_MEASURE_Y);
}
*yval = j / 50;
ClearScreen(BLACK);
while (TOUCH_DOWN)
{
CheckAbort();
}
uSec(25000);
}
else
{
// Capacitive touch calibration
while (!TOUCH_DOWN)
{
CheckAbort();
}
uSec(100000);
// Average multiple readings for X
for (i = j = 0; i < 5; i++)
{
j += GetTouchAxisCap(GET_X_AXIS);
}
*xval = j / 5;
// Average multiple readings for Y
for (i = j = 0; i < 5; i++)
{
j += GetTouchAxisCap(GET_Y_AXIS);
}
*yval = j / 5;
ClearScreen(BLACK);
while (TOUCH_DOWN)
{
CheckAbort();
}
uSec(25000);
}
}
// ========================================
// Touch Reading Functions
// ========================================
/**
* Main function to get a touch reading
* Returns scaled pixel coordinates or TOUCH_ERROR if pen is not down
* @param y: if true, return Y reading; otherwise return X reading
*/
int __not_in_flash_func(GetTouch)(int y)
{
int i = TOUCH_ERROR;
TOUCH_GETIRQTRIS = 0;
// Validate configuration
if (Option.TOUCH_CS == 0 && Option.TOUCH_IRQ == 0)
{
error("Touch option not set");
}
if (!Option.TOUCH_XZERO && !Option.TOUCH_YZERO)
{
error("Touch not calibrated");
}
// Check if pen is down
if (PinRead(Option.TOUCH_IRQ))
{
TOUCH_GETIRQTRIS = 1;
return TOUCH_ERROR;
}
if (Option.TOUCH_CAP == 1)
{
// Capacitive touch reading
uint32_t in;
// Handle second touch point
if (y >= 10)
{
if (readRegister8(FT6X36_ADDR, FT6X36_REG_NUM_TOUCHES) != 2)
{
TOUCH_GETIRQTRIS = 1;
return TOUCH_ERROR;
}
in = readRegister32(FT6X36_ADDR, FT6X36_REG_P2_XH);
y -= 10;
}
else
{
in = readRegister32(FT6X36_ADDR, FT6X36_REG_P1_XH);
}
// Handle axis swap
if (Option.TOUCH_SWAPXY)
{
y = !y;
}
// Extract coordinate
if (y)
{
i = (in & 0xF0000) >> 8;
i |= (in >> 24);
}
else
{
i = (in & 0xF) << 8;
i |= ((in >> 8) & 0xFF);
}
// Restore axis orientation
if (Option.TOUCH_SWAPXY)
{
y = !y;
}
// Scale to screen coordinates
if (y)
{
i = (MMFLOAT)(i - Option.TOUCH_YZERO) * Option.TOUCH_YSCALE;
}
else
{
i = (MMFLOAT)(i - Option.TOUCH_XZERO) * Option.TOUCH_XSCALE;
}
// Validate range
if (i < 0 || i >= (y ? VRes : HRes))
{
i = TOUCH_ERROR;
}
}
else
{
// Resistive touch reading
if (y)
{
i = ((MMFLOAT)(GetTouchAxis(Option.TOUCH_SWAPXY ? CMD_MEASURE_X : CMD_MEASURE_Y) - Option.TOUCH_YZERO) * Option.TOUCH_YSCALE);
}
else
{
i = ((MMFLOAT)(GetTouchAxis(Option.TOUCH_SWAPXY ? CMD_MEASURE_Y : CMD_MEASURE_X) - Option.TOUCH_XZERO) * Option.TOUCH_XSCALE);
}
// Validate range
if (i < 0 || i >= (y ? VRes : HRes))
{
i = TOUCH_ERROR;
}
}
TOUCH_GETIRQTRIS = 1;
return i;
}
/**
* Get a reading from a single axis (resistive touch)
* Returns raw value from touch controller
* Takes multiple readings and averages the middle values
*/
int __not_in_flash_func(GetTouchAxis)(int cmd)
{
int i, j, t, b[TOUCH_SAMPLES];
TOUCH_GETIRQTRIS = 0;
PinSetBit(Option.TOUCH_IRQ, CNPDSET);
#ifdef PICOMITE
if (SPIatRisk)
{
mutex_enter_blocking(&frameBufferMutex);
}
#endif
GetTouchValue(cmd);
// Take TOUCH_SAMPLES readings and sort in descending order
for (i = 0; i < TOUCH_SAMPLES; i++)
{
b[i] = GetTouchValue(cmd);
// Handle audio playback
if (CurrentlyPlaying == P_WAV || CurrentlyPlaying == P_FLAC ||
CurrentlyPlaying == P_MIDI || CurrentlyPlaying == P_MP3 ||
CurrentlyPlaying == P_ARRAY)
{
#ifdef PICOMITE
if (SPIatRisk)
{
mutex_enter_blocking(&frameBufferMutex);
}
#endif
checkWAVinput();
#ifdef PICOMITE
if (SPIatRisk)
{
mutex_exit(&frameBufferMutex);
}
#endif
}
if (CurrentlyPlaying == P_MOD || CurrentlyPlaying == P_STREAM)
{
checkWAVinput();
}
// Sort reading into position
for (j = i; j > 0; j--)
{
if (b[j - 1] < b[j])
{
t = b[j - 1];
b[j - 1] = b[j];
b[j] = t;
}
else
{
break;
}
}
}
// Discard outliers and average middle values
for (j = 0, i = TOUCH_DISCARD; i < TOUCH_SAMPLES - TOUCH_DISCARD; i++)
{
j += b[i];
}
i = j / (TOUCH_SAMPLES - (TOUCH_DISCARD * 2));
GetTouchValue(CMD_PENIRQ_ON);
PinSetBit(Option.TOUCH_IRQ, CNPUSET);
TOUCH_GETIRQTRIS = 1;
#ifdef PICOMITE
if (SPIatRisk)
{
mutex_exit(&frameBufferMutex);
}
#endif
return i;
}
/**
* Get a reading from capacitive touch controller
*/
#if LOWRAM
int GetTouchAxisCap(int y)
#else
int __not_in_flash_func(GetTouchAxisCap)(int y)
#endif
{
uint32_t i, in;
TOUCH_GETIRQTRIS = 0;
in = readRegister32(FT6X36_ADDR, FT6X36_REG_P1_XH);
if (y)
{
i = (in & 0xF0000) >> 8;
i |= (in >> 24);
}
else
{
i = (in & 0xF) << 8;
i |= ((in >> 8) & 0xFF);
}
TOUCH_GETIRQTRIS = 1;
return i;
}
/**
* Get a single reading from the touch controller via SPI
* Assumes PenIRQ line is low and SPI baudrate is correct
* Takes approximately 260µS at 120MHz
*/
int __not_in_flash_func(GetTouchValue)(int cmd)
{
int val;
unsigned int lb, hb;
// Set SPI speed
if (!SSDTYPE)
{
SPISpeedSet(TOUCH);
}
else
{
SPISpeedSet(SLOWTOUCH);
}
// Configure CS pin
gpio_init(TOUCH_CS_PIN);
gpio_set_dir(TOUCH_CS_PIN, GPIO_OUT);
if (Option.CombinedCS)
{
gpio_put(TOUCH_CS_PIN, GPIO_PIN_SET);
}
else
{
gpio_put(TOUCH_CS_PIN, GPIO_PIN_RESET);
}
TDelay();
// Read touch value via SPI
val = xchg_byte(cmd);
hb = xchg_byte(0);
val = (hb & 0b1111111) << 5; // Top 7 bits
lb = xchg_byte(0);
val |= (lb >> 3) & 0b11111; // Bottom 5 bits
// Release CS
if (Option.CombinedCS)
{
gpio_set_dir(TOUCH_CS_PIN, GPIO_IN);
}
else
{
ClearCS(Option.TOUCH_CS);
}
#ifdef PICOMITEWEB
ProcessWeb(1);
#endif
return val;
}
/**
* Provides a small (~200ns) delay for the touch screen controller
*/
void __not_in_flash_func(TDelay)(void)
{
int ticks_per_millisecond = ticks_per_second / 1000;
int T = 16777215 + setuptime - ((4 * ticks_per_millisecond) / 20000);
shortpause(T);
}
// ========================================
// MMBasic TOUCH() Function
// ========================================
/**
* MMBasic TOUCH() function implementation
*/
void fun_touch(void)
{
if (checkstring(ep, (unsigned char *)"X"))
{
iret = GetTouch(GET_X_AXIS);
}
else if (checkstring(ep, (unsigned char *)"Y"))
{
iret = GetTouch(GET_Y_AXIS);
}
else if (checkstring(ep, (unsigned char *)"DOWN"))
{
iret = TOUCH_DOWN;
}
else if (checkstring(ep, (unsigned char *)"UP"))
{
iret = !TOUCH_DOWN;
}
#ifdef GUICONTROLS
else if (checkstring(ep, (unsigned char *)"REF"))
{
iret = CurrentRef;
}
else if (checkstring(ep, (unsigned char *)"LASTREF"))
{
iret = LastRef;
}
else if (checkstring(ep, (unsigned char *)"LASTX"))
{
iret = LastX;
}
else if (checkstring(ep, (unsigned char *)"LASTY"))
{
iret = LastY;
}
#endif
else
{
if (Option.TOUCH_CAP)
{
if (checkstring(ep, (unsigned char *)"X2"))
{
iret = GetTouch(GET_X_AXIS2);
}
else if (checkstring(ep, (unsigned char *)"Y2"))
{
iret = GetTouch(GET_Y_AXIS2);
}
else
{
SyntaxError();
}
}
else
{
SyntaxError();
}
}
targ = T_INT;
}