30
30
31
31
from micropython import const
32
32
import adafruit_framebuf
33
+ from adafruit_bus_device .spi_device import SPIDevice
33
34
34
35
__version__ = "0.0.0-auto.0"
35
36
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SharpMemoryDisplay.git"
@@ -57,11 +58,10 @@ class SharpMemoryDisplay(adafruit_framebuf.FrameBuffer):
57
58
# pylint: disable=too-many-instance-attributes,abstract-method
58
59
59
60
def __init__ (self , spi , scs_pin , width , height , * , baudrate = 2000000 ):
60
- self ._scs_pin = scs_pin
61
61
scs_pin .switch_to_output (value = True )
62
- self ._baudrate = baudrate
63
- # The SCS pin is active HIGH so we can't use bus_device. exciting!
64
- self . _spi = spi
62
+ self .spi_device = SPIDevice (
63
+ spi , scs_pin , cs_active_value = True , baudrate = baudrate
64
+ )
65
65
# prealloc for when we write the display
66
66
self ._buf = bytearray (1 )
67
67
@@ -75,31 +75,25 @@ def __init__(self, spi, scs_pin, width, height, *, baudrate=2000000):
75
75
76
76
def show (self ):
77
77
"""write out the frame buffer via SPI, we use MSB SPI only so some
78
- bit-swapping is rquired . The display also uses inverted CS for some
78
+ bit-swapping is required . The display also uses inverted CS for some
79
79
reason so we con't use bus_device"""
80
80
81
- # CS pin is inverted so we have to do this all by hand
82
- while not self ._spi .try_lock ():
83
- pass
84
- self ._spi .configure (baudrate = self ._baudrate )
85
- self ._scs_pin .value = True
86
-
87
- # toggle the VCOM bit
88
- self ._buf [0 ] = _SHARPMEM_BIT_WRITECMD
89
- if self ._vcom :
90
- self ._buf [0 ] |= _SHARPMEM_BIT_VCOM
91
- self ._vcom = not self ._vcom
92
- self ._spi .write (self ._buf )
93
-
94
- slice_from = 0
95
- line_len = self .width // 8
96
- for line in range (self .height ):
97
- self ._buf [0 ] = reverse_bit (line + 1 )
98
- self ._spi .write (self ._buf )
99
- self ._spi .write (memoryview (self .buffer [slice_from : slice_from + line_len ]))
100
- slice_from += line_len
101
- self ._buf [0 ] = 0
102
- self ._spi .write (self ._buf )
103
- self ._spi .write (self ._buf ) # we send one last 0 byte
104
- self ._scs_pin .value = False
105
- self ._spi .unlock ()
81
+ with self .spi_device as spi :
82
+
83
+ # toggle the VCOM bit
84
+ self ._buf [0 ] = _SHARPMEM_BIT_WRITECMD
85
+ if self ._vcom :
86
+ self ._buf [0 ] |= _SHARPMEM_BIT_VCOM
87
+ self ._vcom = not self ._vcom
88
+ spi .write (self ._buf )
89
+
90
+ slice_from = 0
91
+ line_len = self .width // 8
92
+ for line in range (self .height ):
93
+ self ._buf [0 ] = reverse_bit (line + 1 )
94
+ spi .write (self ._buf )
95
+ spi .write (memoryview (self .buffer [slice_from : slice_from + line_len ]))
96
+ slice_from += line_len
97
+ self ._buf [0 ] = 0
98
+ spi .write (self ._buf )
99
+ spi .write (self ._buf ) # we send one last 0 byte
0 commit comments