Skip to content

Commit e1f8e38

Browse files
authored
Merge pull request #21 from tekktrik/dev/switch-bus-device
Switch to adafruit_bus_device dependency
2 parents 1e77578 + 25cc737 commit e1f8e38

File tree

4 files changed

+33
-33
lines changed

4 files changed

+33
-33
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Dependencies
2020
This driver depends on:
2121

2222
* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
23+
* `Bus Device <https://github.com/adafruit/Adafruit_CircuitPython_BusDevice>`_
2324

2425
Please ensure all dependencies are available on the CircuitPython filesystem.
2526
This is easily achieved by downloading

adafruit_sharpmemorydisplay.py

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
from micropython import const
3232
import adafruit_framebuf
33+
from adafruit_bus_device.spi_device import SPIDevice
3334

3435
try:
3536
import numpy
@@ -62,11 +63,10 @@ class SharpMemoryDisplay(adafruit_framebuf.FrameBuffer):
6263
# pylint: disable=too-many-instance-attributes,abstract-method
6364

6465
def __init__(self, spi, scs_pin, width, height, *, baudrate=2000000):
65-
self._scs_pin = scs_pin
6666
scs_pin.switch_to_output(value=True)
67-
self._baudrate = baudrate
68-
# The SCS pin is active HIGH so we can't use bus_device. exciting!
69-
self._spi = spi
67+
self.spi_device = SPIDevice(
68+
spi, scs_pin, cs_active_value=True, baudrate=baudrate
69+
)
7070
# prealloc for when we write the display
7171
self._buf = bytearray(1)
7272

@@ -80,34 +80,28 @@ def __init__(self, spi, scs_pin, width, height, *, baudrate=2000000):
8080

8181
def show(self):
8282
"""write out the frame buffer via SPI, we use MSB SPI only so some
83-
bit-swapping is rquired. The display also uses inverted CS for some
84-
reason so we con't use bus_device"""
85-
86-
# CS pin is inverted so we have to do this all by hand
87-
while not self._spi.try_lock():
88-
pass
89-
self._spi.configure(baudrate=self._baudrate)
90-
self._scs_pin.value = True
91-
92-
# toggle the VCOM bit
93-
self._buf[0] = _SHARPMEM_BIT_WRITECMD
94-
if self._vcom:
95-
self._buf[0] |= _SHARPMEM_BIT_VCOM
96-
self._vcom = not self._vcom
97-
self._spi.write(self._buf)
98-
99-
slice_from = 0
100-
line_len = self.width // 8
101-
for line in range(self.height):
102-
self._buf[0] = reverse_bit(line + 1)
103-
self._spi.write(self._buf)
104-
self._spi.write(memoryview(self.buffer[slice_from : slice_from + line_len]))
105-
slice_from += line_len
106-
self._buf[0] = 0
107-
self._spi.write(self._buf)
108-
self._spi.write(self._buf) # we send one last 0 byte
109-
self._scs_pin.value = False
110-
self._spi.unlock()
83+
bit-swapping is required.
84+
"""
85+
86+
with self.spi_device as spi:
87+
88+
# toggle the VCOM bit
89+
self._buf[0] = _SHARPMEM_BIT_WRITECMD
90+
if self._vcom:
91+
self._buf[0] |= _SHARPMEM_BIT_VCOM
92+
self._vcom = not self._vcom
93+
spi.write(self._buf)
94+
95+
slice_from = 0
96+
line_len = self.width // 8
97+
for line in range(self.height):
98+
self._buf[0] = reverse_bit(line + 1)
99+
spi.write(self._buf)
100+
spi.write(memoryview(self.buffer[slice_from : slice_from + line_len]))
101+
slice_from += line_len
102+
self._buf[0] = 0
103+
spi.write(self._buf)
104+
spi.write(self._buf) # we send one last 0 byte
111105

112106
def image(self, img):
113107
"""Set buffer to value of Python Imaging Library image. The image should

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
Adafruit-Blinka
66
adafruit-circuitpython-framebuf
7+
adafruit-circuitpython-busdevice

setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@
3434
# Author details
3535
author="Adafruit Industries",
3636
author_email="[email protected]",
37-
install_requires=["Adafruit-Blinka", "adafruit-circuitpython-framebuf"],
37+
install_requires=[
38+
"Adafruit-Blinka",
39+
"adafruit-circuitpython-framebuf",
40+
"adafruit-circuitpython-busdevice",
41+
],
3842
# Choose your license
3943
license="MIT",
4044
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers

0 commit comments

Comments
 (0)