-
Notifications
You must be signed in to change notification settings - Fork 28
/
GD2.h
1520 lines (1349 loc) · 40.8 KB
/
GD2.h
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
/*
* Copyright (C) 2013-2021 by James Bowman <jamesb@excamera.com>
* Gameduino 2/3 library for Arduino, Arduino Due, Teensy 3.2,
* ESP8266 and ESP32.
*/
#ifndef _GD2_H_INCLUDED
#define _GD2_H_INCLUDED
#define GD2_VERSION "%VERSION"
#if defined(RASPBERRY_PI) || defined(DUMPDEV)
#include "wiring.h"
#endif
#include "Arduino.h"
#include <stdarg.h>
#define RGB(r, g, b) ((uint32_t)((((r) & 0xffL) << 16) | (((g) & 0xffL) << 8) | ((b) & 0xffL)))
#define F8(x) (int((x) * 256L))
#define F16(x) ((int32_t)((x) * 65536L))
// Options for GD.begin():
#define GD_CALIBRATE 1 // enable touchscreen calibration at startup
#define GD_TRIM 2 // trim the built-in oscillator
#define GD_STORAGE 4 // initialize attached microSD
#define GD_NOBACKLIGHT 8 // don't turn on the backlight
#ifdef __SAM3X8E__
#define __DUE__ 1
#endif
#if !defined(SD_PIN) // {
#if defined(ESP8266)
#define SD_PIN D9 // pin used for the microSD enable signal
#elif defined(ARDUINO_ARCH_STM32)
#define SD_PIN PB1
#else
#define SD_PIN 9 // pin used for the microSD enable signal
#endif
#endif // }
#define BOARD_FTDI_80x 0
#define BOARD_GAMEDUINO23 1
#define BOARD_SUNFLOWER 2
#define BOARD_OTHER 3
#ifndef BOARD
#define BOARD BOARD_GAMEDUINO23 // board, from above
#endif
#define STORAGE 1 // Want SD storage?
#define CALIBRATION 1 // Want touchscreen calibration?
// FTDI boards do not have storage
#if (BOARD == BOARD_FTDI_80x) || defined(RASPBERRY_PI) || defined(DUMPDEV) || defined(SPIDRIVER)
#undef STORAGE
#define STORAGE 0
#endif
#ifndef DEFAULT_CS
#if defined(ESP8266)
#define DEFAULT_CS D8
#elif defined(ARDUINO_ARCH_STM32)
#define DEFAULT_CS PB0
#elif (BOARD == BOARD_SUNFLOWER)
#define DEFAULT_CS 6
#else
#define DEFAULT_CS 8
#endif
#endif
////////////////////////////////////////////////////////////////////////
// Decide if we want to compile in SDcard support
//
// For stock Arduino models: yes
// Raspberry PI: no
// Arduino Due: no
//
#if !defined(RASPBERRY_PI) && !defined(DUMPDEV)
#define SDCARD 1
#else
#define SDCARD 0
#endif
#if defined(__DUE__)
#define MOSI 11
#define MISO 12
#define SCK 13 // B.27
class ASPI_t {
public:
void begin(void) {
pinMode(MOSI, OUTPUT);
pinMode(MISO, INPUT);
pinMode(SCK, OUTPUT);
digitalWrite(SCK, 0);
// PIOB->PIO_PER = PIO_PB27;
// PIOB->PIO_CODR = PIO_PB27;
// PIOB->PIO_PUDR = PIO_PB27;
}
byte transfer(byte x ) {
byte r = 0;
for (byte i = 8; i; i--) {
if (x & 0x80)
PIOD->PIO_SODR = PIO_PD7;
else
PIOD->PIO_CODR = PIO_PD7;
// digitalWrite(MOSI, (x >> 7) & 1);
x <<= 1;
// digitalWrite(SCK, 1);
PIOB->PIO_SODR = PIO_PB27;
r <<= 1;
r |= digitalRead(MISO);
// digitalWrite(SCK, 0);
PIOB->PIO_CODR = PIO_PB27;
}
return r;
}
void transfer(byte*m, int s) {
while (s--) {
*m = transfer(*m);
m++;
}
}
};
static class ASPI_t ASPI;
#define SPI ASPI
#endif
#if defined(ARDUINO_STM32L4_BLACKICE)
// BlackIce Board uses SPI1 on the Arduino header.
#define SPI SPI1
// Board Support:
// JSON: http://www.hamnavoe.com/package_millerresearch_mystorm_index.json
// Source: https://github.com/millerresearch/arduino-mystorm
#endif
#if SDCARD
#if defined(VERBOSE) && (VERBOSE > 0)
#define INFO(X) Serial.println((X))
#if defined(RASPBERRY_PI)
#define REPORT(VAR) fprintf(stderr, #VAR "=%d\n", (VAR))
#else
#define REPORT(VAR) (Serial.print(#VAR "="), Serial.print(VAR, DEC), Serial.print(' '), Serial.println(VAR, HEX))
#endif
#else
#define INFO(X)
#define REPORT(X)
#endif
struct dirent {
char name[8];
char ext[3];
byte attribute;
byte reserved[8];
uint16_t cluster_hi; // FAT32 only
uint16_t time;
uint16_t date;
uint16_t cluster;
uint32_t size;
};
// https://www.sdcard.org/downloads/pls/simplified_specs/Part_1_Physical_Layer_Simplified_Specification_Ver_3.01_Final_100518.pdf
// page 22
// http://mac6.ma.psu.edu/space2008/RockSat/microController/sdcard_appnote_foust.pdf
// http://elm-chan.org/docs/mmc/mmc_e.html
// http://www.pjrc.com/tech/8051/ide/fat32.html
#define FAT16 0
#define FAT32 1
class sdcard {
public:
void sel() {
digitalWrite(pin, LOW);
delay(1);
}
void desel() {
digitalWrite(pin, HIGH);
SPI.transfer(0xff); // force DO release
}
void sd_delay(byte n) {
while (n--) {
SPI.transfer(0xff);
}
}
void cmd(byte cmd, uint32_t lba = 0, uint8_t crc = 0x95) {
#if VERBOSE > 1
Serial.print("cmd ");
Serial.print(cmd, DEC);
Serial.print(" ");
Serial.print(lba, HEX);
Serial.println();
#endif
sel();
SPI.transfer(0x40 | cmd);
SPI.transfer(0xff & (lba >> 24));
SPI.transfer(0xff & (lba >> 16));
SPI.transfer(0xff & (lba >> 8));
SPI.transfer(0xff & (lba));
SPI.transfer(crc);
}
byte response() {
byte r;
r = SPI.transfer(0xff);
while (r & 0x80) {
r = SPI.transfer(0xff);
}
return r;
}
byte R1() { // read response R1
byte r = response();
desel();
SPI.transfer(0xff); // trailing byte
return r;
}
byte sdR3(uint32_t &ocr) { // read response R3
byte r = response();
for (byte i = 4; i; i--)
ocr = (ocr << 8) | SPI.transfer(0xff);
SPI.transfer(0xff); // trailing byte
desel();
return r;
}
byte sdR7() { // read response R3
byte r = response();
for (byte i = 4; i; i--)
// Serial.println(SPI.transfer(0xff), HEX);
SPI.transfer(0xff);
desel();
return r;
}
void appcmd(byte cc, uint32_t lba = 0) {
cmd(55); R1();
cmd(cc, lba);
}
void begin(byte p) {
byte type_code;
byte sdhc;
pin = p;
pinMode(pin, OUTPUT);
#if !defined(__DUE__) && !defined(TEENSYDUINO) && !defined(ARDUINO_ARCH_STM32L4)
SPI.setClockDivider(SPI_CLOCK_DIV64);
#endif
desel();
// for (;;) SPI.transfer(0xff);
delay(50); // wait for boot
sd_delay(10); // deselected, 80 pulses
INFO("Attempting card reset... ");
byte r1;
static int attempts;
attempts = 0;
do { // reset, enter idle
cmd(0);
while ((r1 = SPI.transfer(0xff)) & 0x80)
if (++attempts == 1000)
goto finished;
desel();
SPI.transfer(0xff); // trailing byte
REPORT(r1);
} while (r1 != 1);
INFO("reset ok\n");
sdhc = 0;
cmd(8, 0x1aa, 0x87);
r1 = sdR7();
sdhc = (r1 == 1);
REPORT(sdhc);
INFO("Sending card init command");
attempts = 0;
while (1) {
appcmd(41, sdhc ? (1UL << 30) : 0); // card init
r1 = R1();
#if VERBOSE
Serial.println(r1, HEX);
#endif
if ((r1 & 1) == 0)
break;
if (++attempts == 300)
goto finished;
delay(1);
}
INFO("OK");
if (sdhc) {
uint32_t OCR = 0;
for (int i = 10; i; i--) {
cmd(58);
sdR3(OCR);
REPORT(OCR);
}
ccs = 1UL & (OCR >> 30);
} else {
ccs = 0;
}
REPORT(ccs);
// Test point: dump sector 0 to serial.
// should see first 512 bytes of card, ending 55 AA.
#if 0
cmd17(0);
for (int i = 0; i < 512; i++) {
delay(10);
byte b = SPI.transfer(0xff);
Serial.print(b, HEX);
Serial.print(' ');
if ((i & 15) == 15)
Serial.println();
}
desel();
for (;;);
#endif
#if !defined(__DUE__) && !defined(ESP8266) && !defined(ESP32) && !defined(ARDUINO_ARCH_STM32L4) && !defined(ARDUINO_ARCH_STM32)
SPI.setClockDivider(SPI_CLOCK_DIV2);
SPSR = (1 << SPI2X);
#endif
#if defined(ESP8266)
SPI.setFrequency(40000000L);
#elif defined(ESP32)
SPI.setFrequency(25000000L);
#elif defined(ARDUINO_ARCH_STM32)
SPI.beginTransaction(SPISettings(18000000, MSBFIRST, SPI_MODE0));
#endif
type_code = rd(0x1be + 0x4);
switch (type_code) {
default:
type = FAT16;
break;
case 0x0b:
case 0x0c:
type = FAT32;
break;
}
REPORT(type_code);
o_partition = 512L * rd4(0x1be + 0x8);
sectors_per_cluster = rd(o_partition + 0xd);
reserved_sectors = rd2(o_partition + 0xe);
cluster_size = 512L * sectors_per_cluster;
REPORT(sectors_per_cluster);
// Serial.println("Bytes per sector: %d\n", rd2(o_partition + 0xb));
// Serial.println("Sectors per cluster: %d\n", sectors_per_cluster);
if (type == FAT16) {
max_root_dir_entries = rd2(o_partition + 0x11);
sectors_per_fat = rd2(o_partition + 0x16);
o_fat = o_partition + 512L * reserved_sectors;
o_root = o_fat + (2 * 512L * sectors_per_fat);
// data area starts with cluster 2, so offset it here
o_data = o_root + (max_root_dir_entries * 32L) - (2L * cluster_size);
} else {
uint32_t sectors_per_fat = rd4(o_partition + 0x24);
root_dir_first_cluster = rd4(o_partition + 0x2c);
uint32_t fat_begin_lba = (o_partition >> 9) + reserved_sectors;
uint32_t cluster_begin_lba = (o_partition >> 9) + reserved_sectors + (2 * sectors_per_fat);
o_fat = 512L * fat_begin_lba;
o_root = (512L * (cluster_begin_lba + (root_dir_first_cluster - 2) * sectors_per_cluster));
o_data = (512L * (cluster_begin_lba - 2 * sectors_per_cluster));
}
finished:
INFO("finished");
;
}
void cmd17(uint32_t off) {
REPORT(off);
if (ccs)
cmd(17, off >> 9);
else
cmd(17, off & ~511L);
R1();
sel();
while (SPI.transfer(0xff) != 0xfe)
;
}
void rdn(byte *d, uint32_t off, uint16_t n) {
cmd17(off);
uint16_t i;
uint16_t bo = (off & 511);
for (i = 0; i < bo; i++)
SPI.transfer(0xff);
for (i = 0; i < n; i++)
*d++ = SPI.transfer(0xff);
for (i = 0; i < (514 - bo - n); i++)
SPI.transfer(0xff);
desel();
}
uint32_t rd4(uint32_t off) {
uint32_t r;
rdn((byte*)&r, off, sizeof(r));
return r;
}
uint16_t rd2(uint32_t off) {
uint16_t r;
rdn((byte*)&r, off, sizeof(r));
return r;
}
byte rd(uint32_t off) {
byte r;
rdn((byte*)&r, off, sizeof(r));
return r;
}
byte pin;
byte ccs;
byte type;
uint16_t sectors_per_cluster;
uint16_t reserved_sectors;
uint16_t max_root_dir_entries;
uint16_t sectors_per_fat;
uint16_t cluster_size;
uint32_t root_dir_first_cluster;
// These are all linear addresses, hence the o_ prefix
uint32_t o_partition;
uint32_t o_fat;
uint32_t o_root;
uint32_t o_data;
};
static void dos83(byte dst[11], const char *ps)
{
byte i = 0;
while (*ps) {
if (*ps != '.')
dst[i++] = toupper(*ps);
else {
while (i < 8)
dst[i++] = ' ';
}
ps++;
}
while (i < 11)
dst[i++] = ' ';
}
#else
class sdcard {
public:
void begin(int p) {};
};
#endif
////////////////////////////////////////////////////////////////////////
class xy {
public:
int x, y;
void set(int _x, int _y);
void rmove(int distance, int angle);
int angleto(class xy &other);
void draw(byte offset = 0);
void rotate(int angle);
int onscreen(void);
class xy operator<<=(int d);
class xy operator+=(class xy &other);
class xy operator-=(class xy &other);
long operator*(class xy &other);
class xy operator*=(int);
int nearer_than(int distance, xy &other);
};
class Bitmap {
public:
xy size, center;
uint32_t source;
uint16_t format;
int8_t handle;
void fromtext(int font, const char* s);
void fromfile(const char *filename, int format = 7);
void bind(uint8_t handle);
void wallpaper();
void draw(int x, int y, int16_t angle = 0);
void draw(const xy &pos, int16_t angle = 0);
private:
void defaults(uint8_t f);
void setup(void);
};
class Bitmap __fromatlas(uint32_t addr);
////////////////////////////////////////////////////////////////////////
enum Primitive {
BITMAPS = 1,
POINTS = 2,
LINES = 3,
LINE_STRIP = 4,
EDGE_STRIP_R = 5,
EDGE_STRIP_L = 6,
EDGE_STRIP_A = 7,
EDGE_STRIP_B = 8,
RECTS = 9,
};
class GDClass {
public:
int w, h;
uint32_t loadptr;
byte vxf; // Vertex Format
void begin(uint8_t options = (GD_CALIBRATE | GD_TRIM | GD_STORAGE), int cs = DEFAULT_CS, int sdcs = SD_PIN);
uint16_t random();
uint16_t random(uint16_t n);
uint16_t random(uint16_t n0, uint16_t n1);
void seed(uint16_t n);
int16_t rsin(int16_t r, uint16_t th);
int16_t rcos(int16_t r, uint16_t th);
void polar(int &x, int &y, int16_t r, uint16_t th);
uint16_t atan2(int16_t y, int16_t x);
#if !defined(ESP8266) && !defined(ESP32) && !defined(TEENSYDUINO)
void copy(const PROGMEM uint8_t *src, int count);
#else
void copy(const uint8_t *src, int count);
#endif
void copyram(byte *src, int count);
void self_calibrate(void);
void swap(void);
void flush(void);
void finish(void);
void play(uint8_t instrument, uint8_t note = 0);
void sample(uint32_t start, uint32_t len, uint16_t freq, uint16_t format, int loop = 0);
void get_inputs(void);
void get_accel(int &x, int &y, int &z);
struct _wii {
byte active;
xy l, r;
uint16_t buttons;
};
struct {
uint16_t track_tag;
uint16_t track_val;
uint16_t rz;
uint16_t __dummy_1;
int16_t y;
int16_t x;
int16_t tag_y;
int16_t tag_x;
uint8_t tag;
uint8_t ptag;
uint8_t touching;
xy xytouch;
struct _wii wii[2];
} inputs;
void AlphaFunc(byte func, byte ref);
void Begin(Primitive prim);
void BitmapHandle(byte handle);
void BitmapLayout(byte format, uint16_t linestride, uint16_t height);
void BitmapSize(byte filter, byte wrapx, byte wrapy, uint16_t width, uint16_t height);
void BitmapSource(uint32_t addr);
void BitmapTransformA(int32_t a);
void BitmapTransformB(int32_t b);
void BitmapTransformC(int32_t c);
void BitmapTransformD(int32_t d);
void BitmapTransformE(int32_t e);
void BitmapTransformF(int32_t f);
void BlendFunc(byte src, byte dst);
void Call(uint16_t dest);
void Cell(byte cell);
void ClearColorA(byte alpha);
void ClearColorRGB(byte red, byte green, byte blue);
void ClearColorRGB(uint32_t rgb);
void Clear(byte c, byte s, byte t);
void Clear(void);
void ClearStencil(byte s);
void ClearTag(byte s);
void ColorA(byte alpha);
void ColorMask(byte r, byte g, byte b, byte a);
void ColorRGB(byte red, byte green, byte blue);
void ColorRGB(uint32_t rgb);
void Display(void);
void End(void);
void Jump(uint16_t dest);
void LineWidth(uint16_t width);
void Macro(byte m);
void PointSize(uint16_t size);
void RestoreContext(void);
void Return(void);
void SaveContext(void);
void ScissorSize(uint16_t width, uint16_t height);
void ScissorXY(uint16_t x, uint16_t y);
void StencilFunc(byte func, byte ref, byte mask);
void StencilMask(byte mask);
void StencilOp(byte sfail, byte spass);
void TagMask(byte mask);
void Tag(byte s);
void Vertex2f(int16_t x, int16_t y);
void Vertex2ii(uint16_t x, uint16_t y, byte handle = 0, byte cell = 0);
void VertexFormat(byte frac);
void BitmapLayoutH(byte linestride, byte height);
void BitmapSizeH(byte width, byte height);
void PaletteSource(uint32_t addr);
void VertexTranslateX(uint32_t x);
void VertexTranslateY(uint32_t y);
void Nop(void);
void BitmapExtFormat(uint16_t format);
void BitmapSwizzle(byte r, byte g, byte b, byte a);
// Higher-level graphics commands
void cmd_append(uint32_t ptr, uint32_t num);
void cmd_bgcolor(uint32_t c);
void cmd_button(int16_t x, int16_t y, uint16_t w, uint16_t h, byte font, uint16_t options, const char *s);
void cmd_calibrate(void);
void cmd_clock(int16_t x, int16_t y, int16_t r, uint16_t options, uint16_t h, uint16_t m, uint16_t s, uint16_t ms);
void cmd_coldstart(void);
void cmd_dial(int16_t x, int16_t y, int16_t r, uint16_t options, uint16_t val);
void cmd_dlstart(void);
void cmd_fgcolor(uint32_t c);
void cmd_gauge(int16_t x, int16_t y, int16_t r, uint16_t options, uint16_t major, uint16_t minor, uint16_t val, uint16_t range);
void cmd_getmatrix(void);
void cmd_getprops(uint32_t &ptr, uint32_t &w, uint32_t &h);
void cmd_getptr(void);
void cmd_gradcolor(uint32_t c);
void cmd_gradient(int16_t x0, int16_t y0, uint32_t rgb0, int16_t x1, int16_t y1, uint32_t rgb1);
void cmd_inflate(uint32_t ptr);
void cmd_interrupt(uint32_t ms);
void cmd_keys(int16_t x, int16_t y, int16_t w, int16_t h, byte font, uint16_t options, const char*s);
void cmd_loadidentity(void);
void cmd_loadimage(uint32_t ptr, int32_t options);
void cmd_memcpy(uint32_t dest, uint32_t src, uint32_t num);
void cmd_memset(uint32_t ptr, byte value, uint32_t num);
uint32_t cmd_memcrc(uint32_t ptr, uint32_t num);
void cmd_memwrite(uint32_t ptr, uint32_t num);
void cmd_regwrite(uint32_t ptr, uint32_t val);
void cmd_number(int16_t x, int16_t y, byte font, uint16_t options, uint32_t n);
void cmd_progress(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t options, uint16_t val, uint16_t range);
void cmd_regread(uint32_t ptr);
void cmd_rotate(int32_t a);
void cmd_scale(int32_t sx, int32_t sy);
void cmd_screensaver(void);
void cmd_scrollbar(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t options, uint16_t val, uint16_t size, uint16_t range);
void cmd_setfont(byte font, uint32_t ptr);
void cmd_setmatrix(void);
void cmd_sketch(int16_t x, int16_t y, uint16_t w, uint16_t h, uint32_t ptr, uint16_t format);
void cmd_slider(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t options, uint16_t val, uint16_t range);
void cmd_snapshot(uint32_t ptr);
void cmd_spinner(int16_t x, int16_t y, byte style, byte scale);
void cmd_stop(void);
void cmd_swap(void);
void cmd_text(int16_t x, int16_t y, byte font, uint16_t options, const char *s);
void cmd_toggle(int16_t x, int16_t y, int16_t w, byte font, uint16_t options, uint16_t state, const char *s);
void cmd_track(int16_t x, int16_t y, uint16_t w, uint16_t h, byte tag);
void cmd_translate(int32_t tx, int32_t ty);
void cmd_playvideo(int32_t options);
void cmd_romfont(uint32_t font, uint32_t romslot);
void cmd_mediafifo(uint32_t ptr, uint32_t size);
void cmd_setbase(uint32_t b);
void cmd_videoframe(uint32_t dst, uint32_t ptr);
void cmd_snapshot2(uint32_t fmt, uint32_t ptr, int16_t x, int16_t y, int16_t w, int16_t h);
void cmd_setfont2(uint32_t font, uint32_t ptr, uint32_t firstchar);
void cmd_setrotate(uint32_t r);
void cmd_videostart();
void cmd_setbitmap(uint32_t source, uint16_t fmt, uint16_t w, uint16_t h);
void cmd_sync();
void cmd_flasherase();
void cmd_flashwrite(uint32_t dest, uint32_t num);
void cmd_flashupdate(uint32_t dst, uint32_t src, uint32_t n);
void cmd_flashread(uint32_t dst, uint32_t src, uint32_t n);
void cmd_flashdetach();
void cmd_flashattach();
uint32_t cmd_flashfast(uint32_t &r);
void cmd_flashspidesel();
void cmd_flashspitx(uint32_t num);
void cmd_flashspirx(uint32_t ptr, uint32_t num);
byte rd(uint32_t addr);
void wr(uint32_t addr, uint8_t v);
uint16_t rd16(uint32_t addr);
void wr16(uint32_t addr, uint16_t v);
uint32_t rd32(uint32_t addr);
void wr32(uint32_t addr, uint32_t v);
void wr_n(uint32_t addr, byte *src, uint32_t n);
void rd_n(byte *dst, uint32_t addr, uint32_t n);
void cmd32(uint32_t b);
void bulkrd(uint32_t a);
void resume(void);
void __end(void);
void reset(void);
void dumpscreen(void);
byte load(const char *filename, void (*progress)(long, long) = NULL);
void safeload(const char *filename);
void alert();
void textsize(int &w, int &h, int font, const char *s);
sdcard SD;
void storage(void);
void tune(void);
private:
static void cFFFFFF(byte v);
static void cI(uint32_t);
static void ci(int32_t);
static void cH(uint16_t);
static void ch(int16_t);
static void cs(const char *);
static void fmtcmd(const char *fmt, ...);
static void align(byte n);
void cmdbyte(uint8_t b);
uint32_t measure_freq(void);
uint32_t rseed;
byte cprim; // current primitive
};
extern GDClass GD;
extern byte ft8xx_model;
#if SDCARD
class Reader {
public:
int openfile(const char *filename) {
int i = 0;
byte dosname[11];
dirent de;
dos83(dosname, filename);
do {
GD.SD.rdn((byte*)&de, GD.SD.o_root + i * 32, sizeof(de));
if (0 == memcmp(de.name, dosname, 11)) {
begin(de);
return 1;
}
i++;
} while (de.name[0]);
return 0;
}
void begin(dirent &de) {
nseq = 0;
size = de.size;
cluster0 = de.cluster;
if (GD.SD.type == FAT32)
cluster0 |= ((long)de.cluster_hi << 16);
rewind();
}
void rewind(void) {
cluster = cluster0;
sector = 0;
offset = 0;
}
void nextcluster() {
if (GD.SD.type == FAT16)
cluster = GD.SD.rd2(GD.SD.o_fat + 2 * cluster);
else
cluster = GD.SD.rd4(GD.SD.o_fat + 4 * cluster);
#if VERBOSE
Serial.print("nextcluster=");
Serial.println(cluster, DEC);
#endif
}
void fetch512(byte *dst) {
#if defined(__DUE__) || defined(TEENSYDUINO) || defined(ESP8266) || defined(ESP32) || 1
#if defined(ARDUINO_ARCH_STM32)
SPI.read(dst, 512);
#elif defined(ESP8266) || defined(ESP32)
SPI.transferBytes(NULL, dst, 512);
#else
// for (int i = 0; i < 512; i++) *dst++ = SPI.transfer(0xff);
memset(dst, 0xff, 512); SPI.transfer(dst, 512);
#endif
SPI.transfer(0xff); // consume CRC
SPI.transfer(0xff);
#else
SPDR = 0xff;
asm volatile("nop"); while (!(SPSR & _BV(SPIF))) ;
for (int i = 0; i < 512; i++) {
while (!(SPSR & _BV(SPIF))) ;
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
*dst++ = SPDR;
SPDR = 0xff;
}
asm volatile("nop"); while (!(SPSR & _BV(SPIF))) ;
SPI.transfer(0xff);
#endif
GD.SD.desel();
}
void nextcluster2(byte *dst) {
if (nseq) {
nseq--;
cluster++;
return;
}
uint32_t off = GD.SD.o_fat + 4 * cluster;
GD.SD.cmd17(off & ~511L);
fetch512(dst);
int i = off & 511;
cluster = *(uint32_t*)&dst[i];
nseq = 0;
for (uint32_t c = cluster;
(i < 512) && *(uint32_t*)&dst[i] == c;
i += 4, c++)
nseq++;
}
void skipcluster() {
nextcluster();
offset += GD.SD.cluster_size;
}
void skipsector() {
if (sector == GD.SD.sectors_per_cluster) {
sector = 0;
nextcluster();
}
sector++;
offset += 512;
}
void seek(uint32_t o) {
union {
uint8_t buf[512];
uint32_t fat32[128];
uint16_t fat16[256];
};
uint32_t co = ~0;
if (o < offset)
rewind();
while (offset < o) {
if ((sector == GD.SD.sectors_per_cluster) && ((o - offset) > (long)GD.SD.cluster_size)) {
uint32_t o;
if (GD.SD.type == FAT16)
o = (GD.SD.o_fat + 2 * cluster) & ~511;
else
o = (GD.SD.o_fat + 4 * cluster) & ~511;
if (o != co) {
GD.SD.rdn(buf, o, 512);
co = o;
}
cluster = fat32[cluster & 127];
offset += GD.SD.cluster_size;
} else
skipsector();
}
}
void readsector(byte *dst) {
if (sector == GD.SD.sectors_per_cluster) {
sector = 0;
nextcluster2(dst);
}
REPORT(cluster);
uint32_t off = GD.SD.o_data + ((long)GD.SD.cluster_size * cluster) + (512L * sector);
REPORT(off);
GD.SD.cmd17(off & ~511L);
REPORT(off);
sector++;
offset += 512;
fetch512(dst);
}
int eof(void) {
return size <= offset;
}
uint32_t cluster, cluster0;
uint32_t offset;
uint32_t size;
byte sector;
byte nseq;
};
#endif
typedef struct {
byte handle;
uint16_t w, h;
uint16_t size;
} shape_t;
// convert integer pixels to subpixels
#define PIXELS(x) int((x) * 16)
// Convert degrees to Furmans
#define DEGREES(n) ((65536L * (n)) / 360)
#define NEVER 0
#define LESS 1
#define LEQUAL 2
#define GREATER 3
#define GEQUAL 4
#define EQUAL 5
#define NOTEQUAL 6
#define ALWAYS 7
#define ARGB1555 0
#define L1 1
#define L4 2
#define L8 3
#define RGB332 4
#define ARGB2 5
#define ARGB4 6
#define RGB565 7
#define PALETTED 8
#define TEXT8X8 9
#define TEXTVGA 10
#define BARGRAPH 11
#define PALETTED565 14
#define PALETTED4444 15
#define PALETTED8 16
#define L2 17
#define GLFORMAT 31
#define NEAREST 0
#define BILINEAR 1
#define BORDER 0
#define REPEAT 1
#define KEEP 1
#define REPLACE 2
#define INCR 3
#define DECR 4
#define INVERT 5
#define DLSWAP_DONE 0
#define DLSWAP_LINE 1
#define DLSWAP_FRAME 2
#define INT_SWAP 1
#define INT_TOUCH 2
#define INT_TAG 4
#define INT_SOUND 8
#define INT_PLAYBACK 16
#define INT_CMDEMPTY 32
#define INT_CMDFLAG 64
#define INT_CONVCOMPLETE 128
#define TOUCHMODE_OFF 0
#define TOUCHMODE_ONESHOT 1
#define TOUCHMODE_FRAME 2
#define TOUCHMODE_CONTINUOUS 3
#define ZERO 0