Skip to content

Commit 4c6817c

Browse files
committed
Add compatibility 0.42in screens 72x40
1 parent 4303387 commit 4c6817c

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

adafruit_ssd1306.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,24 @@
2222
"""
2323
`adafruit_ssd1306`
2424
====================================================
25+
2526
MicroPython SSD1306 OLED driver, I2C and SPI interfaces
27+
2628
* Author(s): Tony DiCola, Michael McWethy
2729
"""
30+
2831
import time
32+
2933
from micropython import const
3034
from adafruit_bus_device import i2c_device, spi_device
3135
try:
3236
import framebuf
3337
except ImportError:
3438
import adafruit_framebuf as framebuf
39+
3540
__version__ = "0.0.0-auto.0"
3641
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SSD1306.git"
42+
3743
#pylint: disable-msg=bad-whitespace
3844
# register definitions
3945
SET_CONTRAST = const(0x81)
@@ -54,6 +60,8 @@
5460
SET_VCOM_DESEL = const(0xdb)
5561
SET_CHARGE_PUMP = const(0x8d)
5662
#pylint: enable-msg=bad-whitespace
63+
64+
5765
class _SSD1306(framebuf.FrameBuffer):
5866
"""Base class for SSD1306 display driver"""
5967
#pylint: disable-msg=too-many-arguments
@@ -73,10 +81,12 @@ def __init__(self, buffer, width, height, *, external_vcc, reset):
7381
self._power = False
7482
self.poweron()
7583
self.init_display()
84+
7685
@property
7786
def power(self):
7887
"""True if the display is currently powered on, otherwise False"""
7988
return self._power
89+
8090
def init_display(self):
8191
"""Base class to initialize display"""
8292
for cmd in (
@@ -107,23 +117,29 @@ def init_display(self):
107117
self.write_cmd(0x30)
108118
self.fill(0)
109119
self.show()
120+
110121
def poweroff(self):
111122
"""Turn off the display (nothing visible)"""
112123
self.write_cmd(SET_DISP | 0x00)
113124
self._power = False
125+
114126
def contrast(self, contrast):
115127
"""Adjust the contrast"""
116128
self.write_cmd(SET_CONTRAST)
117129
self.write_cmd(contrast)
130+
118131
def invert(self, invert):
119132
"""Invert all pixels on the display"""
120133
self.write_cmd(SET_NORM_INV | (invert & 1))
134+
121135
def write_framebuf(self):
122136
"""Derived class must implement this"""
123137
raise NotImplementedError
138+
124139
def write_cmd(self, cmd):
125140
"""Derived class must implement this"""
126141
raise NotImplementedError
142+
127143
def poweron(self):
128144
"Reset device and turn on the display."
129145
if self.reset_pin:
@@ -135,6 +151,7 @@ def poweron(self):
135151
time.sleep(0.010)
136152
self.write_cmd(SET_DISP | 0x01)
137153
self._power = True
154+
138155
def show(self):
139156
"""Update the display"""
140157
xpos0 = 0
@@ -154,16 +171,19 @@ def show(self):
154171
self.write_cmd(0)
155172
self.write_cmd(self.pages - 1)
156173
self.write_framebuf()
174+
157175
class SSD1306_I2C(_SSD1306):
158176
"""
159177
I2C class for SSD1306
178+
160179
:param width: the width of the physical screen in pixels,
161180
:param height: the height of the physical screen in pixels,
162181
:param i2c: the I2C peripheral to use,
163182
:param addr: the 8-bit bus address of the device,
164183
:param external_vcc: whether external high-voltage source is connected.
165184
:param reset: if needed, DigitalInOut designating reset pin
166185
"""
186+
167187
def __init__(self, width, height, i2c, *, addr=0x3c, external_vcc=False, reset=None):
168188
self.i2c_device = i2c_device.I2CDevice(i2c, addr)
169189
self.addr = addr
@@ -177,21 +197,25 @@ def __init__(self, width, height, i2c, *, addr=0x3c, external_vcc=False, reset=N
177197
self.buffer[0] = 0x40 # Set first byte of data buffer to Co=0, D/C=1
178198
super().__init__(memoryview(self.buffer)[1:], width, height,
179199
external_vcc=external_vcc, reset=reset)
200+
180201
def write_cmd(self, cmd):
181202
"""Send a command to the SPI device"""
182203
self.temp[0] = 0x80 # Co=1, D/C#=0
183204
self.temp[1] = cmd
184205
with self.i2c_device:
185206
self.i2c_device.write(self.temp)
207+
186208
def write_framebuf(self):
187209
"""Blast out the frame buffer using a single I2C transaction to support
188210
hardware I2C interfaces."""
189211
with self.i2c_device:
190212
self.i2c_device.write(self.buffer)
213+
191214
#pylint: disable-msg=too-many-arguments
192215
class SSD1306_SPI(_SSD1306):
193216
"""
194217
SPI class for SSD1306
218+
195219
:param width: the width of the physical screen in pixels,
196220
:param height: the height of the physical screen in pixels,
197221
:param spi: the SPI peripheral to use,
@@ -211,12 +235,15 @@ def __init__(self, width, height, spi, dc, reset, cs, *,
211235
self.buffer = bytearray((height // 8) * width)
212236
super().__init__(memoryview(self.buffer), width, height,
213237
external_vcc=external_vcc, reset=reset)
238+
214239
def write_cmd(self, cmd):
215240
"""Send a command to the SPI device"""
216241
self.dc_pin.value = 0
217242
with self.spi_device as spi:
218243
spi.write(bytearray([cmd]))
244+
219245
def write_framebuf(self):
220246
"""write to the frame buffer via SPI"""
221247
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

Comments
 (0)