3333
3434class _SSD1306 :
3535 """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 ):
3737 self .framebuf = framebuffer
3838 self .width = width
3939 self .height = height
4040 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 )
4145 self .pages = self .height // 8
4246 # Note the subclass must initialize self.framebuf to a framebuffer.
4347 # This is necessary because the underlying data buffer is different
@@ -95,8 +99,14 @@ def write_cmd(self, cmd):
9599 raise NotImplementedError
96100
97101 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 )
100110
101111 def show (self ):
102112 """Update the display"""
@@ -139,9 +149,10 @@ class SSD1306_I2C(_SSD1306):
139149 :param i2c: the I2C peripheral to use,
140150 :param addr: the 8-bit bus address of the device,
141151 :param external_vcc: whether external high-voltage source is connected.
152+ :param reset: if needed, DigitalInOut designating reset pin
142153 """
143154
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 ):
145156 self .i2c_device = i2c_device .I2CDevice (i2c , addr )
146157 self .addr = addr
147158 self .temp = bytearray (2 )
@@ -153,7 +164,7 @@ def __init__(self, width, height, i2c, *, addr=0x3c, external_vcc=False):
153164 self .buffer = bytearray (((height // 8 ) * width ) + 1 )
154165 self .buffer [0 ] = 0x40 # Set first byte of data buffer to Co=0, D/C=1
155166 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 )
157168
158169 def write_cmd (self , cmd ):
159170 """Send a command to the SPI device"""
@@ -168,10 +179,6 @@ def write_framebuf(self):
168179 with self .i2c_device :
169180 self .i2c_device .write (self .buffer )
170181
171- def poweron (self ):
172- """Turn power on the device"""
173- self .write_cmd (SET_DISP | 0x01 )
174-
175182#pylint: disable-msg=too-many-arguments
176183class SSD1306_SPI (_SSD1306 ):
177184 """
@@ -181,21 +188,19 @@ class SSD1306_SPI(_SSD1306):
181188 :param height: the height of the physical screen in pixels,
182189 :param spi: the SPI peripheral to use,
183190 :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,
185192 :param cs: the chip-select pin to use (sometimes labeled "SS").
186193 """
187- def __init__ (self , width , height , spi , dc , res , cs , * ,
194+ def __init__ (self , width , height , spi , dc , reset , cs , * ,
188195 external_vcc = False , baudrate = 8000000 , polarity = 0 , phase = 0 ):
189196 self .rate = 10 * 1024 * 1024
190197 dc .switch_to_output (value = 0 )
191- res .switch_to_output (value = 0 )
192198 self .spi_device = spi_device .SPIDevice (spi , cs , baudrate = baudrate ,
193199 polarity = polarity , phase = phase )
194200 self .dc_pin = dc
195- self .reset_pin = res
196201 self .buffer = bytearray ((height // 8 ) * width )
197202 framebuffer = framebuf .FrameBuffer1 (self .buffer , width , height )
198- super ().__init__ (framebuffer , width , height , external_vcc )
203+ super ().__init__ (framebuffer , width , height , external_vcc , reset )
199204
200205 def write_cmd (self , cmd ):
201206 """Send a command to the SPI device"""
@@ -208,11 +213,3 @@ def write_framebuf(self):
208213 self .dc_pin .value = 1
209214 with self .spi_device as spi :
210215 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