Skip to content

Ported stats to SSD1305. Also Black Formatted. #9

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 3 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ confidence=
# no Warning level messages displayed, use"--disable=all --enable=classes
# --disable=W"
# disable=import-error,print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call
disable=print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call,import-error
disable=print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call,import-error,bad-continuation

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
Expand Down
157 changes: 102 additions & 55 deletions adafruit_ssd1305.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

from micropython import const
from adafruit_bus_device import i2c_device, spi_device

try:
import framebuf
except ImportError:
Expand All @@ -60,35 +61,36 @@
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SSD1305.git"

#pylint: disable-msg=bad-whitespace
# pylint: disable-msg=bad-whitespace
# register definitions
SET_CONTRAST = const(0x81)
SET_ENTIRE_ON = const(0xa4)
SET_NORM_INV = const(0xa6)
SET_DISP = const(0xae)
SET_MEM_ADDR = const(0x20)
SET_COL_ADDR = const(0x21)
SET_PAGE_ADDR = const(0x22)
SET_CONTRAST = const(0x81)
SET_ENTIRE_ON = const(0xA4)
SET_NORM_INV = const(0xA6)
SET_DISP = const(0xAE)
SET_MEM_ADDR = const(0x20)
SET_COL_ADDR = const(0x21)
SET_PAGE_ADDR = const(0x22)
SET_DISP_START_LINE = const(0x40)
SET_LUT = const(0x91)
SET_SEG_REMAP = const(0xa0)
SET_MUX_RATIO = const(0xa8)
SET_MASTER_CONFIG = const(0xad)
SET_COM_OUT_DIR = const(0xc0)
SET_COMSCAN_DEC = const(0xc8)
SET_DISP_OFFSET = const(0xd3)
SET_COM_PIN_CFG = const(0xda)
SET_DISP_CLK_DIV = const(0xd5)
SET_AREA_COLOR = const(0xd8)
SET_PRECHARGE = const(0xd9)
SET_VCOM_DESEL = const(0xdb)
SET_CHARGE_PUMP = const(0x8d)
#pylint: enable-msg=bad-whitespace
SET_LUT = const(0x91)
SET_SEG_REMAP = const(0xA0)
SET_MUX_RATIO = const(0xA8)
SET_MASTER_CONFIG = const(0xAD)
SET_COM_OUT_DIR = const(0xC0)
SET_COMSCAN_DEC = const(0xC8)
SET_DISP_OFFSET = const(0xD3)
SET_COM_PIN_CFG = const(0xDA)
SET_DISP_CLK_DIV = const(0xD5)
SET_AREA_COLOR = const(0xD8)
SET_PRECHARGE = const(0xD9)
SET_VCOM_DESEL = const(0xDB)
SET_CHARGE_PUMP = const(0x8D)
# pylint: enable-msg=bad-whitespace


class _SSD1305(framebuf.FrameBuffer):
"""Base class for SSD1305 display driver"""
#pylint: disable-msg=too-many-arguments

# pylint: disable-msg=too-many-arguments
def __init__(self, buffer, width, height, *, external_vcc, reset):
super().__init__(buffer, width, height)
self.width = width
Expand All @@ -101,7 +103,7 @@ def __init__(self, buffer, width, height, *, external_vcc, reset):
self.pages = self.height // 8
self._column_offset = 0
if self.height == 32:
self._column_offset = 4 # hardcoded for now...
self._column_offset = 4 # hardcoded for now...
# Note the subclass must initialize self.framebuf to a framebuffer.
# This is necessary because the underlying data buffer is different
# between I2C and SPI implementations (I2C needs an extra byte).
Expand All @@ -111,26 +113,43 @@ def __init__(self, buffer, width, height, *, external_vcc, reset):
def init_display(self):
"""Base class to initialize display"""
for cmd in (
SET_DISP | 0x00, # off
# timing and driving scheme
SET_DISP_CLK_DIV, 0x80, #SET_DISP_CLK_DIV
SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0 SET_SEG_REMAP
SET_MUX_RATIO, self.height - 1, #SET_MUX_RATIO
SET_DISP_OFFSET, 0x00, #SET_DISP_OFFSET
SET_MASTER_CONFIG, 0x8e, #Set Master Configuration
SET_AREA_COLOR, 0x05, #Set Area Color Mode On/Off & Low Power Display Mode
SET_MEM_ADDR, 0x00, # horizontal SET_MEM_ADDR ADD
SET_DISP_START_LINE | 0x00, 0x2E, #SET_DISP_START_LINE ADD
SET_COMSCAN_DEC, #Set COM Output Scan Direction 64 to 1
SET_COM_PIN_CFG, 0x12, #SET_COM_PIN_CFG
SET_LUT, 0x3f, 0x3f, 0x3f, 0x3f,#Current drive pulse width of BANK0, Color A, B, C
SET_CONTRAST, 0xff, # maximum SET_CONTRAST to maximum
SET_PRECHARGE, 0xd2, #SET_PRECHARGE orig: 0xd9, 0x22 if self.external_vcc else 0xf1,
SET_VCOM_DESEL, 0x34, #SET_VCOM_DESEL 0xdb, 0x30, $ 0.83* Vcc
SET_NORM_INV, # not inverted SET_NORM_INV
SET_ENTIRE_ON, # output follows RAM contents SET_ENTIRE_ON
SET_CHARGE_PUMP, 0x10 if self.external_vcc else 0x14, #SET_CHARGE_PUMP
SET_DISP | 0x01): #//--turn on oled panel
SET_DISP | 0x00, # off
# timing and driving scheme
SET_DISP_CLK_DIV,
0x80, # SET_DISP_CLK_DIV
SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0 SET_SEG_REMAP
SET_MUX_RATIO,
self.height - 1, # SET_MUX_RATIO
SET_DISP_OFFSET,
0x00, # SET_DISP_OFFSET
SET_MASTER_CONFIG,
0x8E, # Set Master Configuration
SET_AREA_COLOR,
0x05, # Set Area Color Mode On/Off & Low Power Display Mode
SET_MEM_ADDR,
0x00, # horizontal SET_MEM_ADDR ADD
SET_DISP_START_LINE | 0x00,
0x2E, # SET_DISP_START_LINE ADD
SET_COMSCAN_DEC, # Set COM Output Scan Direction 64 to 1
SET_COM_PIN_CFG,
0x12, # SET_COM_PIN_CFG
SET_LUT,
0x3F,
0x3F,
0x3F,
0x3F, # Current drive pulse width of BANK0, Color A, B, C
SET_CONTRAST,
0xFF, # maximum SET_CONTRAST to maximum
SET_PRECHARGE,
0xD2, # SET_PRECHARGE orig: 0xd9, 0x22 if self.external_vcc else 0xf1,
SET_VCOM_DESEL,
0x34, # SET_VCOM_DESEL 0xdb, 0x30, $ 0.83* Vcc
SET_NORM_INV, # not inverted SET_NORM_INV
SET_ENTIRE_ON, # output follows RAM contents SET_ENTIRE_ON
SET_CHARGE_PUMP,
0x10 if self.external_vcc else 0x14, # SET_CHARGE_PUMP
SET_DISP | 0x01,
): # //--turn on oled panel
self.write_cmd(cmd)
self.fill(0)
self.show()
Expand Down Expand Up @@ -183,6 +202,7 @@ def show(self):
self.write_cmd(self.pages - 1)
self.write_framebuf()


class SSD1305_I2C(_SSD1305):
"""
I2C class for SSD1305
Expand All @@ -195,7 +215,9 @@ class SSD1305_I2C(_SSD1305):
:param reset: if needed, DigitalInOut designating reset pin
"""

def __init__(self, width, height, i2c, *, addr=0x3c, external_vcc=False, reset=None):
def __init__(
self, width, height, i2c, *, addr=0x3C, external_vcc=False, reset=None
):
self.i2c_device = i2c_device.I2CDevice(i2c, addr)
self.addr = addr
self.temp = bytearray(2)
Expand All @@ -206,12 +228,17 @@ def __init__(self, width, height, i2c, *, addr=0x3c, external_vcc=False, reset=N
# buffer).
self.buffer = bytearray(((height // 8) * width) + 1)
self.buffer[0] = 0x40 # Set first byte of data buffer to Co=0, D/C=1
super().__init__(memoryview(self.buffer)[1:], width, height,
external_vcc=external_vcc, reset=reset)
super().__init__(
memoryview(self.buffer)[1:],
width,
height,
external_vcc=external_vcc,
reset=reset,
)

def write_cmd(self, cmd):
"""Send a command to the SPI device"""
self.temp[0] = 0x80 # Co=1, D/C#=0
self.temp[0] = 0x80 # Co=1, D/C#=0
self.temp[1] = cmd
with self.i2c_device:
self.i2c_device.write(self.temp)
Expand All @@ -222,7 +249,8 @@ def write_framebuf(self):
with self.i2c_device:
self.i2c_device.write(self.buffer)

#pylint: disable-msg=too-many-arguments

# pylint: disable-msg=too-many-arguments
class SSD1305_SPI(_SSD1305):
"""
SPI class for SSD1305
Expand All @@ -234,18 +262,37 @@ class SSD1305_SPI(_SSD1305):
:param reset: the reset pin to use,
:param cs: the chip-select pin to use (sometimes labeled "SS").
"""

# pylint: disable=no-member
# Disable should be reconsidered when refactor can be tested.
def __init__(self, width, height, spi, dc, reset, cs, *,
external_vcc=False, baudrate=8000000, polarity=0, phase=0):
def __init__(
self,
width,
height,
spi,
dc,
reset,
cs,
*,
external_vcc=False,
baudrate=8000000,
polarity=0,
phase=0
):
self.rate = 10 * 1024 * 1024
dc.switch_to_output(value=0)
self.spi_device = spi_device.SPIDevice(spi, cs, baudrate=baudrate,
polarity=polarity, phase=phase)
self.spi_device = spi_device.SPIDevice(
spi, cs, baudrate=baudrate, polarity=polarity, phase=phase
)
self.dc_pin = dc
self.buffer = bytearray((height // 8) * width)
super().__init__(memoryview(self.buffer), width, height,
external_vcc=external_vcc, reset=reset)
super().__init__(
memoryview(self.buffer),
width,
height,
external_vcc=external_vcc,
reset=reset,
)

def write_cmd(self, cmd):
"""Send a command to the SPI device"""
Expand Down
Loading