Skip to content

Commit 3e62708

Browse files
authored
Merge pull request #8 from dhalbert/master
Provide reset pin support for I2C
2 parents 36e9f6d + 374264b commit 3e62708

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

adafruit_ssd1306.py

+24-25
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,16 @@
3333

3434
class _SSD1306:
3535
"""Base class for SSD1306 display driver"""
36-
def __init__(self, framebuffer, width, height, external_vcc):
36+
#pylint: disable-msg=too-many-arguments
37+
def __init__(self, framebuffer, width, height, external_vcc, reset):
3738
self.framebuf = framebuffer
3839
self.width = width
3940
self.height = height
4041
self.external_vcc = external_vcc
42+
# reset may be None if not needed
43+
self.reset_pin = reset
44+
if self.reset_pin:
45+
self.reset_pin.switch_to_output(value=0)
4146
self.pages = self.height // 8
4247
# Note the subclass must initialize self.framebuf to a framebuffer.
4348
# This is necessary because the underlying data buffer is different
@@ -74,7 +79,7 @@ def init_display(self):
7479
self.show()
7580

7681
def poweroff(self):
77-
"""Turn the device Power off"""
82+
"""Turn off the display (nothing visible)"""
7883
self.write_cmd(SET_DISP | 0x00)
7984

8085
def contrast(self, contrast):
@@ -83,7 +88,7 @@ def contrast(self, contrast):
8388
self.write_cmd(contrast)
8489

8590
def invert(self, invert):
86-
"""Invert the pixels on the display"""
91+
"""Invert all pixels on the display"""
8792
self.write_cmd(SET_NORM_INV | (invert & 1))
8893

8994
def write_framebuf(self):
@@ -95,8 +100,15 @@ def write_cmd(self, cmd):
95100
raise NotImplementedError
96101

97102
def poweron(self):
98-
"""Derived class must implement this"""
99-
raise NotImplementedError
103+
"Reset device and turn on the display."
104+
if self.reset_pin:
105+
self.reset_pin.value = 1
106+
time.sleep(0.001)
107+
self.reset_pin.value = 0
108+
time.sleep(0.010)
109+
self.reset_pin.value = 1
110+
time.sleep(0.010)
111+
self.write_cmd(SET_DISP | 0x01)
100112

101113
def show(self):
102114
"""Update the display"""
@@ -115,7 +127,7 @@ def show(self):
115127
self.write_framebuf()
116128

117129
def fill(self, value):
118-
"""Fill the display on or off"""
130+
"""Fill the display with all ones or zeros."""
119131
self.framebuf.fill(value)
120132

121133
def pixel(self, xpos, ypos, value):
@@ -139,9 +151,10 @@ class SSD1306_I2C(_SSD1306):
139151
:param i2c: the I2C peripheral to use,
140152
:param addr: the 8-bit bus address of the device,
141153
:param external_vcc: whether external high-voltage source is connected.
154+
:param reset: if needed, DigitalInOut designating reset pin
142155
"""
143156

144-
def __init__(self, width, height, i2c, *, addr=0x3c, external_vcc=False):
157+
def __init__(self, width, height, i2c, *, addr=0x3c, external_vcc=False, reset=None):
145158
self.i2c_device = i2c_device.I2CDevice(i2c, addr)
146159
self.addr = addr
147160
self.temp = bytearray(2)
@@ -153,7 +166,7 @@ def __init__(self, width, height, i2c, *, addr=0x3c, external_vcc=False):
153166
self.buffer = bytearray(((height // 8) * width) + 1)
154167
self.buffer[0] = 0x40 # Set first byte of data buffer to Co=0, D/C=1
155168
framebuffer = framebuf.FrameBuffer1(memoryview(self.buffer)[1:], width, height)
156-
super().__init__(framebuffer, width, height, external_vcc)
169+
super().__init__(framebuffer, width, height, external_vcc, reset)
157170

158171
def write_cmd(self, cmd):
159172
"""Send a command to the SPI device"""
@@ -168,10 +181,6 @@ def write_framebuf(self):
168181
with self.i2c_device:
169182
self.i2c_device.write(self.buffer)
170183

171-
def poweron(self):
172-
"""Turn power on the device"""
173-
self.write_cmd(SET_DISP | 0x01)
174-
175184
#pylint: disable-msg=too-many-arguments
176185
class SSD1306_SPI(_SSD1306):
177186
"""
@@ -181,21 +190,19 @@ class SSD1306_SPI(_SSD1306):
181190
:param height: the height of the physical screen in pixels,
182191
:param spi: the SPI peripheral to use,
183192
:param dc: the data/command pin to use (often labeled "D/C"),
184-
:param res: the reset pin to use,
193+
:param reset: the reset pin to use,
185194
:param cs: the chip-select pin to use (sometimes labeled "SS").
186195
"""
187-
def __init__(self, width, height, spi, dc, res, cs, *,
196+
def __init__(self, width, height, spi, dc, reset, cs, *,
188197
external_vcc=False, baudrate=8000000, polarity=0, phase=0):
189198
self.rate = 10 * 1024 * 1024
190199
dc.switch_to_output(value=0)
191-
res.switch_to_output(value=0)
192200
self.spi_device = spi_device.SPIDevice(spi, cs, baudrate=baudrate,
193201
polarity=polarity, phase=phase)
194202
self.dc_pin = dc
195-
self.reset_pin = res
196203
self.buffer = bytearray((height // 8) * width)
197204
framebuffer = framebuf.FrameBuffer1(self.buffer, width, height)
198-
super().__init__(framebuffer, width, height, external_vcc)
205+
super().__init__(framebuffer, width, height, external_vcc, reset)
199206

200207
def write_cmd(self, cmd):
201208
"""Send a command to the SPI device"""
@@ -208,11 +215,3 @@ def write_framebuf(self):
208215
self.dc_pin.value = 1
209216
with self.spi_device as spi:
210217
spi.write(self.buffer)
211-
212-
def poweron(self):
213-
"""Turn power off on the device"""
214-
self.reset_pin.value = 1
215-
time.sleep(0.001)
216-
self.reset_pin.value = 0
217-
time.sleep(0.010)
218-
self.reset_pin.value = 1

0 commit comments

Comments
 (0)