/
emuapi.cpp
1502 lines (1346 loc) · 35 KB
/
emuapi.cpp
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define KEYMAP_PRESENT 1
extern "C" {
#include "emuapi.h"
#include "iopins.h"
}
#include "Wire.h"
#ifdef HAS_T4_VGA
#include "vga_t_dma.h"
const uint16_t deflogo[] = {
0,0
};
static const uint16_t * logo = deflogo;
#else
#include "tft_t_dma.h"
#include "logo.h"
#endif
#include "bmpjoy.h"
#include "bmpvbar.h"
#ifdef OLD_LAYOUT
#include "bmpvga.h"
#include "bmptft.h"
#endif
#ifdef HAS_I2CKBD
#include <Wire.h>
#endif
#ifdef HAS_USBKEY
#include "USBHost_t36.h" // Read this header first for key info
USBHost myusb;
USBHub hub1(myusb);
KeyboardController keyboard1(myusb);
#endif
static uint8_t usbnavpad=0;
#ifdef USE_SDFS
#include "uSDFS.h"
static FATFS fatfs;
static FIL file;
#else
#ifdef USE_SDFAT
#include "SdFat.h"
#include "sdios.h"
#ifndef SDCARD_SS_PIN
const uint8_t SD_CS_PIN = SS;
#else // SDCARD_SS_PIN
// Assume built-in SD is used.
const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
#endif // SDCARD_SS_PIN
#define SD_CONFIG SdioConfig(FIFO_SDIO)
// Try to select the best SD card configuration.
#if HAS_SDIO_CLASS
#define SD_CONFIG SdioConfig(FIFO_SDIO)
#elif ENABLE_DEDICATED_SPI
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI)
#else // HAS_SDIO_CLASS
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI)
#endif // HAS_SDIO_CLASS
//------------------------------------------------------------------------------
#if SD_FAT_TYPE == 0
#elif SD_FAT_TYPE == 1
#define SdFat SdFat32
#define File File32
#elif SD_FAT_TYPE == 2
#define SdFat SdExFat
#define File ExFile
#elif SD_FAT_TYPE == 3
#define SdFat SdFs
#define File FsFile
#endif // SD_FAT_TYPE
static SdFat SD;
#else
#include <SD.h>
#endif
static File file;
#endif
#define CALIBRATION_FILE "/cal.cfg"
#define MKEY_L1 1
#define MKEY_L2 2
#define MKEY_L3 3
#define MKEY_L4 4
#define MKEY_L5 5
#define MKEY_L6 6
#define MKEY_L7 7
#define MKEY_L8 8
#define MKEY_L9 9
#define MKEY_UP 20
#define MKEY_DOWN 21
#define MKEY_JOY 22
#define MKEY_TFT 23
#define MKEY_VGA 24
#define MAX_FILES 64
#define MAX_FILENAME_SIZE 24
#define MAX_MENULINES (MKEY_L9)
#define TEXT_HEIGHT 16
#define TEXT_WIDTH 8
#define MENU_FILE_XOFFSET (6*TEXT_WIDTH)
#define MENU_FILE_YOFFSET (2*TEXT_HEIGHT)
#define MENU_FILE_W (MAX_FILENAME_SIZE*TEXT_WIDTH)
#define MENU_FILE_H (MAX_MENULINES*TEXT_HEIGHT)
#define MENU_FILE_BGCOLOR RGBVAL16(0x00,0x00,0x40)
#define MENU_JOYS_YOFFSET (12*TEXT_HEIGHT)
#define MENU_VBAR_XOFFSET (0*TEXT_WIDTH)
#define MENU_VBAR_YOFFSET (MENU_FILE_YOFFSET)
#define MENU_TFT_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8)
#define MENU_TFT_YOFFSET (MENU_VBAR_YOFFSET+32)
#define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8)
#define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37)
extern TFT_T_DMA tft;
static char romspath[64];
static int nbFiles=0;
static int curFile=0;
static int topFile=0;
static char selection[MAX_FILENAME_SIZE+1]="";
static char files[MAX_FILES][MAX_FILENAME_SIZE];
static bool menuRedraw=true;
static int calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1;
static bool i2cKeyboardPresent = false;
static unsigned short * keys;
static int keyMap;
const unsigned short menutouchareas[] = {
TAREA_XY,MENU_FILE_XOFFSET,MENU_FILE_YOFFSET,
TAREA_WH,MENU_FILE_W, TEXT_HEIGHT,
TAREA_NEW_COL,TEXT_HEIGHT,TEXT_HEIGHT,TEXT_HEIGHT,TEXT_HEIGHT,TEXT_HEIGHT,TEXT_HEIGHT,TEXT_HEIGHT,TEXT_HEIGHT,TEXT_HEIGHT,
TAREA_XY,MENU_VBAR_XOFFSET,MENU_VBAR_YOFFSET,
TAREA_WH,32,48,
TAREA_NEW_COL, 72,72,8,40,
TAREA_XY,MENU_TFT_XOFFSET,MENU_TFT_YOFFSET,
TAREA_WH,32,37,
TAREA_NEW_COL, 38,38,
TAREA_END};
const unsigned short menutouchactions[] = {
MKEY_L1,MKEY_L2,MKEY_L3,MKEY_L4,MKEY_L5,MKEY_L6,MKEY_L7,MKEY_L8,MKEY_L9,
MKEY_UP,MKEY_DOWN,ACTION_NONE,MKEY_JOY,
MKEY_TFT,MKEY_VGA};
static int keypadval=0;
static boolean joySwapped = false;
static uint16_t bLastState;
static int xRef;
static int yRef;
static uint8_t prev_zt=0;
static bool menuOn=true;
static bool callibrationOn=false;
static int callibrationStep=0;
static char captureTouchZone(const unsigned short * areas, const unsigned short * actions, int *rx, int *ry, int *rw, int * rh) {
uint16_t xt=0;
uint16_t yt=0;
uint16_t zt=0;
boolean hDir=true;
if (tft.isTouching())
{
if (prev_zt == 0) {
prev_zt =1;
tft.readCal(&xt,&yt,&zt);
if (zt<1000) {
prev_zt=0;
return ACTION_NONE;
}
int i=0;
int k=0;
int y2=0, y1=0;
int x2=0, x1=0;
int x=KEYBOARD_X,y=KEYBOARD_Y;
int w=TAREA_W_DEF,h=TAREA_H_DEF;
uint8_t s;
while ( (s=areas[i++]) != TAREA_END ) {
if (s == TAREA_XY) {
x = areas[i++];
y = areas[i++];
x2 = x;
y2 = y;
}
else if (s == TAREA_WH) {
w = areas[i++];
h = areas[i++];
}
else if (s == TAREA_NEW_ROW) {
hDir = true;
y1 = y2;
y2 = y1 + h;
x2 = x;
}
else if (s == TAREA_NEW_COL) {
hDir = false;
x1 = x2;
x2 = x1 + w;
y2 = y;
}
else {
if (hDir) {
x1 = x2;
x2 = x1+s;
} else {
y1 = y2;
y2 = y1+s;
}
if ( (yt >= y1) && (yt < y2) && (xt >= x1) && (xt < x2) ) {
*rx = x1;
*ry = y1;
*rw = x2-x1;
*rh = y2-y1;
return (actions[k]);
}
k++;
}
}
}
prev_zt =1;
} else {
prev_zt=0;
}
return ACTION_NONE;
}
void emu_printf(char * text)
{
Serial.println(text);
}
void emu_printf(int val)
{
Serial.println(val);
}
void emu_printi(int val)
{
Serial.println(val,HEX);
}
void emu_printh(int val)
{
Serial.println(val,HEX);
}
static int malbufpt = 0;
static char malbuf[EXTRA_HEAP];
void * emu_Malloc(int size)
{
void * retval = malloc(size);
if (!retval) {
emu_printf("failled to allocate");
emu_printf(size);
emu_printf("fallback");
if ( (malbufpt+size) < sizeof(malbuf) ) {
retval = (void *)&malbuf[malbufpt];
malbufpt += size;
}
else {
emu_printf("failure to allocate");
}
}
else {
emu_printf("could allocate dynamic ");
emu_printf(size);
}
return retval;
}
void * emu_MallocI(int size)
{
void * retval = NULL;
if ( (malbufpt+size) < sizeof(malbuf) ) {
retval = (void *)&malbuf[malbufpt];
malbufpt += size;
emu_printf("could allocate static ");
emu_printf(size);
}
else {
emu_printf("failure to allocate");
}
return retval;
}
void emu_Free(void * pt)
{
free(pt);
}
int emu_ReadAnalogJoyX(int min, int max)
{
int val = analogRead(PIN_JOY2_A1X);
#if INVX
val = 4095 - val;
#endif
val = val-xRef;
val = ((val*140)/100);
if ( (val > -512) && (val < 512) ) val = 0;
val = val+2048;
return (val*(max-min))/4096;
}
int emu_ReadAnalogJoyY(int min, int max)
{
int val = analogRead(PIN_JOY2_A2Y);
#if INVY
val = 4095 - val;
#endif
val = val-yRef;
val = ((val*120)/100);
if ( (val > -512) && (val < 512) ) val = 0;
//val = (val*(max-min))/4096;
val = val+2048;
//return val+(max-min)/2;
return (val*(max-min))/4096;
}
static uint16_t readAnalogJoystick(void)
{
uint16_t joysval = 0;
int xReading = emu_ReadAnalogJoyX(0,256);
if (xReading > 128) joysval |= MASK_JOY2_LEFT;
else if (xReading < 128) joysval |= MASK_JOY2_RIGHT;
int yReading = emu_ReadAnalogJoyY(0,256);
if (yReading < 128) joysval |= MASK_JOY2_UP;
else if (yReading > 128) joysval |= MASK_JOY2_DOWN;
#ifdef PIN_JOY2_BTN
joysval |= (digitalRead(PIN_JOY2_BTN) == HIGH ? 0 : MASK_JOY2_BTN);
#endif
return (joysval);
}
int emu_SwapJoysticks(int statusOnly) {
if (!statusOnly) {
if (joySwapped) {
joySwapped = false;
}
else {
joySwapped = true;
}
}
return(joySwapped?1:0);
}
int emu_GetPad(void)
{
return(keypadval/*|((joySwapped?1:0)<<7)*/);
}
int emu_ReadKeys(void)
{
uint16_t retval;
uint16_t j1 = readAnalogJoystick();
uint16_t j2 = 0;
byte inputs;
Wire1.beginTransmission(0x20);
Wire1.write(0x09);
Wire1.endTransmission();
Wire1.requestFrom(0x20, 1);
inputs = Wire1.read();
// Second joystick
if ( ~inputs & 0b01000000 ) j2 |= MASK_JOY2_UP;
if ( ~inputs & 0b00100000 ) j2 |= MASK_JOY2_DOWN;
if ( ~inputs & 0b00000100 ) j2 |= MASK_JOY2_RIGHT;
if ( ~inputs & 0b10000000 ) j2 |= MASK_JOY2_LEFT;
if ( ~inputs & 0b00000001 ) j2 |= MASK_JOY2_BTN;
if (joySwapped) {
retval = ((j1 << 8) | j2);
}
else {
retval = ((j2 << 8) | j1);
}
if (usbnavpad & MASK_JOY2_UP) retval |= MASK_JOY2_UP;
if (usbnavpad & MASK_JOY2_DOWN) retval |= MASK_JOY2_DOWN;
if (usbnavpad & MASK_JOY2_LEFT) retval |= MASK_JOY2_LEFT;
if (usbnavpad & MASK_JOY2_RIGHT) retval |= MASK_JOY2_RIGHT;
if (usbnavpad & MASK_JOY2_BTN) retval |= MASK_JOY2_BTN;
if ( ~inputs & 0b00000010 ) retval |= MASK_KEY_USER1;
if ( ~inputs & 0b00001000 ) retval |= MASK_KEY_USER2;
if ( ~inputs & 0b00010000 ) retval |= MASK_KEY_USER3;
#ifdef PIN_KEY_USER4
if ( digitalRead(PIN_KEY_USER4) == LOW ) retval |= MASK_KEY_USER4;
#endif
// Serial.println(retval,HEX);
if ( ((retval & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2))
|| (retval & MASK_KEY_USER4 ) )
{
// Reset procedure T3.X and T4.0 by Frank Boesing !!
#if defined(__IMXRT1052__) || defined(__IMXRT1062__)
uint32_t tmp = SNVS_LPCR; // save control register
SNVS_LPSR |= 1;
// disable alarm
SNVS_LPCR &= ~0x02;
while (SNVS_LPCR & 0x02);
__disable_irq();
//get Time:
uint32_t lsb, msb;
do {
msb = SNVS_LPSRTCMR;
lsb = SNVS_LPSRTCLR;
} while ( (SNVS_LPSRTCLR != lsb) | (SNVS_LPSRTCMR != msb) );
uint32_t secs = (msb << 17) | (lsb >> 15);
//set alarm
secs += 2;
SNVS_LPTAR = secs;
while (SNVS_LPTAR != secs);
SNVS_LPCR = tmp | 0x02; // restore control register and set alarm
while (!(SNVS_LPCR & 0x02));
SNVS_LPCR |= (1 << 6); // turn off power
while (1) asm("wfi");
#else
*(volatile uint32_t *)0xE000ED0C = 0x5FA0004;
while (true) {
;
}
#endif
}
return (retval);
}
unsigned short emu_DebounceLocalKeys(void)
{
uint16_t bCurState = emu_ReadKeys();
uint16_t bClick = bCurState & ~bLastState;
bLastState = bCurState;
return (bClick);
}
int emu_ReadI2CKeyboard(void) {
int retval=0;
#ifdef HAS_I2CKBD
if (i2cKeyboardPresent) {
byte msg[7]={0,0,0,0,0,0,0};
Wire.requestFrom(8, 7); // request 7 bytes from slave device #8
int i = 0;
int hitindex=-1;
while (Wire.available() && (i<7) ) { // slave may send less than requested
byte b = Wire.read(); // receive a byte
if (b != 0xff) hitindex=i;
msg[i++] = b;
}
if (hitindex >=0 ) {
/*
Serial.println(msg[0], BIN);
Serial.println(msg[1], BIN);
Serial.println(msg[2], BIN);
Serial.println(msg[3], BIN);
Serial.println(msg[4], BIN);
Serial.println(msg[5], BIN);
Serial.println(msg[6], BIN);
*/
unsigned short match = (~msg[hitindex])&0x00FF | (hitindex<<8);
//Serial.println(match,HEX);
for (i=0; i<sizeof(i2ckeys); i++) {
if (match == i2ckeys[i]) {
//Serial.println((int)keys[i]);
return (keys[i]);
}
}
}
}
#endif
return(retval);
}
void emu_InitJoysticks(void) {
// Second Joystick
analogReadResolution(12);
xRef=0; yRef=0;
for (int i=0; i<10; i++) {
xRef += analogRead(PIN_JOY2_A1X);
yRef += analogRead(PIN_JOY2_A2Y);
delay(20);
}
#if INVX
xRef = 4095 -xRef/10;
#else
xRef /= 10;
#endif
#if INVY
yRef = 4095 -yRef/10;
#else
yRef /= 10;
#endif
}
static bool vkbKeepOn = false;
static bool vkbActive = false;
static bool vkeyRefresh=false;
static bool exitVkbd = false;
static uint8_t keyPressCount=0;
bool virtualkeyboardIsActive(void) {
return (vkbActive);
}
void toggleVirtualkeyboard(bool keepOn) {
if (keepOn) {
tft.drawSpriteNoDma(0,0,logo);
//prev_zt = 0;
vkbKeepOn = true;
vkbActive = true;
exitVkbd = false;
}
else {
vkbKeepOn = false;
if ( (vkbActive) /*|| (exitVkbd)*/ ) {
tft.fillScreenNoDma( RGBVAL16(0x00,0x00,0x00) );
tft.startDMA();
//prev_zt = 0;
vkbActive = false;
exitVkbd = false;
}
else {
tft.stopDMA();
tft.drawSpriteNoDma(0,0,logo);
//prev_zt = 0;
vkbActive = true;
exitVkbd = false;
}
}
}
void handleVirtualkeyboard() {
int rx=0,ry=0,rw=0,rh=0;
if (keyPressCount == 0) {
keypadval = 0;
} else {
keyPressCount--;
}
if ( (!virtualkeyboardIsActive()) && (tft.isTouching()) && (!keyPressCount) ) {
toggleVirtualkeyboard(false);
return;
}
if ( ( (vkbKeepOn) || (virtualkeyboardIsActive()) ) ) {
char c = captureTouchZone(keysw, keys, &rx,&ry,&rw,&rh);
if (c) {
tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR );
if ( (c >=1) && (c <= ACTION_MAXKBDVAL) ) {
keypadval = c;
keyPressCount = 10;
delay(50);
vkeyRefresh = true;
exitVkbd = true;
}
else if (c == ACTION_EXITKBD) {
vkeyRefresh = true;
exitVkbd = true;
}
}
}
if (vkeyRefresh) {
vkeyRefresh = false;
tft.drawSpriteNoDma(0,0,logo, rx, ry, rw, rh);
}
if ( (exitVkbd) && (vkbActive) ) {
if (!vkbKeepOn) {
toggleVirtualkeyboard(false);
}
else {
toggleVirtualkeyboard(true);
}
}
}
int emu_setKeymap(int index) {
}
static int readNbFiles(void) {
int totalFiles = 0;
#ifdef USE_SDFS
DIR dir;
FILINFO entry;
f_opendir(&dir, romspath);
while ( (true) && (totalFiles<MAX_FILES) ) {
f_readdir(&dir, &entry);
if (!entry.fname[0]) {
// no more files
break;
}
char * filename = entry.fname;
Serial.println(filename);
if ( !(entry.fattrib & AM_DIR) ) {
strncpy(&files[totalFiles][0], filename, MAX_FILENAME_SIZE-1);
totalFiles++;
}
else {
if ( (strcmp(filename,".")) && (strcmp(filename,"..")) ) {
strncpy(&files[totalFiles][0], filename, MAX_FILENAME_SIZE-1);
totalFiles++;
}
}
}
f_closedir(&dir);
#else
File entry;
file = SD.open(romspath);
while ( (true) && (totalFiles<MAX_FILES) ) {
entry = file.openNextFile();
if (! entry) {
// no more files
break;
}
#ifdef USE_SDFAT
char filename[MAX_FILENAME_SIZE];
entry.getName(&filename[0], MAX_FILENAME_SIZE);
#else
char * filename = entry.name();
#endif
Serial.println(filename);
if (!entry.isDirectory()) {
strncpy(&files[totalFiles][0], filename, MAX_FILENAME_SIZE-1);
totalFiles++;
}
else {
if ( (strcmp(filename,".")) && (strcmp(filename,"..")) ) {
strncpy(&files[totalFiles][0], filename, MAX_FILENAME_SIZE-1);
totalFiles++;
}
}
entry.close();
}
file.close();
#endif
return totalFiles;
}
void backgroundMenu(void) {
menuRedraw=true;
tft.fillScreenNoDma(RGBVAL16(0x00,0x00,0x00));
tft.drawTextNoDma(0,0, TITLE, RGBVAL16(0x00,0xff,0xff), RGBVAL16(0x00,0x00,0xff), true);
#ifndef HAS_T4_VGA
tft.drawSpriteNoDma(MENU_VBAR_XOFFSET,MENU_VBAR_YOFFSET,bmpvbar);
#endif
#ifdef OLD_LAYOUT
tft.drawSpriteNoDma(MENU_TFT_XOFFSET,MENU_TFT_YOFFSET,bmptft);
tft.drawSpriteNoDma(MENU_VGA_XOFFSET,MENU_VGA_YOFFSET,bmpvga);
#endif
}
int handleMenu(uint16_t bClick)
{
int action = ACTION_NONE;
char newpath[80];
strcpy(newpath, romspath);
strcat(newpath, "/");
strcat(newpath, selection);
int rx=0,ry=0,rw=0,rh=0;
char c = 0; //captureTouchZone(menutouchareas, menutouchactions, &rx,&ry,&rw,&rh);
if ( (bClick & MASK_JOY2_BTN) || (c == MKEY_TFT) ) {
emu_printf(newpath);
#ifdef USE_SDFS
FILINFO entry;
f_stat(newpath, &entry);
if ( (entry.fattrib & AM_DIR) ) {
#else
File file = SD.open(newpath);
if (file.isDirectory()) {
#endif
strcpy(romspath,newpath);
curFile = 0;
nbFiles = readNbFiles();
}
else {
action = ACTION_RUNTFT;
}
menuRedraw=true;
}
else if ( (bClick & MASK_KEY_USER1) || (c == MKEY_VGA) ) {
menuRedraw=true;
action = ACTION_RUNVGA;
}
else if ( (c >= MKEY_L1) && (c <= MKEY_L9) ) {
if ( (topFile+(int)c-1) <= (nbFiles-1) )
{
curFile = topFile + (int)c -1;
menuRedraw=true;
//tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR );
}
}
else if (bClick & MASK_JOY2_UP) {
if (curFile!=0) {
menuRedraw=true;
curFile--;
}
}
else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) {
if ((curFile-9)>=0) {
menuRedraw=true;
curFile -= 9;
} else if (curFile!=0) {
menuRedraw=true;
curFile--;
}
}
else if (bClick & MASK_JOY2_DOWN) {
if ((curFile<(nbFiles-1)) && (nbFiles)) {
curFile++;
menuRedraw=true;
}
}
else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) {
if ((curFile<(nbFiles-9)) && (nbFiles)) {
curFile += 9;
menuRedraw=true;
}
else if ((curFile<(nbFiles-1)) && (nbFiles)) {
curFile++;
menuRedraw=true;
}
}
else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) {
emu_SwapJoysticks(0);
menuRedraw=true;
}
if (menuRedraw && nbFiles) {
int fileIndex = 0;
tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR);
// if (curFile <= (MAX_MENULINES/2-1)) topFile=0;
// else topFile=curFile-(MAX_MENULINES/2);
if (curFile <= (MAX_MENULINES-1)) topFile=0;
else topFile=curFile-(MAX_MENULINES/2);
//Serial.print("curfile: ");
//Serial.println(curFile);
//Serial.print("topFile: ");
//Serial.println(topFile);
int i=0;
while (i<MAX_MENULINES) {
if (fileIndex>=nbFiles) {
// no more files
break;
}
char * filename = &files[fileIndex][0];
if (fileIndex >= topFile) {
if ((i+topFile) < nbFiles ) {
if ((i+topFile)==curFile) {
tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true);
strcpy(selection,filename);
}
else {
tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0xff), MENU_FILE_BGCOLOR, true);
}
}
i++;
}
fileIndex++;
}
#ifndef HAS_T4_VGA
tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy);
#endif
tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0x00,0x00,0xff), false);
menuRedraw=false;
}
return (action);
}
bool menuActive(void)
{
return (menuOn);
}
void toggleMenu(bool on) {
if (on) {
callibrationOn=false;
menuOn=true;
backgroundMenu();
} else {
menuOn = false;
}
}
char * menuSelection(void)
{
return (selection);
}
static void callibrationInit(void)
{
callibrationOn=true;
menuOn=false;
callibrationStep = 0;
calMinX=0,calMinY=0,calMaxX=0,calMaxY=0;
tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff));
tft.drawTextNoDma(0,100, " Callibration process:", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true);
tft.drawTextNoDma(0,116, " Hit the red cross at each corner", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true);
tft.drawTextNoDma(0,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true);
prev_zt = 1;
}
static void readCallibration(void)
{
char fileBuffer[64];
#ifdef USE_SDFS
FIL file;
int retval;
if( !(f_open(&file, CALIBRATION_FILE, FA_READ)) ) {
if( !(f_read (&file, fileBuffer, 64, &retval)) ) {
if (retval == 64) {
sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY);
}
}
f_close(&file);
}
#else
File file = SD.open(CALIBRATION_FILE, O_READ);
if (file) {
if ( file.read(fileBuffer, 64) ) {
sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY);
}
file.close();
Serial.println("Current callibration params:");
Serial.println(calMinX);
Serial.println(calMinY);
Serial.println(calMaxX);
Serial.println(calMaxY);
}
#endif
else {
Serial.println("Callibration read error");
}
tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY);
}
static void writeCallibration(void)
{
tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY);
#ifdef USE_SDFS
FIL file;
int retval;
if( !(f_open(&file, CALIBRATION_FILE, FA_WRITE)) ) {
//if( !(f_read (&file, fileBuffer, 64, &retval)) ) {
// if (retval == 64) {
// sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY);
// }
//}
f_close(&file);
}
#else
File file = SD.open(CALIBRATION_FILE, O_WRITE | O_CREAT | O_TRUNC);
if (file) {
file.print(calMinX);
file.print(" ");
file.print(calMinY);
file.print(" ");
file.print(calMaxX);
file.print(" ");
file.println(calMaxY);
file.close();
}
#endif
else {
Serial.println("Callibration write error");
}
}
bool callibrationActive(void)
{
return (callibrationOn);
}
int handleCallibration(uint16_t bClick) {
uint16_t xt=0;
uint16_t yt=0;
uint16_t zt=0;
if (tft.isTouching()) {
if (prev_zt == 0) {
prev_zt = 1;
tft.readRaw(&xt,&yt,&zt);
if (zt < 1000) {
return 0;
}
switch (callibrationStep)
{
case 0:
callibrationStep++;
tft.drawTextNoDma(0,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true);
tft.drawTextNoDma(TFT_REALWIDTH-8,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true);
calMinX += xt;
calMinY += yt;
break;
case 1:
callibrationStep++;
tft.drawTextNoDma(TFT_REALWIDTH-8,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true);
tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true);
calMaxX += xt;
calMinY += yt;
break;
case 2:
callibrationStep++;
tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true);
tft.drawTextNoDma(0,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true);
calMaxX += xt;
calMaxY += yt;
break;
case 3:
tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff));
tft.drawTextNoDma(0,100, " Callibration done!", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true);
tft.drawTextNoDma(0,116, " (Click center to exit)", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true);
callibrationStep++;
calMinX += xt;
calMaxY += yt;
break;
case 4:
//Serial.println(xt);
//Serial.println(yt);
if ( (xt > (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4)
&& (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) {
calMinX /= 2;
calMinY /= 2;
calMaxX /= 2;
calMaxY /= 2;
writeCallibration();
toggleMenu(true);
}
else {
callibrationInit();
}
break;
}
delay(100);
}
}
else {
prev_zt = 0;
}
}
int emu_FileOpen(char * filename)
{
int retval = 0;
char filepath[80];
strcpy(filepath, romspath);
strcat(filepath, "/");
strcat(filepath, filename);