From f351c6db5e4c0bf75d5d647e69a047da739606a8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 17 May 2017 19:53:13 +1000 Subject: [PATCH] drivers/display/lcd160cr: Fix get_line method and enhance screen_dump. The docs are updated and describe the new behaviour of these methods. --- docs/library/lcd160cr.rst | 14 +++++++++++--- drivers/display/lcd160cr.py | 38 ++++++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/docs/library/lcd160cr.rst b/docs/library/lcd160cr.rst index 76828d32da..bd47412986 100644 --- a/docs/library/lcd160cr.rst +++ b/docs/library/lcd160cr.rst @@ -150,11 +150,19 @@ The following methods manipulate individual pixels on the display. .. method:: LCD160CR.get_line(x, y, buf) - Get a line of pixels into the given buffer. + Low-level method to get a line of pixels into the given buffer. + To read `n` pixels `buf` should be `2*n+1` bytes in length. The first byte + is a dummy byte and should be ignored, and subsequent bytes represent the + pixels in the line starting at coordinate `(x, y)`. -.. method:: LCD160CR.screen_dump(buf) +.. method:: LCD160CR.screen_dump(buf, x=0, y=0, w=None, h=None) - Dump the entire screen to the given buffer. + Dump the contents of the screen to the given buffer. The parameters `x` and `y` + specify the starting coordinate, and `w` and `h` the size of the region. If `w` + or `h` are `None` then they will take on their maximum values, set by the size + of the screen minus the given `x` and `y` values. `buf` should be large enough + to hold `2*w*h` bytes. If it's smaller then only the initial horizontal lines + will be stored. .. method:: LCD160CR.screen_load(buf) diff --git a/drivers/display/lcd160cr.py b/drivers/display/lcd160cr.py index 0cf52f2ef3..dd9ab9985b 100644 --- a/drivers/display/lcd160cr.py +++ b/drivers/display/lcd160cr.py @@ -257,6 +257,7 @@ class LCD160CR: def get_line(self, x, y, buf): l = len(buf) // 2 self._fcmd2b(' self.h: - h = self.h - for i in range(h): - ix = i * self.w * 2 - self.get_line(0, i, line) - for j in range(1, len(line)): - buf[ix] = line[j] - ix += 1 - self.get_line(self.w // 2, i, line) - for j in range(1, len(line)): - buf[ix] = line[j] - ix += 1 + def screen_dump(self, buf, x=0, y=0, w=None, h=None): + if w is None: + w = self.w - x + if h is None: + h = self.h - y + if w <= 127: + line = bytearray(2 * w + 1) + line2 = None + else: + # split line if more than 254 bytes needed + buflen = (w + 1) // 2 + line = bytearray(2 * buflen + 1) + line2 = memoryview(line)[:2 * (w - buflen) + 1] + for i in range(min(len(buf) // (2 * w), h)): + ix = i * w * 2 + self.get_line(x, y + i, line) + buf[ix:ix + len(line) - 1] = memoryview(line)[1:] + ix += len(line) - 1 + if line2: + self.get_line(x + buflen, y + i, line2) + buf[ix:ix + len(line2) - 1] = memoryview(line2)[1:] + ix += len(line2) - 1 def screen_load(self, buf): l = self.w * self.h * 2+2