Skip to content

Commit fe0cb4d

Browse files
committed
linty!@
1 parent b32893c commit fe0cb4d

File tree

3 files changed

+102
-51
lines changed

3 files changed

+102
-51
lines changed

adafruit_epd/epd.py

Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,22 @@
2727
"""
2828

2929
import time
30+
from micropython import const
3031
from digitalio import Direction
31-
import adafruit_framebuf
3232
from adafruit_epd import mcp_sram
3333

34-
class Adafruit_EPD:
34+
class Adafruit_EPD: # pylint: disable=too-many-instance-attributes
3535
"""Base class for EPD displays
3636
"""
37-
BLACK = 0
38-
WHITE = 1
39-
INVERSE = 2
40-
RED = 3
41-
DARK = 4
42-
LIGHT = 5
43-
44-
# pylint: disable=too-many-arguments
45-
def __init__(self, width, height, spi, cs_pin, dc_pin, sramcs_pin, rst_pin, busy_pin):
46-
# pylint: enable=too-many-arguments
37+
BLACK = const(0)
38+
WHITE = const(1)
39+
INVERSE = const(2)
40+
RED = const(3)
41+
DARK = const(4)
42+
LIGHT = const(5)
43+
44+
45+
def __init__(self, width, height, spi, cs_pin, dc_pin, sramcs_pin, rst_pin, busy_pin): # pylint: disable=too-many-arguments
4746
self._width = width
4847
self._height = height
4948

@@ -74,8 +73,11 @@ def __init__(self, width, height, spi, cs_pin, dc_pin, sramcs_pin, rst_pin, busy
7473
if sramcs_pin:
7574
self.sram = mcp_sram.Adafruit_MCP_SRAM(sramcs_pin, spi)
7675

76+
self._buf = bytearray(3)
7777
self._buffer1_size = self._buffer2_size = 0
7878
self._buffer1 = self._buffer2 = None
79+
self._framebuf1 = self._framebuf2 = None
80+
self.black_invert = self.red_invert = True
7981
self.hardware_reset()
8082

8183
def display(self):
@@ -89,9 +91,11 @@ def display(self):
8991
pass
9092
self.sram.cs_pin.value = False
9193
#send read command
92-
self.spi_device.write(bytearray([mcp_sram.Adafruit_MCP_SRAM.SRAM_READ]))
94+
self._buf[0] = mcp_sram.Adafruit_MCP_SRAM.SRAM_READ
9395
#send start address
94-
self.spi_device.write(bytearray([0x00, 0x00]))
96+
self._buf[1] = 0
97+
self._buf[2] = 0
98+
self.spi_device.write(self._buf, end=3)
9599
self.spi_device.unlock()
96100

97101
#first data byte from SRAM will be transfered in at the
@@ -122,9 +126,11 @@ def display(self):
122126
pass
123127
self.sram.cs_pin.value = False
124128
#send read command
125-
self.spi_device.write(bytearray([mcp_sram.Adafruit_MCP_SRAM.SRAM_READ]))
129+
self._buf[0] = mcp_sram.Adafruit_MCP_SRAM.SRAM_READ
126130
#send start address
127-
self.spi_device.write(bytearray([(self._buffer1_size >> 8), (self._buffer1_size & 0xFF)]))
131+
self._buf[1] = self._buffer1_size >> 8
132+
self._buf[2] = self._buffer1_size
133+
self.spi_device.write(self._buf, end=3)
128134
self.spi_device.unlock()
129135

130136
#first data byte from SRAM will be transfered in at the
@@ -151,7 +157,7 @@ def display(self):
151157

152158

153159
def hardware_reset(self):
154-
# if we have a reset pin, do a hardware reset
160+
"""If we have a reset pin, do a hardware reset by toggling it"""
155161
if self._rst:
156162
self._rst.value = False
157163
time.sleep(0.1)
@@ -178,6 +184,28 @@ def command(self, cmd, data=None, end=True):
178184

179185
return outbuf[0]
180186

187+
def power_up(self):
188+
"""Power up the display in preparation for writing RAM and updating.
189+
must be implemented in subclass"""
190+
raise NotImplementedError()
191+
192+
def power_down(self):
193+
"""Power down the display, must be implemented in subclass"""
194+
raise NotImplementedError()
195+
196+
def update(self):
197+
"""Update the display from internal memory, must be implemented in subclass"""
198+
raise NotImplementedError()
199+
200+
def write_ram(self, index):
201+
"""Send the one byte command for starting the RAM write process. Returns
202+
the byte read at the same time over SPI. index is the RAM buffer, can be
203+
0 or 1 for tri-color displays. must be implemented in subclass"""
204+
raise NotImplementedError()
205+
206+
def set_ram_address(self, x, y):
207+
"""Set the RAM address location, must be implemented in subclass"""
208+
raise NotImplementedError()
181209

182210
def pixel(self, x, y, color):
183211
"""draw a single pixel in the display buffer"""
@@ -200,40 +228,54 @@ def fill(self, color):
200228
self._framebuf1.fill(black_fill)
201229
self._framebuf2.fill(red_fill)
202230

203-
def rect(self, x, y, width, height, color):
231+
def rect(self, x, y, width, height, color): # pylint: disable=too-many-arguments
204232
"""draw a rectangle"""
205-
self._framebuf1.rect(x, y, width, height, (color == Adafruit_EPD.BLACK) != self.black_invert)
206-
self._framebuf2.rect(x, y, width, height, (color == Adafruit_EPD.RED) != self.red_invert)
233+
self._framebuf1.rect(x, y, width, height,
234+
(color == Adafruit_EPD.BLACK) != self.black_invert)
235+
self._framebuf2.rect(x, y, width, height,
236+
(color == Adafruit_EPD.RED) != self.red_invert)
207237

208-
# pylint: disable=too-many-arguments
209-
def fill_rect(self, x, y, width, height, color):
238+
def fill_rect(self, x, y, width, height, color): # pylint: disable=too-many-arguments
210239
"""fill a rectangle with the passed color"""
211-
self._framebuf1.fill_rect(x, y, width, height, (color == Adafruit_EPD.BLACK) != self.black_invert)
212-
self._framebuf2.fill_rect(x, y, width, height, (color == Adafruit_EPD.RED) != self.red_invert)
213-
214-
def line(self, x_0, y_0, x_1, y_1, color):
215-
self._framebuf1.line(x_0, y_0, x_1, y_1, (color == Adafruit_EPD.BLACK) != self.black_invert)
216-
self._framebuf2.line(x_0, y_0, x_1, y_1, (color == Adafruit_EPD.RED) != self.red_invert)
240+
self._framebuf1.fill_rect(x, y, width, height,
241+
(color == Adafruit_EPD.BLACK) != self.black_invert)
242+
self._framebuf2.fill_rect(x, y, width, height,
243+
(color == Adafruit_EPD.RED) != self.red_invert)
244+
245+
def line(self, x_0, y_0, x_1, y_1, color): # pylint: disable=too-many-arguments
246+
"""Draw a line from (x_0, y_0) to (x_1, y_1) in passed color"""
247+
self._framebuf1.line(x_0, y_0, x_1, y_1,
248+
(color == Adafruit_EPD.BLACK) != self.black_invert)
249+
self._framebuf2.line(x_0, y_0, x_1, y_1,
250+
(color == Adafruit_EPD.RED) != self.red_invert)
217251

218252
def text(self, string, x, y, color, *, font_name="font5x8.bin"):
219-
self._framebuf1.text(string, x, y, (color == Adafruit_EPD.BLACK) != self.black_invert, font_name=font_name)
220-
self._framebuf2.text(string, x, y, (color == Adafruit_EPD.RED) != self.red_invert, font_name=font_name)
253+
"""Write text string at location (x, y) in given color, using font file"""
254+
self._framebuf1.text(string, x, y,
255+
(color == Adafruit_EPD.BLACK) != self.black_invert,
256+
font_name=font_name)
257+
self._framebuf2.text(string, x, y,
258+
(color == Adafruit_EPD.RED) != self.red_invert,
259+
font_name=font_name)
221260

222261
@property
223262
def width(self):
263+
"""The width of the display, accounting for rotation"""
224264
if self.rotation in (0, 2):
225265
return self._width
226266
return self._height
227267

228268
@property
229269
def height(self):
270+
"""The height of the display, accounting for rotation"""
230271
if self.rotation in (0, 2):
231272
return self._height
232273
return self._width
233274

234275
@property
235276
def rotation(self):
236-
return self._framebuf1._rotation
277+
"""The rotation of the display, can be one of (0, 1, 2, 3)"""
278+
return self._framebuf1.rotation
237279

238280
@rotation.setter
239281
def rotation(self, val):
@@ -271,7 +313,7 @@ def image(self, image):
271313
addr = int(((self._width - x) * self._height + y)/8)
272314

273315
if pixel == (0xFF, 0, 0):
274-
addr = addr + self.bw_bufsize
316+
addr = addr + self._buffer1_size
275317
current = self.sram.read8(addr)
276318

277319
if pixel in ((0xFF, 0, 0), (0, 0, 0)):

adafruit_epd/il0373.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
from micropython import const
3131
import adafruit_framebuf
3232
from adafruit_epd.epd import Adafruit_EPD
33-
from adafruit_epd.mcp_sram import Adafruit_MCP_SRAM
3433

3534
_IL0373_POWER_SETTING = const(0x01)
3635
_IL0373_PANEL_SETTING = const(0x00)
@@ -68,35 +67,38 @@ def __init__(self, width, height, spi, *, cs_pin, dc_pin, sramcs_pin, rst_pin, b
6867
self._buffer2_size = int(width * height / 8)
6968

7069
if sramcs_pin:
71-
self._buffer1_addr = 0
72-
self._buffer2_addr = self._buffer1_size
73-
self._framebuf1 = adafruit_framebuf.FrameBuffer(self.sram.get_view(0), width, height, buf_format=adafruit_framebuf.MHMSB)
74-
self._framebuf2 = adafruit_framebuf.FrameBuffer(self.sram.get_view(self._buffer1_size), width, height, buf_format=adafruit_framebuf.MHMSB)
70+
self._buffer1 = self.sram.get_view(0)
71+
self._buffer2 = self.sram.get_view(self._buffer1_size)
7572
else:
7673
self._buffer1 = bytearray((width * height) // 8)
7774
self._buffer2 = bytearray((width * height) // 8)
78-
# since we have *two* framebuffers - one for red and one for black, we dont subclass but manage manually
79-
self._framebuf1 = adafruit_framebuf.FrameBuffer(self._buffer1, width, height, buf_format=adafruit_framebuf.MHMSB)
80-
self._framebuf2 = adafruit_framebuf.FrameBuffer(self._buffer2, width, height, buf_format=adafruit_framebuf.MHMSB)
75+
# since we have *two* framebuffers - one for red and one for black
76+
# we dont subclass but manage manually
77+
self._framebuf1 = adafruit_framebuf.FrameBuffer(self._buffer1, width, height,
78+
buf_format=adafruit_framebuf.MHMSB)
79+
self._framebuf2 = adafruit_framebuf.FrameBuffer(self._buffer2, width, height,
80+
buf_format=adafruit_framebuf.MHMSB)
8181
self.black_invert = True
8282
self.red_invert = True
8383
# pylint: enable=too-many-arguments
8484

8585
def begin(self, reset=True):
8686
"""Begin communication with the display and set basic settings"""
87-
super(Adafruit_IL0373, self).begin(reset)
87+
if reset:
88+
self.hardware_reset()
8889
self.power_down()
8990

9091
def busy_wait(self):
92+
"""Wait for display to be done with current task, either by polling the
93+
busy pin, or pausing"""
9194
if self._busy:
9295
while not self._busy.value:
9396
pass
9497
else:
9598
time.sleep(0.5)
9699

97100
def power_up(self):
98-
"""power up the display"""
99-
101+
"""Power up the display in preparation for writing RAM and updating"""
100102
self.hardware_reset()
101103
self.busy_wait()
102104

@@ -117,25 +119,31 @@ def power_up(self):
117119
self.command(_IL0373_VCM_DC_SETTING, bytearray([0x0A]))
118120
time.sleep(0.05)
119121

120-
def power_down():
122+
def power_down(self):
123+
"""Power down the display - required when not actively displaying!"""
121124
self.command(_IL0373_CDI, bytearray([0x17]))
122125
self.command(_IL0373_VCM_DC_SETTING, bytearray([0x00]))
123126
self.command(_IL0373_POWER_OFF)
124127

125128
def update(self):
126-
"""update the display"""
129+
"""Update the display from internal memory"""
127130
self.command(_IL0373_DISPLAY_REFRESH)
128131
time.sleep(0.1)
129132
self.busy_wait()
130133
if not self._busy:
131134
time.sleep(15) # wait 15 seconds
132135

133136
def write_ram(self, index):
137+
"""Send the one byte command for starting the RAM write process. Returns
138+
the byte read at the same time over SPI. index is the RAM buffer, can be
139+
0 or 1 for tri-color displays."""
134140
if index == 0:
135141
return self.command(_IL0373_DTM1, end=False)
136142
if index == 1:
137143
return self.command(_IL0373_DTM2, end=False)
138144
raise RuntimeError("RAM index must be 0 or 1")
139145

140-
def set_ram_address(self, x, y):
146+
def set_ram_address(self, x, y): # pylint: disable=unused-argument, no-self-use
147+
"""Set the RAM address location, not used on this chipset but required by
148+
the superclass"""
141149
return # on this chip it does nothing

adafruit_epd/mcp_sram.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
"""
2828

2929
from micropython import const
30-
import digitalio
31-
import adafruit_bus_device.spi_device as spi_device
30+
from adafruit_bus_device import spi_device
3231

3332
SRAM_SEQUENTIAL_MODE = const(1 << 6)
3433

3534
class Adafruit_MCP_SRAM_View:
35+
"""A interface class that turns an SRAM chip into something like a memoryview"""
3636
def __init__(self, sram, offset):
3737
self._sram = sram
3838
self._offset = offset
@@ -55,16 +55,17 @@ class Adafruit_MCP_SRAM:
5555

5656
def __init__(self, cs_pin, spi):
5757
# Handle hardware SPI
58-
self._spi = spi_device.SPIDevice(spi,cs_pin, baudrate=8000000)
58+
self._spi = spi_device.SPIDevice(spi, cs_pin, baudrate=8000000)
5959
self.spi_device = spi
6060
self.cs_pin = cs_pin
6161
self._buf = bytearray(3)
6262
self._buf[0] = Adafruit_MCP_SRAM.SRAM_WRSR
6363
self._buf[1] = 0x43
64-
with self._spi as spi:
65-
spi.write(self._buf, end=2)
64+
with self._spi as spidev:
65+
spidev.write(self._buf, end=2)
6666

6767
def get_view(self, offset):
68+
"""Create an object that can be used as a memoryview, with a given offset"""
6869
return Adafruit_MCP_SRAM_View(self, offset)
6970

7071
def write(self, addr, buf, reg=SRAM_WRITE):

0 commit comments

Comments
 (0)