drivers: st7789: Pre-allocate a memoryview

Reduce the cost of slicing the linebuffer by pre-allocating a memoryview.

Signed-off-by: Daniel Thompson <daniel@redfelineninja.org.uk>
This commit is contained in:
Daniel Thompson 2021-01-17 17:44:21 +00:00
parent 5abae5a7f6
commit 6b41c8f3db
3 changed files with 9 additions and 9 deletions

View file

@ -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

View file

@ -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()

View file

@ -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