Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit f351c6d

Browse files
committed
drivers/display/lcd160cr: Fix get_line method and enhance screen_dump.
The docs are updated and describe the new behaviour of these methods.
1 parent e4a5357 commit f351c6d

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

docs/library/lcd160cr.rst

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,19 @@ The following methods manipulate individual pixels on the display.
150150

151151
.. method:: LCD160CR.get_line(x, y, buf)
152152

153-
Get a line of pixels into the given buffer.
154-
155-
.. method:: LCD160CR.screen_dump(buf)
156-
157-
Dump the entire screen to the given buffer.
153+
Low-level method to get a line of pixels into the given buffer.
154+
To read `n` pixels `buf` should be `2*n+1` bytes in length. The first byte
155+
is a dummy byte and should be ignored, and subsequent bytes represent the
156+
pixels in the line starting at coordinate `(x, y)`.
157+
158+
.. method:: LCD160CR.screen_dump(buf, x=0, y=0, w=None, h=None)
159+
160+
Dump the contents of the screen to the given buffer. The parameters `x` and `y`
161+
specify the starting coordinate, and `w` and `h` the size of the region. If `w`
162+
or `h` are `None` then they will take on their maximum values, set by the size
163+
of the screen minus the given `x` and `y` values. `buf` should be large enough
164+
to hold `2*w*h` bytes. If it's smaller then only the initial horizontal lines
165+
will be stored.
158166

159167
.. method:: LCD160CR.screen_load(buf)
160168

drivers/display/lcd160cr.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ def get_pixel(self, x, y):
257257
def get_line(self, x, y, buf):
258258
l = len(buf) // 2
259259
self._fcmd2b('<BBBBB', 0x10, l, x, y)
260+
l *= 2
260261
t = 1000
261262
while t:
262263
self.i2c.readfrom_into(self.i2c_addr, self.buf1)
@@ -267,21 +268,28 @@ def get_line(self, x, y, buf):
267268
sleep_ms(1)
268269
raise OSError(uerrno.ETIMEDOUT)
269270

270-
def screen_dump(self, buf):
271-
line = bytearray(self.w + 1)
272-
h = len(buf) // (2 * self.w)
273-
if h > self.h:
274-
h = self.h
275-
for i in range(h):
276-
ix = i * self.w * 2
277-
self.get_line(0, i, line)
278-
for j in range(1, len(line)):
279-
buf[ix] = line[j]
280-
ix += 1
281-
self.get_line(self.w // 2, i, line)
282-
for j in range(1, len(line)):
283-
buf[ix] = line[j]
284-
ix += 1
271+
def screen_dump(self, buf, x=0, y=0, w=None, h=None):
272+
if w is None:
273+
w = self.w - x
274+
if h is None:
275+
h = self.h - y
276+
if w <= 127:
277+
line = bytearray(2 * w + 1)
278+
line2 = None
279+
else:
280+
# split line if more than 254 bytes needed
281+
buflen = (w + 1) // 2
282+
line = bytearray(2 * buflen + 1)
283+
line2 = memoryview(line)[:2 * (w - buflen) + 1]
284+
for i in range(min(len(buf) // (2 * w), h)):
285+
ix = i * w * 2
286+
self.get_line(x, y + i, line)
287+
buf[ix:ix + len(line) - 1] = memoryview(line)[1:]
288+
ix += len(line) - 1
289+
if line2:
290+
self.get_line(x + buflen, y + i, line2)
291+
buf[ix:ix + len(line2) - 1] = memoryview(line2)[1:]
292+
ix += len(line2) - 1
285293

286294
def screen_load(self, buf):
287295
l = self.w * self.h * 2+2

0 commit comments

Comments
 (0)