From fb32ff21f497dba4097cfe0090863cd2d3850098 Mon Sep 17 00:00:00 2001 From: tekktrik Date: Sat, 12 Mar 2022 18:18:07 -0500 Subject: [PATCH 1/2] Switch to bus device dependency --- README.rst | 1 + adafruit_sharpmemorydisplay.py | 54 +++++++++++++++------------------- requirements.txt | 1 + setup.py | 6 +++- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/README.rst b/README.rst index 6f160e3..2d2357b 100644 --- a/README.rst +++ b/README.rst @@ -20,6 +20,7 @@ Dependencies This driver depends on: * `Adafruit CircuitPython `_ +* `Bus Device `_ Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading diff --git a/adafruit_sharpmemorydisplay.py b/adafruit_sharpmemorydisplay.py index 34804c0..dfbdc1f 100644 --- a/adafruit_sharpmemorydisplay.py +++ b/adafruit_sharpmemorydisplay.py @@ -30,6 +30,7 @@ from micropython import const import adafruit_framebuf +from adafruit_bus_device.spi_device import SPIDevice __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SharpMemoryDisplay.git" @@ -57,11 +58,10 @@ class SharpMemoryDisplay(adafruit_framebuf.FrameBuffer): # pylint: disable=too-many-instance-attributes,abstract-method def __init__(self, spi, scs_pin, width, height, *, baudrate=2000000): - self._scs_pin = scs_pin scs_pin.switch_to_output(value=True) - self._baudrate = baudrate - # The SCS pin is active HIGH so we can't use bus_device. exciting! - self._spi = spi + self.spi_device = SPIDevice( + spi, scs_pin, cs_active_value=True, baudrate=baudrate + ) # prealloc for when we write the display self._buf = bytearray(1) @@ -75,31 +75,25 @@ def __init__(self, spi, scs_pin, width, height, *, baudrate=2000000): def show(self): """write out the frame buffer via SPI, we use MSB SPI only so some - bit-swapping is rquired. The display also uses inverted CS for some + bit-swapping is required. The display also uses inverted CS for some reason so we con't use bus_device""" - # CS pin is inverted so we have to do this all by hand - while not self._spi.try_lock(): - pass - self._spi.configure(baudrate=self._baudrate) - self._scs_pin.value = True - - # toggle the VCOM bit - self._buf[0] = _SHARPMEM_BIT_WRITECMD - if self._vcom: - self._buf[0] |= _SHARPMEM_BIT_VCOM - self._vcom = not self._vcom - self._spi.write(self._buf) - - slice_from = 0 - line_len = self.width // 8 - for line in range(self.height): - self._buf[0] = reverse_bit(line + 1) - self._spi.write(self._buf) - self._spi.write(memoryview(self.buffer[slice_from : slice_from + line_len])) - slice_from += line_len - self._buf[0] = 0 - self._spi.write(self._buf) - self._spi.write(self._buf) # we send one last 0 byte - self._scs_pin.value = False - self._spi.unlock() + with self.spi_device as spi: + + # toggle the VCOM bit + self._buf[0] = _SHARPMEM_BIT_WRITECMD + if self._vcom: + self._buf[0] |= _SHARPMEM_BIT_VCOM + self._vcom = not self._vcom + spi.write(self._buf) + + slice_from = 0 + line_len = self.width // 8 + for line in range(self.height): + self._buf[0] = reverse_bit(line + 1) + spi.write(self._buf) + spi.write(memoryview(self.buffer[slice_from : slice_from + line_len])) + slice_from += line_len + self._buf[0] = 0 + spi.write(self._buf) + spi.write(self._buf) # we send one last 0 byte diff --git a/requirements.txt b/requirements.txt index c4920b4..03ea9c6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ Adafruit-Blinka adafruit-circuitpython-framebuf +adafruit-circuitpython-busdevice diff --git a/setup.py b/setup.py index e30e438..44bbcf9 100644 --- a/setup.py +++ b/setup.py @@ -34,7 +34,11 @@ # Author details author="Adafruit Industries", author_email="circuitpython@adafruit.com", - install_requires=["Adafruit-Blinka", "adafruit-circuitpython-framebuf"], + install_requires=[ + "Adafruit-Blinka", + "adafruit-circuitpython-framebuf", + "adafruit-circuitpython-busdevice", + ], # Choose your license license="MIT", # See https://pypi.python.org/pypi?%3Aaction=list_classifiers From fae7deb8b85dfc615c408453586040057ffc57e2 Mon Sep 17 00:00:00 2001 From: tekktrik Date: Sat, 12 Mar 2022 18:20:46 -0500 Subject: [PATCH 2/2] Edit docstring to remove mention of not being able to use bus_device --- adafruit_sharpmemorydisplay.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_sharpmemorydisplay.py b/adafruit_sharpmemorydisplay.py index dfbdc1f..d639565 100644 --- a/adafruit_sharpmemorydisplay.py +++ b/adafruit_sharpmemorydisplay.py @@ -75,8 +75,8 @@ def __init__(self, spi, scs_pin, width, height, *, baudrate=2000000): def show(self): """write out the frame buffer via SPI, we use MSB SPI only so some - bit-swapping is required. The display also uses inverted CS for some - reason so we con't use bus_device""" + bit-swapping is required. + """ with self.spi_device as spi: