Skip to content

72x40 OLEDs, rotate() and SET_COM_PIN_CFG #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions adafruit_ssd1306.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
SET_DISP_START_LINE = const(0x40)
SET_SEG_REMAP = const(0xA0)
SET_MUX_RATIO = const(0xA8)
SET_IREF_SELECT = const(0xAD)
SET_COM_OUT_DIR = const(0xC0)
SET_DISP_OFFSET = const(0xD3)
SET_COM_PIN_CFG = const(0xDA)
Expand Down Expand Up @@ -96,24 +97,22 @@ def init_display(self):
# 64, 48: 0x80 0x12
# 64, 32: 0x80 0x12
for cmd in (
SET_DISP | 0x00, # off
SET_DISP, # off
# address setting
SET_MEM_ADDR,
0x10 # Page Addressing Mode
if self.page_addressing
else 0x00, # Horizontal Addressing Mode
# resolution and layout
SET_DISP_START_LINE | 0x00,
SET_DISP_START_LINE,
SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0
SET_MUX_RATIO,
self.height - 1,
SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0
SET_DISP_OFFSET,
0x00,
SET_COM_PIN_CFG,
0x02
if (self.height == 32 or self.height == 16) and (self.width != 64)
else 0x12,
0x02 if self.width > 2 * self.height else 0x12,
# timing and driving scheme
SET_DISP_CLK_DIV,
0x80,
Expand All @@ -126,21 +125,20 @@ def init_display(self):
0xFF, # maximum
SET_ENTIRE_ON, # output follows RAM contents
SET_NORM_INV, # not inverted
SET_IREF_SELECT,
0x30, # enable internal IREF during display on
# charge pump
SET_CHARGE_PUMP,
0x10 if self.external_vcc else 0x14,
SET_DISP | 0x01,
): # on
SET_DISP | 0x01, # display on
):
self.write_cmd(cmd)
if self.width == 72:
self.write_cmd(0xAD)
self.write_cmd(0x30)
self.fill(0)
self.show()

def poweroff(self):
"""Turn off the display (nothing visible)"""
self.write_cmd(SET_DISP | 0x00)
self.write_cmd(SET_DISP)
self._power = False

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

def rotate(self, rotate):
"""Rotate the display 0 or 180 degrees"""
self.write_cmd(SET_COM_OUT_DIR | ((rotate & 1) << 3))
self.write_cmd(SET_SEG_REMAP | (rotate & 1))
# com output (vertical mirror) is changed immediately
# you need to call show() for the seg remap to be visible

def write_framebuf(self):
"""Derived class must implement this"""
raise NotImplementedError
Expand All @@ -177,14 +182,11 @@ def show(self):
if not self.page_addressing:
xpos0 = 0
xpos1 = self.width - 1
if self.width == 64:
# displays with width of 64 pixels are shifted by 32
xpos0 += 32
xpos1 += 32
if self.width == 72:
# displays with width of 72 pixels are shifted by 28
xpos0 += 28
xpos1 += 28
if self.width != 128:
# narrow displays use centered columns
col_offset = (128 - self.width) // 2
xpos0 += col_offset
xpos1 += col_offset
self.write_cmd(SET_COL_ADDR)
self.write_cmd(xpos0)
self.write_cmd(xpos1)
Expand Down