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
try :
35
36
import numpy
@@ -62,11 +63,10 @@ class SharpMemoryDisplay(adafruit_framebuf.FrameBuffer):
62
63
# pylint: disable=too-many-instance-attributes,abstract-method
63
64
64
65
def __init__ (self , spi , scs_pin , width , height , * , baudrate = 2000000 ):
65
- self ._scs_pin = scs_pin
66
66
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
+ )
70
70
# prealloc for when we write the display
71
71
self ._buf = bytearray (1 )
72
72
@@ -80,34 +80,28 @@ def __init__(self, spi, scs_pin, width, height, *, baudrate=2000000):
80
80
81
81
def show (self ):
82
82
"""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
111
105
112
106
def image (self , img ):
113
107
"""Set buffer to value of Python Imaging Library image. The image should
0 commit comments