Skip to content

Commit f7c3501

Browse files
committed
begin framebuffification
1 parent 346a9d4 commit f7c3501

File tree

2 files changed

+54
-74
lines changed

2 files changed

+54
-74
lines changed

adafruit_epd/epd.py

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import time
3030
import digitalio
31+
import adafruit_framebuf
3132
from adafruit_epd import mcp_sram
3233

3334
class Adafruit_EPD:
@@ -46,41 +47,47 @@ def __init__(self, width, height, spi, cs_pin, dc_pin, sramcs_pin, rst_pin, busy
4647
self.width = width
4748
self.height = height
4849

49-
# Setup reset pin.
50+
# Setup reset pin, if we have one
5051
self._rst = rst_pin
5152
if rst_pin:
5253
self._rst.direction = digitalio.Direction.OUTPUT
5354

54-
# Setup busy pin.
55+
# Setup busy pin, if we have one
5556
self._busy = busy_pin
5657
if busy_pin:
5758
self._busy.direction = digitalio.Direction.INPUT
5859

59-
# Setup dc pin.
60+
# Setup dc pin (required)
6061
self._dc = dc_pin
6162
self._dc.direction = digitalio.Direction.OUTPUT
6263
self._dc.value = False
6364

64-
# Setup cs pin.
65+
# Setup cs pin (required)
6566
self._cs = cs_pin
6667
self._cs.direction = digitalio.Direction.OUTPUT
6768
self._cs.value = True
6869

70+
# SPI interface (required)
6971
self.spi_device = spi
7072

7173
if sramcs_pin:
7274
self.sram = mcp_sram.Adafruit_MCP_SRAM(sramcs_pin, spi)
7375
else:
7476
self.sram = None
75-
self.bw_buffer = bytearray((width // 8) * height)
76-
self.red_buffer = bytearray((width // 8) * height)
77+
self._bw_buffer = bytearray((width // 8) * height)
78+
self._red_buffer = bytearray((width // 8) * height)
79+
# since we have *two* framebuffers - one for red and one for black, we dont subclass but manage manually
80+
self._red_framebuf = adafruit_framebuf.FrameBuffer(self._red_buffer, width, height, buf_format=adafruit_framebuf.MHMSB)
81+
self._bw_framebuf = adafruit_framebuf.FrameBuffer(self._bw_buffer, width, height, buf_format=adafruit_framebuf.MHMSB)
7782

83+
# if we hav ea reset pin, do a hardware reset
7884
if self._rst:
7985
self._rst.value = False
8086
time.sleep(.1)
8187
self._rst.value = True
8288
time.sleep(.1)
8389

90+
8491
def command(self, cmd, data=None, end=True):
8592
"""Send command byte to display."""
8693
self._cs.value = True
@@ -109,42 +116,34 @@ def data(self, dat):
109116
self._cs.value = True
110117
self.spi_device.unlock()
111118

112-
def draw_pixel(self, x, y, color):
119+
def fill(self, color):
120+
#This should be overridden in the subclass
121+
self._bw_framebuf.fill((color == Adafruit_EPD.BLACK) != self.black_invert)
122+
self._red_framebuf.fill((color == Adafruit_EPD.RED) != self.red_invert)
123+
124+
def pixel(self, x, y, color=None):
113125
"""This should be overridden in the subclass"""
114-
pass
126+
self._bw_framebuf.pixel(x, y, (color == Adafruit_EPD.BLACK) != self.black_invert)
127+
self._red_framebuf.pixel(x, y, (color == Adafruit_EPD.RED) != self.red_invert)
115128

116-
#framebuf methods
117-
def fill(self, color):
118-
"""fill the screen with the passed color"""
119-
self.fill_rect(0, 0, self.width, self.height, color)
129+
def rect(self, x, y, width, height, color):
130+
"""draw a rectangle"""
131+
self._bw_framebuf.rect(x, y, width, height, (color == Adafruit_EPD.BLACK) != self.black_invert)
132+
self._red_framebuf.rect(x, y, width, height, (color == Adafruit_EPD.RED) != self.red_invert)
120133

121134
# pylint: disable=too-many-arguments
122135
def fill_rect(self, x, y, width, height, color):
123136
"""fill a rectangle with the passed color"""
124-
if width < 1 or height < 1 or (x+width) <= 0:
125-
return
126-
if (y+height) <= 0 or y >= self.height or x >= self.width:
127-
return
128-
xend = min(self.width, x+width)
129-
yend = min(self.height, y+height)
130-
x = max(x, 0)
131-
y = max(y, 0)
132-
for _x in range(xend - x):
133-
for _y in range(yend - y):
134-
self.draw_pixel(x + _x, y + _y, color)
135-
return
137+
self._bw_framebuf.fill_rect(x, y, width, height, (color == Adafruit_EPD.BLACK) != self.black_invert)
138+
self._red_framebuf.fill_rect(x, y, width, height, (color == Adafruit_EPD.RED) != self.red_invert)
136139

137-
def pixel(self, x, y, color=None):
138-
"""draw a pixel"""
139-
if x < 0 or x >= self.width or y < 0 or y >= self.height:
140-
return None
141-
#TODO: figure this out when we know what framebuffer we
142-
# will actually use
143-
#if color is None:
144-
# return self.get_pixel(self, x, y)
140+
def line(self, x_0, y_0, x_1, y_1, color):
141+
self._bw_framebuf.line(x_0, y_0, x_1, y_1, (color == Adafruit_EPD.BLACK) != self.black_invert)
142+
self._red_framebuf.line(x_0, y_0, x_1, y_1, (color == Adafruit_EPD.RED) != self.red_invert)
145143

146-
self.draw_pixel(x, y, color)
147-
return None
144+
def text(self, string, x, y, color, *, font_name="font5x8.bin"):
145+
self._bw_framebuf.text(string, x, y, (color == Adafruit_EPD.BLACK) != self.black_invert, font_name)
146+
self._red_framebuf.text(string, x, y, (color == Adafruit_EPD.RED) != self.red_invert, font_name)
148147

149148
def hline(self, x, y, width, color):
150149
"""draw a horizontal line"""
@@ -153,10 +152,3 @@ def hline(self, x, y, width, color):
153152
def vline(self, x, y, height, color):
154153
"""draw a vertical line"""
155154
self.fill_rect(x, y, 1, height, color)
156-
157-
def rect(self, x, y, width, height, color):
158-
"""draw a rectangle"""
159-
self.fill_rect(x, y, width, 1, color)
160-
self.fill_rect(x, y+height, width, 1, color)
161-
self.fill_rect(x, y, 1, height, color)
162-
self.fill_rect(x+width, y, 1, height, color)

adafruit_epd/il0373.py

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import time
3030
from micropython import const
31+
import adafruit_framebuf
3132
from adafruit_epd.epd import Adafruit_EPD
3233
from adafruit_epd.mcp_sram import Adafruit_MCP_SRAM
3334

@@ -65,6 +66,8 @@ def __init__(self, width, height, spi, *, cs_pin, dc_pin, sramcs_pin, rst_pin, b
6566

6667
self.bw_bufsize = int(width * height / 8)
6768
self.red_bufsize = int(width * height / 8)
69+
self.black_invert = True
70+
self.red_invert = True
6871
# pylint: enable=too-many-arguments
6972

7073
def begin(self, reset=True):
@@ -172,7 +175,7 @@ def display(self):
172175
while not self.spi_device.try_lock():
173176
pass
174177
self._dc.value = True
175-
self.spi_device.write(self.bw_buffer)
178+
self.spi_device.write(self._bw_buffer)
176179
self._cs.value = True
177180
self.spi_device.unlock()
178181

@@ -182,7 +185,7 @@ def display(self):
182185
while not self.spi_device.try_lock():
183186
pass
184187
self._dc.value = True
185-
self.spi_device.write(self.red_buffer)
188+
self.spi_device.write(self._red_buffer)
186189
self._cs.value = True
187190
self.spi_device.unlock()
188191

@@ -220,45 +223,32 @@ def image(self, image):
220223

221224
self.sram.write8(addr, current)
222225

223-
def draw_pixel(self, x, y, color):
226+
def pixel(self, x, y, color):
224227
"""draw a single pixel in the display buffer"""
225-
if (x < 0) or (x >= self.width) or (y < 0) or (y >= self.height):
226-
return
227-
228-
if x == 0:
229-
x = 1
230-
231-
addr = ((self.width - x) * self.height + y) // 8
232-
233228
if self.sram:
229+
if (x < 0) or (x >= self.width) or (y < 0) or (y >= self.height):
230+
return
231+
if x == 0:
232+
x = 1
233+
addr = ((self.width - x) * self.height + y) // 8
234234
if color == Adafruit_EPD.RED:
235235
current = self.sram.read8(addr + self.bw_bufsize)
236236
else:
237237
current = self.sram.read8(addr)
238-
else:
239-
if color == Adafruit_EPD.RED:
240-
current = self.red_buffer[addr]
241-
else:
242-
current = self.bw_buffer[addr]
243238

244-
if color == Adafruit_EPD.WHITE:
245-
current = current | (1 << (7 - y%8))
246-
elif color in (Adafruit_EPD.RED, Adafruit_EPD.BLACK):
247-
current = current & ~(1 << (7 - y%8))
248-
elif color == Adafruit_EPD.INVERSE:
249-
current = current ^ (1 << (7 - y%8))
239+
if color == Adafruit_EPD.WHITE:
240+
current = current | (1 << (7 - y%8))
241+
elif color in (Adafruit_EPD.RED, Adafruit_EPD.BLACK):
242+
current = current & ~(1 << (7 - y%8))
243+
elif color == Adafruit_EPD.INVERSE:
244+
current = current ^ (1 << (7 - y%8))
250245

251-
if self.sram:
252246
if color == Adafruit_EPD.RED:
253-
current = self.sram.write8(addr + self.bw_bufsize, current)
247+
self.sram.write8(addr + self.bw_bufsize, current)
254248
else:
255-
current = self.sram.write8(addr, current)
249+
self.sram.write8(addr, current)
256250
else:
257-
if color == Adafruit_EPD.RED:
258-
self.red_buffer[addr] = current
259-
else:
260-
self.bw_buffer[addr] = current
261-
return
251+
super().pixel(x, y, color)
262252

263253
def fill(self, color):
264254
"""fill the screen with the passed color"""
@@ -272,6 +262,4 @@ def fill(self, color):
272262
self.sram.erase(0x00, self.bw_bufsize, black_fill)
273263
self.sram.erase(self.bw_bufsize, self.red_bufsize, red_fill)
274264
else:
275-
for i in range(len(self.bw_buffer)):
276-
self.bw_buffer[i] = black_fill
277-
self.red_buffer[i] = red_fill
265+
super().fill(color)

0 commit comments

Comments
 (0)