Skip to content

Commit 1224b6a

Browse files
committed
Limit the number of writes to improve performance
1 parent 7a3bdd0 commit 1224b6a

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

adafruit_sharpmemorydisplay.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
"""
2929
# pylint: enable=line-too-long
3030

31-
from micropython import const
3231
import adafruit_framebuf
3332
from adafruit_bus_device.spi_device import SPIDevice
33+
from micropython import const
3434

3535
try:
3636
import numpy
@@ -85,23 +85,26 @@ def show(self):
8585

8686
with self.spi_device as spi:
8787

88+
_imageBuf = bytearray()
8889
# toggle the VCOM bit
8990
self._buf[0] = _SHARPMEM_BIT_WRITECMD
9091
if self._vcom:
9192
self._buf[0] |= _SHARPMEM_BIT_VCOM
9293
self._vcom = not self._vcom
93-
spi.write(self._buf)
94+
_imageBuf.extend(self._buf)
9495

9596
slice_from = 0
9697
line_len = self.width // 8
9798
for line in range(self.height):
9899
self._buf[0] = reverse_bit(line + 1)
99-
spi.write(self._buf)
100-
spi.write(memoryview(self.buffer[slice_from : slice_from + line_len]))
100+
_imageBuf.extend(self._buf)
101+
_imageBuf.extend(
102+
self.buffer[slice_from: slice_from + line_len])
101103
slice_from += line_len
102104
self._buf[0] = 0
103-
spi.write(self._buf)
104-
spi.write(self._buf) # we send one last 0 byte
105+
_imageBuf.extend(self._buf)
106+
_imageBuf.extend(self._buf)
107+
spi.write(_imageBuf)
105108

106109
def image(self, img):
107110
"""Set buffer to value of Python Imaging Library image. The image should
@@ -135,7 +138,7 @@ def image(self, img):
135138
self.buf[i] = 0
136139
# Iterate through the pixels
137140
for x in range(width): # yes this double loop is slow,
138-
for y in range(height): # but these displays are small!
141+
for y in range(height): # but these displays are small!
139142
if img.mode == "RGB":
140143
self.pixel(x, y, pixels[(x, y)])
141144
elif pixels[(x, y)]:

0 commit comments

Comments
 (0)