22
22
"""
23
23
`adafruit_ssd1306`
24
24
====================================================
25
+
25
26
MicroPython SSD1306 OLED driver, I2C and SPI interfaces
27
+
26
28
* Author(s): Tony DiCola, Michael McWethy
27
29
"""
30
+
28
31
import time
32
+
29
33
from micropython import const
30
34
from adafruit_bus_device import i2c_device , spi_device
31
35
try :
32
36
import framebuf
33
37
except ImportError :
34
38
import adafruit_framebuf as framebuf
39
+
35
40
__version__ = "0.0.0-auto.0"
36
41
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SSD1306.git"
42
+
37
43
#pylint: disable-msg=bad-whitespace
38
44
# register definitions
39
45
SET_CONTRAST = const (0x81 )
54
60
SET_VCOM_DESEL = const (0xdb )
55
61
SET_CHARGE_PUMP = const (0x8d )
56
62
#pylint: enable-msg=bad-whitespace
63
+
64
+
57
65
class _SSD1306 (framebuf .FrameBuffer ):
58
66
"""Base class for SSD1306 display driver"""
59
67
#pylint: disable-msg=too-many-arguments
@@ -73,10 +81,12 @@ def __init__(self, buffer, width, height, *, external_vcc, reset):
73
81
self ._power = False
74
82
self .poweron ()
75
83
self .init_display ()
84
+
76
85
@property
77
86
def power (self ):
78
87
"""True if the display is currently powered on, otherwise False"""
79
88
return self ._power
89
+
80
90
def init_display (self ):
81
91
"""Base class to initialize display"""
82
92
for cmd in (
@@ -107,23 +117,29 @@ def init_display(self):
107
117
self .write_cmd (0x30 )
108
118
self .fill (0 )
109
119
self .show ()
120
+
110
121
def poweroff (self ):
111
122
"""Turn off the display (nothing visible)"""
112
123
self .write_cmd (SET_DISP | 0x00 )
113
124
self ._power = False
125
+
114
126
def contrast (self , contrast ):
115
127
"""Adjust the contrast"""
116
128
self .write_cmd (SET_CONTRAST )
117
129
self .write_cmd (contrast )
130
+
118
131
def invert (self , invert ):
119
132
"""Invert all pixels on the display"""
120
133
self .write_cmd (SET_NORM_INV | (invert & 1 ))
134
+
121
135
def write_framebuf (self ):
122
136
"""Derived class must implement this"""
123
137
raise NotImplementedError
138
+
124
139
def write_cmd (self , cmd ):
125
140
"""Derived class must implement this"""
126
141
raise NotImplementedError
142
+
127
143
def poweron (self ):
128
144
"Reset device and turn on the display."
129
145
if self .reset_pin :
@@ -135,6 +151,7 @@ def poweron(self):
135
151
time .sleep (0.010 )
136
152
self .write_cmd (SET_DISP | 0x01 )
137
153
self ._power = True
154
+
138
155
def show (self ):
139
156
"""Update the display"""
140
157
xpos0 = 0
@@ -154,16 +171,19 @@ def show(self):
154
171
self .write_cmd (0 )
155
172
self .write_cmd (self .pages - 1 )
156
173
self .write_framebuf ()
174
+
157
175
class SSD1306_I2C (_SSD1306 ):
158
176
"""
159
177
I2C class for SSD1306
178
+
160
179
:param width: the width of the physical screen in pixels,
161
180
:param height: the height of the physical screen in pixels,
162
181
:param i2c: the I2C peripheral to use,
163
182
:param addr: the 8-bit bus address of the device,
164
183
:param external_vcc: whether external high-voltage source is connected.
165
184
:param reset: if needed, DigitalInOut designating reset pin
166
185
"""
186
+
167
187
def __init__ (self , width , height , i2c , * , addr = 0x3c , external_vcc = False , reset = None ):
168
188
self .i2c_device = i2c_device .I2CDevice (i2c , addr )
169
189
self .addr = addr
@@ -177,21 +197,25 @@ def __init__(self, width, height, i2c, *, addr=0x3c, external_vcc=False, reset=N
177
197
self .buffer [0 ] = 0x40 # Set first byte of data buffer to Co=0, D/C=1
178
198
super ().__init__ (memoryview (self .buffer )[1 :], width , height ,
179
199
external_vcc = external_vcc , reset = reset )
200
+
180
201
def write_cmd (self , cmd ):
181
202
"""Send a command to the SPI device"""
182
203
self .temp [0 ] = 0x80 # Co=1, D/C#=0
183
204
self .temp [1 ] = cmd
184
205
with self .i2c_device :
185
206
self .i2c_device .write (self .temp )
207
+
186
208
def write_framebuf (self ):
187
209
"""Blast out the frame buffer using a single I2C transaction to support
188
210
hardware I2C interfaces."""
189
211
with self .i2c_device :
190
212
self .i2c_device .write (self .buffer )
213
+
191
214
#pylint: disable-msg=too-many-arguments
192
215
class SSD1306_SPI (_SSD1306 ):
193
216
"""
194
217
SPI class for SSD1306
218
+
195
219
:param width: the width of the physical screen in pixels,
196
220
:param height: the height of the physical screen in pixels,
197
221
:param spi: the SPI peripheral to use,
@@ -211,12 +235,15 @@ def __init__(self, width, height, spi, dc, reset, cs, *,
211
235
self .buffer = bytearray ((height // 8 ) * width )
212
236
super ().__init__ (memoryview (self .buffer ), width , height ,
213
237
external_vcc = external_vcc , reset = reset )
238
+
214
239
def write_cmd (self , cmd ):
215
240
"""Send a command to the SPI device"""
216
241
self .dc_pin .value = 0
217
242
with self .spi_device as spi :
218
243
spi .write (bytearray ([cmd ]))
244
+
219
245
def write_framebuf (self ):
220
246
"""write to the frame buffer via SPI"""
221
247
self .dc_pin .value = 1
222
- with self .spi_device as spi :
248
+ with self .spi_device as spi :
249
+ spi .write (self .buffer )
0 commit comments