Skip to content

Commit 284c65b

Browse files
committed
DM: fix for new screen and latest cirpy
1 parent 445767e commit 284c65b

File tree

10 files changed

+292
-350
lines changed

10 files changed

+292
-350
lines changed

Adafruit_EPD/__init__.py

Whitespace-only changes.

Adafruit_EPD.py renamed to Adafruit_EPD/epd.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import time
2-
from Adafruit_MCP_SRAM import *
2+
from Adafruit_EPD import mcp_sram
33
import digitalio
44
import busio
55
from board import *
66

7-
from adafruit_bus_device.spi_device import SPIDevice
8-
97
class Adafruit_EPD(object):
108
"""Base class for EPD displays
119
"""
10+
BLACK = 0
11+
WHITE = 1
12+
INVERSE = 2
13+
RED = 3
14+
DARK = 4
15+
LIGHT = 5
1216

13-
def __init__(self, width, height, rst, dc, busy, srcs=None, cs=None,
14-
spi=None):
17+
def __init__(self, width, height, rst, dc, busy, srcs, cs,
18+
spi):
1519
self.width = width
1620
self.height = height
1721

@@ -33,7 +37,7 @@ def __init__(self, width, height, rst, dc, busy, srcs=None, cs=None,
3337

3438
self.spi_device = spi
3539

36-
self.sram = Adafruit_MCP_SRAM(cs=srcs, spi=spi)
40+
self.sram = mcp_sram.Adafruit_MCP_SRAM(srcs, spi)
3741

3842
def begin(self, reset=True):
3943
self._cs.value = True
@@ -50,18 +54,24 @@ def command(self, c, data=None, end=True):
5054
self._cs.value = True
5155
self._dc.value = False
5256
self._cs.value = False
53-
with self.spi_device as spi:
54-
spi.write(bytearray([c]))
57+
outbuf = bytearray(1)
58+
59+
while not self.spi_device.try_lock():
60+
pass
61+
self.spi_device.write_readinto(bytearray([c]), outbuf)
5562

5663
if data is not None:
5764
self.data(data)
5865

5966
elif end:
6067
self._cs.value = True
6168

69+
self.spi_device.unlock()
70+
return outbuf[0]
71+
6272
def data(self, d):
6373
"""Send data to display."""
6474
self._dc.value = True
65-
with self.spi_device as spi:
66-
spi.write(d)
67-
self._cs.value = True
75+
self.spi_device.write(d)
76+
self._cs.value = True
77+
self.spi_device.unlock()

Adafruit_EPD/il0373.py

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
from Adafruit_EPD.epd import Adafruit_EPD
2+
from Adafruit_EPD.mcp_sram import Adafruit_MCP_SRAM
3+
from micropython import const
4+
import time
5+
6+
IL0373_PANEL_SETTING = const(0x00)
7+
IL0373_POWER_SETTING = const(0x01)
8+
IL0373_POWER_OFF = const(0x02)
9+
IL0373_POWER_OFF_SEQUENCE = const(0x03)
10+
IL0373_POWER_ON = const(0x04)
11+
IL0373_POWER_ON_MEASURE = const(0x05)
12+
IL0373_BOOSTER_SOFT_START = const(0x06)
13+
IL0373_DEEP_SLEEP = const(0x07)
14+
IL0373_DTM1 = const(0x10)
15+
IL0373_DATA_STOP = const(0x11)
16+
IL0373_DISPLAY_REFRESH = const(0x12)
17+
IL0373_DTM2 = const(0x13)
18+
IL0373_PDTM1 = const(0x14)
19+
IL0373_PDTM2 = const(0x15)
20+
IL0373_PDRF = const(0x16)
21+
IL0373_LUT1 = const(0x20)
22+
IL0373_LUTWW = const(0x21)
23+
IL0373_LUTBW = const(0x22)
24+
IL0373_LUTWB = const(0x23)
25+
IL0373_LUTBB = const(0x24)
26+
IL0373_PLL = const(0x30)
27+
IL0373_CDI = const(0x50)
28+
IL0373_RESOLUTION = const(0x61)
29+
IL0373_VCM_DC_SETTING = const(0x82)
30+
31+
class Adafruit_IL0373(Adafruit_EPD):
32+
def __init__(self, width, height, rst, dc, busy, srcs, cs, spi):
33+
super().__init__(width, height, rst, dc, busy, srcs, cs, spi)
34+
35+
self.bw_bufsize = int(width * height / 8)
36+
self.red_bufsize = int(width * height / 8)
37+
38+
self.begin()
39+
40+
def begin(self, reset=True):
41+
super(Adafruit_IL0373, self).begin(reset)
42+
43+
while self._busy.value == False:
44+
pass
45+
46+
self.command(IL0373_POWER_SETTING, bytearray([0x03, 0x00, 0x2b, 0x2b, 0x09]))
47+
self.command(IL0373_BOOSTER_SOFT_START, bytearray([0x17, 0x17, 0x17]))
48+
49+
def update(self):
50+
self.command(IL0373_DISPLAY_REFRESH)
51+
52+
while self._busy.value == False:
53+
pass
54+
55+
self.command(IL0373_CDI, bytearray([0x17]))
56+
self.command(IL0373_VCM_DC_SETTING, bytearray([0x00]))
57+
self.command(IL0373_POWER_OFF)
58+
time.sleep(2)
59+
60+
def power_up(self):
61+
self.command(IL0373_POWER_ON)
62+
63+
while self._busy.value == False:
64+
pass
65+
66+
time.sleep(.2)
67+
68+
self.command(IL0373_PANEL_SETTING, bytearray([0xCF]))
69+
self.command(IL0373_CDI, bytearray([0x37]))
70+
self.command(IL0373_PLL, bytearray([0x29]))
71+
b1 = self.height & 0xFF
72+
b2 = (self.height >> 8) & 0xFF
73+
b3 = self.width & 0xFF
74+
b4 = (self.width >> 8) & 0xFF
75+
self.command(IL0373_RESOLUTION, bytearray([b1, b2, b3, b4]))
76+
self.command(IL0373_VCM_DC_SETTING, bytearray([0x0A]))
77+
78+
79+
def display(self):
80+
self.power_up()
81+
82+
while not self.spi_device.try_lock():
83+
pass
84+
self.sram.cs.value = False
85+
#send read command
86+
self.spi_device.write(bytearray([Adafruit_MCP_SRAM.SRAM_READ]))
87+
#send start address
88+
self.spi_device.write(bytearray([0x00, 0x00]))
89+
self.spi_device.unlock()
90+
91+
#first data byte from SRAM will be transfered in at the same time as the EPD command is transferred out
92+
c = self.command(IL0373_DTM1, end=False)
93+
94+
while not self.spi_device.try_lock():
95+
pass
96+
self._dc.value = True
97+
xfer = bytearray([c])
98+
outbuf = bytearray(1)
99+
for i in range(self.bw_bufsize):
100+
outbuf[0] = xfer[0]
101+
self.spi_device.write_readinto(outbuf, xfer)
102+
self._cs.value = True
103+
self.sram.cs.value = True
104+
105+
time.sleep(.002)
106+
107+
self.sram.cs.value = False
108+
#send read command
109+
self.spi_device.write(bytearray([Adafruit_MCP_SRAM.SRAM_READ]))
110+
#send start address
111+
self.spi_device.write(bytearray([(self.bw_bufsize >> 8), (self.bw_bufsize & 0xFF)]))
112+
self.spi_device.unlock()
113+
114+
#first data byte from SRAM will be transfered in at the same time as the EPD command is transferred out
115+
c = self.command(IL0373_DTM2, end=False)
116+
117+
while not self.spi_device.try_lock():
118+
pass
119+
self._dc.value = True
120+
xfer = bytearray([c])
121+
outbuf = bytearray(1)
122+
for i in range(self.bw_bufsize):
123+
outbuf[0] = xfer[0]
124+
self.spi_device.write_readinto(outbuf, xfer)
125+
self._cs.value = True
126+
self.sram.cs.value = True
127+
self.spi_device.unlock()
128+
129+
self.update()
130+
131+
def draw_pixel(self, x, y, color):
132+
if (x < 0) or (x >= self.width) or (y < 0) or (y >= self.height):
133+
return
134+
135+
if x == 0:
136+
x = 1
137+
138+
addr = int(((self.width - x) * self.height + y)/8)
139+
if color == Adafruit_EPD.RED:
140+
addr = addr + self.bw_bufsize
141+
c = self.sram.read8(addr)
142+
143+
if color == Adafruit_EPD.WHITE:
144+
c = c | (1 << (7 - y%8))
145+
elif color == Adafruit_EPD.RED or color == Adafruit_EPD.BLACK:
146+
c = c & ~(1 << (7 - y%8))
147+
elif color == Adafruit_EPD.INVERSE:
148+
c = c ^ (1 << (7 - y%8))
149+
150+
self.sram.write8(addr, c)
151+
152+
def clear_buffer(self):
153+
self.sram.erase(0x00, self.bw_bufsize, 0xFF)
154+
self.sram.erase(self.bw_bufsize, self.red_bufsize, 0xFF)
155+
156+
def clear_display(self):
157+
self.clear_buffer()
158+
self.display()
159+

Adafruit_EPD/mcp_sram.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import time
2+
from micropython import const
3+
import digitalio
4+
5+
from adafruit_bus_device.spi_device import SPIDevice
6+
import busio
7+
from board import *
8+
9+
SRAM_SEQUENTIAL_MODE = const(1 << 6)
10+
11+
class Adafruit_MCP_SRAM:
12+
13+
SRAM_READ = 0x03
14+
SRAM_WRITE = 0x02
15+
SRAM_RDSR = 0x05
16+
SRAM_WRSR = 0x01
17+
18+
def __init__(self, cs, spi):
19+
# Handle hardware SPI
20+
self.spi_device = spi
21+
self.cs = cs
22+
23+
self.cs.direction = digitalio.Direction.OUTPUT
24+
while not self.spi_device.try_lock():
25+
pass
26+
self.cs.value = False
27+
self.spi_device.write(bytearray([Adafruit_MCP_SRAM.SRAM_WRSR, 0x43]))
28+
self.cs.value = True
29+
self.spi_device.unlock()
30+
31+
def write(self, addr, buf, reg=SRAM_WRITE):
32+
c = bytearray([reg, (addr >> 8) & 0xFF, addr & 0xFF] + buf)
33+
34+
while not self.spi_device.try_lock():
35+
pass
36+
self.cs.value = False
37+
self.spi_device.write(c)
38+
self.cs.value = True
39+
self.spi_device.unlock()
40+
41+
def read(self, addr, length, reg=SRAM_READ):
42+
c = bytearray([reg, (addr >> 8) & 0xFF, addr & 0xFF])
43+
44+
buf = bytearray(length)
45+
while not self.spi_device.try_lock():
46+
pass
47+
self.cs.value = False
48+
self.spi_device.write(c)
49+
self.spi_device.readinto(buf)
50+
self.cs.value = True
51+
self.spi_device.unlock()
52+
return buf
53+
54+
def read8(self, addr, reg=SRAM_READ):
55+
return self.read(addr, 1, reg)[0]
56+
57+
def read16(self, addr, reg=SRAM_READ):
58+
buf = self.read(addr, 2, reg)
59+
return (buf[0] << 8 | buf[1])
60+
61+
def write8(self, addr, value, reg=SRAM_WRITE):
62+
self.write(addr, [value], reg)
63+
64+
def write16(self, addr, value, reg=SRAM_WRITE):
65+
self.write(addr, [value >> 8, value], reg)
66+
67+
def erase(self, addr, length, value):
68+
c = bytearray([Adafruit_MCP_SRAM.SRAM_WRITE, (addr >> 8) & 0xFF, addr & 0xFF])
69+
70+
while not self.spi_device.try_lock():
71+
pass
72+
self.cs.value = False
73+
self.spi_device.write(c)
74+
for x in range(length):
75+
self.spi_device.write(bytearray([value]))
76+
self.cs.value = True
77+
self.spi_device.unlock()

0 commit comments

Comments
 (0)