forked from pr3y/Bruce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VectorDisplay.h
executable file
·1320 lines (1130 loc) · 41.7 KB
/
VectorDisplay.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
#ifndef _VECTOR_DISPLAY_H
#define _VECTOR_DISPLAY_H
#ifndef ARDUINO
#define NO_SERIAL
#include <stdint.h>
#define pgm_read_byte_near(a) (*(uint8_t*)(a))
#include <sys/timeb.h>
#include <string>
typedef std::string String;
uint32_t millis() {
struct timeb t;
ftime(&t);
return t.millitm + t.time * 1000;
}
class Print {
public:
virtual size_t write(uint8_t c) = 0;
virtual size_t write(const uint8_t* s, size_t length) {
size_t wrote = 0;
while (length > 0) {
size_t b = write(*s++);
if (b <= 0)
break;
b++;
length--;
}
return wrote;
}
virtual size_t write(const char* s) {
return write((uint8_t*)s, strlen(s));
}
};
class Stream : public Print {
public:
virtual int read() = 0;
virtual int available() = 0;
};
#else
#include <Arduino.h>
#endif
#ifdef ESP8266
# include <ESP8266WiFi.h>
#endif
#define VECTOR_DISPLAY_MESSAGE_SIZE 8
#define VECTOR_DISPLAY_MAX_STRING 256
#define VECTOR_DISPLAY_DEFAULT_WIDTH 240
#define VECTOR_DISPLAY_DEFAULT_HEIGHT 320
#define ALIGN_LEFT 'l'
#define ALIGN_RIGHT 'r'
#define ALIGN_CENTER 'c'
#define ALIGN_TOP 't'
#define ALIGN_BOTTOM 'b'
#define ALIGN_BASELINE 'l'
#ifndef VECTOR_DISPLAY_SEND_DELAY
#define VECTOR_DISPLAY_SEND_DELAY 0
#endif
#define TFT_BLACK 0x0000 /* 0, 0, 0 */
#define TFT_NAVY 0x000F /* 0, 0, 128 */
#define TFT_DARKGREEN 0x03E0 /* 0, 128, 0 */
#define TFT_DARKCYAN 0x03EF /* 0, 128, 128 */
#define TFT_MAROON 0x7800 /* 128, 0, 0 */
#define TFT_PURPLE 0x780F /* 128, 0, 128 */
#define TFT_OLIVE 0x7BE0 /* 128, 128, 0 */
#define TFT_LIGHTGREY 0xC618 /* 192, 192, 192 */
#define TFT_DARKGREY 0x7BEF /* 128, 128, 128 */
#define TFT_BLUE 0x001F /* 0, 0, 255 */
#define TFT_GREEN 0x07E0 /* 0, 255, 0 */
#define TFT_CYAN 0x07FF /* 0, 255, 255 */
#define TFT_RED 0xF800 /* 255, 0, 0 */
#define TFT_MAGENTA 0xF81F /* 255, 0, 255 */
#define TFT_YELLOW 0xFFE0 /* 255, 255, 0 */
#define TFT_WHITE 0xFFFF /* 255, 255, 255 */
#define TFT_ORANGE 0xFD20 /* 255, 165, 0 */
#define TFT_GREENYELLOW 0xAFE5 /* 173, 255, 47 */
#define TFT_PINK 0xF81F
// Color definitions for backwards compatibility
#define ILI9341_BLACK 0x0000 /* 0, 0, 0 */
#define ILI9341_NAVY 0x000F /* 0, 0, 128 */
#define ILI9341_DARKGREEN 0x03E0 /* 0, 128, 0 */
#define ILI9341_DARKCYAN 0x03EF /* 0, 128, 128 */
#define ILI9341_MAROON 0x7800 /* 128, 0, 0 */
#define ILI9341_PURPLE 0x780F /* 128, 0, 128 */
#define ILI9341_OLIVE 0x7BE0 /* 128, 128, 0 */
#define ILI9341_LIGHTGREY 0xC618 /* 192, 192, 192 */
#define ILI9341_DARKGREY 0x7BEF /* 128, 128, 128 */
#define ILI9341_BLUE 0x001F /* 0, 0, 255 */
#define ILI9341_GREEN 0x07E0 /* 0, 255, 0 */
#define ILI9341_CYAN 0x07FF /* 0, 255, 255 */
#define ILI9341_RED 0xF800 /* 255, 0, 0 */
#define ILI9341_MAGENTA 0xF81F /* 255, 0, 255 */
#define ILI9341_YELLOW 0xFFE0 /* 255, 255, 0 */
#define ILI9341_WHITE 0xFFFF /* 255, 255, 255 */
#define ILI9341_ORANGE 0xFD20 /* 255, 165, 0 */
#define ILI9341_GREENYELLOW 0xAFE5 /* 173, 255, 47 */
#define ILI9341_PINK 0xF81F
#define MESSAGE_DOWN 'D'
#define MESSAGE_UP 'U'
#define MESSAGE_MOVE 'M'
#define MESSAGE_BUTTON 'B'
#define MESSAGE_ACK 'A'
typedef uint32_t FixedPoint32;
#define TO_FP32(f) ((uint32_t)((f)*65536. + 0.5))
struct VectorDisplayMessage {
char what;
char what2;
union {
uint8_t button;
struct {
int16_t x;
int16_t y;
} xy;
} data;
} __attribute__((packed));
class VectorDisplayClass : public Print {
private:
static const uint32_t MAX_BUFFER = (uint32_t)1024*256;
static const uint32_t MESSAGE_TIMEOUT = 3000;
static const uint8_t FLAG_LOW_ENDIAN_BITS = 1;
static const uint8_t FLAG_HAVE_MASK = 2;
static const uint8_t FLAG_PAD_BYTE = 4;
static const uint8_t FLAG_LOW_ENDIAN_BYTES = 8;
bool waitForAck = true;
int gfxFontSize = 1;
int curx = 0;
int cury = 0;
int readPos = 0;
int32_t curForeColor565 = -1;
uint32_t lastMessageStart = 0;
int pointerX;
int pointerY;
int curWidth = VECTOR_DISPLAY_DEFAULT_WIDTH;
int curHeight = VECTOR_DISPLAY_DEFAULT_HEIGHT;
uint8_t curRotation = 0;
bool pointerDown = false;
bool wrap = 1;
bool fixCP437 = true;
uint16_t polyLineCount;
uint8_t polyLineSum;
uint32_t delayTime = 0;
uint8_t readBuf[VECTOR_DISPLAY_MESSAGE_SIZE];
union {
uint32_t color;
uint16_t twoByte[9];
struct {
uint16_t x;
uint16_t y;
char text[VECTOR_DISPLAY_MAX_STRING+1];
} __attribute__((packed)) xyText;
struct {
uint16_t endianness;
uint16_t width;
uint16_t height;
FixedPoint32 aspectRatio;
uint16_t reserved[3];
} __attribute__((packed)) initialize;
struct {
uint8_t c;
char text[VECTOR_DISPLAY_MAX_STRING+1];
} __attribute__((packed)) charText;
struct {
uint16_t width;
uint16_t height;
} __attribute__((packed)) coords;
struct {
char attr;
uint8_t value;
} __attribute__((packed)) attribute8;
struct {
char attr;
uint16_t value;
} __attribute__((packed)) attribute16;
struct {
char attr;
uint32_t value;
} __attribute__((packed)) attribute32;
struct {
uint32_t length;
uint8_t depth;
uint8_t flags;
uint16_t x;
uint16_t y;
uint16_t w;
uint16_t h;
uint32_t foreColor; // only if depth==1
uint32_t backColor; // only if depth==1
} __attribute__((packed)) bitmap;
struct {
uint16_t x1;
uint16_t y1;
uint16_t x2;
uint16_t y2;
uint16_t r;
uint8_t filled;
} __attribute__((packed)) roundedRectangle;
struct {
uint16_t x;
uint16_t y;
uint16_t r;
FixedPoint32 angle1;
FixedPoint32 sweep;
uint8_t filled;
} __attribute__((packed)) arc;
struct {
char attr;
uint16_t values[2];
} __attribute__((packed)) attribute16x2;
uint8_t bytes[VECTOR_DISPLAY_MAX_STRING+1];
char text[VECTOR_DISPLAY_MAX_STRING+1];
} args;
uint32_t lastSend = 0;
private:
inline void sendDelay() {
if (delayTime>0) {
while(millis()-lastSend < delayTime) ;
lastSend = millis();
}
}
public:
int textsize = 1;
uint32_t textcolor = TFT_WHITE;
uint32_t textbgcolor = TFT_BLACK;
void setWaitForAck(bool wait) {
waitForAck = wait;
}
void setDelay(uint32_t delayMillis) {
delayTime = delayMillis;
lastSend = millis();
}
virtual void remoteFlush() {
while(remoteAvailable())
remoteRead();
}
virtual int remoteRead() = 0; // must be non-blocking
virtual void remoteWrite(uint8_t c) = 0;
virtual void remoteWrite(const void* data, size_t n) = 0;
virtual size_t remoteAvailable() = 0;
void attribute8(char a, uint8_t value) {
args.attribute8.attr = a;
args.attribute8.value = value;
sendCommand('Y', &args, 2);
}
void attribute8(char a, bool value) {
args.attribute8.attr = a;
args.attribute8.value = value ? 1 : 0;
sendCommand('Y', &args, 2);
}
void attribute16(char a, uint16_t value) {
args.attribute16.attr = a;
args.attribute16.value = value;
sendCommand('A', &args, 3);
}
void attribute32(char a, uint32_t value) {
args.attribute32.attr = a;
args.attribute32.value = value;
sendCommand('B', &args, 5);
}
void sendCommand(char c, const void* arguments, int argumentsLength) {
sendDelay();
remoteWrite(c);
remoteWrite(c^0xFF);
if (argumentsLength > 0)
remoteWrite((uint8_t*)arguments, argumentsLength);
uint8_t sum = 0;
for (int i = 0; i<argumentsLength; i++)
sum += ((uint8_t*)arguments)[i];
remoteWrite((uint8_t)(sum^0xFF));
}
void sendCommandWithAck(char c, const void* arguments, int argumentsLength) {
readPos = 0;
bool done = false;
do {
uint32_t t0 = millis();
sendCommand(c, arguments, argumentsLength);
if (!waitForAck)
return;
while ((millis()-t0) < 500) {
if (readMessage(NULL) && !memcmp(readBuf, "Acknwld", 7) && readBuf[7]==c) {
done = true;
break;
}
}
} while(!done);
}
uint16_t width() {
return (curRotation%2)?curHeight:curWidth;
}
uint16_t height() {
return (curRotation%2)?curWidth:curHeight;
}
uint8_t sumBytes(void* data, int length) {
uint8_t* p = (uint8_t*)data;
uint8_t s = 0;
while(length-- > 0)
s += *p++;
return s;
}
void startPoly(char c, uint16_t n) {
polyLineCount = n;
remoteWrite(c);
remoteWrite(c^0xFF);
args.twoByte[0] = n;
remoteWrite((uint8_t*)&args, 2);
polyLineSum = args.bytes[0] + args.bytes[1];
}
void startFillPoly(uint16_t n) {
startPoly('N', n);
}
void startPolyLine(uint16_t n) {
startPoly('O', n);
}
void addPolyLine(int16_t x, int16_t y) {
if (polyLineCount>0) {
args.twoByte[0] = x;
args.twoByte[1] = y;
remoteWrite((uint8_t*)&args, 4);
polyLineSum += args.bytes[0] + args.bytes[1] + args.bytes[2] + args.bytes[3];
polyLineCount--;
if (polyLineCount == 0) {
remoteWrite(0xFF^polyLineSum);
}
}
}
void line(int x1, int y1, int x2, int y2) {
args.twoByte[0] = x1;
args.twoByte[1] = y1;
args.twoByte[2] = x2;
args.twoByte[3] = y2;
sendCommand('L', &args, 8);
}
void fillRectangle(int x1, int y1, int x2, int y2) {
args.twoByte[0] = x1;
args.twoByte[1] = y1;
args.twoByte[2] = x2;
args.twoByte[3] = y2;
sendCommand('R', &args, 8);
}
void rectangle(int x1, int y1, int x2, int y2, bool fill=false) {
if (fill)
fillRectangle(x1,y1,x2,y2);
else {
startPolyLine(4);
addPolyLine(x1,y1);
addPolyLine(x2,y1);
addPolyLine(x2,y2);
addPolyLine(x1,y2);
}
}
void roundedRectangle(int x1, int y1, int x2, int y2, int r, bool fill) {
args.roundedRectangle.filled = fill ? 1 : 0;
args.roundedRectangle.x1 = x1;
args.roundedRectangle.x2 = x2;
args.roundedRectangle.y1 = y1;
args.roundedRectangle.y2 = y2;
args.roundedRectangle.r = r;
sendCommand('Q', &args, 11);
}
void roundedRectangle(int x1, int y1, int x2, int y2, int r) {
roundedRectangle(x1,y1,x2,y2,r,false);
}
void fillRoundedRectangle(int x1, int y1, int x2, int y2, int r) {
roundedRectangle(x1,y1,x2,y2,r,true);
}
void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3) {
args.twoByte[0] = x1;
args.twoByte[1] = y1;
args.twoByte[2] = x2;
args.twoByte[3] = y2;
args.twoByte[4] = x3;
args.twoByte[5] = y3;
sendCommand('G', &args, 12);
}
/* void initialize() {
args.twoByte[0] = 0x1234; // endianness detector
args.twoByte[1] = 0;
sendCommandWithAck('H', &args, 4);
} */
void initialize(int w=VECTOR_DISPLAY_DEFAULT_WIDTH, int h=VECTOR_DISPLAY_DEFAULT_HEIGHT) {
args.initialize.endianness = 0x1234; // endianness detector
args.initialize.width = w;
args.initialize.height = h;
args.initialize.aspectRatio = TO_FP32(1.);
args.initialize.reserved[0] = 0;
args.initialize.reserved[1] = 0;
args.initialize.reserved[2] = 0;
curWidth = w;
curHeight = h;
sendCommandWithAck('Z', &args, 16);
}
void fillCircle(int x, int y, int r) {
args.twoByte[0] = x;
args.twoByte[1] = y;
args.twoByte[2] = r;
sendCommand('J', &args, 6);
}
void circle(int x, int y, int r) {
args.twoByte[0] = x;
args.twoByte[1] = y;
args.twoByte[2] = r;
sendCommand('I', &args, 6);
}
void point(int x, int y) {
args.twoByte[0] = x;
args.twoByte[1] = y;
sendCommand('P', &args, 4);
}
void arc(int x, int y, int r, FixedPoint32 angle1, FixedPoint32 sweep, bool fill=false) {
args.arc.x = x;
args.arc.y = y;
args.arc.r = r;
args.arc.angle1 = angle1;
args.arc.sweep = sweep;
args.arc.filled = fill ? 1 : 0;
sendCommand('S', &args, 15);
}
void arc(int x, int y, int r, float angle1, float sweep, bool fill=false) {
arc(x,y,r,TO_FP32(angle1),TO_FP32(sweep),fill);
}
// 32-bit fixed point
void textSize(FixedPoint32 s) {
args.attribute32.attr = 's';
args.attribute32.value = s;
sendCommand('B', &args, 5);
}
void text(int x, int y, const char* str, int n) {
args.xyText.x = x;
args.xyText.y = y;
if (n>VECTOR_DISPLAY_MAX_STRING)
n = VECTOR_DISPLAY_MAX_STRING;
strncpy(args.xyText.text, str, n);
if (fixCP437) {
for (int i=0;i<n;i++) {
if ((uint8_t)args.xyText.text[i] >=176)
args.xyText.text[i]++;
}
}
args.xyText.text[n] = 0;
sendCommand('T', &args, 4+strlen(args.xyText.text)+1);
}
void text(int x, int y, const char* str) {
text(x, y, str, strlen(str));
}
void text(int x, int y, String str) {
text(x,y,str.c_str(), str.length());
}
void deleteButton(uint8_t command) {
sendCommand('D', &command, 1);
}
void addButton(uint8_t command, const char* str) {
args.charText.c = command;
strncpy(args.charText.text, str, VECTOR_DISPLAY_MAX_STRING);
args.charText.text[VECTOR_DISPLAY_MAX_STRING] = 0;
sendCommand('U', &args, 1+strlen(args.charText.text)+1);
}
void addButton(uint8_t command, String str) {
addButton(command, str.c_str());
}
void toast(const char* str, unsigned n) {
if (VECTOR_DISPLAY_MAX_STRING < n)
n = VECTOR_DISPLAY_MAX_STRING;
strncpy(args.text, str, n);
args.text[n] = 0;
sendCommand('M', &args, n+1);
}
void toast(const char* str) {
toast(str, strlen(str));
}
void toast(String text) {
toast(text.c_str(), text.length());
}
void foreColor(uint32_t color) {
args.attribute32.attr = 'f';
args.attribute32.value = color;
sendCommand('B', &args, 5);
curForeColor565 = -1;
}
void backColor(uint32_t color) {
args.attribute32.attr = 'b';
args.attribute32.value = color;
sendCommand('B', &args, 5);
}
void textBackColor(uint32_t color) {
args.attribute32.attr = 'k';
args.attribute32.value = color;
sendCommand('B', &args, 5);
}
void textForeColor(uint32_t color) {
args.attribute32.attr = 'F';
args.attribute32.value = color;
sendCommand('B', &args, 5);
}
void foreColor565(uint16_t color) {
args.attribute16.attr = 'f';
args.attribute16.value = color;
sendCommand('A', &args, 3);
curForeColor565 = color;
}
void backColor565(uint16_t color) {
args.attribute16.attr = 'b';
args.attribute16.value = color;
sendCommand('A', &args, 3);
}
void textBackColor565(uint16_t color) {
args.attribute16.attr = 'k';
args.attribute16.value = color;
sendCommand('A', &args, 3);
}
void textForeColor565(uint16_t color) {
args.attribute16.attr = 'F';
args.attribute16.value = color;
sendCommand('A', &args, 3);
}
void rounded(uint8_t value) {
args.attribute8.attr = 'n';
args.attribute8.value = value ? 1 : 0;
sendCommand('Y', &args, 2);
}
void thickness(FixedPoint32 t) {
args.attribute32.attr = 't';
args.attribute32.value = t;
sendCommand('B', &args, 5);
}
void pixelAspectRatio(FixedPoint32 a) {
args.attribute32.attr = 'a';
args.attribute32.value = a;
sendCommand('B', &args, 5);
}
#ifdef SUPPORT_FLOATING_POINT
inline void setThickness(double thickness) {
setThickness(TO_FP32(thickness));
}
inline void setPixelAspectRatio(double aspect) {
setThickness(TO_FP32(aspect));
}
#endif
void clear() {
sendCommand('C', NULL, 0);
}
void update() {
sendCommand('F', NULL, 0);
}
/* void reset() {
sendCommandWithAck('E', NULL, 0);
} */
void coordinates(int width, int height) {
args.attribute16x2.attr = 'c';
curWidth = width;
curHeight = height;
args.attribute16x2.values[0] = width;
args.attribute16x2.values[1] = height;
sendCommandWithAck('B', &args, 5);
}
void continuousUpdate(bool value) {
args.attribute8.attr = 'c';
args.attribute8.value = value ? 1 : 0;
sendCommand('Y', &args, 2);
}
void textHorizontalAlign(char hAlign) {
args.attribute8.attr = 'h';
args.attribute8.value = hAlign;
sendCommand('Y', &args, 2);
}
void textVerticalAlign(char hAlign) {
args.attribute8.attr = 'v';
args.attribute8.value = hAlign;
sendCommand('Y', &args, 2);
}
void textOpaqueBackground(bool opaque) {
args.attribute8.attr = 'o';
args.attribute8.value = opaque ? 1 : 0;
sendCommand('Y', &args, 2);
}
void textBold(bool bold) {
args.attribute8.attr = 'b';
args.attribute8.value = bold ? 1 : 0;
sendCommand('Y', &args, 2);
}
bool isTouchDown() {
return pointerDown;
}
int getTouchX() {
return pointerX;
}
int getTouchY() {
return pointerY;
}
bool readMessage(VectorDisplayMessage* msg) {
while (remoteAvailable()) {
uint8_t c = remoteRead();
if (0 < readPos && millis()-lastMessageStart > MESSAGE_TIMEOUT)
readPos = 0;
if (2 <= readPos) {
readBuf[readPos++] = c;
if (readPos >= VECTOR_DISPLAY_MESSAGE_SIZE) {
readPos = 0;
if (msg != NULL)
memcpy(msg, readBuf, sizeof(VectorDisplayMessage));
else
msg = (VectorDisplayMessage*)readBuf;
if (msg->what == MESSAGE_DOWN || msg->what == MESSAGE_UP || msg->what == MESSAGE_MOVE) {
pointerDown = msg->what != MESSAGE_UP;
pointerX = msg->data.xy.x;
pointerY = msg->data.xy.y;
}
return true;
}
continue;
}
if (1 <= readPos) {
if ( (*readBuf == 'U' && c == 'P') ||
(*readBuf == 'D' && c == 'N') ||
(*readBuf == 'M' && c == 'V') ||
(*readBuf == 'B' && c == 'T') ||
(*readBuf == 'A' && c == 'c')
) {
readBuf[readPos++] = c;
continue;
}
readPos = 0;
}
if (readPos == 0 && (c == 'U' || c == 'D' || c == 'M' || c == 'B' || c == 'A')) {
readBuf[readPos++] = c;
lastMessageStart = millis();
}
}
return false;
}
uint32_t color565To8888(uint16_t c) {
return 0xFF000000 | ((((c>>11) & 0x1F) * 255 / 0x1F) << 16) | ((((c>>5) & 0x3F) * 255 / 0x3F) << 8) | ((c & 0x1F) * 255 / 0x1F);
}
uint16_t color565(uint8_t r, uint8_t g, uint8_t b) {
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
uint32_t getBitmap1Size(int16_t w, int16_t h, uint8_t flags=0) {
return (flags & FLAG_PAD_BYTE) ? ((uint32_t)w+7)/8*h : ((uint32_t)w*h+7)/8;
}
uint32_t getBitmapSize(int16_t w, int16_t h, uint8_t depth=1, uint8_t flags=0) {
if (depth==1) {
return getBitmap1Size(w,h,flags);
}
else {
return w*h*(depth/8);
}
}
/*TODO: stubs*/
void* createSprite(int16_t width, int16_t height, uint8_t frames = 1) { return NULL; }
void pushSprite(int32_t x, int32_t y) {}
void deleteSprite(void) {}
void fillSprite(uint32_t color) {}
void bitmap_progmem(int16_t x, int16_t y, const uint8_t* bmp,
int16_t w, int16_t h, uint8_t depth=1, uint8_t flags=0, const uint8_t* mask=NULL,
uint32_t foreColor=0xFFFFFFFF,
uint32_t backColor=0x00FFFFFF) /* PROGMEM */ {
if (mask != NULL)
flags |= FLAG_HAVE_MASK;
uint32_t bitmapSize = getBitmapSize(w,h,depth,flags);
int headerSize = depth==1 ? 22 : 14;
uint32_t maskSize = mask == NULL ? 0 : getBitmap1Size(w,h,flags);
uint32_t fullSize = bitmapSize + headerSize + maskSize;
if (fullSize + 1 > MAX_BUFFER)
return;
sendDelay();
remoteWrite('K');
remoteWrite('K'^0xFF);
args.bitmap.length = fullSize;
args.bitmap.depth = 1;
args.bitmap.flags = flags;
args.bitmap.x = x;
args.bitmap.y = y;
args.bitmap.w = w;
args.bitmap.h = h;
if (depth == 1) {
args.bitmap.foreColor = foreColor;
args.bitmap.backColor = backColor;
}
uint8_t sum = sumBytes(&args, headerSize);
remoteWrite(&args,headerSize);
for (uint32_t i=0; i<bitmapSize; i++) {
uint8_t c = pgm_read_byte_near(bmp+i);
remoteWrite(c);
sum += c;
}
for (uint32_t i=0; i<maskSize; i++) {
uint8_t c = pgm_read_byte_near(mask+i);
remoteWrite(c);
sum += c;
}
remoteWrite(sum^0xFF);
}
void bitmap(int16_t x, int16_t y, uint8_t *bmp,
int16_t w, int16_t h, uint8_t depth, uint8_t flags=0,
uint8_t* mask=NULL,
uint32_t foreColor=0xFFFFFFFF,
uint32_t backColor=0x00FFFFFF) {
if (mask != NULL)
flags |= FLAG_HAVE_MASK;
uint32_t bitmapSize = getBitmapSize(w,h,depth,flags);
int headerSize = depth==1 ? 22 : 14;
uint32_t maskSize = mask == NULL ? 0 : getBitmap1Size(w,h,flags);
uint32_t fullSize = bitmapSize + (headerSize-14) + maskSize;
if (fullSize + 1 > MAX_BUFFER)
return;
sendDelay();
remoteWrite('K');
remoteWrite('K'^0xFF);
args.bitmap.length = fullSize;
args.bitmap.depth = depth;
args.bitmap.flags = flags;
args.bitmap.x = x;
args.bitmap.y = y;
args.bitmap.w = w;
args.bitmap.h = h;
if (depth == 1) {
args.bitmap.foreColor = foreColor;
args.bitmap.backColor = backColor;
}
remoteWrite(&args,headerSize);
remoteWrite(bmp,bitmapSize);
uint8_t sum = sumBytes(&args, headerSize) + sumBytes((void*)bmp, bitmapSize);
if (maskSize > 0) {
remoteWrite(mask,maskSize);
sum += sumBytes((void*)mask, maskSize);
}
remoteWrite(sum^0xFF);
}
void utf8() {
fixCP437 = false;
args.attribute8.attr = 'i';
args.attribute8.value = 0;
sendCommand('Y', &args, 2);
}
/* The following are meant to be compatible with Adafruit GFX */
void cp437(bool s) {
// if true, activates real cp437 mode; if false, activates buggy Arduino compatible cp437 mode
fixCP437 = !s;
args.attribute8.attr = 'i';
args.attribute8.value = 1;
sendCommand('Y', &args, 2);
}
void setRotation(uint8_t r) {
args.attribute8.attr = 'r';
args.attribute8.value = r;
curRotation = r & 3;
sendCommand('Y', &args, 2);
}
void setTextSize(uint8_t size) {
gfxFontSize = size;
textsize = size;
textSize((FixedPoint32)size * 8 * 65536);
}
void setTextColor(uint16_t f, uint16_t b) {
textBackColor565(b);
textForeColor565(f);
textcolor = f;
textbgcolor = b;
textOpaqueBackground(true);
}
void setTextColor(uint16_t f) {
textForeColor565(f);
textcolor = f;
textOpaqueBackground(false);
}
void setCursor(int16_t x, int16_t y) {
curx = x;
cury = y;
}
int16_t getCursorX(void) {
return curx;
}
int16_t getCursorY(void) {
return cury;
}
void setTextWrap(bool w) {
wrap = w;
}
int16_t drawRightString(const char *string, int32_t x, int32_t y, uint8_t font) {
// TODO: add spaces
return drawString(string, x, y);
}
int16_t drawRightString(const String& string, int32_t x, int32_t y, uint8_t font) {
return drawRightString(string.c_str(), x, y, font);
}
int16_t drawCentreString(const char *string, int32_t x, int32_t y, uint8_t font) {
// TODO: add spaces
return drawString(string, x, y);
}
int16_t drawCentreString(const String& string, int32_t x, int32_t y, uint8_t font) {
return drawCentreString(string.c_str(), x, y, font);
}
int16_t drawString(const String& string, int32_t x, int32_t y) {
return drawString(string.c_str(), x, y);
}
int16_t drawString(const char *string, int32_t x, int32_t y) {
setCursor(x, y);
return write(string);
};
int16_t drawChar(uint16_t uniCode, int32_t x, int32_t y) {
setCursor(x, y);
return write(uniCode);
}
// TODO: fix back color handling
size_t write(uint8_t c) override {
if (wrap && curx + 5*gfxFontSize>width()) {
curx = 0;
cury += 8*gfxFontSize;
}
text(curx, cury, (char*)&c, 1);
curx += 5*gfxFontSize;
return 0;
}
// TODO: fix back color handling
size_t write(const char* s) /*override*/ { //ESP8266 core doesn't supply write(const char*)
int l = strlen(s);
int w = width();
if (!wrap || curx + 5*gfxFontSize*l <= w) {
text(curx, cury, s);
curx += 5*gfxFontSize*l;
}
else {
while(l>0) {
int end = ((int)w-curx)/(5*gfxFontSize);
if (end <= 0) {
curx = 0;
cury += 8*gfxFontSize;
end = w/(5*gfxFontSize);
}
if (end > l)
end = l;
text(curx, cury, s, end);
l-=end;
s += end;
curx = 5*gfxFontSize*end;
}
}
return 0;
}
void drawPixel(int16_t x, int16_t y, uint16_t color) {
if (color != curForeColor565) {
foreColor565(color);
}
point(x, y);
}
void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) {
if (color != curForeColor565) {
foreColor565(color);
}
rectangle(x,y,x+w-1,y+h-1);
}
void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) {
if (color != curForeColor565) {
foreColor565(color);
}
fillRectangle(x,y,x+w-1,y+h-1);
}
void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) {
if (color != curForeColor565) {
foreColor565(color);
}
line(x,y,x+w,y);
}
void drawLine(int16_t x, int16_t y, int16_t x2, int16_t y2, uint16_t color) {
if (color != curForeColor565) {
foreColor565(color);