forked from SolderedElectronics/Inkplate-micropython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inkplate10.py
1081 lines (935 loc) · 35.2 KB
/
inkplate10.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
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
# Copyright © 2020 by Thorsten von Eicken.
import time
import micropython
import framebuf
import os
from machine import ADC, I2C, Pin, SDCard
from uarray import array
from mcp23017 import MCP23017
from micropython import const
from shapes import Shapes
from gfx import GFX
from gfx_standard_font_01 import text_dict as std_font
# Raw display constants for Inkplate 10
D_ROWS = const(825)
D_COLS = const(1200)
# Waveforms for 2 bits per pixel grey-scale.
# Order of 4 values in each tuple: blk, dk-grey, light-grey, white
# Meaning of values: 0=dischg, 1=black, 2=white, 3=skip
# Uses "colors" 0 (black), 3, 5, and 7 (white) from 3-bit waveforms below
WAVE_2B = ( # original mpy driver for Ink 6, differs from arduino driver below
(1, 1, 2, 0),
(1, 1, 1, 0),
(0, 2, 1, 0),
(1, 2, 1, 0),
(0, 0, 2, 2),
(1, 1, 0, 0),
(0, 0, 0, 0)
)
# Ink10 WAVEFORM3BIT from arduino driver
# {{0,0,0,0,0,0,1,0},{0,0,2,2,2,1,1,0},{0,2,1,1,2,2,1,0},{1,2,2,1,2,2,1,0},
# {0,2,1,2,2,2,1,0},{2,2,2,2,2,2,1,0},{0,0,0,0,2,1,2,0},{0,0,2,2,2,2,2,0}};
TPS65186_addr = const(0x48) # I2C address
# ESP32 GPIO set and clear registers to twiddle 32 gpio bits at once
# from esp-idf:
# define DR_REG_GPIO_BASE 0x3ff44000
# define GPIO_OUT_W1TS_REG (DR_REG_GPIO_BASE + 0x0008)
# define GPIO_OUT_W1TC_REG (DR_REG_GPIO_BASE + 0x000c)
ESP32_GPIO = const(0x3FF44000) # ESP32 GPIO base
# register offsets from ESP32_GPIO
W1TS0 = const(2) # offset for "write one to set" register for gpios 0..31
W1TC0 = const(3) # offset for "write one to clear" register for gpios 0..31
W1TS1 = const(5) # offset for "write one to set" register for gpios 32..39
W1TC1 = const(6) # offset for "write one to clear" register for gpios 32..39
# bit masks in W1TS/W1TC registers
EPD_DATA = const(0x0E8C0030) # EPD_D0..EPD_D7
EPD_CL = const(0x00000001) # in W1Tx0
EPD_LE = const(0x00000004) # in W1Tx0
EPD_CKV = const(0x00000001) # in W1Tx1
EPD_SPH = const(0x00000002) # in W1Tx1
# Inkplate provides access to the pins of the Inkplate 6 as well as to low-level display
# functions.
class _Inkplate:
@classmethod
def init(cls, i2c):
cls._i2c = i2c
cls._mcp23017 = MCP23017(i2c)
# Display control lines
cls.EPD_CL = Pin(0, Pin.OUT, value=0)
cls.EPD_LE = Pin(2, Pin.OUT, value=0)
cls.EPD_CKV = Pin(32, Pin.OUT, value=0)
cls.EPD_SPH = Pin(33, Pin.OUT, value=1)
cls.EPD_OE = cls._mcp23017.pin(0, Pin.OUT, value=0)
cls.EPD_GMODE = cls._mcp23017.pin(1, Pin.OUT, value=0)
cls.EPD_SPV = cls._mcp23017.pin(2, Pin.OUT, value=1)
# Display data lines - we only use the Pin class to init the pins
Pin(4, Pin.OUT)
Pin(5, Pin.OUT)
Pin(18, Pin.OUT)
Pin(19, Pin.OUT)
Pin(23, Pin.OUT)
Pin(25, Pin.OUT)
Pin(26, Pin.OUT)
Pin(27, Pin.OUT)
# TPS65186 power regulator control
cls.TPS_WAKEUP = cls._mcp23017.pin(3, Pin.OUT, value=0)
cls.TPS_PWRUP = cls._mcp23017.pin(4, Pin.OUT, value=0)
cls.TPS_VCOM = cls._mcp23017.pin(5, Pin.OUT, value=0)
cls.TPS_INT = cls._mcp23017.pin(6, Pin.IN)
cls.TPS_PWR_GOOD = cls._mcp23017.pin(7, Pin.IN)
# Misc
cls.GPIO0_PUP = cls._mcp23017.pin(8, Pin.OUT, value=0)
cls.VBAT_EN = cls._mcp23017.pin(9, Pin.OUT, value=1)
cls.VBAT = ADC(Pin(35))
cls.VBAT.atten(ADC.ATTN_11DB)
cls.VBAT.width(ADC.WIDTH_12BIT)
# Touch sensors
cls.TOUCH1 = cls._mcp23017.pin(10, Pin.IN)
cls.TOUCH2 = cls._mcp23017.pin(11, Pin.IN)
cls.TOUCH3 = cls._mcp23017.pin(12, Pin.IN)
cls._on = False # whether panel is powered on or not
if len(_Inkplate.byte2gpio) == 0:
_Inkplate.gen_byte2gpio()
@classmethod
def begin(self):
_Inkplate.init(I2C(0, scl=Pin(22), sda=Pin(21)))
self.ipg = InkplateGS2()
self.ipm = InkplateMono()
self.ipp = InkplatePartial(self.ipm)
self.GFX = GFX(
D_COLS,
D_ROWS,
self.writePixel,
self.writeFastHLine,
self.writeFastVLine,
self.writeFillRect,
None,
None,
)
@classmethod
def clearDisplay(self):
self.ipg.clear()
self.ipm.clear()
@classmethod
def display(self):
if self.displayMode == 0:
self.ipm.display()
elif self.displayMode == 1:
self.ipg.display()
@classmethod
def partialUpdate(self):
if self.displayMode == 1:
return
self.ipp.display()
# Read the battery voltage. Note that the result depends on the ADC calibration, and be a bit off.
@classmethod
def read_battery(cls):
cls.VBAT_EN.value(0)
# Probably don't need to delay since Micropython is slow, but we do it anyway
time.sleep_ms(1)
value = cls.VBAT.read()
cls.VBAT_EN.value(1)
result = (value / 4095.0) * 1.1 * 3.548133892 * 2
return result
# Read panel temperature. I varies +- 2 degree
@classmethod
def read_temperature(cls):
# start temperature measurement and wait 5 ms
cls._i2c.writeto_mem(TPS65186_addr, 0x0D, bytes((0x80,)))
time.sleep_ms(5)
# request temperature data from panel
cls._i2c.writeto(TPS65186_addr, bytearray((0x00,)))
cls._temperature = cls._i2c.readfrom(TPS65186_addr, 1)
# convert data from bytes to integer
cls.temperatureInt = int.from_bytes(cls._temperature, "big", True)
return cls.temperatureInt
# _tps65186_write writes an 8-bit value to a register
@classmethod
def _tps65186_write(cls, reg, v):
cls._i2c.writeto_mem(TPS65186_addr, reg, bytes((v,)))
# _tps65186_read reads an 8-bit value from a register
@classmethod
def _tps65186_read(cls, reg):
cls._i2c.readfrom_mem(TPS65186_addr, reg, 1)[0]
# power_on turns the voltage regulator on and wakes up the display (GMODE and OE)
@classmethod
def power_on(cls):
if cls._on:
return
cls._on = True
# turn on power regulator
cls.TPS_WAKEUP(1)
cls.TPS_PWRUP(1)
cls.TPS_VCOM(1)
# enable all rails
cls._tps65186_write(0x01, 0x3F) # ???
time.sleep_ms(40)
cls._tps65186_write(0x0D, 0x80) # ???
time.sleep_ms(2)
cls._temperature = cls._tps65186_read(1)
# wake-up display
cls.EPD_GMODE(1)
cls.EPD_OE(1)
# power_off puts the display to sleep and cuts the power
# TODO: also tri-state gpio pins to avoid current leakage during deep-sleep
@classmethod
def power_off(cls):
if not cls._on:
return
cls._on = False
# put display to sleep
cls.EPD_GMODE(0)
cls.EPD_OE(0)
# turn off power regulator
cls.TPS_PWRUP(0)
cls.TPS_WAKEUP(0)
cls.TPS_VCOM(0)
# ===== Methods that are independent of pixel bit depth
# vscan_start begins a vertical scan by toggling CKV and SPV
# sleep_us calls are commented out 'cause MP is slow enough...
@classmethod
def vscan_start(cls):
def ckv_pulse():
cls.EPD_CKV(0)
cls.EPD_CKV(1)
# start a vertical scan pulse
cls.EPD_CKV(1) # time.sleep_us(7)
cls.EPD_SPV(0) # time.sleep_us(10)
ckv_pulse() # time.sleep_us(8)
cls.EPD_SPV(1) # time.sleep_us(10)
# pulse through 3 scan lines that end up being invisible
ckv_pulse() # time.sleep_us(18)
ckv_pulse() # time.sleep_us(18)
ckv_pulse()
# vscan_write latches the row into the display pixels and moves to the next row
@micropython.viper
@staticmethod
def vscan_write():
w1ts0 = ptr32(int(ESP32_GPIO + 4 * W1TS0))
w1tc0 = ptr32(int(ESP32_GPIO + 4 * W1TC0))
w1tc0[W1TC1 - W1TC0] = EPD_CKV # remove gate drive
w1ts0[0] = EPD_LE # pulse to latch row --
w1ts0[0] = EPD_LE # delay a tiny bit
w1tc0[0] = EPD_LE
w1tc0[0] = EPD_LE # delay a tiny bit
w1ts0[W1TS1 - W1TS0] = EPD_CKV # apply gate drive to next row
# byte2gpio converts a byte of data for the screen to 32 bits of gpio0..31
# (oh, e-radionica, why didn't you group the gpios better?!)
byte2gpio = []
@classmethod
def gen_byte2gpio(cls):
cls.byte2gpio = array("L", bytes(4 * 256))
for b in range(256):
cls.byte2gpio[b] = (
(b & 0x3) << 4 | (b & 0xC) << 16 | (
b & 0x10) << 19 | (b & 0xE0) << 20
)
# sanity check that all EPD_DATA bits got set at some point and no more
union = 0
for i in range(256):
union |= cls.byte2gpio[i]
assert union == EPD_DATA
# fill_screen writes the same value to all bytes of the screen, it is used for cleaning
@micropython.viper
@staticmethod
def fill_screen(data: int):
w1ts0 = ptr32(int(ESP32_GPIO + 4 * W1TS0))
w1tc0 = ptr32(int(ESP32_GPIO + 4 * W1TC0))
# set the data output gpios
w1tc0[0] = EPD_DATA | EPD_CL
w1ts0[0] = data
vscan_write = _Inkplate.vscan_write
epd_cl = EPD_CL
# send all rows
for r in range(D_ROWS):
# send first byte of row with start-row signal
w1tc0[W1TC1 - W1TC0] = EPD_SPH
w1ts0[0] = epd_cl
w1tc0[0] = epd_cl
w1ts0[W1TS1 - W1TS0] = EPD_SPH
# send remaining bytes (we overshoot by one, which is OK)
i = int(D_COLS >> 3)
while i > 0:
w1ts0[0] = epd_cl
w1tc0[0] = epd_cl
w1ts0[0] = epd_cl
w1tc0[0] = epd_cl
i -= 1
# latch row and increment to next
# inlined vscan_write()
w1tc0[W1TC1 - W1TC0] = EPD_CKV # remove gate drive
w1ts0[0] = EPD_LE # pulse to latch row --
w1ts0[0] = EPD_LE # delay a tiny bit
w1tc0[0] = EPD_LE
w1tc0[0] = EPD_LE # delay a tiny bit
w1ts0[W1TS1 - W1TS0] = EPD_CKV # apply gate drive to next row
# clean fills the screen with one of the four possible pixel patterns
@classmethod
def clean(cls, patt, rep):
c = [0xAA, 0x55, 0x00, 0xFF][patt]
data = _Inkplate.byte2gpio[c] & ~EPD_CL
for i in range(rep):
cls.vscan_start()
cls.fill_screen(data)
class InkplateMono(framebuf.FrameBuffer):
def __init__(self):
self._framebuf = bytearray(D_ROWS * D_COLS // 8)
super().__init__(self._framebuf, D_COLS, D_ROWS, framebuf.MONO_HMSB)
ip = InkplateMono
ip._gen_luts()
ip._wave = [ip.lut_blk, ip.lut_blk, ip.lut_blk,
ip.lut_blk, ip.lut_blk, ip.lut_bw]
# gen_luts generates the look-up tables to convert a nibble (4 bits) of pixels to the
# 32-bits that need to be pushed into the gpio port.
# The LUTs used here were copied from the e-Radionica Inkplate-6-Arduino-library.
@classmethod
def _gen_luts(cls):
# is there a better way to init an array with 16 words???
b16 = bytes(4 * 16)
# bits to ship to gpio to make pixels white
cls.lut_wht = array("L", b16)
# bits to ship to gpio to make pixels black
cls.lut_blk = array("L", b16)
# bits to ship to gpio to make pixels black and white
cls.lut_bw = array("L", b16)
for i in range(16):
wht = 0
blk = 0
bw = 0
# display uses 2 bits per pixel: 00=discharge, 01=black, 10=white, 11=skip
for bit in range(4):
wht = wht | ((2 if (i >> bit) & 1 == 0 else 3) << (2 * bit))
blk = blk | ((1 if (i >> bit) & 1 == 1 else 3) << (2 * bit))
bw = bw | ((1 if (i >> bit) & 1 == 1 else 2) << (2 * bit))
cls.lut_wht[i] = _Inkplate.byte2gpio[wht] | EPD_CL
cls.lut_blk[i] = _Inkplate.byte2gpio[blk] | EPD_CL
cls.lut_bw[i] = _Inkplate.byte2gpio[bw] | EPD_CL
# print("Black: %08x, White:%08x Data:%08x" % (cls.lut_bw[0xF], cls.lut_bw[0], EPD_DATA))
# _send_row writes a row of data to the display
@micropython.viper
@staticmethod
def _send_row(lut_in, framebuf, row: int):
ROW_LEN = D_COLS >> 3 # length of row in bytes
# cache vars into locals
w1ts0 = ptr32(int(ESP32_GPIO + 4 * W1TS0))
w1tc0 = ptr32(int(ESP32_GPIO + 4 * W1TC0))
off = int(EPD_DATA | EPD_CL) # mask with all data bits and clock bit
fb = ptr8(framebuf)
ix = int(row * ROW_LEN + ROW_LEN - 1) # index into framebuffer
lut = ptr32(lut_in)
# send first byte
data = int(fb[ix])
ix -= 1
w1tc0[0] = off
w1tc0[W1TC1 - W1TC0] = EPD_SPH
w1ts0[0] = lut[data >> 4] # set data bits and assert clock
# w1tc0[0] = EPD_CL # clear clock, leaving data bits (unreliable if data also cleared)
w1tc0[0] = off # clear data bits as well ready for next byte
w1ts0[W1TS1 - W1TS0] = EPD_SPH
w1ts0[0] = lut[data & 0xF]
# w1tc0[0] = EPD_CL
w1tc0[0] = off
# send the remaining bytes
for c in range(ROW_LEN - 1):
data = int(fb[ix])
ix -= 1
w1ts0[0] = lut[data >> 4]
# w1tc0[0] = EPD_CL
w1tc0[0] = off
w1ts0[0] = lut[data & 0xF]
# w1tc0[0] = EPD_CL
w1tc0[0] = off
# display_mono sends the monochrome buffer to the display, clearing it first
def display(self):
ip = _Inkplate
ip.power_on()
# clean the display
t0 = time.ticks_ms()
ip.clean(0, 1)
ip.clean(1, 12)
ip.clean(2, 1)
ip.clean(0, 11)
ip.clean(2, 1)
ip.clean(1, 12)
ip.clean(2, 1)
ip.clean(0, 11)
# the display gets written N times
t1 = time.ticks_ms()
n = 0
send_row = InkplateMono._send_row
vscan_write = ip.vscan_write
fb = self._framebuf
for lut in self._wave:
ip.vscan_start()
# write all rows
r = D_ROWS - 1
while r >= 0:
send_row(lut, fb, r)
vscan_write()
r -= 1
n += 1
t2 = time.ticks_ms()
tc = time.ticks_diff(t1, t0)
td = time.ticks_diff(t2, t1)
tt = time.ticks_diff(t2, t0)
print(
"Mono: clean %dms (%dms ea), draw %dms (%dms ea), total %dms"
% (tc, tc // (4 + 22 + 24), td, td // len(self._wave), tt)
)
ip.clean(2, 2)
ip.clean(3, 1)
ip.power_off()
# @micropython.viper
def clear(self):
self.fill(0)
# fb = ptr8(self._framebuf)
# for ix in range(D_ROWS * D_COLS // 8):
# fb[ix] = 0
Shapes.__mix_me_in(InkplateMono)
# Inkplate display with 2 bits of gray scale (4 levels)
class InkplateGS2(framebuf.FrameBuffer):
_wave = None
def __init__(self):
self._framebuf = bytearray(D_ROWS * D_COLS // 4)
super().__init__(self._framebuf, D_COLS, D_ROWS, framebuf.GS2_HMSB)
if not InkplateGS2._wave:
InkplateGS2._gen_wave()
# _gen_wave generates the waveform table. The table consists of N phases or steps during
# each of which the entire display gets written. The array in each phase gets indexed with
# a nibble of data and contains the 32-bits that need to be pushed into the gpio port.
# The waveform used here was adapted from the e-Radionica Inkplate-6-Arduino-library
# by taking colors 0 (black), 3, 5, and 7 (white) from "waveform3Bit[8][7]".
@classmethod
def _gen_wave(cls):
# genlut generates the lookup table that maps a nibble (2 pixels, 4 bits) to a 32-bit
# word to push into the GPIO port
def genlut(op):
return bytes([op[j] | op[i] << 2 for i in range(4) for j in range(4)])
cls._wave = [genlut(w) for w in WAVE_2B]
# _send_row writes a row of data to the display
@micropython.viper
@staticmethod
def _send_row(lut_in, framebuf, row: int):
ROW_LEN = D_COLS >> 2 # length of row in bytes
# cache vars into locals
w1ts0 = ptr32(int(ESP32_GPIO + 4 * W1TS0))
w1tc0 = ptr32(int(ESP32_GPIO + 4 * W1TC0))
off = int(EPD_DATA | EPD_CL) # mask with all data bits and clock bit
fb = ptr8(framebuf)
ix = int(row * ROW_LEN + (ROW_LEN - 1)) # index into framebuffer
lut = ptr8(lut_in)
b2g = ptr32(_Inkplate.byte2gpio)
# send first byte
data = int(fb[ix])
ix -= 1
w1tc0[0] = off
w1tc0[W1TC1 - W1TC0] = EPD_SPH
w1ts0[0] = b2g[lut[data >> 4] << 4 | lut[data & 0xF]
] | EPD_CL # set data bits and clock
# w1tc0[0] = EPD_CL # clear clock, leaving data bits (unreliable if data also cleared)
w1tc0[0] = off # clear data bits as well ready for next byte
w1ts0[W1TS1 - W1TS0] = EPD_SPH
# send the remaining bytes
for c in range(ROW_LEN - 1):
data = int(fb[ix])
ix -= 1
w1ts0[0] = b2g[lut[data >> 4] << 4 | lut[data & 0xF]] | EPD_CL
# w1tc0[0] = EPD_CL
w1tc0[0] = off
# display_mono sends the monochrome buffer to the display, clearing it first
def display(self):
ip = _Inkplate
ip.power_on()
# clean the display
t0 = time.ticks_ms()
ip.clean(0, 1)
ip.clean(1, 12)
ip.clean(2, 1)
ip.clean(0, 11)
ip.clean(2, 1)
ip.clean(1, 12)
ip.clean(2, 1)
ip.clean(0, 11)
# the display gets written N times
t1 = time.ticks_ms()
n = 0
send_row = InkplateGS2._send_row
vscan_write = ip.vscan_write
fb = self._framebuf
for lut in InkplateGS2._wave:
ip.vscan_start()
# write all rows
r = D_ROWS - 1
while r >= 0:
send_row(lut, fb, r)
vscan_write()
r -= 1
n += 1
t2 = time.ticks_ms()
tc = time.ticks_diff(t1, t0)
td = time.ticks_diff(t2, t1)
tt = time.ticks_diff(t2, t0)
print(
"GS2: clean %dms (%dms ea), draw %dms (%dms ea), total %dms"
% (tc, tc // (4 + 22 + 24), td, td // len(InkplateGS2._wave), tt)
)
ip.clean(2, 1) # ??
ip.clean(3, 1)
ip.power_off()
# @micropython.viper
def clear(self):
self.fill(3)
# fb = ptr8(self._framebuf)
# for ix in range(int(len(self._framebuf))):
# fb[ix] = 0xFF
Shapes.__mix_me_in(InkplateGS2)
# InkplatePartial managed partial updates. It starts by making a copy of the current framebuffer
# and then when asked to draw it renders the differences between the copy and the new framebuffer
# state. The constructor needs a reference to the current/main display object (InkplateMono).
# Only InkplateMono is supported at the moment.
class InkplatePartial:
def __init__(self, base):
self._base = base
self._framebuf = bytearray(len(base._framebuf))
InkplatePartial._gen_lut_mono()
# start makes a reference copy of the current framebuffer
def start(self):
self._framebuf[:] = self._base._framebuf[:]
# display the changes between our reference copy and the current framebuffer contents
def display(self, x=0, y=0, w=D_COLS, h=D_ROWS):
ip = _Inkplate
ip.power_on()
# the display gets written a couple of times
t0 = time.ticks_ms()
n = 0
send_row = InkplatePartial._send_row
skip_rows = InkplatePartial._skip_rows
vscan_write = ip.vscan_write
nfb = self._base._framebuf # new framebuffer
ofb = self._framebuf # old framebuffer
lut = InkplatePartial._lut_mono
h -= 1
for _ in range(5):
ip.vscan_start()
r = D_ROWS - 1
# skip rows that supposedly have no change
if r > y + h:
skip_rows(r - (y + h))
r = y + h
# write changed rows
while r >= y:
send_row(lut, ofb, nfb, r)
vscan_write()
r -= 1
# skip remaining rows (doesn't seem to be necessary for Inkplate 6 but it is for 10)
if r > 0:
skip_rows(r)
n += 1
t1 = time.ticks_ms()
td = time.ticks_diff(t1, t0)
print(
"Partial: draw %dms (%dms/frame %dus/row) (y=%d..%d)"
% (td, td // n, td * 1000 // n // (D_ROWS - y), y, y + h + 1)
)
ip.clean(2, 2)
ip.clean(3, 1)
ip.power_off()
# gen_lut_mono generates a look-up tables to change the display from a nibble of old
# pixels (4 bits = 4 pixels) to a nibble of new pixels. The LUT contains the
# 32-bits that need to be pushed into the gpio port to effect the change.
@classmethod
def _gen_lut_mono(cls):
lut = cls._lut_mono = array("L", bytes(4 * 256))
for o in range(16): # iterate through all old-pixels combos
for n in range(16): # iterate through all new-pixels combos
bw = 0
for bit in range(4):
# value to send to display: turns out that if we juxtapose the old and new
# bits we get the right value except for the 00 combination...
val = (((o >> bit) << 1) & 2) | ((n >> bit) & 1)
if val == 0:
val = 3
bw = bw | (val << (2 * bit))
lut[o * 16 + n] = _Inkplate.byte2gpio[bw] | EPD_CL
# print("Black: %08x, White:%08x Data:%08x" % (cls.lut_bw[0xF], cls.lut_bw[0], EPD_DATA))
# _skip_rows skips N rows
@micropython.viper
@staticmethod
def _skip_rows(rows: int):
if rows <= 0:
return
# cache vars into locals
w1ts0 = ptr32(int(ESP32_GPIO + 4 * W1TS0))
w1tc0 = ptr32(int(ESP32_GPIO + 4 * W1TC0))
# need to fill the column latches with "no-change" values (all ones)
epd_cl = EPD_CL
w1tc0[0] = epd_cl
w1ts0[0] = EPD_DATA
# send first byte of row with start-row signal
w1tc0[W1TC1 - W1TC0] = EPD_SPH
w1ts0[0] = epd_cl
w1tc0[0] = epd_cl
w1ts0[W1TS1 - W1TS0] = EPD_SPH
# send remaining bytes
i = int(D_COLS >> 3)
while i > 0:
w1ts0[0] = epd_cl
w1tc0[0] = epd_cl
w1ts0[0] = epd_cl
w1tc0[0] = epd_cl
i -= 1
# write the same row over and over, weird thing is that we need the sleep otherwise
# the rows we subsequently draw don't draw proper whites leaving ghosts behind - hard to
# understand why the speed at which we "skip" rows affects rows that are drawn later...
while rows > 0:
_Inkplate.vscan_write()
rows -= 1
time.sleep_us(50)
# _send_row writes a row of data to the display
@micropython.viper
@staticmethod
def _send_row(lut_in, old_framebuf, new_framebuf, row: int):
ROW_LEN = D_COLS >> 3 # length of row in bytes
# cache vars into locals
w1ts0 = ptr32(int(ESP32_GPIO + 4 * W1TS0))
w1tc0 = ptr32(int(ESP32_GPIO + 4 * W1TC0))
off = int(EPD_DATA | EPD_CL) # mask with all data bits and clock bit
ofb = ptr8(old_framebuf)
nfb = ptr8(new_framebuf)
ix = int(row * ROW_LEN + (ROW_LEN - 1)) # index into framebuffer
lut = ptr32(lut_in)
# send first byte
odata = int(ofb[ix])
ndata = int(nfb[ix])
ix -= 1
w1tc0[0] = off
w1tc0[W1TC1 - W1TC0] = EPD_SPH
if odata == ndata:
w1ts0[0] = off # send all-ones: no change to any of the pixels
w1tc0[0] = EPD_CL
w1ts0[W1TS1 - W1TS0] = EPD_SPH
w1ts0[0] = EPD_CL
w1tc0[0] = off
else:
w1ts0[0] = lut[(odata & 0xF0) + (ndata >> 4)]
w1tc0[0] = off # clear data bits as well ready for next byte
w1ts0[W1TS1 - W1TS0] = EPD_SPH
w1ts0[0] = lut[((odata & 0xF) << 4) + (ndata & 0xF)]
w1tc0[0] = off
# send the remaining bytes
for c in range(ROW_LEN - 1):
odata = int(ofb[ix])
ndata = int(nfb[ix])
ix -= 1
if odata == ndata:
w1ts0[0] = off # send all-ones: no change to any of the pixels
w1tc0[0] = EPD_CL
w1ts0[0] = EPD_CL
w1tc0[0] = off
else:
w1ts0[0] = lut[(odata & 0xF0) + ((ndata >> 4) & 0xF)]
w1tc0[0] = off
w1ts0[0] = lut[((odata & 0xF) << 4) + (ndata & 0xF)]
w1tc0[0] = off
# Inkplate wraper to make it more easy for use
class Inkplate:
INKPLATE_1BIT = 0
INKPLATE_2BIT = 1
BLACK = 1
WHITE = 0
_width = D_COLS
_height = D_ROWS
rotation = 0
displayMode = 0
textSize = 1
def __init__(self, mode):
self.displayMode = mode
try:
os.mount(
SDCard(
slot=3,
miso=Pin(12),
mosi=Pin(13),
sck=Pin(14),
cs=Pin(15)),
"/sd"
)
except:
print("Sd card could not be read")
def begin(self):
_Inkplate.init(I2C(0, scl=Pin(22), sda=Pin(21)))
self.ipg = InkplateGS2()
self.ipm = InkplateMono()
self.ipp = InkplatePartial(self.ipm)
self.TOUCH1 = _Inkplate.TOUCH1
self.TOUCH2 = _Inkplate.TOUCH2
self.TOUCH3 = _Inkplate.TOUCH3
self.GFX = GFX(
D_COLS,
D_ROWS,
self.writePixel,
self.writeFastHLine,
self.writeFastVLine,
self.writeFillRect,
None,
None,
)
def clearDisplay(self):
self.ipm.clear()
self.ipg.clear()
def display(self):
if self.displayMode == 0:
self.ipm.display()
elif self.displayMode == 1:
self.ipg.display()
self.ipp.start() # making framebuffer copy for partial update
def partialUpdate(self):
if self.displayMode == self.INKPLATE_2BIT:
return
self.ipp.display()
self.ipp.start() # making framebuffer copy for partial update
def clean(self):
self.einkOn()
_Inkplate.clean(0, 1)
_Inkplate.clean(1, 12)
_Inkplate.clean(2, 1)
_Inkplate.clean(0, 11)
_Inkplate.clean(2, 1)
_Inkplate.clean(1, 12)
_Inkplate.clean(2, 1)
_Inkplate.clean(0, 11)
self.einkOff()
def einkOn(self):
_Inkplate.power_on()
def einkOff(self):
_Inkplate.power_off()
def readBattery(self):
return _Inkplate.read_battery()
def readTemperature(self):
return _Inkplate.read_temperature()
def width(self):
return self._width
def height(self):
return self._height
# Arduino compatibility functions
def setRotation(self, x):
self.rotation = x % 4
if self.rotation == 0 or self.rotation == 2:
self._width = D_COLS
self._height = D_ROWS
elif self.rotation == 1 or self.rotation == 3:
self._width = D_ROWS
self._height = D_COLS
def getRotation(self):
return self.rotation
def drawPixel(self, x, y, c):
self.startWrite()
self.writePixel(x, y, c)
self.endWrite()
def startWrite(self):
pass
def _rotateCoordinates(self, x, y):
if self.rotation == 1:
x, y = y, x
x = self.height() - x - 1
elif self.rotation == 2:
x = self.width() - x - 1
y = self.height() - y - 1
elif self.rotation == 3:
x, y = y, x
y = self.width() - y - 1
return x, y
def writePixel(self, x, y, c):
if x > self.width() - 1 or y > self.height() - 1 or x < 0 or y < 0:
return
if self.rotation == 1:
x, y = y, x
x = self.height() - x - 1
elif self.rotation == 2:
x = self.width() - x - 1
y = self.height() - y - 1
elif self.rotation == 3:
x, y = y, x
y = self.width() - y - 1
(self.ipm.pixel if self.displayMode == self.INKPLATE_1BIT else self.ipg.pixel)(
x, y, c
)
def writeFillRect(self, x, y, w, h, c):
x, y = self._rotateCoordinates(x, y)
if self.rotation in (1, 3):
w, h = h, w
if self.rotation in (1, 2):
x -= w - 1
if self.rotation in (2, 3):
y -= h - 1
(self.ipm.fill_rect if self.displayMode == self.INKPLATE_1BIT else self.ipg.fill_rect)(
x, y, w, h, c
)
def writeFastVLine(self, x, y, h, c):
if self.rotation in (1, 3):
self._writeFastHLine(x, y, h, c)
return
self._writeFastVLine(x, y, h, c)
def _writeFastVLine(self, x, y, h, c):
x, y = self._rotateCoordinates(x, y)
if self.rotation in (2, 3):
y -= h - 1
(self.ipm.vline if self.displayMode == self.INKPLATE_1BIT else self.ipg.vline)(
x, y, h, c
)
def writeFastHLine(self, x, y, w, c):
if self.rotation in (1, 3):
self._writeFastVLine(x, y, w, c)
return
self._writeFastHLine(x, y, w, c)
def _writeFastHLine(self, x, y, w, c):
x, y = self._rotateCoordinates(x, y)
if self.rotation in (1, 2):
x -= w - 1
(self.ipm.hline if self.displayMode == self.INKPLATE_1BIT else self.ipg.hline)(
x, y, w, c
)
def writeLine(self, x0, y0, x1, y1, c):
self.GFX.line(x0, y0, x1, y1, c)
def endWrite(self):
pass
def drawFastVLine(self, x, y, h, c):
self.startWrite()
self.writeFastVLine(x, y, h, c)
self.endWrite()
def drawFastHLine(self, x, y, w, c):
self.startWrite()
self.writeFastHLine(x, y, w, c)
self.endWrite()
def fillRect(self, x, y, w, h, c):
self.startWrite()
self.writeFillRect(x, y, w, h, c)
self.endWrite()
def fillScreen(self, c):
self.fillRect(0, 0, self.width(), self.height(), c)
def drawLine(self, x0, y0, x1, y1, c):
self.startWrite()
self.writeLine(x0, y0, x1, y1, c)
self.endWrite()
def drawRect(self, x, y, w, h, c):
self.GFX.rect(x, y, w, h, c)
def drawCircle(self, x, y, r, c):
self.GFX.circle(x, y, r, c)
def fillCircle(self, x, y, r, c):
self.GFX.fill_circle(x, y, r, c)
def drawTriangle(self, x0, y0, x1, y1, x2, y2, c):
self.GFX.triangle(x0, y0, x1, y1, x2, y2, c)
def fillTriangle(self, x0, y0, x1, y1, x2, y2, c):
self.GFX.fill_triangle(x0, y0, x1, y1, x2, y2, c)
def drawRoundRect(self, x, y, q, h, r, c):
self.GFX.round_rect(x, y, q, h, r, c)
def fillRoundRect(self, x, y, q, h, r, c):
self.GFX.fill_round_rect(x, y, q, h, r, c)
def setDisplayMode(self, mode):
self.displayMode = mode
def selectDisplayMode(self, mode):
self.displayMode = mode
def getDisplayMode(self):
return self.displayMode
def setTextSize(self, s):
self.textSize = s
def setFont(self, f):
self.GFX.font = f
def printText(self, x, y, s):
self.GFX._very_slow_text(x, y, s, self.textSize, 1)
def drawBitmap(self, x, y, data, w, h):
byteWidth = (w + 7) // 8
byte = 0
self.startWrite()
for j in range(h):
for i in range(w):
if i & 7:
byte <<= 1
else:
byte = data[j * byteWidth + i // 8]
if byte & 0x80:
self.writePixel(x + i, y + j, 1)
self.endWrite()
def drawImageFile(self, x, y, path, invert=False):
with open(path, "rb") as f:
header14 = f.read(14)
if header14[0] != 0x42 or header14[1] != 0x4D:
return 0
header40 = f.read(40)
w = int(
(header40[7] << 24)
+ (header40[6] << 16)
+ (header40[5] << 8)
+ header40[4]
)
h = int(
(header40[11] << 24)
+ (header40[10] << 16)
+ (header40[9] << 8)