Skip to content

Commit

Permalink
fix: black formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
boutreet committed Nov 22, 2024
1 parent bbebaa1 commit 638e2f6
Showing 1 changed file with 49 additions and 34 deletions.
83 changes: 49 additions & 34 deletions leaphymicropython/actuators/ssd1306.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@
# pylint: disable=all

# 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_SEG_REMAP = const(0xa0)
SET_MUX_RATIO = const(0xa8)
SET_COM_OUT_DIR = const(0xc0)
SET_DISP_OFFSET = const(0xd3)
SET_COM_PIN_CFG = const(0xda)
SET_DISP_CLK_DIV = const(0xd5)
SET_PRECHARGE = const(0xd9)
SET_VCOM_DESEL = const(0xdb)
SET_CHARGE_PUMP = const(0x8d)
SET_SEG_REMAP = const(0xA0)
SET_MUX_RATIO = const(0xA8)
SET_COM_OUT_DIR = const(0xC0)
SET_DISP_OFFSET = const(0xD3)
SET_COM_PIN_CFG = const(0xDA)
SET_DISP_CLK_DIV = const(0xD5)
SET_PRECHARGE = const(0xD9)
SET_VCOM_DESEL = const(0xDB)
SET_CHARGE_PUMP = const(0x8D)


class SSD1306:
"""
A class for the SSD1306 display
"""

def __init__(self, width, height, external_vcc):
"""
Initialize SSD1306 display with specified width, height, and external VCC configuration.
Expand All @@ -50,27 +51,37 @@ def init_display(self):
Initialize the display and the addresses.
"""
for cmd in (
SET_DISP | 0x00, # off
SET_DISP | 0x00, # off
# address setting
SET_MEM_ADDR, 0x00, # horizontal
SET_MEM_ADDR,
0x00, # horizontal
# resolution and layout
SET_DISP_START_LINE | 0x00,
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 else 0x12,
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 else 0x12,
# timing and driving scheme
SET_DISP_CLK_DIV, 0x80,
SET_PRECHARGE, 0x22 if self.external_vcc else 0xf1,
SET_VCOM_DESEL, 0x30, # 0.83*Vcc
SET_DISP_CLK_DIV,
0x80,
SET_PRECHARGE,
0x22 if self.external_vcc else 0xF1,
SET_VCOM_DESEL,
0x30, # 0.83*Vcc
# display
SET_CONTRAST, 0xff, # maximum
SET_ENTIRE_ON, # output follows RAM contents
SET_NORM_INV, # not inverted
SET_CONTRAST,
0xFF, # maximum
SET_ENTIRE_ON, # output follows RAM contents
SET_NORM_INV, # not inverted
# charge pump
SET_CHARGE_PUMP, 0x10 if self.external_vcc else 0x14,
SET_DISP | 0x01): # on
SET_CHARGE_PUMP,
0x10 if self.external_vcc else 0x14,
SET_DISP | 0x01,
): # on
self.write_cmd(cmd)
self.fill(0)
self.show()
Expand Down Expand Up @@ -153,7 +164,8 @@ class SSD1306_I2C(SSD1306):
"""
A class for the ssd1306 I2C driver
"""
def __init__(self, width, height, i2c, addr=0x3c, external_vcc=False):

def __init__(self, width, height, i2c, addr=0x3C, external_vcc=False):
"""
Initialize SSD1306 display over I2C with specified width, height, I2C interface,
address, and external VCC configuration.
Expand All @@ -173,15 +185,17 @@ def __init__(self, width, height, i2c, addr=0x3c, external_vcc=False):
# buffer).
self.buffer = bytearray(((height // 8) * width) + 1)
self.buffer[0] = 0x40 # Set first byte of data buffer to Co=0, D/C=1
self.framebuf = framebuf.FrameBuffer1(memoryview(self.buffer)[1:], width, height)
self.framebuf = framebuf.FrameBuffer1(
memoryview(self.buffer)[1:], width, height
)
super().__init__(width, height, external_vcc)

def write_cmd(self, cmd):
"""
Write a command to the display over I2C.
:param cmd: int, command value
"""
self.temp[0] = 0x80 # Co=1, D/C#=0
self.temp[0] = 0x80 # Co=1, D/C#=0
self.temp[1] = cmd
self.i2c.writeto(self.addr, self.temp)

Expand All @@ -204,6 +218,7 @@ class SSD1306_SPI(SSD1306):
"""
SSD1306 implementation of spi
"""

def __init__(self, width, height, spi, dc, res, cs, external_vcc=False):
"""
Initialize SSD1306 display over SPI with specified width, height, SPI interface, data/command pin, reset pin,
Expand Down

0 comments on commit 638e2f6

Please sign in to comment.