forked from ZinggJM/GxEPD2_32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GxEPD2_32_BW.cpp
1049 lines (1008 loc) · 27.7 KB
/
GxEPD2_32_BW.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Display Library for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
// Requires HW SPI and Adafruit_GFX. Caution: these e-papers require 3.3V supply AND data lines!
//
// based on Demo Example from Good Display: http://www.good-display.com/download_list/downloadcategoryid=34&isMode=false.html
//
// Author: Jean-Marc Zingg
//
// Version: see library.properties
//
// Library: https://github.com/ZinggJM/GxEPD2_32
#include "GxEPD2_32_BW.h"
#include "WaveTables.h"
GxEPD2_32_BW::GxEPD2_32_BW(GxEPD2::Panel panel, int8_t cs, int8_t dc, int8_t rst, int8_t busy) :
Adafruit_GFX(GxEPD2::ScreenDimensions[panel].width, GxEPD2::ScreenDimensions[panel].height),
_panel(panel), _cs(cs), _dc(dc), _rst(rst), _busy(busy),
_current_page(-1), _using_partial_mode(false), _mirror(false)
{
_initial = true;
_power_is_on = false;
_width_bytes = uint16_t(WIDTH) / 8; // just discard any (WIDTH % 8) pixels
_pixel_bytes = _width_bytes * uint16_t(HEIGHT); // save uint16_t range
_page_height = buffer_size / _width_bytes;
_pages = (HEIGHT / _page_height) + ((HEIGHT % _page_height) > 0);
_ram_data_entry_mode = (_panel == GxEPD2::GDE0213B1) ? 0x01 : 0x03;
_reverse = (_panel == GxEPD2::GDE0213B1);
_busy_active_level = (_panel < GxEPD2::GDEW027W3) ? HIGH : LOW;
}
void GxEPD2_32_BW::drawPixel(int16_t x, int16_t y, uint16_t color)
{
if ((x < 0) || (x >= width()) || (y < 0) || (y >= height())) return;
if (_mirror) x = width() - x - 1;
// check rotation, move pixel around if necessary
switch (getRotation())
{
case 1:
swap(x, y);
x = WIDTH - x - 1;
break;
case 2:
x = WIDTH - x - 1;
y = HEIGHT - y - 1;
break;
case 3:
swap(x, y);
y = HEIGHT - y - 1;
break;
}
if (_reverse)
{
// flip y for y-decrement mode
y = HEIGHT - y - 1;
}
uint16_t i = x / 8 + y * _width_bytes;
if (_current_page < 1)
{
if (i >= sizeof(_buffer)) return;
}
else
{
y -= _current_page * _page_height;
if ((y < 0) || (y >= _page_height)) return;
i = x / 8 + y * _width_bytes;
}
if (!color)
_buffer[i] = (_buffer[i] | (1 << (7 - x % 8)));
else
_buffer[i] = (_buffer[i] & (0xFF ^ (1 << (7 - x % 8))));
}
bool GxEPD2_32_BW::mirror(bool m)
{
swap (_mirror, m);
return m;
}
void GxEPD2_32_BW::init()
{
// Serial.print(WIDTH); Serial.print("x"); Serial.print(HEIGHT);
// Serial.print(" : "); Serial.print(_pages); Serial.print(" pages of ");
// Serial.print(_page_height); Serial.println(" height");
if (_cs >= 0)
{
digitalWrite(_cs, HIGH);
pinMode(_cs, OUTPUT);
}
if (_dc >= 0)
{
digitalWrite(_dc, HIGH);
pinMode(_dc, OUTPUT);
}
if (_rst >= 0)
{
digitalWrite(_rst, HIGH);
pinMode(_rst, OUTPUT);
delay(20);
digitalWrite(_rst, LOW);
delay(20);
digitalWrite(_rst, HIGH);
delay(200);
}
if (_busy >= 0)
{
pinMode(_busy, INPUT);
}
SPI.begin();
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
#if defined(SPI_HAS_TRANSACTION)
// true also for STM32F1xx Boards
SPISettings settings(4000000, MSBFIRST, SPI_MODE0);
SPI.beginTransaction(settings);
SPI.endTransaction();
//Serial.println("SPI has Transaction");
#elif defined(ESP8266) || defined(ESP32)
SPI.setFrequency(4000000);
#endif
fillScreen(GxEPD_WHITE);
_initial = true;
_power_is_on = false;
_current_page = -1;
}
void GxEPD2_32_BW::fillScreen(uint16_t color)
{
uint8_t data = (color == GxEPD_BLACK) ? 0xFF : 0x00;
for (uint16_t x = 0; x < sizeof(_buffer); x++)
{
_buffer[x] = data;
}
}
void GxEPD2_32_BW::setFullWindow()
{
_using_partial_mode = false;
}
void GxEPD2_32_BW::setPartialWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
{
_rotate(x, y, w, h);
_using_partial_mode = true;
_pw_x = gx_uint16_min(x, WIDTH);
_pw_y = gx_uint16_min(y, HEIGHT);
_pw_w = gx_uint16_min(w, WIDTH - _pw_x);
_pw_h = gx_uint16_min(h, HEIGHT - _pw_y);
}
void GxEPD2_32_BW::firstPage()
{
_current_page = 0;
_second_phase = false;
if (!_using_partial_mode)
{
switch (_panel)
{
case GxEPD2::GDEP015OC1:
case GxEPD2::GDE0213B1:
case GxEPD2::GDEH029A1:
_Init_Full(_ram_data_entry_mode);
_writeCommand(0x24);
break;
case GxEPD2::GDEW027W3:
case GxEPD2::GDEW042T2:
_Init_Full(_ram_data_entry_mode);
_writeCommand(0x13);
break;
case GxEPD2::GDEW075T8:
_Init_Full(_ram_data_entry_mode);
_writeCommand(0x10);
break;
}
}
else
{
if ((_pw_w > 0) && (_pw_h > 0))
{
switch (_panel)
{
case GxEPD2::GDEP015OC1:
case GxEPD2::GDE0213B1:
case GxEPD2::GDEH029A1:
_Init_Part(_ram_data_entry_mode);
_setRamEntryWindow(_pw_x, _pw_y, _pw_w, _pw_h, _ram_data_entry_mode);
break;
case GxEPD2::GDEW027W3:
_Init_Part(_ram_data_entry_mode);
_setPartialRamArea(_pw_x, _pw_y, _pw_w, _pw_h);
break;
case GxEPD2::GDEW042T2:
_Init_Part(_ram_data_entry_mode);
_writeCommand(0x91); // partial in
_setPartialRamArea(_pw_x, _pw_y, _pw_w, _pw_h);
_writeCommand(0x13);
break;
case GxEPD2::GDEW075T8:
_Init_Part(_ram_data_entry_mode);
_writeCommand(0x91); // partial in
_setPartialRamArea(_pw_x, _pw_y, _pw_w, _pw_h);
_writeCommand(0x10);
}
}
}
}
bool GxEPD2_32_BW::nextPage()
{
if (!_using_partial_mode)
{
switch (_panel)
{
case GxEPD2::GDEP015OC1:
case GxEPD2::GDE0213B1:
case GxEPD2::GDEH029A1:
return _nextPageFull();
case GxEPD2::GDEW027W3:
return _nextPageFull27();
case GxEPD2::GDEW042T2:
return _nextPageFull42();
case GxEPD2::GDEW075T8:
return _nextPageFull75();
}
}
else if ((_pw_w > 0) && (_pw_h > 0))
{
switch (_panel)
{
case GxEPD2::GDEP015OC1:
case GxEPD2::GDE0213B1:
case GxEPD2::GDEH029A1:
return _nextPagePart();
case GxEPD2::GDEW027W3:
return _nextPagePart27();
case GxEPD2::GDEW042T2:
return _nextPagePart42();
case GxEPD2::GDEW075T8:
return _nextPagePart75();
}
}
return false;
}
void GxEPD2_32_BW::powerOff(void)
{
_PowerOff();
}
void GxEPD2_32_BW::drawInvertedBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color)
{
// taken from Adafruit_GFX.cpp, modified
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
for (int16_t j = 0; j < h; j++)
{
for (int16_t i = 0; i < w; i++ )
{
if (i & 7) byte <<= 1;
else
{
#if defined(__AVR) || defined(ESP8266) || defined(ESP32)
byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
#else
byte = bitmap[j * byteWidth + i / 8];
#endif
}
if (!(byte & 0x80))
{
drawPixel(x + i, y + j, color);
}
}
}
}
bool GxEPD2_32_BW::_nextPageFull()
{
uint16_t page_ys = _current_page * _page_height;
uint16_t bytes = (_current_page < (_pages - 1) ? _page_height : HEIGHT - page_ys) * _width_bytes;
for (uint16_t idx = 0; idx < bytes; idx++)
{
uint8_t data = (idx < sizeof(_buffer)) ? _buffer[idx] : 0x00;
_writeData(~data);
}
_current_page++;
if (_current_page < _pages)
{
return true;
}
if (!_second_phase)
{
_Update_Full();
delay(200);
// second phase needed for subsequent partial updates
_second_phase = true;
_current_page = 0;
_Init_Part(_ram_data_entry_mode);
_writeCommand(0x24);
return true;
}
_PowerOff();
_current_page = -1;
return false;
}
bool GxEPD2_32_BW::_nextPagePart()
{
uint16_t page_ys = _current_page * _page_height;
uint16_t page_ye = _current_page < (_pages - 1) ? page_ys + _page_height : HEIGHT;
uint16_t dest_ys = gx_uint16_max(_pw_y, page_ys);
uint16_t dest_ye = gx_uint16_min(_pw_y + _pw_h, page_ye);
if (_reverse)
{
dest_ys = gx_uint16_max(HEIGHT - _pw_y - _pw_h, page_ys);
dest_ye = gx_uint16_min(HEIGHT - _pw_y, page_ye);
}
if (dest_ye > dest_ys)
{
uint16_t xs_d8 = _pw_x / 8;
uint16_t xe_d8 = (_pw_x + _pw_w + 7) / 8;
uint16_t ys = dest_ys - page_ys;
uint16_t ye = dest_ye - page_ys;
for (uint16_t y = ys; y < ye; y++)
{
for (uint16_t x = xs_d8; x < xe_d8; x++)
{
uint16_t idx = x + y * _width_bytes;
uint8_t data = (idx < sizeof(_buffer)) ? _buffer[idx] : 0x00;
_writeData(~data);
}
}
}
_current_page++;
if (_current_page < _pages)
{
fillScreen(GxEPD_WHITE);
return true;
}
if (!_second_phase)
{
_Update_Part();
delay(200);
_second_phase = true;
_current_page = 0;
_setRamEntryWindow(_pw_x, _pw_y, _pw_w, _pw_h, _ram_data_entry_mode); // needed!
return true;
}
delay(200);
//_PowerOff();
_current_page = -1;
return false;
}
bool GxEPD2_32_BW::_nextPageFull27()
{
uint16_t page_ys = _current_page * _page_height;
uint16_t bytes = (_current_page < (_pages - 1) ? _page_height : HEIGHT - page_ys) * _width_bytes;
for (uint16_t idx = 0; idx < bytes; idx++)
{
uint8_t data = (idx < sizeof(_buffer)) ? _buffer[idx] : 0x00;
_writeData(~data);
}
_current_page++;
if (_current_page < _pages)
{
return true;
}
_Update_Full();
delay(200);
_PowerOff();
_current_page = -1;
return false;
}
bool GxEPD2_32_BW::_nextPagePart27()
{
uint16_t page_ys = _current_page * _page_height;
uint16_t page_ye = _current_page < (_pages - 1) ? page_ys + _page_height : HEIGHT;
uint16_t dest_ys = gx_uint16_max(_pw_y, page_ys);
uint16_t dest_ye = gx_uint16_min(_pw_y + _pw_h, page_ye);
if (dest_ye > dest_ys)
{
uint16_t xs_d8 = _pw_x / 8; // byte boundary
uint16_t xe_d8 = ((_pw_x + _pw_w - 1) | 0x0007) / 8; // byte boundary inclusive (last byte)
uint16_t ys = dest_ys - page_ys;
uint16_t ye = dest_ye - page_ys; // exclusive
for (uint16_t y = ys; y < ye; y++)
{
for (uint16_t x = xs_d8; x <= xe_d8; x++)
{
uint16_t idx = x + y * _width_bytes;
uint8_t data = (idx < sizeof(_buffer)) ? _buffer[idx] : 0x00;
_writeData(~data);
}
}
}
_current_page++;
if (_current_page < _pages)
{
fillScreen(GxEPD_WHITE);
return true;
}
_refreshWindow(_pw_x, _pw_y, _pw_w, _pw_h);
_waitWhileBusy("_nextPage27");
delay(500); // don't stress this display
//_PowerOff();
_current_page = -1;
return false;
}
bool GxEPD2_32_BW::_nextPageFull42()
{
uint16_t page_ys = _current_page * _page_height;
uint16_t bytes = (_current_page < (_pages - 1) ? _page_height : HEIGHT - page_ys) * _width_bytes;
for (uint16_t idx = 0; idx < bytes; idx++)
{
uint8_t data = (idx < sizeof(_buffer)) ? _buffer[idx] : 0x00;
_writeData(~data);
}
_current_page++;
if (_current_page < _pages)
{
return true;
}
if (_initial)
{
_Update_Full();
delay(200);
_initial = false;
_current_page = 0;
_writeCommand(0x13);
return true;
}
_Update_Full();
delay(200);
_PowerOff();
_current_page = -1;
return false;
}
bool GxEPD2_32_BW::_nextPagePart42()
{
uint16_t page_ys = _current_page * _page_height;
uint16_t page_ye = _current_page < (_pages - 1) ? page_ys + _page_height : HEIGHT;
uint16_t dest_ys = gx_uint16_max(_pw_y, page_ys);
uint16_t dest_ye = gx_uint16_min(_pw_y + _pw_h, page_ye);
if (dest_ye > dest_ys)
{
uint16_t xe_d8 = ((_pw_x + _pw_w - 1) | 0x0007) / 8; // byte boundary inclusive (last byte)
uint16_t xs_d8 = _pw_x / 8; // byte boundary
uint16_t ys = dest_ys - page_ys;
uint16_t ye = dest_ye - page_ys; // exclusive
for (uint16_t y = ys; y < ye; y++)
{
for (uint16_t x = xs_d8; x <= xe_d8; x++)
{
uint16_t idx = x + y * _width_bytes;
uint8_t data = (idx < sizeof(_buffer)) ? _buffer[idx] : 0x00;
_writeData(~data);
}
}
}
_current_page++;
if (_current_page < _pages)
{
fillScreen(GxEPD_WHITE);
return true;
}
if (!_second_phase)
{
_Update_Part();
_current_page = 0;
_second_phase = true;
fillScreen(GxEPD_WHITE);
_writeCommand(0x13);
return true;
}
_writeCommand(0x92); // partial out
_current_page = -1;
return false;
}
bool GxEPD2_32_BW::_nextPageFull75()
{
uint16_t page_ys = _current_page * _page_height;
uint16_t bytes = (_current_page < (_pages - 1) ? _page_height : HEIGHT - page_ys) * _width_bytes;
for (uint16_t idx = 0; idx < bytes; idx++)
{
uint8_t data = (idx < sizeof(_buffer)) ? _buffer[idx] : 0x00;
_send8pixel(data);
}
_current_page++;
if (_current_page < _pages)
{
return true;
}
_Update_Full();
delay(200);
_PowerOff();
_current_page = -1;
return false;
}
bool GxEPD2_32_BW::_nextPagePart75()
{
uint16_t page_ys = _current_page * _page_height;
uint16_t page_ye = _current_page < (_pages - 1) ? page_ys + _page_height : HEIGHT;
uint16_t dest_ys = gx_uint16_max(_pw_y, page_ys);
uint16_t dest_ye = gx_uint16_min(_pw_y + _pw_h, page_ye);
if (dest_ye > dest_ys)
{
uint16_t xe_d8 = ((_pw_x + _pw_w - 1) | 0x0007) / 8; // byte boundary inclusive (last byte)
uint16_t xs_d8 = _pw_x / 8; // byte boundary
uint16_t ys = dest_ys - page_ys;
uint16_t ye = dest_ye - page_ys; // exclusive
for (uint16_t y = ys; y < ye; y++)
{
for (uint16_t x = xs_d8; x <= xe_d8; x++)
{
uint16_t idx = x + y * _width_bytes;
uint8_t data = (idx < sizeof(_buffer)) ? _buffer[idx] : 0x00;
_send8pixel(data);
}
}
}
_current_page++;
if (_current_page < _pages)
{
fillScreen(GxEPD_WHITE);
return true;
}
_Update_Part();
_writeCommand(0x92); // partial out
_current_page = -1;
return false;
}
void GxEPD2_32_BW::_send8pixel(uint8_t data)
{
for (uint8_t j = 0; j < 8; j++)
{
uint8_t t = data & 0x80 ? 0x00 : 0x03;
t <<= 4;
data <<= 1;
j++;
t |= data & 0x80 ? 0x00 : 0x03;
data <<= 1;
_writeData(t);
}
}
void GxEPD2_32_BW::_writeCommand(uint8_t c)
{
if (_dc >= 0) digitalWrite(_dc, LOW);
if (_cs >= 0) digitalWrite(_cs, LOW);
SPI.transfer(c);
if (_cs >= 0) digitalWrite(_cs, HIGH);
if (_dc >= 0) digitalWrite(_dc, HIGH);
}
void GxEPD2_32_BW::_writeData(uint8_t d)
{
if (_cs >= 0) digitalWrite(_cs, LOW);
SPI.transfer(d);
if (_cs >= 0) digitalWrite(_cs, HIGH);
}
void GxEPD2_32_BW::_writeData(const uint8_t* data, uint16_t n)
{
if (_cs >= 0) digitalWrite(_cs, LOW);
for (uint8_t i = 0; i < n; i++)
{
SPI.transfer(*data++);
}
if (_cs >= 0) digitalWrite(_cs, HIGH);
}
void GxEPD2_32_BW::_writeCommandData(const uint8_t* pCommandData, uint8_t datalen)
{
if (_dc >= 0) digitalWrite(_dc, LOW);
if (_cs >= 0) digitalWrite(_cs, LOW);
SPI.transfer(*pCommandData++);
if (_dc >= 0) digitalWrite(_dc, HIGH);
for (uint8_t i = 0; i < datalen - 1; i++) // sub the command
{
SPI.transfer(*pCommandData++);
}
if (_cs >= 0) digitalWrite(_cs, HIGH);
}
void GxEPD2_32_BW::_waitWhileBusy(const char* comment)
{
unsigned long start = micros();
while (1)
{
if (digitalRead(_busy) != _busy_active_level) break;
delay(1);
if (micros() - start > 10000000)
{
Serial.println("Busy Timeout!");
break;
}
}
if (comment)
{
#if !defined(DISABLE_DIAGNOSTIC_OUTPUT)
unsigned long elapsed = micros() - start;
Serial.print(comment);
Serial.print(" : ");
Serial.println(elapsed);
#endif
}
(void) start;
}
void GxEPD2_32_BW::_setRamEntryWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t em)
{
em = gx_uint16_min(em, 0x03);
_writeCommand(0x11);
_writeData(em);
switch (em)
{
case 0x00: // x decrease, y decrease
_setRamArea((x + w - 1), x, (y + h - 1), y);
_setRamPointer((x + w - 1), (y + h - 1));
break;
case 0x01: // x increase, y decrease : as in demo code
_setRamArea(x, (x + w - 1), (y + h - 1), y);
_setRamPointer(x, (y + h - 1));
break;
case 0x02: // x decrease, y increase
_setRamArea((x + w - 1), x, y, (y + h - 1));
_setRamPointer((x + w - 1), y);
break;
case 0x03: // x increase, y increase : normal mode
_setRamArea(x, (x + w - 1), y, (y + h - 1));
_setRamPointer(x, y);
break;
}
_writeCommand(0x24);
}
void GxEPD2_32_BW::_setRamArea(uint16_t xs, uint16_t xe, uint16_t ys, uint16_t ye)
{
_writeCommand(0x44);
_writeData(xs / 8);
_writeData(xe / 8);
_writeCommand(0x45);
_writeData(ys % 256);
_writeData(ys / 256);
_writeData(ye % 256);
_writeData(ye / 256);
}
void GxEPD2_32_BW::_setRamPointer(uint16_t x, uint16_t y)
{
_writeCommand(0x4e);
_writeData(x / 8);
_writeCommand(0x4f);
_writeData(y % 256);
_writeData(y / 256);
}
void GxEPD2_32_BW::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
{
if (_panel == GxEPD2::GDEW027W3)
{
w = (w + 7 + (x % 8)) & 0xfff8; // byte boundary exclusive (round up)
_writeCommand(0x15);
_writeData(x >> 8);
_writeData(x & 0xf8);
_writeData(y >> 8);
_writeData(y & 0xff);
_writeData(w >> 8);
_writeData(w & 0xf8);
_writeData(h >> 8);
_writeData(h & 0xff);
}
else
{
uint16_t xe = (x + w - 1) | 0x0007; // byte boundary inclusive (last byte)
x &= 0xFFF8; // byte boundary
uint16_t ye = y + h - 1;
_writeCommand(0x90); // partial window
_writeData(x / 256);
_writeData(x % 256);
_writeData(xe / 256);
_writeData(xe % 256);
_writeData(y / 256);
_writeData(y % 256);
_writeData(ye / 256);
_writeData(ye % 256);
_writeData(0x01); // don't see any difference
//_writeData(0x00); // don't see any difference
}
}
void GxEPD2_32_BW::_refreshWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
{
w = (w + 7 + (x % 8)) & 0xfff8; // byte boundary exclusive (round up)
h = gx_uint16_min(h, 256); // strange controller error
_writeCommand(0x16);
_writeData(x >> 8);
_writeData(x & 0xf8);
_writeData(y >> 8);
_writeData(y & 0xff);
_writeData(w >> 8);
_writeData(w & 0xf8);
_writeData(h >> 8);
_writeData(h & 0xff);
}
void GxEPD2_32_BW::_PowerOn(void)
{
if (!_power_is_on)
{
if (_panel < GxEPD2::GDEW027W3)
{
_writeCommand(0x22);
_writeData(0xc0);
_writeCommand(0x20);
}
else
{
_writeCommand(0x04);
}
_waitWhileBusy("_PowerOn");
}
_power_is_on = true;
}
void GxEPD2_32_BW::_PowerOff(void)
{
if (_panel < GxEPD2::GDEW027W3)
{
_writeCommand(0x22);
_writeData(0xc3);
_writeCommand(0x20);
}
else
{
_writeCommand(0x02); // power off
}
_waitWhileBusy("_PowerOff");
_power_is_on = false;
}
void GxEPD2_32_BW::_InitDisplay(uint8_t em)
{
switch (_panel)
{
case GxEPD2::GDEP015OC1:
_writeCommand(0x01); // Panel configuration, Gate selection
_writeData((HEIGHT - 1) % 256);
_writeData((HEIGHT - 1) / 256);
_writeData(0x00);
_writeCommand(0x0c); // softstart
_writeData(0xd7);
_writeData(0xd6);
_writeData(0x9d);
_writeCommand(0x2c); // VCOM setting
_writeData(0x9b);
_writeCommand(0x3a); // DummyLine
_writeData(0x1a); // 4 dummy line per gate
_writeCommand(0x3b); // Gatetime
_writeData(0x08); // 2us per line
_setRamEntryWindow(0, 0, WIDTH, HEIGHT, em);
break;
case GxEPD2::GDE0213B1:
_writeCommand(0x01); // Panel configuration, Gate selection
_writeData((HEIGHT - 1) % 256);
_writeData((HEIGHT - 1) / 256);
_writeData(0x00);
_writeCommand(0x0c); // softstart
_writeData(0xd7);
_writeData(0xd6);
_writeData(0x9d);
_writeCommand(0x2c); // VCOM setting
_writeData(0xa8); // * different
_writeCommand(0x3a); // DummyLine
_writeData(0x1a); // 4 dummy line per gate
_writeCommand(0x3b); // Gatetime
_writeData(0x08); // 2us per line
_setRamEntryWindow(0, 0, WIDTH, HEIGHT, em);
break;
case GxEPD2::GDEH029A1:
_writeCommand(0x01); // Panel configuration, Gate selection
_writeData((HEIGHT - 1) % 256);
_writeData((HEIGHT - 1) / 256);
_writeData(0x00);
_writeCommand(0x0c); // softstart
_writeData(0xd7);
_writeData(0xd6);
_writeData(0x9d);
_writeCommand(0x2c); // VCOM setting
_writeData(0xa8); // * different
_writeCommand(0x3a); // DummyLine
_writeData(0x1a); // 4 dummy line per gate
_writeCommand(0x3b); // Gatetime
_writeData(0x08); // 2us per line
_setRamEntryWindow(0, 0, WIDTH, HEIGHT, em);
break;
case GxEPD2::GDEW027W3:
_writeCommand(0x01);
_writeData (0x03);
_writeData (0x00);
_writeData (0x2b);
_writeData (0x2b);
_writeData (0x09);
_writeCommand(0x06);
_writeData (0x07);
_writeData (0x07);
_writeData (0x17);
_writeCommand(0xF8);
_writeData (0x60);
_writeData (0xA5);
_writeCommand(0xF8);
_writeData (0x89);
_writeData (0xA5);
_writeCommand(0xF8);
_writeData (0x90);
_writeData (0x00);
_writeCommand(0xF8);
_writeData (0x93);
_writeData (0x2A);
_writeCommand(0xF8);
_writeData (0xa0);
_writeData (0xa5);
_writeCommand(0xF8);
_writeData (0xa1);
_writeData (0x00);
_writeCommand(0xF8);
_writeData (0x73);
_writeData (0x41);
_writeCommand(0x16);
_writeData(0x00);
//_writeCommand(0x04);
//_waitWhileBusy("_wakeUp Power On");
_writeCommand(0x00);
_writeData(0x9f); // b/w, by OTP LUT
_writeCommand(0x30);
_writeData (0x3a); //3A 100HZ
_writeCommand(0x61);
_writeData (0x00);
_writeData (0xb0); //176
_writeData (0x01);
_writeData (0x08); //264
_writeCommand(0x82);
_writeData (0x12);
break;
case GxEPD2::GDEW042T2:
_writeCommand(0x06); // boost
_writeData(0x17);
_writeData(0x17);
_writeData(0x17);
//_writeCommand(0x04);
//_waitWhileBusy("Power On");
_writeCommand(0x00);
//_writeData(0x1f); // LUT from OTP Pixel with B/W.
_writeData(0x3F); //300x400 B/W mode, LUT set by register
break;
case GxEPD2::GDEW075T8:
if (!_power_is_on && (_rst >= 0))
{
digitalWrite(_rst, 0);
delay(10);
digitalWrite(_rst, 1);
delay(10);
}
/**********************************release flash sleep**********************************/
_writeCommand(0X65); //FLASH CONTROL
_writeData(0x01);
_writeCommand(0xAB);
_writeCommand(0X65); //FLASH CONTROL
_writeData(0x00);
/**********************************release flash sleep**********************************/
_writeCommand(0x01);
_writeData (0x37); //POWER SETTING
_writeData (0x00);
_writeCommand(0X00); //PANNEL SETTING
_writeData(0xCF);
_writeData(0x08);
_writeCommand(0x06); //boost
_writeData (0xc7);
_writeData (0xcc);
_writeData (0x28);
_writeCommand(0x30); //PLL setting
_writeData (0x3c);
_writeCommand(0X41); //TEMPERATURE SETTING
_writeData(0x00);
_writeCommand(0X50); //VCOM AND DATA INTERVAL SETTING
_writeData(0x77);
_writeCommand(0X60); //TCON SETTING
_writeData(0x22);
_writeCommand(0x61); //tres 640*384
_writeData (0x02); //source 640
_writeData (0x80);
_writeData (0x01); //gate 384
_writeData (0x80);
_writeCommand(0X82); //VDCS SETTING
_writeData(0x1E); //decide by LUT file
_writeCommand(0xe5); //FLASH MODE
_writeData(0x03);
//_writeCommand(0x04); //POWER ON
//_waitWhileBusy();
break;
}
}
void GxEPD2_32_BW::_Init_Full(uint8_t em)
{
_InitDisplay(em);
switch (_panel)
{
case GxEPD2::GDEP015OC1:
_writeCommandData(GDEP015OC1_LUTDefault_full, sizeof(GDEP015OC1_LUTDefault_full));
break;
case GxEPD2::GDE0213B1:
_writeCommandData(GxGDE0213B1_LUTDefault_full, sizeof(GxGDE0213B1_LUTDefault_full));
break;
case GxEPD2::GDEH029A1:
_writeCommandData(GxGDEH029A1_LUTDefault_full, sizeof(GxGDEH029A1_LUTDefault_full));
break;
case GxEPD2::GDEW027W3:
_writeCommand(0x20);
_writeData(GxGDEW027W3_lut_20_vcomDC, sizeof(GxGDEW027W3_lut_20_vcomDC));
_writeCommand(0x21);
_writeData(GxGDEW027W3_lut_21_ww, sizeof(GxGDEW027W3_lut_21_ww));
_writeCommand(0x22);
_writeData(GxGDEW027W3_lut_22_bw, sizeof(GxGDEW027W3_lut_22_bw));
_writeCommand(0x23);
_writeData(GxGDEW027W3_lut_23_wb, sizeof(GxGDEW027W3_lut_23_wb));
_writeCommand(0x24);
_writeData(GxGDEW027W3_lut_24_bb, sizeof(GxGDEW027W3_lut_24_bb));
break;
case GxEPD2::GDEW042T2:
_writeCommand(0x20);
_writeData(GxGDEW042T2_lut_20_vcom0_full, sizeof(GxGDEW042T2_lut_20_vcom0_full));
_writeCommand(0x21);
_writeData(GxGDEW042T2_lut_21_ww_full, sizeof(GxGDEW042T2_lut_21_ww_full));
_writeCommand(0x22);
_writeData(GxGDEW042T2_lut_22_bw_full, sizeof(GxGDEW042T2_lut_22_bw_full));
_writeCommand(0x23);
_writeData(GxGDEW042T2_lut_23_wb_full, sizeof(GxGDEW042T2_lut_23_wb_full));
_writeCommand(0x24);
_writeData(GxGDEW042T2_lut_24_bb_full, sizeof(GxGDEW042T2_lut_24_bb_full));
break;
}
_PowerOn();
}
void GxEPD2_32_BW::_Init_Part(uint8_t em)
{
_InitDisplay(em);
switch (_panel)
{
case GxEPD2::GDEP015OC1:
_writeCommandData(GDEP015OC1_LUTDefault_part, sizeof(GDEP015OC1_LUTDefault_part));
break;
case GxEPD2::GDE0213B1:
_writeCommandData(GxGDE0213B1_LUTDefault_part, sizeof(GxGDE0213B1_LUTDefault_part));
break;
case GxEPD2::GDEH029A1:
_writeCommandData(GxGDEH029A1_LUTDefault_part, sizeof(GxGDEH029A1_LUTDefault_part));
break;
case GxEPD2::GDEW027W3:
// no partial update LUT
_writeCommand(0x20);
_writeData(GxGDEW027W3_lut_20_vcomDC, sizeof(GxGDEW027W3_lut_20_vcomDC));
_writeCommand(0x21);
_writeData(GxGDEW027W3_lut_21_ww, sizeof(GxGDEW027W3_lut_21_ww));
_writeCommand(0x22);
_writeData(GxGDEW027W3_lut_22_bw, sizeof(GxGDEW027W3_lut_22_bw));
_writeCommand(0x23);
_writeData(GxGDEW027W3_lut_23_wb, sizeof(GxGDEW027W3_lut_23_wb));
_writeCommand(0x24);
_writeData(GxGDEW027W3_lut_24_bb, sizeof(GxGDEW027W3_lut_24_bb));
break;
case GxEPD2::GDEW042T2:
_writeCommand(0x20);
_writeData(GxGDEW042T2_lut_20_vcom0_partial, sizeof(GxGDEW042T2_lut_20_vcom0_partial));
_writeCommand(0x21);
_writeData(GxGDEW042T2_lut_21_ww_partial, sizeof(GxGDEW042T2_lut_21_ww_partial));
_writeCommand(0x22);
_writeData(GxGDEW042T2_lut_22_bw_partial, sizeof(GxGDEW042T2_lut_22_bw_partial));
_writeCommand(0x23);
_writeData(GxGDEW042T2_lut_23_wb_partial, sizeof(GxGDEW042T2_lut_23_wb_partial));
_writeCommand(0x24);
_writeData(GxGDEW042T2_lut_24_bb_partial, sizeof(GxGDEW042T2_lut_24_bb_partial));
break;
}
_PowerOn();
}
void GxEPD2_32_BW::_Update_Full(void)
{
if (_panel < GxEPD2::GDEW027W3)
{
_writeCommand(0x22);
_writeData(0xc4);