21
21
except ImportError :
22
22
import adafruit_framebuf as framebuf
23
23
24
+ try :
25
+ # Used only for typing
26
+ from typing import Optional
27
+ import busio
28
+ import digitalio
29
+ except ImportError :
30
+ pass
31
+
24
32
__version__ = "0.0.0-auto.0"
25
33
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SSD1306.git"
26
34
@@ -49,7 +57,16 @@ class _SSD1306(framebuf.FrameBuffer):
49
57
"""Base class for SSD1306 display driver"""
50
58
51
59
# pylint: disable-msg=too-many-arguments
52
- def __init__ (self , buffer , width , height , * , external_vcc , reset , page_addressing ):
60
+ def __init__ (
61
+ self ,
62
+ buffer : memoryview ,
63
+ width : int ,
64
+ height : int ,
65
+ * ,
66
+ external_vcc : bool ,
67
+ reset : Optional [digitalio .DigitalInOut ],
68
+ page_addressing : bool
69
+ ):
53
70
super ().__init__ (buffer , width , height )
54
71
self .width = width
55
72
self .height = height
@@ -67,9 +84,9 @@ def __init__(self, buffer, width, height, *, external_vcc, reset, page_addressin
67
84
# Parameters for efficient Page Addressing Mode (typical of U8Glib libraries)
68
85
# Important as not all screens appear to support Horizontal Addressing Mode
69
86
if self .page_addressing :
70
- self .pagebuffer = bytearray (width + 1 )
87
+ self .pagebuffer = bytearray (width + 1 ) # type: Optional[bytearray]
71
88
self .pagebuffer [0 ] = 0x40 # Set first byte of data buffer to Co=0, D/C=1
72
- self .page_column_start = bytearray (2 )
89
+ self .page_column_start = bytearray (2 ) # type: Optional[bytearray]
73
90
self .page_column_start [0 ] = self .width % 32
74
91
self .page_column_start [1 ] = 0x10 + self .width // 32
75
92
else :
@@ -80,11 +97,11 @@ def __init__(self, buffer, width, height, *, external_vcc, reset, page_addressin
80
97
self .init_display ()
81
98
82
99
@property
83
- def power (self ):
100
+ def power (self ) -> bool :
84
101
"""True if the display is currently powered on, otherwise False"""
85
102
return self ._power
86
103
87
- def init_display (self ):
104
+ def init_display (self ) -> None :
88
105
"""Base class to initialize display"""
89
106
# The various screen sizes available with the ssd1306 OLED driver
90
107
# chip require differing configuration values for the display clock
@@ -136,36 +153,36 @@ def init_display(self):
136
153
self .fill (0 )
137
154
self .show ()
138
155
139
- def poweroff (self ):
156
+ def poweroff (self ) -> None :
140
157
"""Turn off the display (nothing visible)"""
141
158
self .write_cmd (SET_DISP )
142
159
self ._power = False
143
160
144
- def contrast (self , contrast ) :
161
+ def contrast (self , contrast : int ) -> None :
145
162
"""Adjust the contrast"""
146
163
self .write_cmd (SET_CONTRAST )
147
164
self .write_cmd (contrast )
148
165
149
- def invert (self , invert ) :
166
+ def invert (self , invert : bool ) -> None :
150
167
"""Invert all pixels on the display"""
151
168
self .write_cmd (SET_NORM_INV | (invert & 1 ))
152
169
153
- def rotate (self , rotate ) :
170
+ def rotate (self , rotate : bool ) -> None :
154
171
"""Rotate the display 0 or 180 degrees"""
155
172
self .write_cmd (SET_COM_OUT_DIR | ((rotate & 1 ) << 3 ))
156
173
self .write_cmd (SET_SEG_REMAP | (rotate & 1 ))
157
174
# com output (vertical mirror) is changed immediately
158
175
# you need to call show() for the seg remap to be visible
159
176
160
- def write_framebuf (self ):
177
+ def write_framebuf (self ) -> None :
161
178
"""Derived class must implement this"""
162
179
raise NotImplementedError
163
180
164
- def write_cmd (self , cmd ) :
181
+ def write_cmd (self , cmd : int ) -> None :
165
182
"""Derived class must implement this"""
166
183
raise NotImplementedError
167
184
168
- def poweron (self ):
185
+ def poweron (self ) -> None :
169
186
"Reset device and turn on the display."
170
187
if self .reset_pin :
171
188
self .reset_pin .value = 1
@@ -177,7 +194,7 @@ def poweron(self):
177
194
self .write_cmd (SET_DISP | 0x01 )
178
195
self ._power = True
179
196
180
- def show (self ):
197
+ def show (self ) -> None :
181
198
"""Update the display"""
182
199
if not self .page_addressing :
183
200
xpos0 = 0
@@ -210,14 +227,14 @@ class SSD1306_I2C(_SSD1306):
210
227
211
228
def __init__ (
212
229
self ,
213
- width ,
214
- height ,
215
- i2c ,
230
+ width : int ,
231
+ height : int ,
232
+ i2c : busio . I2C ,
216
233
* ,
217
- addr = 0x3C ,
218
- external_vcc = False ,
219
- reset = None ,
220
- page_addressing = False
234
+ addr : int = 0x3C ,
235
+ external_vcc : bool = False ,
236
+ reset : Optional [ digitalio . DigitalInOut ] = None ,
237
+ page_addressing : bool = False
221
238
):
222
239
self .i2c_device = i2c_device .I2CDevice (i2c , addr )
223
240
self .addr = addr
@@ -239,14 +256,14 @@ def __init__(
239
256
page_addressing = self .page_addressing ,
240
257
)
241
258
242
- def write_cmd (self , cmd ) :
259
+ def write_cmd (self , cmd : int ) -> None :
243
260
"""Send a command to the I2C device"""
244
261
self .temp [0 ] = 0x80 # Co=1, D/C#=0
245
262
self .temp [1 ] = cmd
246
263
with self .i2c_device :
247
264
self .i2c_device .write (self .temp )
248
265
249
- def write_framebuf (self ):
266
+ def write_framebuf (self ) -> None :
250
267
"""Blast out the frame buffer using a single I2C transaction to support
251
268
hardware I2C interfaces."""
252
269
if self .page_addressing :
@@ -281,18 +298,18 @@ class SSD1306_SPI(_SSD1306):
281
298
# Disable should be reconsidered when refactor can be tested.
282
299
def __init__ (
283
300
self ,
284
- width ,
285
- height ,
286
- spi ,
287
- dc ,
288
- reset ,
289
- cs ,
301
+ width : int ,
302
+ height : int ,
303
+ spi : busio . SPI ,
304
+ dc : digitalio . DigitalInOut ,
305
+ reset : Optional [ digitalio . DigitalInOut ] ,
306
+ cs : digitalio . DigitalInOut ,
290
307
* ,
291
- external_vcc = False ,
292
- baudrate = 8000000 ,
293
- polarity = 0 ,
294
- phase = 0 ,
295
- page_addressing = False
308
+ external_vcc : bool = False ,
309
+ baudrate : int = 8000000 ,
310
+ polarity : int = 0 ,
311
+ phase : int = 0 ,
312
+ page_addressing : bool = False
296
313
):
297
314
self .page_addressing = page_addressing
298
315
if self .page_addressing :
@@ -316,13 +333,13 @@ def __init__(
316
333
page_addressing = self .page_addressing ,
317
334
)
318
335
319
- def write_cmd (self , cmd ) :
336
+ def write_cmd (self , cmd : int ) -> None :
320
337
"""Send a command to the SPI device"""
321
338
self .dc_pin .value = 0
322
339
with self .spi_device as spi :
323
340
spi .write (bytearray ([cmd ]))
324
341
325
- def write_framebuf (self ):
342
+ def write_framebuf (self ) -> None :
326
343
"""write to the frame buffer via SPI"""
327
344
self .dc_pin .value = 1
328
345
with self .spi_device as spi :
0 commit comments