33
33
34
34
class _SSD1306 :
35
35
"""Base class for SSD1306 display driver"""
36
- def __init__ (self , framebuffer , width , height , external_vcc ):
36
+ def __init__ (self , framebuffer , width , height , external_vcc , reset ):
37
37
self .framebuf = framebuffer
38
38
self .width = width
39
39
self .height = height
40
40
self .external_vcc = external_vcc
41
+ # reset may be None if not needed
42
+ self .reset_pin = reset
43
+ if self .reset_pin :
44
+ self .reset_pin .switch_to_output (value = 0 )
41
45
self .pages = self .height // 8
42
46
# Note the subclass must initialize self.framebuf to a framebuffer.
43
47
# This is necessary because the underlying data buffer is different
@@ -95,8 +99,14 @@ def write_cmd(self, cmd):
95
99
raise NotImplementedError
96
100
97
101
def poweron (self ):
98
- """Derived class must implement this"""
99
- raise NotImplementedError
102
+ if self .reset_pin :
103
+ self .reset_pin .value = 1
104
+ time .sleep (0.001 )
105
+ self .reset_pin .value = 0
106
+ time .sleep (0.010 )
107
+ self .reset_pin .value = 1
108
+ time .sleep (0.010 )
109
+ self .write_cmd (SET_DISP | 0x01 )
100
110
101
111
def show (self ):
102
112
"""Update the display"""
@@ -139,9 +149,10 @@ class SSD1306_I2C(_SSD1306):
139
149
:param i2c: the I2C peripheral to use,
140
150
:param addr: the 8-bit bus address of the device,
141
151
:param external_vcc: whether external high-voltage source is connected.
152
+ :param reset: if needed, DigitalInOut designating reset pin
142
153
"""
143
154
144
- def __init__ (self , width , height , i2c , * , addr = 0x3c , external_vcc = False ):
155
+ def __init__ (self , width , height , i2c , * , addr = 0x3c , external_vcc = False , reset = None ):
145
156
self .i2c_device = i2c_device .I2CDevice (i2c , addr )
146
157
self .addr = addr
147
158
self .temp = bytearray (2 )
@@ -153,7 +164,7 @@ def __init__(self, width, height, i2c, *, addr=0x3c, external_vcc=False):
153
164
self .buffer = bytearray (((height // 8 ) * width ) + 1 )
154
165
self .buffer [0 ] = 0x40 # Set first byte of data buffer to Co=0, D/C=1
155
166
framebuffer = framebuf .FrameBuffer1 (memoryview (self .buffer )[1 :], width , height )
156
- super ().__init__ (framebuffer , width , height , external_vcc )
167
+ super ().__init__ (framebuffer , width , height , external_vcc , reset )
157
168
158
169
def write_cmd (self , cmd ):
159
170
"""Send a command to the SPI device"""
@@ -168,10 +179,6 @@ def write_framebuf(self):
168
179
with self .i2c_device :
169
180
self .i2c_device .write (self .buffer )
170
181
171
- def poweron (self ):
172
- """Turn power on the device"""
173
- self .write_cmd (SET_DISP | 0x01 )
174
-
175
182
#pylint: disable-msg=too-many-arguments
176
183
class SSD1306_SPI (_SSD1306 ):
177
184
"""
@@ -181,21 +188,19 @@ class SSD1306_SPI(_SSD1306):
181
188
:param height: the height of the physical screen in pixels,
182
189
:param spi: the SPI peripheral to use,
183
190
:param dc: the data/command pin to use (often labeled "D/C"),
184
- :param res : the reset pin to use,
191
+ :param reset : the reset pin to use,
185
192
:param cs: the chip-select pin to use (sometimes labeled "SS").
186
193
"""
187
- def __init__ (self , width , height , spi , dc , res , cs , * ,
194
+ def __init__ (self , width , height , spi , dc , reset , cs , * ,
188
195
external_vcc = False , baudrate = 8000000 , polarity = 0 , phase = 0 ):
189
196
self .rate = 10 * 1024 * 1024
190
197
dc .switch_to_output (value = 0 )
191
- res .switch_to_output (value = 0 )
192
198
self .spi_device = spi_device .SPIDevice (spi , cs , baudrate = baudrate ,
193
199
polarity = polarity , phase = phase )
194
200
self .dc_pin = dc
195
- self .reset_pin = res
196
201
self .buffer = bytearray ((height // 8 ) * width )
197
202
framebuffer = framebuf .FrameBuffer1 (self .buffer , width , height )
198
- super ().__init__ (framebuffer , width , height , external_vcc )
203
+ super ().__init__ (framebuffer , width , height , external_vcc , reset )
199
204
200
205
def write_cmd (self , cmd ):
201
206
"""Send a command to the SPI device"""
@@ -208,11 +213,3 @@ def write_framebuf(self):
208
213
self .dc_pin .value = 1
209
214
with self .spi_device as spi :
210
215
spi .write (self .buffer )
211
-
212
- def poweron (self ):
213
- """Turn power off on the device"""
214
- self .reset_pin .value = 1
215
- time .sleep (0.001 )
216
- self .reset_pin .value = 0
217
- time .sleep (0.010 )
218
- self .reset_pin .value = 1
0 commit comments