-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawing.cpp
executable file
·989 lines (876 loc) · 31.4 KB
/
drawing.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
#define EEPROM_XFER_BLOCKSIZE 512 // shouldn't be any less than 256 bytes probably
#define DRAWITEM_LIST_SIZE 24
struct __attribute__((packed)) imageHeader {
uint16_t width;
uint16_t height;
uint8_t bpp : 4;
uint8_t reserved : 4;
};
static drawItem *drawItems[DRAWITEM_LIST_SIZE] = {0};
void addBufferedImage(uint16_t x, uint16_t y, bool color, enum rotation ro, const uint8_t *image, bool mask) {
drawItem *di = new drawItem;
di->setRotation(ro);
if (di->direction ^ epd->drawDirectionRight) {
int16_t temp = x;
x = y;
y = temp;
}
uint16_t originalWidthBytes = (((uint16_t *)image)[0]) / 8;
uint16_t size = 0;
uint16_t width = ((uint16_t *)image)[0];
// find out if the original data was aligned in one byte; if not, add a byte
if (((uint16_t *)image)[0] % 8) originalWidthBytes++;
// if we're drawing in X direction, we shift the content here. Add extra space for shifting!
if (!di->direction) {
width += x % 8;
}
// check if the size is aligned in bytes; if not, add an extra for good measure;
if (width % 8) {
width /= 8;
width++;
} else {
width /= 8;
}
size = width * ((uint16_t *)image)[1];
size += 2; // not needed
uint8_t *im = (uint8_t *)calloc(size, 1);
for (uint16_t copyY = 0; copyY < ((uint16_t *)image)[1]; copyY++) {
memcpy(im + (copyY * width), image + 4 + (copyY * originalWidthBytes), originalWidthBytes);
// if we draw in X direction, we need to shift bytes in the array
if (!di->direction && (x % 8)) {
drawItem::shiftBytesRight(im + (copyY * width), x % 8, width);
}
}
di->addItem(im, width * 8, ((uint16_t *)image)[1]);
di->xpos = x;
di->ypos = y;
di->color = color;
if (mask)
di->type = drawItem::drawType::DRAW_MASK;
else
di->type = drawItem::drawType::DRAW_BUFFERED_1BPP;
di->checkBounds();
di->addToList();
}
void addFlashImage(uint16_t x, uint16_t y, bool color, enum rotation ro, const uint8_t *image) {
drawItem *di = new drawItem;
di->setRotation(ro);
if (di->direction ^ epd->drawDirectionRight) {
int16_t temp = x;
x = y;
y = temp;
}
di->addItem((uint8_t *)(image + 4), ((uint16_t *)image)[0], ((uint16_t *)image)[1]);
di->xpos = x;
di->ypos = y;
di->color = color;
di->cleanUp = false;
di->type = drawItem::drawType::DRAW_BUFFERED_1BPP;
di->checkBounds();
di->addToList();
}
#ifdef ENABLE_OEPLFS
void addFSImage(uint16_t x, uint16_t y, uint8_t color, enum rotation ro, char *name) {
drawItem *di = new drawItem;
di->setRotation(ro);
if (di->direction ^ epd->drawDirectionRight) {
int16_t temp = x;
x = y;
y = temp;
}
OEPLFile *file = fs->getFile(name);
if (!file) {
delete di;
return;
}
uint16_t width, height;
file->getBlock(0, (uint8_t *)&width, 2);
file->getBlock(2, (uint8_t *)&height, 2);
di->addItem((uint8_t *)file, width, height);
di->xpos = x;
di->ypos = y;
di->color = color;
di->cleanUp = true;
if (color == 2) {
di->type = drawItem::drawType::DRAW_OEPLFS_2BPP;
} else {
di->type = drawItem::drawType::DRAW_OEPLFS_1BPP;
}
di->checkBounds();
di->addToList();
}
void addCompressedFSImage(uint16_t x, uint16_t y, enum rotation ro, char *name) {
drawItem *di = new drawItem;
di->type = drawItem::drawType::DRAW_COMPRESSED;
di->setRotation(ro);
if (di->direction ^ epd->drawDirectionRight) {
int16_t temp = x;
x = y;
y = temp;
}
decompress *decomp = new decompress;
if (!decomp->openFromFile(name)) {
delete di;
delete decomp;
return;
}
di->imageHeaderOffset = decomp->readByte(0);
struct imageHeader imgheader;
decomp->getBlock(1, (uint8_t *)&imgheader, sizeof(struct imageHeader));
di->addItem((uint8_t *)decomp, imgheader.width, imgheader.height);
di->xpos = x;
di->ypos = y;
di->color = imgheader.bpp;
if (di->color == 1) di->color = 0;
di->cleanUp = true;
di->checkBounds();
di->addToList();
}
#endif
void addQR(uint16_t x, uint16_t y, uint8_t version, uint8_t scale, const char *c, ...) {
char out_buffer[256];
va_list lst;
va_start(lst, c);
vsnprintf(out_buffer, 255, c, lst);
va_end(lst);
QRCode qrcode;
// Allocate a chunk of memory to store the QR code
uint8_t qrcodeBytes[qrcode_getBufferSize(version)];
qrcode_initText(&qrcode, qrcodeBytes, version, ECC_LOW, out_buffer);
drawItem *di = new drawItem;
di->setRotation(rotation::ROTATE_0);
uint8_t xbytes = (qrcode.size * scale) / 8;
if (qrcode.size % 8) xbytes++;
uint16_t size = xbytes * (qrcode.size * scale);
uint8_t *im = (uint8_t *)calloc(size + 1, 1);
uint8_t *qrbuf = im;
for (uint8_t qry = 0; qry < qrcode.size; qry++) {
for (uint8_t scale_y = 0; scale_y < scale; scale_y++) {
for (uint8_t qrx = 0; qrx < qrcode.size; qrx++) {
for (uint8_t scale_x = 0; scale_x < scale; scale_x++) {
if (qrcode_getModule(&qrcode, qrx, qry)) {
// Calculate the position in the framebuffer for the scaled pixel
uint8_t scaled_qrx = (qrx * scale) + scale_x;
uint8_t scaled_qry = (qry * scale) + scale_y;
// Calculate the byte and bit positions in the framebuffer
uint8_t fb_byte = scaled_qrx / 8;
uint8_t fb_bit = 7 - (scaled_qrx % 8);
// Set the bit in the framebuffer
qrbuf[fb_byte + (scaled_qry * xbytes)] |= (1 << fb_bit);
}
}
}
}
}
di->addItem(im, xbytes * 8, qrcode.size * scale);
di->xpos = x;
di->ypos = y;
di->color = 0;
di->type = drawItem::drawType::DRAW_BUFFERED_1BPP;
di->checkBounds();
di->addToList();
}
void drawImageAtAddressWrap(uint32_t addr, uint8_t lut) {
selectLUT(lut);
struct EepromImageHeader eih;
HAL_flashRead(addr, (uint8_t *)&eih, sizeof(struct EepromImageHeader));
#ifdef DEBUG_DRAWING
printf("Drawing image of type 0x%02X from location 0x%08X\n", eih.dataType, addr);
#endif
switch (eih.dataType) {
case DATATYPE_IMG_RAW_1BPP: {
#ifdef DEBUG_DRAWING
printf("DRAW: 1BPP\n");
#endif
drawItem *di = new drawItem;
// di->setRotation(ro);
di->xpos = 0;
di->ypos = 0;
di->color = 0;
di->addItem((uint8_t *)addr, epd->effectiveXRes, epd->effectiveYRes);
di->type = drawItem::drawType::DRAW_EEPROM_1BPP;
di->direction = false;
if (di->mirrorH) {
di->mirrorH = 0;
di->mirrorV = !di->mirrorV;
}
di->cleanUp = false;
di->checkBounds();
di->addToList();
} break;
case DATATYPE_IMG_RAW_2BPP: {
#ifdef DEBUG_DRAWING
printf("DRAW: 2BPP\n");
#endif
drawItem *di = new drawItem;
// di->setRotation(ro);
di->xpos = 0;
di->ypos = 0;
di->color = 0;
di->addItem((uint8_t *)addr, epd->effectiveXRes, epd->effectiveYRes);
di->type = drawItem::drawType::DRAW_EEPROM_2BPP;
di->direction = false;
if (di->mirrorH) {
di->mirrorH = 0;
di->mirrorV = !di->mirrorV;
}
di->cleanUp = false;
di->checkBounds();
di->addToList();
} break;
case DATATYPE_IMG_ZLIB: {
#ifdef DEBUG_DRAWING
printf("DRAW: drawing compressed image\n");
#endif
drawItem *di = new drawItem;
decompress *decomp = new decompress;
di->type = drawItem::drawType::DRAW_COMPRESSED;
addr += sizeof(struct EepromImageHeader);
if (!decomp->openFromFlash(addr, eih.size)) {
printf("DRAW: failed to open\n");
delete di;
delete decomp;
return;
}
di->imageHeaderOffset = decomp->readByte(0);
struct imageHeader imgheader;
decomp->getBlock(1, (uint8_t *)&imgheader, sizeof(struct imageHeader));
di->addItem((uint8_t *)decomp, imgheader.width, imgheader.height);
di->xpos = 0;
di->ypos = 0;
di->direction = false;
if (di->mirrorH) {
di->mirrorH = 0;
di->mirrorV = !di->mirrorV;
}
if (imgheader.bpp == 1) di->color = 0;
if (imgheader.bpp == 2) di->color = 2;
di->cleanUp = true;
di->checkBounds();
di->addToList();
} break;
}
addOverlay();
draw();
}
extern "C" {
void drawImageAtAddress(uint32_t addr, uint8_t lut) {
drawImageAtAddressWrap(addr, lut);
}
}
void drawRoundedRectangle(uint16_t xpos, uint16_t ypos, uint16_t width, uint16_t height, bool color) {
uint16_t widthBytes = width / 8;
if (width % 8) widthBytes++;
uint32_t framebufferSize = widthBytes * height;
uint8_t *framebuffer = (uint8_t *)calloc(framebufferSize + 4, 1);
if (framebuffer == NULL) {
return;
}
uint8_t frameBufferZerosize = width;
if ((width % 8) != 0) {
frameBufferZerosize++;
}
((uint16_t *)framebuffer)[0] = frameBufferZerosize;
((uint16_t *)framebuffer)[1] = height;
framebuffer += 4;
uint16_t w = width - 1;
uint16_t x = 1;
while (w--) {
framebuffer[(x / 8)] |= (uint8_t)(1 << (7 - ((uint8_t)x % 8)));
x++;
}
for (uint16_t curY = 1; curY < (height - 1); curY++) {
framebuffer[widthBytes * curY] = 0x80;
if (width % 8)
framebuffer[(widthBytes * curY) + widthBytes - 1] = (uint8_t)(1 << (7 - ((uint8_t)width % 8)));
else
framebuffer[(widthBytes * curY) + widthBytes - 1] = 0x01;
}
w = width - 1;
x = 1;
while (w--) {
framebuffer[(x / 8) + ((height - 1) * widthBytes)] |= (uint8_t)(1 << (7 - ((uint8_t)x % 8)));
x++;
}
framebuffer -= 4;
addBufferedImage(xpos, ypos, color, rotation::ROTATE_0, framebuffer, DRAW_NORMAL);
free(framebuffer);
}
void drawMask(uint16_t xpos, uint16_t ypos, uint16_t width, uint16_t height, bool color) {
uint16_t widthBytes = width / 8;
if (width % 8) widthBytes++;
uint32_t framebufferSize = widthBytes * height;
uint8_t *framebuffer = (uint8_t *)calloc(framebufferSize + 4, 1);
if (framebuffer == NULL) {
return;
}
((uint16_t *)framebuffer)[0] = width;
((uint16_t *)framebuffer)[1] = height;
framebuffer += 4;
for (uint16_t curY = 0; curY < height; curY++) {
uint16_t w = width;
uint16_t x = 0;
while (w--) {
framebuffer[(x / 8) + (curY * widthBytes)] |= (uint8_t)(1 << (7 - ((uint8_t)x % 8)));
x++;
}
}
framebuffer -= 4;
addBufferedImage(xpos, ypos, color, rotation::ROTATE_0, framebuffer, DRAW_INVERTED);
free(framebuffer);
}
// drawItem (sprite) functions
void drawItem::shiftBytesRight(uint8_t *data, uint8_t shift, uint8_t len) {
// Ensure the shift value is within bounds (0 to 7)
shift = shift % 8;
// Handle the case where shift is 0 or len is 0
if (shift == 0 || len == 0) {
return;
}
// Loop through the array from right to left
for (int i = len - 1; i > 0; i--) {
// Perform the shift by combining bits from the current byte
// and the next byte to its right
data[i] = (data[i] >> shift) | (data[i - 1] << (8 - shift));
}
// For the leftmost byte, simply shift it to the right
data[0] >>= shift;
}
uint8_t drawItem::bitReverse(uint8_t byte) {
byte = ((byte >> 1) & 0x55) | ((byte << 1) & 0xAA);
byte = ((byte >> 2) & 0x33) | ((byte << 2) & 0xCC);
byte = (byte >> 4) | (byte << 4);
return byte;
}
void drawItem::reverseBytes(uint8_t *src, uint8_t src_len) {
// Check for valid input
if (src == NULL || src_len == 0) {
return;
}
// Reverse the entire source array
for (uint8_t i = 0; i < src_len / 2; i++) {
uint8_t temp = src[i];
src[i] = src[src_len - i - 1];
src[src_len - i - 1] = temp;
}
// Reverse the bits within the bytes
for (uint8_t i = 0; i < src_len; i++) {
src[i] = bitReverse(src[i]);
}
}
void drawItem::copyWithByteShift(uint8_t *dst, uint8_t *src, uint8_t src_len, uint8_t offset) {
switch (type) {
case DRAW_MASK:
for (uint8_t i = 0; i < src_len; i++) {
dst[i + offset] &= ~(src[i]);
}
break;
default:
for (uint8_t i = 0; i < src_len; i++) {
dst[i + offset] |= src[i];
}
break;
}
}
void drawItem::renderDrawLine(uint8_t *line, uint16_t number, uint8_t c) {
drawItem *curDrawItem;
for (uint8_t i = 0; i < DRAWITEM_LIST_SIZE; i++) {
curDrawItem = drawItems[i];
if (curDrawItem != nullptr) {
curDrawItem->getDrawLine(line, number, c);
}
}
}
void drawItem::flushDrawItems() {
drawItem *curDrawItem;
for (uint8_t i = 0; i < DRAWITEM_LIST_SIZE; i++) {
curDrawItem = drawItems[i];
if (curDrawItem != nullptr) {
delete curDrawItem;
drawItems[i] = nullptr;
}
}
}
void drawItem::getXLine(uint8_t *line, uint16_t y, uint8_t c) {
switch (type) {
case DRAW_FONT:
case DRAW_BUFFERED_1BPP:
case DRAW_MASK:
if (c != color) return;
if ((y >= ypos) && (y < height + ypos)) { // was y > ypos, not >=
// y = height-y;
if (mirrorV) {
if (mirrorH) {
reverseBytes(&buffer[((height - (y - ypos)) * widthBytes)], widthBytes);
// reverseBytes(&buffer[((y - ypos) * widthBytes)], widthBytes);
} else {
reverseBytes(&buffer[((y - ypos) * widthBytes)], widthBytes);
}
}
if (mirrorH) {
copyWithByteShift(line, &buffer[((height - (y - ypos)) * widthBytes)], drawnWidthBytes, xpos / 8);
} else {
copyWithByteShift(line, &buffer[((y - ypos) * widthBytes)], drawnWidthBytes, xpos / 8);
}
}
break;
#ifdef ENABLE_OEPLFS
case DRAW_OEPLFS_1BPP:
case DRAW_OEPLFS_2BPP:
if ((color < 2) && (c != color)) return;
if ((y >= ypos) && (y < height + ypos)) { // was y > ypos, not >=
uint32_t offset = 4;
offset += c * height * widthBytes;
OEPLFile *file = (OEPLFile *)this->buffer;
uint8_t *dbuffer = (uint8_t *)malloc(widthBytes);
if (mirrorH) {
file->getBlock(offset + (height - (y - ypos)) * widthBytes, dbuffer, widthBytes);
} else {
file->getBlock(offset + (y - ypos) * widthBytes, dbuffer, widthBytes);
}
if (mirrorV) {
reverseBytes(dbuffer, widthBytes);
}
copyWithByteShift(line, dbuffer, drawnWidthBytes, xpos / 8);
free(dbuffer);
}
break;
#endif
case DRAW_COMPRESSED:
if ((color < 2) && (c != color)) return;
if ((y >= ypos) && (y < height + ypos)) {
uint32_t offset = this->imageHeaderOffset;
offset += c * height * widthBytes;
decompress *decomp = (decompress *)this->buffer;
uint8_t *dbuffer = (uint8_t *)malloc(widthBytes);
if (mirrorH) {
decomp->getBlock(offset + (height - (y - ypos)) * widthBytes, dbuffer, widthBytes);
} else {
decomp->getBlock(offset + (y - ypos) * widthBytes, dbuffer, widthBytes);
}
if (mirrorV) {
reverseBytes(dbuffer, widthBytes);
}
copyWithByteShift(line, dbuffer, drawnWidthBytes, xpos / 8);
free(dbuffer);
}
break;
case DRAW_EEPROM_1BPP:
if (c != color) return;
if (mirrorH)
y = epd->effectiveYRes - 1 - y;
if (mirrorV) {
uint8_t *dbuffer = (uint8_t *)malloc(widthBytes);
HAL_flashRead((uint32_t)buffer + sizeof(struct EepromImageHeader) + (y * (epd->effectiveXRes / 8)), dbuffer, (epd->effectiveXRes / 8));
reverseBytes(dbuffer, widthBytes);
memcpy(line, dbuffer, widthBytes);
free(dbuffer);
} else {
HAL_flashRead((uint32_t)buffer + sizeof(struct EepromImageHeader) + (y * (epd->effectiveXRes / 8)), line, (epd->effectiveXRes / 8));
}
break;
case DRAW_EEPROM_2BPP:
if (mirrorH)
y = epd->effectiveYRes - 1 - y;
if (mirrorV) {
uint8_t *dbuffer = (uint8_t *)malloc(widthBytes);
HAL_flashRead((uint32_t)(buffer + sizeof(struct EepromImageHeader) + ((y + (c * epd->effectiveYRes)) * (epd->effectiveXRes / 8))), dbuffer, (epd->effectiveXRes / 8));
reverseBytes(dbuffer, widthBytes);
memcpy(line, dbuffer, widthBytes);
free(dbuffer);
} else {
HAL_flashRead((uint32_t)(buffer + sizeof(struct EepromImageHeader) + ((y + (c * epd->effectiveYRes)) * (epd->effectiveXRes / 8))), line, (epd->effectiveXRes / 8));
}
break;
default:
printf("DRAW: Not supported mode!\n");
break;
}
}
void drawItem::getYLine(uint8_t *line, uint16_t x, uint8_t c) {
switch (type) {
case DRAW_FONT:
case DRAW_BUFFERED_1BPP:
if (c != color) return;
if ((x >= xpos) && (x < width + xpos)) {
x -= xpos;
for (uint16_t curY = 0; curY < height; curY++) {
uint16_t curYMirrored = curY;
if (!mirrorH) curYMirrored = height - 1 - curY;
if (mirrorV) {
if (buffer[((width - x) / 8) + (curYMirrored * widthBytes)] & (1 << (7 - ((width - x) % 8)))) {
line[(curY + ypos) / 8] |= (1 << (7 - ((curY + ypos) % 8)));
}
} else {
if (buffer[(x / 8) + (curYMirrored * widthBytes)] & (1 << (7 - (x % 8)))) {
line[(curY + ypos) / 8] |= (1 << (7 - ((curY + ypos) % 8)));
}
}
}
}
break;
case DRAW_MASK:
if (c != color) return;
if ((x >= xpos) && (x < width + xpos)) {
x -= xpos;
for (uint16_t curY = 0; curY < height; curY++) {
uint16_t curYMirrored = curY;
if (!mirrorH) curYMirrored = height - 1 - curY;
if (mirrorV) {
if (buffer[((width - x) / 8) + (curYMirrored * widthBytes)] & (1 << (7 - ((width - x) % 8)))) {
line[(curY + ypos) / 8] &= ~(1 << (7 - ((curY + ypos) % 8)));
}
} else {
if (buffer[(x / 8) + (curYMirrored * widthBytes)] & (1 << (7 - (x % 8)))) {
line[(curY + ypos) / 8] &= ~(1 << (7 - ((curY + ypos) % 8)));
}
}
}
}
break;
#ifdef ENABLE_OEPLFS
case DRAW_OEPLFS_1BPP:
case DRAW_OEPLFS_2BPP:
// this is incredibly slow. For larger images, it'll probably read 128 bytes of eeprom for every -bit- in the image.
if ((color < 2) && (c != color)) return;
if ((x >= xpos) && (x < width + xpos)) {
uint32_t offset = 4;
offset += c * height * widthBytes;
OEPLFile *file = (OEPLFile *)this->buffer;
x -= xpos;
for (uint16_t curY = 0; curY < height; curY++) {
uint16_t curYMirrored = curY;
if (!mirrorH) curYMirrored = height - 1 - curY;
if (mirrorV) {
if (file->readByte(offset + ((width - x) / 8) + (curYMirrored * widthBytes)) & (1 << (7 - ((width - x) % 8)))) {
line[(curY + ypos) / 8] |= (1 << (7 - ((curY + ypos) % 8)));
}
} else {
if (file->readByte(offset + (x / 8) + (curYMirrored * widthBytes)) & (1 << (7 - (x % 8)))) {
line[(curY + ypos) / 8] |= (1 << (7 - ((curY + ypos) % 8)));
}
}
}
}
break;
#endif
case DRAW_COMPRESSED:
// this is incredibly slow, and very naive. Just don't. Maybe if you want to test if everything works. It'll decompress about 50% of the file *PER PIXEL*. Good for load testing
if ((color < 2) && (c != color)) return;
if ((x >= xpos) && (x < width + xpos)) {
uint32_t offset = this->imageHeaderOffset;
offset += c * height * widthBytes;
decompress *decomp = (decompress *)this->buffer;
x -= xpos;
for (uint16_t curY = 0; curY < height; curY++) {
uint16_t curYMirrored = curY;
if (!mirrorH) curYMirrored = height - 1 - curY;
if (mirrorV) {
if (decomp->readByte(offset + ((width - x) / 8) + (curYMirrored * widthBytes)) & (1 << (7 - ((width - x) % 8)))) {
line[(curY + ypos) / 8] |= (1 << (7 - ((curY + ypos) % 8)));
}
} else {
if (decomp->readByte(offset + (x / 8) + (curYMirrored * widthBytes)) & (1 << (7 - (x % 8)))) {
line[(curY + ypos) / 8] |= (1 << (7 - ((curY + ypos) % 8)));
}
}
}
}
break;
default:
break;
}
}
void drawItem::getDrawLine(uint8_t *line, uint16_t number, uint8_t c) {
if (direction) {
getYLine(line, number, c);
} else {
getXLine(line, number, c);
}
}
void drawItem::addItem(uint8_t *data, uint16_t w, uint16_t h) {
width = w;
height = h;
widthBytes = w / 8;
if (w % 8) widthBytes++;
buffer = data;
}
bool drawItem::addToList() {
for (uint8_t i = 0; i < DRAWITEM_LIST_SIZE; i++) {
if (drawItems[i] == nullptr) {
drawItems[i] = this;
return true;
};
}
return false;
}
void drawItem::checkBounds() {
drawnWidthBytes = widthBytes;
// if(!direction){ // draw X lines
uint8_t availBytesWidth = epd->effectiveXRes / 8;
availBytesWidth -= (xpos / 8);
if (widthBytes < availBytesWidth) availBytesWidth = widthBytes;
drawnWidthBytes = availBytesWidth;
}
drawItem::~drawItem() {
if (cleanUp) {
switch (this->type) {
#ifdef ENABLE_OEPLFS
case drawItem::drawType::DRAW_OEPLFS_1BPP:
case drawItem::drawType::DRAW_OEPLFS_2BPP: {
OEPLFile *file = (OEPLFile *)this->buffer;
if (file) delete file;
} break;
#endif
case drawItem::drawType::DRAW_COMPRESSED: {
decompress *dec = (decompress *)this->buffer;
if (dec) delete dec;
} break;
default:
free(buffer);
break;
}
}
}
drawItem::drawItem() {
if (epd->drawDirectionRight) {
direction = true;
mirrorH = true;
}
}
void drawItem::setRotation(enum rotation ro) {
if (epd->drawDirectionRight) {
direction = true;
mirrorH = true;
}
switch (ro) {
case ROTATE_0:
break;
case ROTATE_270:
direction = !direction;
mirrorH = !mirrorH;
mirrorV = !mirrorV;
break;
case ROTATE_180:
mirrorH = !mirrorH;
mirrorV = !mirrorV;
break;
case ROTATE_90:
direction = !direction;
break;
};
}
void fontrender::drawFastHLine(uint16_t x, uint16_t y, uint16_t w) {
while (w--) {
fb[(x / 8) + (y * bufferByteWidth)] |= (uint8_t)(1 << (7 - ((uint8_t)x % 8)));
x++;
}
}
void fontrender::fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h) {
for (uint16_t curY = y; curY < y + h; curY++) {
drawFastHLine(x, curY, w);
}
}
#ifdef ENABLE_OEPLFS
// font rendering functions for OEPLFS
fontrender::fontrender(char *name) {
this->setFont(name);
}
void fontrender::setFont(char *name) {
if (this->glyphFile) delete this->glyphFile;
if (this->bitmapFile) delete this->bitmapFile;
OEPLFile *font = fs->getFile(name);
if (!font) {
#ifdef DEBUG_DRAWING
printf("DRAW: Couldn't open font file %s\n", name);
#endif
}
font->getBlock(0, (uint8_t *)&this->gfxFont, sizeof(GFXFontOEPL));
this->glyphFile = fs->getFile(this->gfxFont.glyphFile);
this->bitmapFile = fs->getFile(this->gfxFont.bitmapFile);
if (!this->glyphFile) printf("DRAW: Couldn't open font glyph file %s\n", name);
if (!this->bitmapFile) printf("DRAW: Couldn't open font bitmap file %s\n", name);
if (font) delete (font);
}
fontrender::~fontrender() {
if (this->glyphFile) delete this->glyphFile;
if (this->bitmapFile) delete this->bitmapFile;
}
uint8_t fontrender::getCharWidth(uint16_t c) {
if ((c >= gfxFont.first) && (c <= gfxFont.last)) {
c -= gfxFont.first;
GFXglyph glyph = this->getGlyph(c);
return glyph.xAdvance;
}
return 0;
}
GFXglyph fontrender::getGlyph(uint16_t c) {
GFXglyph g;
this->glyphFile->getBlock(sizeof(GFXglyph) * c, (uint8_t *)&g, sizeof(GFXglyph));
return g;
}
#else
// 'regular' font rendering functions
uint8_t fontrender::getCharWidth(uint16_t c) {
if ((c >= gfxFont->first) && (c <= gfxFont->last)) {
c -= gfxFont->first;
GFXglyph *glyph = &(gfxFont->glyph[c]);
return glyph->xAdvance;
}
return 0;
}
fontrender::fontrender(const GFXfont *font) {
gfxFont = (GFXfont *)font;
}
void fontrender::setFont(const GFXfont *font) {
gfxFont = (GFXfont *)font;
}
#endif
uint8_t fontrender::drawChar(int32_t x, int32_t y, uint16_t c, uint8_t size) {
// Filter out bad characters not present in font
#ifdef ENABLE_OEPLFS
if ((c >= gfxFont.first) && (c <= gfxFont.last)) {
c -= gfxFont.first;
GFXglyph glyph = this->getGlyph(c);
// uint8_t *bitmap = (uint8_t *)gfxFont->bitmap;
uint32_t bo = glyph.bitmapOffset;
uint8_t w = glyph.width,
h = glyph.height;
int8_t xo = glyph.xOffset,
yo = glyph.yOffset;
#else
if ((c >= gfxFont->first) && (c <= gfxFont->last)) {
c -= gfxFont->first;
GFXglyph *glyph = &(gfxFont->glyph[c]);
uint8_t *bitmap = (uint8_t *)gfxFont->bitmap;
uint32_t bo = glyph->bitmapOffset;
uint8_t w = glyph->width,
h = glyph->height;
int8_t xo = glyph->xOffset,
yo = glyph->yOffset;
#endif
uint8_t xx, yy, bits = 0, bit = 0;
int16_t xo16 = 0, yo16 = 0;
if (size > 1) {
xo16 = xo;
yo16 = yo;
}
// GFXFF rendering speed up
uint16_t hpc = 0; // Horizontal foreground pixel count
for (yy = 0; yy < h; yy++) {
for (xx = 0; xx < w; xx++) {
if (bit == 0) {
#ifdef ENABLE_OEPLFS
bits = (*this->bitmapFile)(bo++);
#else
bits = bitmap[bo++];
#endif
bit = 0x80;
}
if (bits & bit)
hpc++;
else {
if (hpc) {
if (size == 1)
drawFastHLine(x + xo + xx - hpc, y + yo + yy, hpc);
else
fillRect(x + (xo16 + xx - hpc) * size, y + (yo16 + yy) * size, size * hpc, size);
hpc = 0;
}
}
bit >>= 1;
}
// Draw pixels for this line as we are about to increment yy
if (hpc) {
if (size == 1)
drawFastHLine(x + xo + xx - hpc, y + yo + yy, hpc);
else
fillRect(x + (xo16 + xx - hpc) * size, y + (yo16 + yy) * size, size * hpc, size);
hpc = 0;
}
}
#ifdef ENABLE_OEPLFS
return glyph.xAdvance;
#else
return glyph->xAdvance;
#endif
}
return 0;
}
void fontrender::epdPrintf(uint16_t x, uint16_t y, bool color, enum rotation ro, const char *c, ...) {
drawItem *di = new drawItem;
if (di == nullptr) return;
di->setRotation(ro);
// prepare a drawItem, exchange x/y if necessary.
if (di->direction ^ epd->drawDirectionRight) {
int16_t temp = x;
x = y;
y = temp;
}
// output string using vsnprintf
char out_buffer[256];
va_list lst;
va_start(lst, c);
uint8_t len = vsnprintf(out_buffer, 255, c, lst);
va_end(lst);
// account for offset in font rendering
if (!di->direction) {
Xpixels = x % 8; // total drawing width increased by x%8
} else {
Xpixels = 0;
}
// find out the total length of the string
for (uint8_t c = 0; c < len; c++) {
Xpixels += (uint16_t)getCharWidth(out_buffer[c]);
}
// find out the high and low points for given font
int8_t high = 0;
int8_t low = 0;
for (uint8_t curchar = 0; curchar < len; curchar++) {
uint8_t c = out_buffer[curchar];
#ifdef ENABLE_OEPLFS
if ((c >= gfxFont.first) && (c <= gfxFont.last)) {
c -= gfxFont.first;
GFXglyph g = this->getGlyph(c);
int8_t glyphUL = g.yOffset;
if (glyphUL < high) high = glyphUL;
int8_t glyphHeight = g.height;
#else
if ((c >= gfxFont->first) && (c <= gfxFont->last)) {
c -= gfxFont->first;
int8_t glyphUL = gfxFont->glyph[c].yOffset;
if (glyphUL < high) high = glyphUL;
int8_t glyphHeight = gfxFont->glyph[c].height;
#endif
if ((glyphUL + glyphHeight) > low) low = glyphUL + glyphHeight;
}
}
// Actual font height (reduces memory footprint)
int8_t height = -1 * (high - low) + 1;
// determine actual width
bufferByteWidth = Xpixels / 8;
if (Xpixels % 8) bufferByteWidth++;
// allocate framebuffer
fb = (uint8_t *)calloc(bufferByteWidth * height, 1);
if (!fb) {
printf("DRAW: Tried to allocate a buffer %d x %d\n and failed...", bufferByteWidth, height);
delete di;
return;
}
uint16_t curX;
if (!di->direction) {
curX = x % 8; // start drawing at x%8s
} else {
curX = 0;
}
for (uint8_t c = 0; c < len; c++) {
curX += (uint16_t)drawChar(curX, height - low, out_buffer[c], 1);
}
di->addItem(fb, curX, height);
di->ypos = y;
di->xpos = x;
di->color = color;
di->type = drawItem::drawType::DRAW_FONT;
di->checkBounds();
di->addToList();
}