3232
3333from micropython import const
3434from adafruit_bus_device import i2c_device , spi_device
35+
3536try :
3637 import framebuf
3738except ImportError :
4041__version__ = "0.0.0-auto.0"
4142__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SSD1306.git"
4243
43- #pylint: disable-msg=bad-whitespace
44+ # pylint: disable-msg=bad-whitespace
4445# 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 )
5253SET_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
6364
6465
6566class _SSD1306 (framebuf .FrameBuffer ):
6667 """Base class for SSD1306 display driver"""
67- #pylint: disable-msg=too-many-arguments
68+
69+ # pylint: disable-msg=too-many-arguments
6870 def __init__ (self , buffer , width , height , * , external_vcc , reset ):
6971 super ().__init__ (buffer , width , height )
7072 self .width = width
@@ -90,27 +92,37 @@ def power(self):
9092 def init_display (self ):
9193 """Base class to initialize display"""
9294 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
114126 self .write_cmd (cmd )
115127 if self .width == 72 :
116128 self .write_cmd (0xAD )
@@ -172,6 +184,7 @@ def show(self):
172184 self .write_cmd (self .pages - 1 )
173185 self .write_framebuf ()
174186
187+
175188class SSD1306_I2C (_SSD1306 ):
176189 """
177190 I2C class for SSD1306
@@ -184,7 +197,9 @@ class SSD1306_I2C(_SSD1306):
184197 :param reset: if needed, DigitalInOut designating reset pin
185198 """
186199
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+ ):
188203 self .i2c_device = i2c_device .I2CDevice (i2c , addr )
189204 self .addr = addr
190205 self .temp = bytearray (2 )
@@ -195,12 +210,17 @@ def __init__(self, width, height, i2c, *, addr=0x3c, external_vcc=False, reset=N
195210 # buffer).
196211 self .buffer = bytearray (((height // 8 ) * width ) + 1 )
197212 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+ )
200220
201221 def write_cmd (self , cmd ):
202222 """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
204224 self .temp [1 ] = cmd
205225 with self .i2c_device :
206226 self .i2c_device .write (self .temp )
@@ -211,7 +231,8 @@ def write_framebuf(self):
211231 with self .i2c_device :
212232 self .i2c_device .write (self .buffer )
213233
214- #pylint: disable-msg=too-many-arguments
234+
235+ # pylint: disable-msg=too-many-arguments
215236class SSD1306_SPI (_SSD1306 ):
216237 """
217238 SPI class for SSD1306
@@ -223,18 +244,37 @@ class SSD1306_SPI(_SSD1306):
223244 :param reset: the reset pin to use,
224245 :param cs: the chip-select pin to use (sometimes labeled "SS").
225246 """
247+
226248 # pylint: disable=no-member
227249 # 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+ ):
230264 self .rate = 10 * 1024 * 1024
231265 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+ )
234269 self .dc_pin = dc
235270 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+ )
238278
239279 def write_cmd (self , cmd ):
240280 """Send a command to the SPI device"""
0 commit comments