forked from mcarr823/PaperTTY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drivers_partial.py
890 lines (745 loc) · 31.8 KB
/
drivers_partial.py
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
# Copyright (c) 2018 Jouko Strömmer
# Copyright (c) 2017 Waveshare
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from papertty.drivers.drivers_base import WaveshareEPD
from papertty.drivers.drivers_base import GPIO
class WavesharePartial(WaveshareEPD):
"""Displays that support partial refresh (*monochrome*): 1.54", 2.13", 2.9".
The code is almost entirely identical with these, just small differences in the 2.13"."""
BOOSTER_SOFT_START_CONTROL = 0x0C
BORDER_WAVEFORM_CONTROL = 0x3C
DATA_ENTRY_MODE_SETTING = 0x11
DEEP_SLEEP_MODE = 0x10
DISPLAY_UPDATE_CONTROL_1 = 0x21
DISPLAY_UPDATE_CONTROL_2 = 0x22
DRIVER_OUTPUT_CONTROL = 0x01
GATE_SCAN_START_POSITION = 0x0F
MASTER_ACTIVATION = 0x20
SET_DUMMY_LINE_PERIOD = 0x3A
SET_GATE_TIME = 0x3B
SET_RAM_X_ADDRESS_COUNTER = 0x4E
SET_RAM_X_ADDRESS_START_END_POSITION = 0x44
SET_RAM_Y_ADDRESS_COUNTER = 0x4F
SET_RAM_Y_ADDRESS_START_END_POSITION = 0x45
SW_RESET = 0x12
TEMPERATURE_SENSOR_CONTROL = 0x1A
TERMINATE_FRAME_READ_WRITE = 0xFF
WRITE_LUT_REGISTER = 0x32
WRITE_RAM = 0x24
WRITE_VCOM_REGISTER = 0x2C
# these LUTs are used by 1.54" and 2.9" - 2.13" overrides them
lut_full_update = [
0x02, 0x02, 0x01, 0x11, 0x12, 0x12, 0x22, 0x22,
0x66, 0x69, 0x69, 0x59, 0x58, 0x99, 0x99, 0x88,
0x00, 0x00, 0x00, 0x00, 0xF8, 0xB4, 0x13, 0x51,
0x35, 0x51, 0x51, 0x19, 0x01, 0x00
]
lut_partial_update = [
0x10, 0x18, 0x18, 0x08, 0x18, 0x18, 0x08, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x13, 0x14, 0x44, 0x12,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
]
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.supports_partial = True
self.colors = 2
self.lut = None
def init(self, partial=True, **kwargs):
self.partial_refresh = partial
if self.epd_init() != 0:
return -1
# EPD hardware init start
self.lut = self.lut_partial_update if partial else self.lut_full_update
self.reset()
self.send_command(self.DRIVER_OUTPUT_CONTROL)
self.send_data((self.height - 1) & 0xFF)
self.send_data(((self.height - 1) >> 8) & 0xFF)
self.send_data(0x00) # GD = 0 SM = 0 TB = 0
self.send_command(self.BOOSTER_SOFT_START_CONTROL)
self.send_data(0xD7)
self.send_data(0xD6)
self.send_data(0x9D)
self.send_command(self.WRITE_VCOM_REGISTER)
self.send_data(0xA8) # VCOM 7C
self.send_command(self.SET_DUMMY_LINE_PERIOD)
self.send_data(0x1A) # 4 dummy lines per gate
self.send_command(self.SET_GATE_TIME)
self.send_data(0x08) # 2us per line
self.send_command(self.DATA_ENTRY_MODE_SETTING)
self.send_data(0x03) # X increment Y increment
self.set_lut(self.lut)
# EPD hardware init end
return 0
def wait_until_idle(self):
while self.digital_read(self.BUSY_PIN) == 1: # 0: idle, 1: busy
self.delay_ms(100)
def set_lut(self, lut):
self.lut = lut
self.send_command(self.WRITE_LUT_REGISTER)
# the length of look-up table is 30 bytes
for i in range(0, len(lut)):
self.send_data(self.lut[i])
def get_frame_buffer(self, image):
buf = [0x00] * int(self.width * self.height / 8)
# Set buffer to value of Python Imaging Library image.
# Image must be in mode 1.
image_monocolor = image.convert('1')
imwidth, imheight = image_monocolor.size
if imwidth != self.width or imheight != self.height:
raise ValueError('Image must be same dimensions as display \
({0}x{1}).'.format(self.width, self.height))
pixels = image_monocolor.load()
for y in range(self.height):
for x in range(self.width):
# Set the bits for the column of pixels at the current position.
if pixels[x, y] != 0:
buf[int((x + y * self.width) / 8)] |= 0x80 >> (x % 8)
return buf
# this differs with 2.13" but is the same for 1.54" and 2.9"
def set_frame_memory(self, image, x, y):
if image is None or x < 0 or y < 0:
return
image_monocolor = image.convert('1')
image_width, image_height = image_monocolor.size
# x point must be the multiple of 8 or the last 3 bits will be ignored
x = x & 0xF8
image_width = image_width & 0xF8
if x + image_width >= self.width:
x_end = self.width - 1
else:
x_end = x + image_width - 1
if y + image_height >= self.height:
y_end = self.height - 1
else:
y_end = y + image_height - 1
self.set_memory_area(x, y, x_end, y_end)
self.set_memory_pointer(x, y)
self.send_command(self.WRITE_RAM)
# send the image data
pixels = image_monocolor.load()
byte_to_send = 0x00
for j in range(0, y_end - y + 1):
# 1 byte = 8 pixels, steps of i = 8
for i in range(0, x_end - x + 1):
# Set the bits for the column of pixels at the current position.
if pixels[i, j] != 0:
byte_to_send |= 0x80 >> (i % 8)
if i % 8 == 7:
self.send_data(byte_to_send)
byte_to_send = 0x00
def clear_frame_memory(self, color):
self.set_memory_area(0, 0, self.width - 1, self.height - 1)
self.set_memory_pointer(0, 0)
self.send_command(self.WRITE_RAM)
# send the color data
for i in range(0, int(self.width / 8 * self.height)):
self.send_data(color)
def display_frame(self):
self.send_command(self.DISPLAY_UPDATE_CONTROL_2)
self.send_data(0xC4)
self.send_command(self.MASTER_ACTIVATION)
self.send_command(self.TERMINATE_FRAME_READ_WRITE)
self.wait_until_idle()
def set_memory_area(self, x_start, y_start, x_end, y_end):
self.send_command(self.SET_RAM_X_ADDRESS_START_END_POSITION)
# x point must be the multiple of 8 or the last 3 bits will be ignored
self.send_data((x_start >> 3) & 0xFF)
self.send_data((x_end >> 3) & 0xFF)
self.send_command(self.SET_RAM_Y_ADDRESS_START_END_POSITION)
self.send_data(y_start & 0xFF)
self.send_data((y_start >> 8) & 0xFF)
self.send_data(y_end & 0xFF)
self.send_data((y_end >> 8) & 0xFF)
def set_memory_pointer(self, x, y):
self.send_command(self.SET_RAM_X_ADDRESS_COUNTER)
# x point must be the multiple of 8 or the last 3 bits will be ignored
self.send_data((x >> 3) & 0xFF)
self.send_command(self.SET_RAM_Y_ADDRESS_COUNTER)
self.send_data(y & 0xFF)
self.send_data((y >> 8) & 0xFF)
self.wait_until_idle()
def sleep(self):
self.send_command(self.DEEP_SLEEP_MODE)
self.wait_until_idle()
def draw(self, x, y, image):
"""Replace a particular area on the display with an image"""
self.set_frame_memory(image, x, y)
self.display_frame()
if self.partial_refresh:
# set the memory again if partial refresh LUT is used
self.set_frame_memory(image, x, y)
self.display_frame()
class EPD1in54(WavesharePartial):
"""Waveshare 1.54" - monochrome"""
def __init__(self):
super().__init__(name='1.54" BW', width=200, height=200)
class EPD2in13(WavesharePartial):
"""Waveshare 2.13" - monochrome"""
lut_full_update = [
0x22, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x11,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00
]
lut_partial_update = [
0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
]
def __init__(self):
# the actual pixel width is 122, but 128 is the 'logical' width
super().__init__(name='2.13" BW', width=128, height=250)
def set_frame_memory(self, image, x, y):
if image is None or x < 0 or y < 0:
return
image_monocolor = image.convert('1')
image_width, image_height = image_monocolor.size
# x point must be the multiple of 8 or the last 3 bits will be ignored
x = x & 0xF8
image_width = image_width & 0xF8
if x + image_width >= self.width:
x_end = self.width - 1
else:
x_end = x + image_width - 1
if y + image_height >= self.height:
y_end = self.height - 1
else:
y_end = y + image_height - 1
self.set_memory_area(x, y, x_end, y_end)
# send the image data
pixels = image_monocolor.load()
byte_to_send = 0x00
for j in range(y, y_end + 1):
self.set_memory_pointer(x, j)
self.send_command(self.WRITE_RAM)
# 1 byte = 8 pixels, steps of i = 8
for i in range(x, x_end + 1):
# Set the bits for the column of pixels at the current position.
if pixels[i - x, j - y] != 0:
byte_to_send |= 0x80 >> (i % 8)
if i % 8 == 7:
self.send_data(byte_to_send)
byte_to_send = 0x00
class EPD2in13v2(WavesharePartial):
"""Waveshare 2.13" V2 - monochrome"""
lut_full_update = [
0x80,0x60,0x40,0x00,0x00,0x00,0x00, #LUT0: BB: VS 0 ~7
0x10,0x60,0x20,0x00,0x00,0x00,0x00, #LUT1: BW: VS 0 ~7
0x80,0x60,0x40,0x00,0x00,0x00,0x00, #LUT2: WB: VS 0 ~7
0x10,0x60,0x20,0x00,0x00,0x00,0x00, #LUT3: WW: VS 0 ~7
0x00,0x00,0x00,0x00,0x00,0x00,0x00, #LUT4: VCOM: VS 0 ~7
0x03,0x03,0x00,0x00,0x02, # TP0 A~D RP0
0x09,0x09,0x00,0x00,0x02, # TP1 A~D RP1
0x03,0x03,0x00,0x00,0x02, # TP2 A~D RP2
0x00,0x00,0x00,0x00,0x00, # TP3 A~D RP3
0x00,0x00,0x00,0x00,0x00, # TP4 A~D RP4
0x00,0x00,0x00,0x00,0x00, # TP5 A~D RP5
0x00,0x00,0x00,0x00,0x00, # TP6 A~D RP6
0x15,0x41,0xA8,0x32,0x30,0x0A
]
lut_partial_update = [
0x00,0x00,0x00,0x00,0x00,0x00,0x00, #LUT0: BB: VS 0 ~7
0x80,0x00,0x00,0x00,0x00,0x00,0x00, #LUT1: BW: VS 0 ~7
0x40,0x00,0x00,0x00,0x00,0x00,0x00, #LUT2: WB: VS 0 ~7
0x00,0x00,0x00,0x00,0x00,0x00,0x00, #LUT3: WW: VS 0 ~7
0x00,0x00,0x00,0x00,0x00,0x00,0x00, #LUT4: VCOM: VS 0 ~7
0x0A,0x00,0x00,0x00,0x00, # TP0 A~D RP0
0x00,0x00,0x00,0x00,0x00, # TP1 A~D RP1
0x00,0x00,0x00,0x00,0x00, # TP2 A~D RP2
0x00,0x00,0x00,0x00,0x00, # TP3 A~D RP3
0x00,0x00,0x00,0x00,0x00, # TP4 A~D RP4
0x00,0x00,0x00,0x00,0x00, # TP5 A~D RP5
0x00,0x00,0x00,0x00,0x00, # TP6 A~D RP6
0x15,0x41,0xA8,0x32,0x30,0x0A,
]
def __init__(self):
# the actual pixel width is 122, but 128 is the 'logical' width
super().__init__(name='2.13" BW V2 (full refresh only)', width=128, height=250)
class EPD2in9(WavesharePartial):
"""Waveshare 2.9" - monochrome"""
def __init__(self):
super().__init__(name='2.9" BW', width=128, height=296)
class EPD2in13d(WavesharePartial):
"""Waveshare 2.13" D - monochrome (flexible)"""
# Note: the original code for this display was pretty broken and seemed
# to have been written by some other person than the rest of the drivers.
def __init__(self):
super().__init__(name='2.13" D', width=104, height=212)
lut_vcomDC = [
0x00, 0x08, 0x00, 0x00, 0x00, 0x02,
0x60, 0x28, 0x28, 0x00, 0x00, 0x01,
0x00, 0x14, 0x00, 0x00, 0x00, 0x01,
0x00, 0x12, 0x12, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
]
lut_ww = [
0x40, 0x08, 0x00, 0x00, 0x00, 0x02,
0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
0x40, 0x14, 0x00, 0x00, 0x00, 0x01,
0xA0, 0x12, 0x12, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]
lut_bw = [
0x40, 0x17, 0x00, 0x00, 0x00, 0x02,
0x90, 0x0F, 0x0F, 0x00, 0x00, 0x03,
0x40, 0x0A, 0x01, 0x00, 0x00, 0x01,
0xA0, 0x0E, 0x0E, 0x00, 0x00, 0x02,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]
lut_wb = [
0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]
lut_bb = [
0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]
lut_vcom1 = [
0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
]
lut_ww1 = [
0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]
lut_bw1 = [
0x80, 0x19, 0x01, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]
lut_wb1 = [
0x40, 0x19, 0x01, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]
lut_bb1 = [
0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]
def wait_until_idle(self):
"""This particular model's code sends the GET_STATUS command while waiting - dunno why."""
while self.digital_read(self.BUSY_PIN) == 0: # 0: idle, 1: busy
self.send_command(self.GET_STATUS)
self.delay_ms(100)
def init(self, **kwargs):
if self.epd_init() != 0:
return -1
self.reset()
self.send_command(0x01) # POWER SETTING
self.send_data(0x03)
self.send_data(0x00)
self.send_data(0x2b)
self.send_data(0x2b)
self.send_data(0x03)
self.send_command(0x06) # boost soft start
self.send_data(0x17) # A
self.send_data(0x17) # B
self.send_data(0x17) # C
self.send_command(0x04)
self.wait_until_idle()
self.send_command(0x00) # panel setting
self.send_data(0xbf) # LUT from OTP,128x296
self.send_data(0x0d) # VCOM to 0V fast
self.send_command(0x30) # PLL setting
self.send_data(0x3a) # 3a 100HZ 29 150Hz 39 200HZ 31 171HZ
self.send_command(0x61) # resolution setting
self.send_data(self.width)
self.send_data((self.height >> 8) & 0xff)
self.send_data(self.height & 0xff)
self.send_command(0x82) # vcom_DC setting
self.send_data(0x28)
# self.send_command(0X50) #VCOM AND DATA INTERVAL SETTING
# self.send_data(0xb7) #WBmode:VBDF 17|D7 VBDW 97 VBDB 57 WBRmode:VBDF F7 VBDW 77 VBDB 37 VBDR B7
return 0
def set_full_reg(self):
self.send_command(0x82)
self.send_data(0x00)
self.send_command(0X50)
self.send_data(0xb7)
self.send_command(0x20) # vcom
for count in range(0, 44):
self.send_data(self.lut_vcomDC[count])
self.send_command(0x21) # ww --
for count in range(0, 42):
self.send_data(self.lut_ww[count])
self.send_command(0x22) # bw r
for count in range(0, 42):
self.send_data(self.lut_bw[count])
self.send_command(0x23) # wb w
for count in range(0, 42):
self.send_data(self.lut_wb[count])
self.send_command(0x24) # bb b
for count in range(0, 42):
self.send_data(self.lut_bb[count])
def set_part_reg(self):
self.send_command(0x82)
self.send_data(0x03)
self.send_command(0X50)
self.send_data(0x47)
self.send_command(0x20) # vcom
for count in range(0, 44):
self.send_data(self.lut_vcom1[count])
self.send_command(0x21) # ww --
for count in range(0, 42):
self.send_data(self.lut_ww1[count])
self.send_command(0x22) # bw r
for count in range(0, 42):
self.send_data(self.lut_bw1[count])
self.send_command(0x23) # wb w
for count in range(0, 42):
self.send_data(self.lut_wb1[count])
self.send_command(0x24) # bb b
for count in range(0, 42):
self.send_data(self.lut_bb1[count])
def turn_on_display(self):
self.send_command(0x12)
self.delay_ms(10)
self.wait_until_idle()
def clear(self):
self.send_command(0x10)
for i in range(0, int(self.width * self.height / 8)):
self.send_data(0x00)
self.delay_ms(10)
self.send_command(0x13)
for i in range(0, int(self.width * self.height / 8)):
self.send_data(0xFF)
self.delay_ms(10)
self.set_full_reg()
self.turn_on_display()
def display_full(self, frame_buffer):
if not frame_buffer:
return
self.send_command(0x10)
for i in range(0, int(self.width * self.height / 8)):
self.send_data(0x00)
self.delay_ms(10)
self.send_command(0x13)
for i in range(0, int(self.width * self.height / 8)):
self.send_data(frame_buffer[i])
self.delay_ms(10)
self.set_full_reg()
self.turn_on_display()
def display_partial(self, frame_buffer, x_start, y_start, x_end, y_end):
if not frame_buffer:
return
self.set_part_reg()
self.send_command(0x91)
self.send_command(0x90)
self.send_data(x_start)
self.send_data(x_end - 1)
self.send_data(y_start / 256)
self.send_data(y_start % 256)
self.send_data(y_end / 256)
self.send_data(y_end % 256 - 1)
self.send_data(0x28)
self.send_command(0x10)
for i in range(0, int(self.width * self.height / 8)):
# print(frame_buffer[i],'%d','0x10')
self.send_data(frame_buffer[i])
self.delay_ms(10)
self.send_command(0x13)
for i in range(0, int(self.width * self.height / 8)):
# print(~frame_buffer[i],'%d','0x13')
self.send_data(~frame_buffer[i])
self.delay_ms(10)
# self.set_full_reg()
self.turn_on_display()
# after this, call epd.init() to awaken the module
def sleep(self):
self.send_command(0x50)
self.send_data(0xf7)
self.send_command(0x02) # power off
self.send_command(0x07) # deep sleep
self.send_data(0xA5)
def draw(self, x, y, image):
"""Replace a particular area on the display with an image"""
if self.partial_refresh:
self.display_partial(self.get_frame_buffer(image), x, y, x + image.width, x + image.height)
else:
self.display_full(self.get_frame_buffer(image))
class EPD2in13v4(WavesharePartial):
# Adapted from
# https://github.com/waveshareteam/e-Paper/blob/master/RaspberryPi_JetsonNano/python/lib/waveshare_epd/epd2in13_V4.py
def __init__(self):
# the actual pixel width is 122, but 128 is the 'logical' width
super().__init__(name='2.13" BW V4', width=128, height=250)
self.cached_buffer = None
self.supports_partial = True
def reset(self):
self.digital_write(self.RST_PIN, GPIO.HIGH)
self.delay_ms(20)
self.digital_write(self.RST_PIN, GPIO.LOW)
self.delay_ms(2)
self.digital_write(self.RST_PIN, GPIO.HIGH)
self.delay_ms(20)
def send_command(self, command):
self.digital_write(self.CS_PIN, GPIO.LOW)
super().send_command(command)
self.digital_write(self.CS_PIN, GPIO.HIGH)
def send_data(self, data):
self.digital_write(self.CS_PIN, GPIO.LOW)
super().send_data(data)
self.digital_write(self.CS_PIN, GPIO.HIGH)
def send_data_multi(self, data):
self.digital_write(self.CS_PIN, GPIO.LOW)
super().send_data_multi(data)
self.digital_write(self.CS_PIN, GPIO.HIGH)
def wait_until_idle(self):
while self.digital_read(self.BUSY_PIN) == 1: # 0: idle, 1: busy
self.delay_ms(10)
def turn_on_display(self):
self.send_command(self.DISPLAY_UPDATE_CONTROL_2)
self.send_data(0xf7)
self.send_command(self.MASTER_ACTIVATION)
self.wait_until_idle()
def turn_on_display_fast(self):
self.send_command(self.DISPLAY_UPDATE_CONTROL_2)
self.send_data(0xC7)
self.send_command(self.MASTER_ACTIVATION)
self.wait_until_idle()
def turn_on_display_part(self):
self.send_command(self.DISPLAY_UPDATE_CONTROL_2)
self.send_data(0xff)
self.send_command(self.MASTER_ACTIVATION)
self.wait_until_idle()
def set_memory_area(self, x_start, y_start, x_end, y_end):
self.send_command(self.SET_RAM_X_ADDRESS_START_END_POSITION)
# x point must be the multiple of 8 or the last 3 bits will be ignored
self.send_data_multi([
(x_start>>3) & 0xFF,
(x_end>>3) & 0xFF
])
self.send_command(self.SET_RAM_Y_ADDRESS_START_END_POSITION)
self.send_data_multi([
y_start & 0xFF,
(y_start >> 8) & 0xFF,
y_end & 0xFF,
(y_end >> 8) & 0xFF
])
def set_memory_pointer(self, x, y):
self.send_command(self.SET_RAM_X_ADDRESS_COUNTER)
# x point must be the multiple of 8 or the last 3 bits will be ignored
self.send_data(x & 0xFF)
self.send_command(self.SET_RAM_Y_ADDRESS_COUNTER)
self.send_data_multi([
y & 0xFF,
(y >> 8) & 0xFF
])
def init(self, partial=True, **kwargs):
self.partial_refresh = partial
if self.epd_init() != 0:
return -1
self.reset()
self.wait_until_idle()
self.send_command(self.SW_RESET)
self.wait_until_idle()
self.send_command(self.DRIVER_OUTPUT_CONTROL)
self.send_data_multi([0xf9,0x00,0x00])
self.send_command(self.DATA_ENTRY_MODE_SETTING)
self.send_data(0x03)
self.set_memory_area(0, 0, self.width-1, self.height-1)
self.set_memory_pointer(0, 0)
self.send_command(0x3c)
self.send_data(0x05)
self.send_command(self.DISPLAY_UPDATE_CONTROL_1)
self.send_data_multi([0x00,0x80])
self.send_command(0x18)
self.send_data(0x80)
self.wait_until_idle()
return 0
def init_fast(self, partial=True, **kwargs):
self.partial_refresh = partial
if self.epd_init() != 0:
return -1
self.reset()
self.send_command(self.SW_RESET)
self.wait_until_idle()
self.send_command(0x18) # Read built-in temperature sensor
# The below is send_command instead of send_data in the waveshare
# examples, but I think that was a typo.
self.send_data(0x80)
self.send_command(self.DATA_ENTRY_MODE_SETTING)
self.send_data(0x03)
self.set_memory_area(0, 0, self.width-1, self.height-1)
self.set_memory_pointer(0, 0)
self.send_command(0x22) # Load temperature value
self.send_data(0xB1)
self.send_command(0x20)
self.wait_until_idle()
self.send_command(0x1A) # Write to temperature register
self.send_data_multi([0x64,0x00])
self.send_command(0x22) # Load temperature value
self.send_data(0x91)
self.send_command(0x20)
self.wait_until_idle()
return 0
def display_full(self, frame_buffer):
self.send_command(self.WRITE_RAM)
self.send_data_multi(frame_buffer)
self.turn_on_display()
def display_fast(self, frame_buffer):
self.send_command(self.WRITE_RAM)
self.send_data_multi(frame_buffer)
self.turn_on_display_fast()
def display_partial(self, frame_buffer, x_start, y_start, x_end, y_end):
self.digital_write(self.RST_PIN, GPIO.LOW)
self.delay_ms(1)
self.digital_write(self.RST_PIN, GPIO.HIGH)
self.send_command(0x3C) # BorderWavefrom
self.send_data(0x80)
self.send_command(self.DRIVER_OUTPUT_CONTROL) # Driver output control
self.send_data_multi([0xF9,0x00,0x00])
self.send_command(0x11) # data entry mode
self.send_data(0x03)
# Currently, `draw` always sets the start values to 0, and the
# end values to the panel's full size.
# This matches the waveshare docs/examples, but I suspect that
# it's wrong.
# After all, if it's always full-screen, how is the panel supposed
# to know which part of the display has changed for partial refresh?
#
# So what's currently in place works and matches the docs, and
# attempts to do it differently all failed.
# But I'm guessing there's a better way to do this.
# So if you want to make this panel's partial refresh faster, I'm
# thinking this might be part of the equation.
self.set_memory_area(x_start, y_start, x_end - 1, y_end - 1)
self.set_memory_pointer(x_start, y_start)
self.send_command(self.WRITE_RAM)
self.send_data_multi(frame_buffer)
self.turn_on_display_part()
def displayPartBaseImage(self, frame_buffer):
# Write the "base" image to the panel, to be used for partial
# refreshes.
# This panel's partial refresh seems to work by writing a base
# image, then updating it each time you draw.
self.send_command(self.WRITE_RAM)
self.send_data_multi(frame_buffer)
self.send_command(0x26)
self.send_data_multi(frame_buffer)
self.turn_on_display()
def clear(self):
self.send_command(self.WRITE_RAM)
self.send_data_multi([0xFF] * int(self.height * self.width//8))
self.turn_on_display()
def get_frame_buffer(self, image):
# Convert the image to a byte array.
# This function assumes the image is black and white.
# ie. Image mode '1'
# If you try to use it with a grayscale image, it may not work.
return bytearray(image.tobytes('raw'))
def draw(self, x, y, image):
"""Replace a particular area on the display with an image"""
# Partial refresh works a bit differently for this panel.
# In spite of being a partial refresh, the image still needs
# to be a full-screen image, and we still need to send all of
# the bytes across to the panel.
# So in terms of data transmission, it's no faster than a full refresh.
#
# However, the refresh itself is much faster, and it relies on
# the data written to 0x26 via displayPartBaseImage in order to
# work properly.
#
# I'm sure there's some way to improve this, but after much
# experimenting this is the best I've got right now.
# First off, check if we have already written an image before.
# If not, build a buffer (self.cached_buffer) in memory.
# We do this for 2 reasons.
#
# First, because this panel requires a full-screen image each time.
# PaperTTY will send images which aren't the exact dimensions
# of this screen, even if partial is turned off, due to banding.
# So building a full-screen image here for the initial buffer works
# around that, and also means the code works with partial refreshes.
#
# Second, for speed.
# By reusing the buffer from the previous frame we can just overwrite
# the parts which have changed.
# eg. If the buffer is 128x250, and we get a new draw for an image which
# is 64x64, then we can reuse most of the already processed image and only
# replace that small bit which changed.
if self.cached_buffer is None:
self.cached_buffer = [0xFF] * int(self.height * self.width//8)
# If partial refresh is enabled, write the initial image to the
# appropriate register.
# This is optional for full refreshes, but it is required for
# partial refresh.
if self.partial_refresh:
self.displayPartBaseImage(self.cached_buffer)
# Now build the new buffer.
# We do this by converting the image to a byte array, then replacing
# the bytes in the cache with the bytes from the new array in the
# appropriate positions.
# This isn't as "pretty" as pasting the image over the old one and
# converting the whole thing, but it is more efficient.
new_buffer = self.get_frame_buffer(image)
width = image.width
width_bytes = width // 8
panel_width_bytes = self.width // 8
height = image.height
for h in range(0, height):
src_start = h * width_bytes
src_end = src_start + width_bytes
dst_start = (y+h) * panel_width_bytes + (x // 8)
dst_end = dst_start + width_bytes
self.cached_buffer[dst_start:dst_end] = new_buffer[src_start:src_end]
# Finally, draw the image.
if self.partial_refresh:
self.display_partial(self.cached_buffer, 0, 0, self.width, self.height)
else:
self.display_full(self.cached_buffer)