Skip to content

Commit 4303387

Browse files
committed
add 0.42 screen (72x40) compatibility
1 parent 3e6b7f1 commit 4303387

File tree

1 file changed

+11
-31
lines changed

1 file changed

+11
-31
lines changed

adafruit_ssd1306.py

+11-31
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,18 @@
2222
"""
2323
`adafruit_ssd1306`
2424
====================================================
25-
2625
MicroPython SSD1306 OLED driver, I2C and SPI interfaces
27-
2826
* Author(s): Tony DiCola, Michael McWethy
2927
"""
30-
3128
import time
32-
3329
from micropython import const
3430
from adafruit_bus_device import i2c_device, spi_device
3531
try:
3632
import framebuf
3733
except ImportError:
3834
import adafruit_framebuf as framebuf
39-
4035
__version__ = "0.0.0-auto.0"
4136
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SSD1306.git"
42-
4337
#pylint: disable-msg=bad-whitespace
4438
# register definitions
4539
SET_CONTRAST = const(0x81)
@@ -60,8 +54,6 @@
6054
SET_VCOM_DESEL = const(0xdb)
6155
SET_CHARGE_PUMP = const(0x8d)
6256
#pylint: enable-msg=bad-whitespace
63-
64-
6557
class _SSD1306(framebuf.FrameBuffer):
6658
"""Base class for SSD1306 display driver"""
6759
#pylint: disable-msg=too-many-arguments
@@ -81,12 +73,10 @@ def __init__(self, buffer, width, height, *, external_vcc, reset):
8173
self._power = False
8274
self.poweron()
8375
self.init_display()
84-
8576
@property
8677
def power(self):
8778
"""True if the display is currently powered on, otherwise False"""
8879
return self._power
89-
9080
def init_display(self):
9181
"""Base class to initialize display"""
9282
for cmd in (
@@ -95,14 +85,14 @@ def init_display(self):
9585
SET_MEM_ADDR, 0x00, # horizontal
9686
# resolution and layout
9787
SET_DISP_START_LINE | 0x00,
98-
SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0
88+
SET_SEG_REMAP | 0x00, # column addr 127 mapped to SEG0
9989
SET_MUX_RATIO, self.height - 1,
10090
SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0
10191
SET_DISP_OFFSET, 0x00,
102-
SET_COM_PIN_CFG, 0x02 if self.height == 32 else 0x12,
92+
SET_COM_PIN_CFG, 0x02 if self.height == 32 or self.height == 16 else 0x12,
10393
# timing and driving scheme
10494
SET_DISP_CLK_DIV, 0x80,
105-
SET_PRECHARGE, 0x22 if self.external_vcc else 0xf1,
95+
SET_PRECHARGE, 0x22 if self.external_vcc else 0x22,
10696
SET_VCOM_DESEL, 0x30, # 0.83*Vcc
10797
# display
10898
SET_CONTRAST, 0xff, # maximum
@@ -112,31 +102,28 @@ def init_display(self):
112102
SET_CHARGE_PUMP, 0x10 if self.external_vcc else 0x14,
113103
SET_DISP | 0x01): # on
114104
self.write_cmd(cmd)
105+
if self.width == 72:
106+
self.write_cmd(0xAD)
107+
self.write_cmd(0x30)
115108
self.fill(0)
116109
self.show()
117-
118110
def poweroff(self):
119111
"""Turn off the display (nothing visible)"""
120112
self.write_cmd(SET_DISP | 0x00)
121113
self._power = False
122-
123114
def contrast(self, contrast):
124115
"""Adjust the contrast"""
125116
self.write_cmd(SET_CONTRAST)
126117
self.write_cmd(contrast)
127-
128118
def invert(self, invert):
129119
"""Invert all pixels on the display"""
130120
self.write_cmd(SET_NORM_INV | (invert & 1))
131-
132121
def write_framebuf(self):
133122
"""Derived class must implement this"""
134123
raise NotImplementedError
135-
136124
def write_cmd(self, cmd):
137125
"""Derived class must implement this"""
138126
raise NotImplementedError
139-
140127
def poweron(self):
141128
"Reset device and turn on the display."
142129
if self.reset_pin:
@@ -148,7 +135,6 @@ def poweron(self):
148135
time.sleep(0.010)
149136
self.write_cmd(SET_DISP | 0x01)
150137
self._power = True
151-
152138
def show(self):
153139
"""Update the display"""
154140
xpos0 = 0
@@ -157,26 +143,27 @@ def show(self):
157143
# displays with width of 64 pixels are shifted by 32
158144
xpos0 += 32
159145
xpos1 += 32
146+
if self.width == 72:
147+
# displays with width of 72 pixels are shifted by 28
148+
xpos0 += 28
149+
xpos1 += 28
160150
self.write_cmd(SET_COL_ADDR)
161151
self.write_cmd(xpos0)
162152
self.write_cmd(xpos1)
163153
self.write_cmd(SET_PAGE_ADDR)
164154
self.write_cmd(0)
165155
self.write_cmd(self.pages - 1)
166156
self.write_framebuf()
167-
168157
class SSD1306_I2C(_SSD1306):
169158
"""
170159
I2C class for SSD1306
171-
172160
:param width: the width of the physical screen in pixels,
173161
:param height: the height of the physical screen in pixels,
174162
:param i2c: the I2C peripheral to use,
175163
:param addr: the 8-bit bus address of the device,
176164
:param external_vcc: whether external high-voltage source is connected.
177165
:param reset: if needed, DigitalInOut designating reset pin
178166
"""
179-
180167
def __init__(self, width, height, i2c, *, addr=0x3c, external_vcc=False, reset=None):
181168
self.i2c_device = i2c_device.I2CDevice(i2c, addr)
182169
self.addr = addr
@@ -190,25 +177,21 @@ def __init__(self, width, height, i2c, *, addr=0x3c, external_vcc=False, reset=N
190177
self.buffer[0] = 0x40 # Set first byte of data buffer to Co=0, D/C=1
191178
super().__init__(memoryview(self.buffer)[1:], width, height,
192179
external_vcc=external_vcc, reset=reset)
193-
194180
def write_cmd(self, cmd):
195181
"""Send a command to the SPI device"""
196182
self.temp[0] = 0x80 # Co=1, D/C#=0
197183
self.temp[1] = cmd
198184
with self.i2c_device:
199185
self.i2c_device.write(self.temp)
200-
201186
def write_framebuf(self):
202187
"""Blast out the frame buffer using a single I2C transaction to support
203188
hardware I2C interfaces."""
204189
with self.i2c_device:
205190
self.i2c_device.write(self.buffer)
206-
207191
#pylint: disable-msg=too-many-arguments
208192
class SSD1306_SPI(_SSD1306):
209193
"""
210194
SPI class for SSD1306
211-
212195
:param width: the width of the physical screen in pixels,
213196
:param height: the height of the physical screen in pixels,
214197
:param spi: the SPI peripheral to use,
@@ -228,15 +211,12 @@ def __init__(self, width, height, spi, dc, reset, cs, *,
228211
self.buffer = bytearray((height // 8) * width)
229212
super().__init__(memoryview(self.buffer), width, height,
230213
external_vcc=external_vcc, reset=reset)
231-
232214
def write_cmd(self, cmd):
233215
"""Send a command to the SPI device"""
234216
self.dc_pin.value = 0
235217
with self.spi_device as spi:
236218
spi.write(bytearray([cmd]))
237-
238219
def write_framebuf(self):
239220
"""write to the frame buffer via SPI"""
240221
self.dc_pin.value = 1
241-
with self.spi_device as spi:
242-
spi.write(self.buffer)
222+
with self.spi_device as spi:

0 commit comments

Comments
 (0)