Skip to content

Commit 6aff687

Browse files
authored
Merge pull request #66 from mcauser/patch
72x40 OLEDs, rotate() and SET_COM_PIN_CFG
2 parents b4198fd + 5577330 commit 6aff687

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

adafruit_ssd1306.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
SET_DISP_START_LINE = const(0x40)
3636
SET_SEG_REMAP = const(0xA0)
3737
SET_MUX_RATIO = const(0xA8)
38+
SET_IREF_SELECT = const(0xAD)
3839
SET_COM_OUT_DIR = const(0xC0)
3940
SET_DISP_OFFSET = const(0xD3)
4041
SET_COM_PIN_CFG = const(0xDA)
@@ -96,24 +97,22 @@ def init_display(self):
9697
# 64, 48: 0x80 0x12
9798
# 64, 32: 0x80 0x12
9899
for cmd in (
99-
SET_DISP | 0x00, # off
100+
SET_DISP, # off
100101
# address setting
101102
SET_MEM_ADDR,
102103
0x10 # Page Addressing Mode
103104
if self.page_addressing
104105
else 0x00, # Horizontal Addressing Mode
105106
# resolution and layout
106-
SET_DISP_START_LINE | 0x00,
107+
SET_DISP_START_LINE,
107108
SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0
108109
SET_MUX_RATIO,
109110
self.height - 1,
110111
SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0
111112
SET_DISP_OFFSET,
112113
0x00,
113114
SET_COM_PIN_CFG,
114-
0x02
115-
if (self.height == 32 or self.height == 16) and (self.width != 64)
116-
else 0x12,
115+
0x02 if self.width > 2 * self.height else 0x12,
117116
# timing and driving scheme
118117
SET_DISP_CLK_DIV,
119118
0x80,
@@ -126,21 +125,20 @@ def init_display(self):
126125
0xFF, # maximum
127126
SET_ENTIRE_ON, # output follows RAM contents
128127
SET_NORM_INV, # not inverted
128+
SET_IREF_SELECT,
129+
0x30, # enable internal IREF during display on
129130
# charge pump
130131
SET_CHARGE_PUMP,
131132
0x10 if self.external_vcc else 0x14,
132-
SET_DISP | 0x01,
133-
): # on
133+
SET_DISP | 0x01, # display on
134+
):
134135
self.write_cmd(cmd)
135-
if self.width == 72:
136-
self.write_cmd(0xAD)
137-
self.write_cmd(0x30)
138136
self.fill(0)
139137
self.show()
140138

141139
def poweroff(self):
142140
"""Turn off the display (nothing visible)"""
143-
self.write_cmd(SET_DISP | 0x00)
141+
self.write_cmd(SET_DISP)
144142
self._power = False
145143

146144
def contrast(self, contrast):
@@ -152,6 +150,13 @@ def invert(self, invert):
152150
"""Invert all pixels on the display"""
153151
self.write_cmd(SET_NORM_INV | (invert & 1))
154152

153+
def rotate(self, rotate):
154+
"""Rotate the display 0 or 180 degrees"""
155+
self.write_cmd(SET_COM_OUT_DIR | ((rotate & 1) << 3))
156+
self.write_cmd(SET_SEG_REMAP | (rotate & 1))
157+
# com output (vertical mirror) is changed immediately
158+
# you need to call show() for the seg remap to be visible
159+
155160
def write_framebuf(self):
156161
"""Derived class must implement this"""
157162
raise NotImplementedError
@@ -177,14 +182,11 @@ def show(self):
177182
if not self.page_addressing:
178183
xpos0 = 0
179184
xpos1 = self.width - 1
180-
if self.width == 64:
181-
# displays with width of 64 pixels are shifted by 32
182-
xpos0 += 32
183-
xpos1 += 32
184-
if self.width == 72:
185-
# displays with width of 72 pixels are shifted by 28
186-
xpos0 += 28
187-
xpos1 += 28
185+
if self.width != 128:
186+
# narrow displays use centered columns
187+
col_offset = (128 - self.width) // 2
188+
xpos0 += col_offset
189+
xpos1 += col_offset
188190
self.write_cmd(SET_COL_ADDR)
189191
self.write_cmd(xpos0)
190192
self.write_cmd(xpos1)

0 commit comments

Comments
 (0)