33
33
34
34
class _SSD1306 :
35
35
"""Base class for SSD1306 display driver"""
36
- def __init__ (self , framebuffer , width , height , external_vcc ):
36
+ #pylint: disable-msg=too-many-arguments
37
+ def __init__ (self , framebuffer , width , height , external_vcc , reset ):
37
38
self .framebuf = framebuffer
38
39
self .width = width
39
40
self .height = height
40
41
self .external_vcc = external_vcc
42
+ # reset may be None if not needed
43
+ self .reset_pin = reset
44
+ if self .reset_pin :
45
+ self .reset_pin .switch_to_output (value = 0 )
41
46
self .pages = self .height // 8
42
47
# Note the subclass must initialize self.framebuf to a framebuffer.
43
48
# This is necessary because the underlying data buffer is different
@@ -74,7 +79,7 @@ def init_display(self):
74
79
self .show ()
75
80
76
81
def poweroff (self ):
77
- """Turn the device Power off """
82
+ """Turn off the display (nothing visible) """
78
83
self .write_cmd (SET_DISP | 0x00 )
79
84
80
85
def contrast (self , contrast ):
@@ -83,7 +88,7 @@ def contrast(self, contrast):
83
88
self .write_cmd (contrast )
84
89
85
90
def invert (self , invert ):
86
- """Invert the pixels on the display"""
91
+ """Invert all pixels on the display"""
87
92
self .write_cmd (SET_NORM_INV | (invert & 1 ))
88
93
89
94
def write_framebuf (self ):
@@ -95,8 +100,15 @@ def write_cmd(self, cmd):
95
100
raise NotImplementedError
96
101
97
102
def poweron (self ):
98
- """Derived class must implement this"""
99
- raise NotImplementedError
103
+ "Reset device and turn on the display."
104
+ if self .reset_pin :
105
+ self .reset_pin .value = 1
106
+ time .sleep (0.001 )
107
+ self .reset_pin .value = 0
108
+ time .sleep (0.010 )
109
+ self .reset_pin .value = 1
110
+ time .sleep (0.010 )
111
+ self .write_cmd (SET_DISP | 0x01 )
100
112
101
113
def show (self ):
102
114
"""Update the display"""
@@ -115,7 +127,7 @@ def show(self):
115
127
self .write_framebuf ()
116
128
117
129
def fill (self , value ):
118
- """Fill the display on or off """
130
+ """Fill the display with all ones or zeros. """
119
131
self .framebuf .fill (value )
120
132
121
133
def pixel (self , xpos , ypos , value ):
@@ -139,9 +151,10 @@ class SSD1306_I2C(_SSD1306):
139
151
:param i2c: the I2C peripheral to use,
140
152
:param addr: the 8-bit bus address of the device,
141
153
:param external_vcc: whether external high-voltage source is connected.
154
+ :param reset: if needed, DigitalInOut designating reset pin
142
155
"""
143
156
144
- def __init__ (self , width , height , i2c , * , addr = 0x3c , external_vcc = False ):
157
+ def __init__ (self , width , height , i2c , * , addr = 0x3c , external_vcc = False , reset = None ):
145
158
self .i2c_device = i2c_device .I2CDevice (i2c , addr )
146
159
self .addr = addr
147
160
self .temp = bytearray (2 )
@@ -153,7 +166,7 @@ def __init__(self, width, height, i2c, *, addr=0x3c, external_vcc=False):
153
166
self .buffer = bytearray (((height // 8 ) * width ) + 1 )
154
167
self .buffer [0 ] = 0x40 # Set first byte of data buffer to Co=0, D/C=1
155
168
framebuffer = framebuf .FrameBuffer1 (memoryview (self .buffer )[1 :], width , height )
156
- super ().__init__ (framebuffer , width , height , external_vcc )
169
+ super ().__init__ (framebuffer , width , height , external_vcc , reset )
157
170
158
171
def write_cmd (self , cmd ):
159
172
"""Send a command to the SPI device"""
@@ -168,10 +181,6 @@ def write_framebuf(self):
168
181
with self .i2c_device :
169
182
self .i2c_device .write (self .buffer )
170
183
171
- def poweron (self ):
172
- """Turn power on the device"""
173
- self .write_cmd (SET_DISP | 0x01 )
174
-
175
184
#pylint: disable-msg=too-many-arguments
176
185
class SSD1306_SPI (_SSD1306 ):
177
186
"""
@@ -181,21 +190,19 @@ class SSD1306_SPI(_SSD1306):
181
190
:param height: the height of the physical screen in pixels,
182
191
:param spi: the SPI peripheral to use,
183
192
:param dc: the data/command pin to use (often labeled "D/C"),
184
- :param res : the reset pin to use,
193
+ :param reset : the reset pin to use,
185
194
:param cs: the chip-select pin to use (sometimes labeled "SS").
186
195
"""
187
- def __init__ (self , width , height , spi , dc , res , cs , * ,
196
+ def __init__ (self , width , height , spi , dc , reset , cs , * ,
188
197
external_vcc = False , baudrate = 8000000 , polarity = 0 , phase = 0 ):
189
198
self .rate = 10 * 1024 * 1024
190
199
dc .switch_to_output (value = 0 )
191
- res .switch_to_output (value = 0 )
192
200
self .spi_device = spi_device .SPIDevice (spi , cs , baudrate = baudrate ,
193
201
polarity = polarity , phase = phase )
194
202
self .dc_pin = dc
195
- self .reset_pin = res
196
203
self .buffer = bytearray ((height // 8 ) * width )
197
204
framebuffer = framebuf .FrameBuffer1 (self .buffer , width , height )
198
- super ().__init__ (framebuffer , width , height , external_vcc )
205
+ super ().__init__ (framebuffer , width , height , external_vcc , reset )
199
206
200
207
def write_cmd (self , cmd ):
201
208
"""Send a command to the SPI device"""
@@ -208,11 +215,3 @@ def write_framebuf(self):
208
215
self .dc_pin .value = 1
209
216
with self .spi_device as spi :
210
217
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