forked from wuspy/portal_calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisplayGDEW075T7.cpp
753 lines (669 loc) · 21.4 KB
/
DisplayGDEW075T7.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
/**
* Display class supporting 4-level greyscale for GDEW075T7 e-paper display, aka the 800x480 7.5" B/W from Waveshare.
*
* https://www.good-display.com/product/244.html
*
* Datasheet (for a different display controller) that has the LUT commands documented:
* https://www.smart-prototyping.com/image/data/9_Modules/EinkDisplay/GDEW0154T8/IL0373.pdf
*/
#include <Arduino.h>
#include <stdlib.h>
#include "DisplayGDEW075T7.h"
#include "config.h"
#include "unicode.h"
// Display commands
const uint8_t CMD_PSR = 0x00;
const uint8_t CMD_PWR = 0x01;
const uint8_t CMD_POWEROFF = 0x02;
const uint8_t CMD_POWERON = 0x04;
const uint8_t CMD_BTST = 0x06;
const uint8_t CMD_DEEPSLEEP = 0x07;
const uint8_t CHK_DEEPSLEEP = 0xA5;
const uint8_t CMD_DTM1 = 0x10;
const uint8_t CMD_REFRESH = 0x12;
const uint8_t CMD_DSP = 0x11;
const uint8_t CMD_DTM2 = 0x13;
const uint8_t CMD_DUAL_SPI = 0x15;
const uint8_t CMD_SET_LUTVCOM = 0x20;
const uint8_t CMD_SET_LUTWW = 0x21;
const uint8_t CMD_SET_LUTBW = 0x22;
const uint8_t CMD_SET_LUTWB = 0x23;
const uint8_t CMD_SET_LUTBB = 0x24;
const uint8_t CMD_SET_LUTBD = 0x25;
const uint8_t CMD_PLL = 0x30;
const uint8_t CMD_VCOM_CDI = 0x50;
const uint8_t CMD_TCON = 0x60;
const uint8_t CMD_TRES = 0x61;
const uint8_t CMD_GSST = 0x65;
const uint8_t CMD_FLG = 0x71;
const uint8_t CMD_VDCS = 0x82;
// Supported LUT voltage levels
#define LEVEL_GND 0b00
#define LEVEL_VDH 0b01
#define LEVEL_VDL 0b10
#define LEVEL_VDHR 0b11
#define LEVEL_VCOM_VCMDC 0b00
#define LEVEL_VCOM_FLOAT 0b11
/**
* Macro to generate an LUT row while preserving some readability.
*
* l* params are the voltages to apply at each step, and t* are the number of frames to keep that voltage applied.
* r is the number of times the enitre sequence is to be repeated.
*/
#define LUT_ROW(l0, t0, l1, t1, l2, t2, l3, t3, r) (l0 << 6) | (l1 << 4) | (l2 << 2) | l3, t0, t1, t2, t3, r
#define LUT_ROW_NOOP LUT_ROW(0, 0, 0, 0, 0, 0, 0, 0, 0)
/**
* These LUTs are the original ones provided for this display by Waveshare.
*
* Currently not used, but kept here for reference.
*
* https://github.com/waveshare/e-Paper/blob/master/Arduino/epd7in5_V2/epd7in5_V2.cpp
*/
const uint8_t LUT_VCOM_1BIT[] = {
LUT_ROW(LEVEL_VCOM_VCMDC, 15, LEVEL_VCOM_VCMDC, 15, 0, 0, 0, 0, 1),
LUT_ROW(LEVEL_VCOM_VCMDC, 15, LEVEL_VCOM_VCMDC, 1, LEVEL_VCOM_VCMDC, 15, LEVEL_VCOM_VCMDC, 1, 2),
LUT_ROW(LEVEL_VCOM_VCMDC, 15, LEVEL_VCOM_VCMDC, 15, 0, 0, 0, 0, 1),
LUT_ROW_NOOP,
LUT_ROW_NOOP,
LUT_ROW_NOOP,
LUT_ROW_NOOP,
};
const uint8_t LUT_WW_1BIT[] = {
LUT_ROW(LEVEL_GND, 15, LEVEL_VDH, 15, 0, 0, 0, 0, 1),
LUT_ROW(LEVEL_VDL, 15, LEVEL_GND, 1, LEVEL_VDH, 15, LEVEL_GND, 1, 2),
LUT_ROW(LEVEL_GND, 15, LEVEL_VDL, 15, 0, 0, 0, 0, 1),
LUT_ROW_NOOP,
LUT_ROW_NOOP,
LUT_ROW_NOOP,
LUT_ROW_NOOP,
};
const uint8_t LUT_BW_1BIT[] = {
LUT_ROW(LEVEL_GND, 15, LEVEL_VDH, 15, 0, 0, 0, 0, 1),
LUT_ROW(LEVEL_VDL, 15, LEVEL_GND, 1, LEVEL_VDH, 15, LEVEL_GND, 1, 2),
LUT_ROW(LEVEL_GND, 15, LEVEL_VDL, 15, 0, 0, 0, 0, 1),
LUT_ROW_NOOP,
LUT_ROW_NOOP,
LUT_ROW_NOOP,
LUT_ROW_NOOP,
};
const uint8_t LUT_WB_1BIT[] = {
LUT_ROW(LEVEL_VDL, 15, LEVEL_GND, 15, 0, 0, 0, 0, 1),
LUT_ROW(LEVEL_VDL, 15, LEVEL_GND, 1, LEVEL_VDH, 15, LEVEL_GND, 1, 2),
LUT_ROW(LEVEL_VDH, 15, LEVEL_GND, 15, 0, 0, 0, 0, 1),
LUT_ROW_NOOP,
LUT_ROW_NOOP,
LUT_ROW_NOOP,
LUT_ROW_NOOP,
};
const uint8_t LUT_BB_1BIT[] = {
LUT_ROW(LEVEL_VDL, 15, LEVEL_GND, 15, 0, 0, 0, 0, 1),
LUT_ROW(LEVEL_VDL, 15, LEVEL_GND, 1, LEVEL_VDH, 15, LEVEL_GND, 1, 2),
LUT_ROW(LEVEL_VDH, 15, LEVEL_GND, 15, 0, 0, 0, 0, 1),
LUT_ROW_NOOP,
LUT_ROW_NOOP,
LUT_ROW_NOOP,
LUT_ROW_NOOP,
};
/**
* These LUTs are from the GxEPD2 project, they enable support for 4 color greyscale.
* The author of that project says these waveforms are provided by GoodDisplay, however
* they aren't meant for this particular display and may have varying results between displays,
* although they look perfect on the one I have.
*
* The only modification I've made is repeating the 2nd waveform twice to reduce ghosting.
* I don't forsee this having any negative effects since that waveform is balanced between VDH/VDL.
* Ghosting seems to increase as temperature decreases, which makes sense because the oil in the display
* will be more viscous. Increasing R2 will help reduce ghosting more at low temperatures at the cost of
* increated refresh time.
*
* https://github.com/ZinggJM/GxEPD2_4G/blob/master/src/epd/GxEPD2_750_T7.cpp
*/
#define T1 10
#define T2 20
#define R2 2
#define T3_1 20
#define T3_2 10
const uint8_t LUT_VCOM_2BIT[] = {
LUT_ROW(LEVEL_VCOM_VCMDC, T1, 0, 0, 0, 0, 0, 0, 1),
LUT_ROW(LEVEL_VDH, T2, LEVEL_VDL, T2, 0, 0, 0, 0, R2),
LUT_ROW(LEVEL_VCOM_VCMDC, T3_1, 0, 0, 0, 0, 0, 0, 1),
LUT_ROW(LEVEL_VCOM_VCMDC, 19, LEVEL_VCOM_VCMDC, 10, LEVEL_VCOM_VCMDC, 1, 0, 0, 1),
LUT_ROW_NOOP,
LUT_ROW_NOOP,
LUT_ROW_NOOP,
};
const uint8_t LUT_WHITE_2BIT[] = {
LUT_ROW(LEVEL_VDH, T1, 0, 0, 0, 0, 0, 0, 1),
LUT_ROW(LEVEL_VDL, T2, LEVEL_VDH, T2, 0, 0, 0, 0, R2),
LUT_ROW(LEVEL_GND, T3_1, LEVEL_VDH, T3_2, 0, 0, 0, 0, 1),
LUT_ROW(LEVEL_VDL, 19, LEVEL_VDL, 1, 0, 0, 0, 0, 1),
LUT_ROW_NOOP,
LUT_ROW_NOOP,
LUT_ROW_NOOP,
};
const uint8_t LUT_DGREY_2BIT[] = {
LUT_ROW(LEVEL_VDH, T1, 0, 0, 0, 0, 0, 0, 1),
LUT_ROW(LEVEL_VDL, T2, LEVEL_VDH, T2, 0, 0, 0, 0, R2),
LUT_ROW(LEVEL_GND, T3_1, LEVEL_GND, T3_2, 0, 0, 0, 0, 1),
LUT_ROW(LEVEL_VDL, 12, LEVEL_VDH, 1, LEVEL_VDL, 3, LEVEL_VDH, 4, 1),
LUT_ROW_NOOP,
LUT_ROW_NOOP,
LUT_ROW_NOOP,
};
const uint8_t LUT_LGREY_2BIT[] = {
LUT_ROW(LEVEL_VDH, T1, 0, 0, 0, 0, 0, 0, 1),
LUT_ROW(LEVEL_VDL, T2, LEVEL_VDH, T2, 0, 0, 0, 0, R2),
LUT_ROW(LEVEL_GND, T3_1, LEVEL_GND, T3_2, 0, 0, 0, 0, 1),
LUT_ROW(LEVEL_VDL, 11, LEVEL_VDH, 4, LEVEL_VDL, 4, LEVEL_VDH, 1, 1),
LUT_ROW_NOOP,
LUT_ROW_NOOP,
LUT_ROW_NOOP,
};
const uint8_t LUT_BLACK_2BIT[] = {
LUT_ROW(LEVEL_VDL, T1, 0, 0, 0, 0, 0, 0, 1),
LUT_ROW(LEVEL_VDL, T2, LEVEL_VDH, T2, 0, 0, 0, 0, R2),
LUT_ROW(LEVEL_GND, T3_1, LEVEL_VDL, T3_2, 0, 0, 0, 0, 1),
LUT_ROW(LEVEL_VDH, 19, LEVEL_VDH, 1, 0, 0, 0, 0, 1),
LUT_ROW_NOOP,
LUT_ROW_NOOP,
LUT_ROW_NOOP,
};
/**
* These aren't waveform LUTs, they map 2-bit color values to the old (DTM1) and new (DTM2)
* data values sent to the display.
*/
const uint8_t LUT_DTM1[] = {
/* WHITE */ 0,
/* LGREY */ 0,
/* DGREY */ 1,
/* BLACK */ 1,
};
const uint8_t LUT_DTM2[] = {
/* WHITE */ 0,
/* LGREY */ 1,
/* DGREY */ 0,
/* BLACK */ 1,
};
#define BUSY_TIMEOUT 5000
DisplayGDEW075T7::~DisplayGDEW075T7() {
#ifndef HEADLESS
_spi->endTransaction();
_spi->end();
delete _spi;
#endif
delete[] _frameBuffer;
};
DisplayGDEW075T7::DisplayGDEW075T7(uint8_t spi_bus, uint8_t sck_pin, uint8_t copi_pin, uint8_t cs_pin, uint8_t reset_pin, uint8_t dc_pin, uint8_t busy_pin, uint8_t pwr_pin)
{
_resetPin = reset_pin;
_dcPin = dc_pin;
_csPin = cs_pin;
_busyPin = busy_pin;
_pwrPin = pwr_pin;
pinMode(_csPin, OUTPUT);
pinMode(_resetPin, OUTPUT);
pinMode(_dcPin, OUTPUT);
pinMode(_busyPin, INPUT);
pinMode(_pwrPin, OUTPUT);
#ifndef HEADLESS
_spi = new SPIClass(spi_bus);
_spi->begin(sck_pin, -1, copi_pin, cs_pin);
_spi->beginTransaction(SPISettings(7000000, MSBFIRST, SPI_MODE0));
#endif
_frameBuffer = new uint8_t[FRAMEBUFFER_LENGTH];
setRotation(ROTATION_0);
setAlpha(NO_ALPHA);
clear();
};
void DisplayGDEW075T7::wakeup()
{
digitalWrite(_pwrPin, HIGH);
digitalWrite(_resetPin, HIGH);
delay(20);
digitalWrite(_resetPin, LOW);
delay(4);
digitalWrite(_resetPin, HIGH);
delay(20);
digitalWrite(_csPin, LOW);
// Most of this is Waveshare's defaults
sendCommand(CMD_PWR); // power setting
sendData(0x17); // BD_EN=1, VSR_EN=1, VS_EN=1, VG_EN=1, waveshare
sendData(0x17); // VPP_EN=0, VCOM_SLEW=1, VGH=20v, VGL=-20v
sendData(0x3F); // VDH=15v
sendData(0x3F); // VDL=-15v
sendData(0x11); // VDHR=5.8v
sendCommand(CMD_VDCS); // VCOM DC Setting (min 0x00 = -0.1v, max 0x4F = -4.05v)
sendData(0x22); // -1.8v
sendCommand(CMD_BTST); // Booster Setting
sendData(0x27);
sendData(0x27);
sendData(0x2F);
sendData(0x17);
sendCommand(CMD_PLL);
sendData(0x06); // 150hz
sendCommand(CMD_POWERON);
delay(100);
waitUntilIdle();
sendCommand(CMD_PSR); // PANNEL SETTING
sendData(0x3F); // Custom LUT, KW mode, UD=1, SHL=1, SHD_N=1, RST_N=1
sendCommand(CMD_TRES);
sendData(NATIVE_WIDTH / 256);
sendData(NATIVE_WIDTH % 256);
sendData(NATIVE_HEIGHT / 256);
sendData(NATIVE_HEIGHT % 256);
sendCommand(CMD_DUAL_SPI);
sendData(0x00);
sendCommand(CMD_VCOM_CDI); // VCOM AND DATA INTERVAL SETTING
sendData(0x00); // BDZ=0, BDV=00, N2OCP=0, DDX=00
// sendData(0x00); // CDI=17
sendData(0x07); // CDI=10
sendCommand(CMD_TCON); // TCON SETTING
sendData(0x22);
sendCommand(CMD_GSST); // Resolution setting
sendData(0x00);
sendData(0x00);
sendData(0x00);
sendData(0x00);
setLut(CMD_SET_LUTVCOM, LUT_VCOM_2BIT);
setLut(CMD_SET_LUTWW, LUT_WHITE_2BIT);
setLut(CMD_SET_LUTBW, LUT_DGREY_2BIT);
setLut(CMD_SET_LUTWB, LUT_LGREY_2BIT);
setLut(CMD_SET_LUTBB, LUT_BLACK_2BIT);
setLut(CMD_SET_LUTBD, LUT_WHITE_2BIT);
}
void DisplayGDEW075T7::sendCommand(uint8_t command)
{
#ifndef HEADLESS
digitalWrite(_dcPin, LOW);
_spi->transfer(command);
digitalWrite(_dcPin, HIGH);
#endif
}
void DisplayGDEW075T7::sendData(uint8_t data)
{
#ifndef HEADLESS
_spi->transfer(data);
#endif
}
void DisplayGDEW075T7::waitUntilIdle()
{
#ifndef HEADLESS
unsigned long start = millis();
do {
delay(5);
} while (digitalRead(_busyPin) == LOW && millis() - start < BUSY_TIMEOUT);
#endif
delay(20);
}
void DisplayGDEW075T7::setLut(uint8_t cmd, const uint8_t* lut)
{
sendCommand(cmd);
for (size_t i = 0; i < 42; ++i) {
sendData(lut[i]);
}
}
void DisplayGDEW075T7::refresh()
{
wakeup();
size_t i;
uint8_t j;
uint16_t chunk; // Holds 8px of frame_buffer
uint8_t data; // Holds 8px of display output
sendCommand(CMD_DTM1);
for (i = 0; i < FRAMEBUFFER_LENGTH; i += 2) {
data = 0;
chunk = (_frameBuffer[i] << 8) | _frameBuffer[i + 1];
for (j = 0; j < 8; ++j) {
data |= LUT_DTM1[(chunk >> (j * 2)) & 0b11] << j;
}
sendData(data);
}
sendCommand(CMD_DTM2);
for (i = 0; i < FRAMEBUFFER_LENGTH; i += 2) {
data = 0;
chunk = (_frameBuffer[i] << 8) | _frameBuffer[i + 1];
for (j = 0; j < 8; ++j) {
data |= LUT_DTM2[(chunk >> (j * 2)) & 0b11] << j;
}
sendData(data);
}
sendCommand(CMD_REFRESH);
delay(100);
waitUntilIdle();
sleep();
}
void DisplayGDEW075T7::sleep()
{
sendCommand(CMD_POWEROFF);
waitUntilIdle();
sendCommand(CMD_DEEPSLEEP);
sendData(CHK_DEEPSLEEP);
digitalWrite(_csPin, HIGH);
digitalWrite(_pwrPin, LOW);
}
void DisplayGDEW075T7::clear(Color color)
{
memset(_frameBuffer, (color << 6) | (color << 4) | (color << 2) | color, FRAMEBUFFER_LENGTH);
}
void DisplayGDEW075T7::test()
{
memset(
_frameBuffer,
(BLACK << 6) | (BLACK << 4) | (BLACK << 2) | BLACK,
FRAMEBUFFER_LENGTH / 4
);
memset(
&_frameBuffer[FRAMEBUFFER_LENGTH / 4],
(DGREY << 6) | (DGREY << 4) | (DGREY << 2) | DGREY,
FRAMEBUFFER_LENGTH / 4
);
memset(
&_frameBuffer[FRAMEBUFFER_LENGTH / 2],
(LGREY << 6) | (LGREY << 4) | (LGREY << 2) | LGREY,
FRAMEBUFFER_LENGTH / 4
);
memset(
&_frameBuffer[FRAMEBUFFER_LENGTH * 3 / 4],
(WHITE << 6) | (WHITE << 4) | (WHITE << 2) | WHITE,
FRAMEBUFFER_LENGTH / 4
);
}
size_t DisplayGDEW075T7::getPixelIndex(int32_t x, int32_t y)
{
if (x < 0 || x >= _width || y < 0 || y >= _height) {
return SIZE_MAX;
}
switch (_rotation) {
case ROTATION_90:
std::swap(x, y);
x = NATIVE_WIDTH - 1 - x;
break;
case ROTATION_180:
x = NATIVE_WIDTH - 1 - x;
y = NATIVE_HEIGHT - 1 - y;
break;
case ROTATION_270:
std::swap(x, y);
y = NATIVE_HEIGHT - 1 - y;
break;
default:
break;
}
return NATIVE_WIDTH * y + x;
}
uint8_t DisplayGDEW075T7::getPx(int32_t x, int32_t y)
{
const size_t i = getPixelIndex(x, y);
if (i != SIZE_MAX) {
return (_frameBuffer[i / 4] >> ((3 - i % 4) * 2)) & 0b11;
} else {
return 0;
}
}
void DisplayGDEW075T7::setPx(int32_t x, int32_t y, Color color)
{
const size_t i = getPixelIndex(x, y);
if (i != SIZE_MAX) {
uint8_t *fb = &_frameBuffer[i / 4];
const uint8_t shift = (3 - i % 4) * 2;
*fb &= ~(0b11 << shift);
*fb |= (color & 0b11) << shift;
}
}
void DisplayGDEW075T7::drawImage(const Image &image, int32_t x, int32_t y, Align align)
{
ImageReader reader = ImageReader(image);
adjustAlignment(&x, &y, image.width, image.height, align);
Color color;
uint32_t y_dst;
for (uint32_t y_src = 0; y_src < image.height; ++y_src) {
y_dst = y + y_src;
for (uint32_t x_src = 0; x_src < image.width; ++x_src) {
color = static_cast<Color>(reader.next());
if (color != _alpha) {
setPx(x + x_src, y_dst, color);
}
}
}
}
uint32_t DisplayGDEW075T7::measureText(String str, const Font &font, int32_t tracking)
{
if (str.length() == 0) {
return 0;
}
uint32_t length = 0;
Utf8Iterator it = Utf8Iterator(str);
uint16_t cp;
while ((cp = it.next())) {
if (isSpaceCodePoint(cp)) {
length += font.spaceWidth + tracking;
} else {
const FontGlyph glyph = font.getGlyph(cp);
length += glyph.width + glyph.left + tracking;
}
}
return length - tracking;
}
std::vector<String> DisplayGDEW075T7::wordWrap(String str, const Font &font, uint32_t maxLineLength, int32_t tracking)
{
std::vector<String> lines;
if (str.length() == 0) {
return lines;
}
unsigned int lineStart = 0, safeLineEnd = 0;
uint32_t length = 0, safeLength = 0;
Utf8Iterator it = Utf8Iterator(str);
uint16_t cp;
while ((cp = it.next())) {
if (isNewlineCodePoint(cp)) {
if (maxLineLength > 0 && length > maxLineLength + tracking) {
// Wrap at the last word too
lines.push_back(str.substring(lineStart, safeLineEnd - 1));
lineStart = safeLineEnd;
}
// Wrap here
lines.push_back(str.substring(lineStart, it.getCurrentPosition() - 1));
lineStart = safeLineEnd = it.getCurrentPosition();
length = safeLength = 0;
} else if (isSpaceCodePoint(cp)) {
if (maxLineLength > 0 && length > maxLineLength + tracking) {
if (safeLineEnd == lineStart) {
// Line cannot be word wrapped, so wrap at current position
lines.push_back(str.substring(lineStart, it.getCurrentPosition() - 1));
lineStart = it.getCurrentPosition();
length = 0;
} else {
// Wrap at last word
lines.push_back(str.substring(lineStart, safeLineEnd - 1));
lineStart = safeLineEnd;
length -= safeLength;
}
} else {
length += font.spaceWidth + tracking;
}
safeLineEnd = it.getCurrentPosition();
safeLength = length;
} else {
const FontGlyph glyph = font.getGlyph(cp);
length += glyph.width + glyph.left + tracking;
}
}
if (lineStart < str.length()) {
lines.push_back(str.substring(lineStart));
}
return lines;
}
void DisplayGDEW075T7::drawText(String str, const Font &font, int32_t x, int32_t y, Align align, int32_t tracking)
{
if (align != TOP_LEFT) {
uint32_t width;
// Measurement isn't needed and width isn't used by adjustAligment if horizontal alignment is left
if (!(align & _ALIGN_LEFT)) {
width = measureText(str, font);
}
adjustAlignment(&x, &y, width, font.ascent + font.descent, align);
}
Utf8Iterator it = Utf8Iterator(str);
uint16_t cp;
while ((cp = it.next())) {
if (isSpaceCodePoint(cp)) {
x += font.spaceWidth + tracking;
} else {
const FontGlyph glyph = font.getGlyph(cp);
x += glyph.left;
drawImage(glyph, x, y + glyph.top);
x += glyph.width + tracking;
}
}
}
void DisplayGDEW075T7::drawMultilineText(
String str,
const Font &font,
int32_t x,
int32_t y,
uint32_t maxLineLength,
Align align,
int32_t tracking,
int32_t leading
) {
// This implementation is simple because it assumes justification equals the horizontal alignment,
// and that's all I needed it to do.
leading += font.ascent + font.descent;
std::vector<String> lines = wordWrap(str, font, maxLineLength, tracking);
if (!(align & _ALIGN_TOP)) {
adjustAlignment(&x, &y, 0, leading * lines.size(), align);
}
// Use top alignment for each line since that axis has already been adjusted for the whole block
align = (Align)(align & ~_ALIGN_BOTTOM & ~_ALIGN_VCENTER | _ALIGN_TOP);
for (String str : lines) {
drawText(str, font, x, y, align, tracking);
y += leading;
}
}
void DisplayGDEW075T7::drawQrCode(qrcodegen::QrCode qrcode, int32_t x, int32_t y, int32_t scale, Align align)
{
const int32_t size = qrcode.getSize() * scale;
adjustAlignment(&x, &y, size, size, align);
DisplayGDEW075T7::Color color;
int32_t y2;
for(int y1 = 0; y1 < size; ++y1) {
y2 = y1 / scale;
for (int x1 = 0; x1 < size; ++x1) {
setPx(x + x1, y + y1, qrcode.getModule(x1 / scale, y2) ? BLACK : WHITE);
}
}
}
void DisplayGDEW075T7::drawHLine(int32_t x, int32_t y, int32_t length, uint32_t thickness, Color color, Align align)
{
if (length < 0) {
x += length;
length = -length;
}
adjustAlignment(&x, &y, length, thickness, align);
const int32_t ymax = y + thickness;
int32_t i, y1;
for (i = 0; i < length; ++i) {
for (y1 = y; y1 < ymax; ++y1) {
setPx(x + i, y1, color);
}
}
}
void DisplayGDEW075T7::drawVLine(int32_t x, int32_t y, int32_t length, uint32_t thickness, Color color, Align align)
{
if (length < 0) {
y += length;
length = -length;
}
adjustAlignment(&x, &y, thickness, length, align);
const int32_t xmax = x + thickness;
int32_t i, x1;
for (i = 0; i < length; ++i) {
for (x1 = x; x1 < xmax; ++x1) {
setPx(x1, y + i, color);
}
}
}
void DisplayGDEW075T7::fillRect(int32_t x, int32_t y, int32_t width, int32_t height, Color color, Align align)
{
if (width < 0) {
x += width;
width = -width;
}
if (height < 0) {
y += height;
height = -height;
}
adjustAlignment(&x, &y, width, height, align);
int32_t x2 = x + width, y2 = y + height;
for (int xi = x; xi < x2; ++xi) {
for (int yi = y; yi < y2; ++yi) {
setPx(xi, yi, color);
}
}
}
void DisplayGDEW075T7::strokeRect(int32_t x, int32_t y, int32_t width, int32_t height, uint32_t strokeWidth, Color color, bool strokeOutside, Align align)
{
if (width < 0) {
x += width;
width = -width;
}
if (height < 0) {
y += height;
height = -height;
}
adjustAlignment(&x, &y, width, height, align);
if (strokeOutside) {
x -= strokeWidth;
y -= strokeWidth;
width += strokeWidth * 2;
height += strokeWidth * 2;
}
drawHLine(x, y, width, strokeWidth, color, TOP_LEFT);
drawVLine(x, y, height, strokeWidth, color, TOP_LEFT);
drawHLine(x, y + height, width, strokeWidth, color, BOTTOM_LEFT);
drawVLine(x + width, y, height, strokeWidth, color, TOP_RIGHT);
}
void DisplayGDEW075T7::adjustAlignment(int32_t *x, int32_t *y, int32_t width, int32_t height, Align align)
{
if (align & _ALIGN_HCENTER) {
*x -= width / 2;
} else if (align & _ALIGN_RIGHT) {
*x -= width;
}
if (align & _ALIGN_VCENTER) {
*y -= height / 2;
} else if (align & _ALIGN_BOTTOM) {
*y -= height;
}
}
uint32_t DisplayGDEW075T7::getWidth()
{
return _width;
}
uint32_t DisplayGDEW075T7::getHeight()
{
return _height;
}
DisplayGDEW075T7::Rotation DisplayGDEW075T7::getRotation()
{
return _rotation;
}
void DisplayGDEW075T7::setRotation(Rotation rotation)
{
_rotation = rotation;
if (rotation == ROTATION_0 || rotation == ROTATION_180) {
_width = NATIVE_WIDTH;
_height = NATIVE_HEIGHT;
} else {
_width = NATIVE_HEIGHT;
_height = NATIVE_WIDTH;
}
}
uint8_t DisplayGDEW075T7::getAlpha()
{
return _alpha;
}
void DisplayGDEW075T7::setAlpha(uint8_t alpha)
{
_alpha = alpha;
}