From 6b41c8f3db1778826ec8556f8900878a8f32c87a Mon Sep 17 00:00:00 2001 From: Daniel Thompson Date: Sun, 17 Jan 2021 17:44:21 +0000 Subject: [PATCH] drivers: st7789: Pre-allocate a memoryview Reduce the cost of slicing the linebuffer by pre-allocating a memoryview. Signed-off-by: Daniel Thompson --- wasp/apps/gameoflife.py | 4 ++-- wasp/draw565.py | 10 +++++----- wasp/drivers/st7789.py | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/wasp/apps/gameoflife.py b/wasp/apps/gameoflife.py index 109888c..7e2b518 100644 --- a/wasp/apps/gameoflife.py +++ b/wasp/apps/gameoflife.py @@ -223,7 +223,7 @@ class GameOfLifeApp(): display = wasp.watch.display lb = display.linebuffer - alive = memoryview(lb)[0:2*16] + alive = lb[0:2*16] self._color = xorshift12(self._color) rgbhi = get_color(self._color) rgblo = rgbhi & 0xff @@ -234,7 +234,7 @@ class GameOfLifeApp(): for i in (0, 3, 12, 15): alive[i*2] = 0 alive[i*2+1] = 0 - dead = memoryview(lb)[2*16:4*16] + dead = lb[2*16:4*16] for i in range(len(dead)): dead[i] = 0 diff --git a/wasp/draw565.py b/wasp/draw565.py index 5f61a6c..37e96b5 100644 --- a/wasp/draw565.py +++ b/wasp/draw565.py @@ -85,7 +85,7 @@ def _bounding_box(s, font): def _draw_glyph(display, glyph, x, y, bgfg): (px, h, w) = glyph - buf = memoryview(display.linebuffer)[0:2*(w+1)] + buf = display.linebuffer[0:2*(w+1)] buf[2*w] = 0 buf[2*w + 1] = 0 bytes_per_row = (w + 7) // 8 @@ -155,7 +155,7 @@ class Draw565(object): # Populate the line buffer buf = display.linebuffer - sz = len(display.linebuffer) // 2 + sz = len(buf) // 2 _fill(buf, bg, min(sz, remaining), 0) display.quick_start() @@ -163,7 +163,7 @@ class Draw565(object): quick_write(buf) remaining -= sz if remaining: - quick_write(memoryview(display.linebuffer)[0:2*remaining]) + quick_write(buf[0:2*remaining]) display.quick_end() @micropython.native @@ -195,7 +195,7 @@ class Draw565(object): display.set_window(pos[0], pos[1], sx, sy) - buf = memoryview(display.linebuffer)[0:2*sx] + buf = display.linebuffer[0:2*sx] bp = 0 color = bg @@ -233,7 +233,7 @@ class Draw565(object): palette = array.array('H', (0, c1, c2, fg)) next_color = 1 rl = 0 - buf = memoryview(display.linebuffer)[0:2*sx] + buf = display.linebuffer[0:2*sx] bp = 0 display.quick_start() diff --git a/wasp/drivers/st7789.py b/wasp/drivers/st7789.py index e689e4e..817c414 100644 --- a/wasp/drivers/st7789.py +++ b/wasp/drivers/st7789.py @@ -44,7 +44,7 @@ class ST7789(object): """ self.width = width self.height = height - self.linebuffer = bytearray(2 * width) + self.linebuffer = memoryview(bytearray(2 * width)) self.init_display() def init_display(self): @@ -167,7 +167,7 @@ class ST7789(object): self.set_window(x, y, w, h) # Populate the line buffer - buf = memoryview(self.linebuffer)[0:2*w] + buf = self.linebuffer[0:2*w] for xi in range(0, 2*w, 2): buf[xi] = bg >> 8 buf[xi+1] = bg & 0xff