32
32
33
33
from micropython import const
34
34
from adafruit_bus_device import i2c_device , spi_device
35
+
35
36
try :
36
37
import framebuf
37
38
except ImportError :
40
41
__version__ = "0.0.0-auto.0"
41
42
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SSD1306.git"
42
43
43
- #pylint: disable-msg=bad-whitespace
44
+ # pylint: disable-msg=bad-whitespace
44
45
# register definitions
45
- SET_CONTRAST = const (0x81 )
46
- SET_ENTIRE_ON = const (0xa4 )
47
- SET_NORM_INV = const (0xa6 )
48
- SET_DISP = const (0xae )
49
- SET_MEM_ADDR = const (0x20 )
50
- SET_COL_ADDR = const (0x21 )
51
- SET_PAGE_ADDR = const (0x22 )
46
+ SET_CONTRAST = const (0x81 )
47
+ SET_ENTIRE_ON = const (0xA4 )
48
+ SET_NORM_INV = const (0xA6 )
49
+ SET_DISP = const (0xAE )
50
+ SET_MEM_ADDR = const (0x20 )
51
+ SET_COL_ADDR = const (0x21 )
52
+ SET_PAGE_ADDR = const (0x22 )
52
53
SET_DISP_START_LINE = const (0x40 )
53
- SET_SEG_REMAP = const (0xa0 )
54
- SET_MUX_RATIO = const (0xa8 )
55
- SET_COM_OUT_DIR = const (0xc0 )
56
- SET_DISP_OFFSET = const (0xd3 )
57
- SET_COM_PIN_CFG = const (0xda )
58
- SET_DISP_CLK_DIV = const (0xd5 )
59
- SET_PRECHARGE = const (0xd9 )
60
- SET_VCOM_DESEL = const (0xdb )
61
- SET_CHARGE_PUMP = const (0x8d )
62
- #pylint: enable-msg=bad-whitespace
54
+ SET_SEG_REMAP = const (0xA0 )
55
+ SET_MUX_RATIO = const (0xA8 )
56
+ SET_COM_OUT_DIR = const (0xC0 )
57
+ SET_DISP_OFFSET = const (0xD3 )
58
+ SET_COM_PIN_CFG = const (0xDA )
59
+ SET_DISP_CLK_DIV = const (0xD5 )
60
+ SET_PRECHARGE = const (0xD9 )
61
+ SET_VCOM_DESEL = const (0xDB )
62
+ SET_CHARGE_PUMP = const (0x8D )
63
+ # pylint: enable-msg=bad-whitespace
63
64
64
65
65
66
class _SSD1306 (framebuf .FrameBuffer ):
66
67
"""Base class for SSD1306 display driver"""
67
- #pylint: disable-msg=too-many-arguments
68
+
69
+ # pylint: disable-msg=too-many-arguments
68
70
def __init__ (self , buffer , width , height , * , external_vcc , reset ):
69
71
super ().__init__ (buffer , width , height )
70
72
self .width = width
@@ -90,27 +92,37 @@ def power(self):
90
92
def init_display (self ):
91
93
"""Base class to initialize display"""
92
94
for cmd in (
93
- SET_DISP | 0x00 , # off
94
- # address setting
95
- SET_MEM_ADDR , 0x00 , # horizontal
96
- # resolution and layout
97
- SET_DISP_START_LINE | 0x00 ,
98
- SET_SEG_REMAP | 0x01 , # column addr 127 mapped to SEG0
99
- SET_MUX_RATIO , self .height - 1 ,
100
- SET_COM_OUT_DIR | 0x08 , # scan from COM[N] to COM0
101
- SET_DISP_OFFSET , 0x00 ,
102
- SET_COM_PIN_CFG , 0x02 if self .height == 32 or self .height == 16 else 0x12 ,
103
- # timing and driving scheme
104
- SET_DISP_CLK_DIV , 0x80 ,
105
- SET_PRECHARGE , 0x22 if self .external_vcc else 0xf1 ,
106
- SET_VCOM_DESEL , 0x30 , # 0.83*Vcc
107
- # display
108
- SET_CONTRAST , 0xff , # maximum
109
- SET_ENTIRE_ON , # output follows RAM contents
110
- SET_NORM_INV , # not inverted
111
- # charge pump
112
- SET_CHARGE_PUMP , 0x10 if self .external_vcc else 0x14 ,
113
- SET_DISP | 0x01 ): # on
95
+ SET_DISP | 0x00 , # off
96
+ # address setting
97
+ SET_MEM_ADDR ,
98
+ 0x00 , # horizontal
99
+ # resolution and layout
100
+ SET_DISP_START_LINE | 0x00 ,
101
+ SET_SEG_REMAP | 0x01 , # column addr 127 mapped to SEG0
102
+ SET_MUX_RATIO ,
103
+ self .height - 1 ,
104
+ SET_COM_OUT_DIR | 0x08 , # scan from COM[N] to COM0
105
+ SET_DISP_OFFSET ,
106
+ 0x00 ,
107
+ SET_COM_PIN_CFG ,
108
+ 0x02 if self .height == 32 or self .height == 16 else 0x12 ,
109
+ # timing and driving scheme
110
+ SET_DISP_CLK_DIV ,
111
+ 0x80 ,
112
+ SET_PRECHARGE ,
113
+ 0x22 if self .external_vcc else 0xF1 ,
114
+ SET_VCOM_DESEL ,
115
+ 0x30 , # 0.83*Vcc
116
+ # display
117
+ SET_CONTRAST ,
118
+ 0xFF , # maximum
119
+ SET_ENTIRE_ON , # output follows RAM contents
120
+ SET_NORM_INV , # not inverted
121
+ # charge pump
122
+ SET_CHARGE_PUMP ,
123
+ 0x10 if self .external_vcc else 0x14 ,
124
+ SET_DISP | 0x01 ,
125
+ ): # on
114
126
self .write_cmd (cmd )
115
127
if self .width == 72 :
116
128
self .write_cmd (0xAD )
@@ -172,6 +184,7 @@ def show(self):
172
184
self .write_cmd (self .pages - 1 )
173
185
self .write_framebuf ()
174
186
187
+
175
188
class SSD1306_I2C (_SSD1306 ):
176
189
"""
177
190
I2C class for SSD1306
@@ -184,7 +197,9 @@ class SSD1306_I2C(_SSD1306):
184
197
:param reset: if needed, DigitalInOut designating reset pin
185
198
"""
186
199
187
- def __init__ (self , width , height , i2c , * , addr = 0x3c , external_vcc = False , reset = None ):
200
+ def __init__ (
201
+ self , width , height , i2c , * , addr = 0x3C , external_vcc = False , reset = None
202
+ ):
188
203
self .i2c_device = i2c_device .I2CDevice (i2c , addr )
189
204
self .addr = addr
190
205
self .temp = bytearray (2 )
@@ -195,12 +210,17 @@ def __init__(self, width, height, i2c, *, addr=0x3c, external_vcc=False, reset=N
195
210
# buffer).
196
211
self .buffer = bytearray (((height // 8 ) * width ) + 1 )
197
212
self .buffer [0 ] = 0x40 # Set first byte of data buffer to Co=0, D/C=1
198
- super ().__init__ (memoryview (self .buffer )[1 :], width , height ,
199
- external_vcc = external_vcc , reset = reset )
213
+ super ().__init__ (
214
+ memoryview (self .buffer )[1 :],
215
+ width ,
216
+ height ,
217
+ external_vcc = external_vcc ,
218
+ reset = reset ,
219
+ )
200
220
201
221
def write_cmd (self , cmd ):
202
222
"""Send a command to the SPI device"""
203
- self .temp [0 ] = 0x80 # Co=1, D/C#=0
223
+ self .temp [0 ] = 0x80 # Co=1, D/C#=0
204
224
self .temp [1 ] = cmd
205
225
with self .i2c_device :
206
226
self .i2c_device .write (self .temp )
@@ -211,7 +231,8 @@ def write_framebuf(self):
211
231
with self .i2c_device :
212
232
self .i2c_device .write (self .buffer )
213
233
214
- #pylint: disable-msg=too-many-arguments
234
+
235
+ # pylint: disable-msg=too-many-arguments
215
236
class SSD1306_SPI (_SSD1306 ):
216
237
"""
217
238
SPI class for SSD1306
@@ -223,18 +244,37 @@ class SSD1306_SPI(_SSD1306):
223
244
:param reset: the reset pin to use,
224
245
:param cs: the chip-select pin to use (sometimes labeled "SS").
225
246
"""
247
+
226
248
# pylint: disable=no-member
227
249
# Disable should be reconsidered when refactor can be tested.
228
- def __init__ (self , width , height , spi , dc , reset , cs , * ,
229
- external_vcc = False , baudrate = 8000000 , polarity = 0 , phase = 0 ):
250
+ def __init__ (
251
+ self ,
252
+ width ,
253
+ height ,
254
+ spi ,
255
+ dc ,
256
+ reset ,
257
+ cs ,
258
+ * ,
259
+ external_vcc = False ,
260
+ baudrate = 8000000 ,
261
+ polarity = 0 ,
262
+ phase = 0
263
+ ):
230
264
self .rate = 10 * 1024 * 1024
231
265
dc .switch_to_output (value = 0 )
232
- self .spi_device = spi_device .SPIDevice (spi , cs , baudrate = baudrate ,
233
- polarity = polarity , phase = phase )
266
+ self .spi_device = spi_device .SPIDevice (
267
+ spi , cs , baudrate = baudrate , polarity = polarity , phase = phase
268
+ )
234
269
self .dc_pin = dc
235
270
self .buffer = bytearray ((height // 8 ) * width )
236
- super ().__init__ (memoryview (self .buffer ), width , height ,
237
- external_vcc = external_vcc , reset = reset )
271
+ super ().__init__ (
272
+ memoryview (self .buffer ),
273
+ width ,
274
+ height ,
275
+ external_vcc = external_vcc ,
276
+ reset = reset ,
277
+ )
238
278
239
279
def write_cmd (self , cmd ):
240
280
"""Send a command to the SPI device"""
0 commit comments