Source code for openbricks.drivers.ssd1306

# SPDX-License-Identifier: MIT
"""
SSD1306 OLED driver — thin wrapper around ``micropython-lib``'s
``ssd1306.SSD1306_I2C``.

The micropython-lib driver is already battle-tested and subclasses
``framebuf.FrameBuffer``. We re-expose its core methods (``text``,
``pixel``, ``fill``, ``show``) explicitly and delegate the rest
(``rect``, ``line``, ``hline``, ``scroll``, ``contrast``, …) via
``__getattr__``. An extra ``clear()`` convenience erases the buffer
and pushes a blank frame.

The ``ssd1306`` module is frozen into each board's firmware image (see
``native/boards/*/manifest.py``), so ``import`` works out of the box on
flashed hardware.

A Display is an external I2C component, not part of the hub — instantiate
one directly wherever you want to draw text / pixels.
"""


[docs] class SSD1306: """128×64 (or 128×32) SSD1306 OLED on I2C.""" def __init__(self, i2c, addr=0x3C, width=128, height=64): from ssd1306 import SSD1306_I2C self._impl = SSD1306_I2C(width, height, i2c, addr=addr) self.width = width self.height = height
[docs] def text(self, s, x, y, c=1): """Draw string ``s`` with the 8x8 font at pixel (x, y); ``c=1`` lit, ``c=0`` dark. Buffered — call ``show()``.""" self._impl.text(s, x, y, c)
[docs] def pixel(self, x, y, c=None): """Set pixel (x, y) to ``c`` (1 lit / 0 dark), or return its current value when ``c`` is None.""" self._impl.pixel(x, y, c)
[docs] def fill(self, c): """Fill the whole buffer with ``c`` (1 lit / 0 dark).""" self._impl.fill(c)
[docs] def show(self): """Push the buffer to the panel (nothing appears until this).""" self._impl.show()
[docs] def clear(self): """Blank the display immediately (fill 0 + show).""" self._impl.fill(0) self._impl.show()
# Anything not explicitly overridden (rect, line, contrast, invert, # scroll, blit, …) reaches the impl via attribute lookup. def __getattr__(self, name): return getattr(self._impl, name)