Skip to content

MicroPython and CircuitPython Display UI Guide ​

This guide covers both Python platforms supported by Lopaka: MicroPython (framebuf for monochrome SSD1306 / SSD1309 OLEDs on RP2040 and ESP32) and CircuitPython (displayio for RGB TFT and OLED displays). Lopaka generates the Python layout code and bitmap assets; you initialize the I2C/SPI bus, display driver, and main loop.

MicroPython vs. CircuitPython in Lopaka ​

FeatureMicroPython (framebuf)CircuitPython (displayio)
Color modelMonochrome (1 white, 0 black)Full 24-bit RGB (0xRRGGBB)
Rendering modelImmediate pixel buffer + display.show()Retained displayio.Group scene graph
Built-in fontFixed 8×8 (display.text, Petme 8×8)terminalio.FONT (integer scale, 0/90/180/270°)
Rounded cornersNoYes (RoundRect)
ImagesInline bytearray or .pbm fileExternal .bmp via displayio.OnDiskBitmap
Typical displaysSSD1306, SSD1309, SH1106 monochrome OLEDsST7789, ILI9341, SSD1351, FeatherWing, PyPortal

Part 1: MicroPython (framebuf, SSD1306 / SSD1309) ​

Before you start (MicroPython) ​

You need:

  • An RP2040 (Raspberry Pi Pico), ESP32, or ESP8266 board flashed with MicroPython.
  • A 128×64 (or 128×32) monochrome OLED such as SSD1306 or SSD1309 over I2C or SPI.
  • The PerfecXX MicroPython-SSD1306 driver copied to your board as ssd1306.py.

Use PerfecXX's ssd1306.py for ellipses, triangles, and polygons

Lopaka emits display.framebuf.ellipse(...) and display.polygon(...) so the editor preview matches hardware pixel for pixel. The minimal ssd1306.py from micropython-lib does not expose display.framebuf or display.polygon(), so ellipses, triangles, and polygons will raise AttributeError unless you install PerfecXX's driver.

Step 1: Create and design the MicroPython project ​

  1. Open lopaka.app, create a project, and choose Micropython.
  2. Set Screen Size to match your OLED (for example 128 × 64).
  3. Draw with the supported monochrome layers:
Lopaka layerGenerated MicroPython output
Linedisplay.line(x1, y1, x2, y2, color)
Rectangledisplay.rect() / display.fill_rect()
Ellipsedisplay.framebuf.ellipse(x, y, rx, ry, color, fill)
Triangledisplay.polygon([[x1, y1], [x2, y2], [x3, y3]], color, fill)
Polygondisplay.polygon([[x1, y1], ...], color, fill)
Textdisplay.text("Hello", x, y, color) (fixed 8×8 font)
Paint, icon, imageframebuf.FrameBuffer(image_bits, w, h, framebuf.MONO_HLSB) + display.blit()

Circles and rounded corners are not supported on the MicroPython target; use an Ellipse with equal radii (rx == ry) for a circle.

Step 2: Export MicroPython images (inline bytearray vs. .pbm) ​

  • Default (Include image files off): Lopaka embeds each 1-bit MSB-first icon directly as image_<Name>_bits = bytearray(b'\x...') and wraps it in a framebuf.FrameBuffer(..., framebuf.MONO_HLSB) for display.blit().
  • External files (Include image files on): Lopaka emits a compact load_pbm(path) helper and loads each downloaded image_<Name>_bits.pbm file from your board's flash filesystem. Download every .pbm from Code Settings → Images and copy them next to main.py.

To convert standalone PNG or BMP icons to MicroPython bytearray code with no account, use the free Image Converter and select Micropython.

Step 3: Run on ESP32 or RP2040 ​

python
from machine import I2C, Pin
import framebuf
from ssd1306 import SSD1306_I2C

# Hardware-specific: adjust I2C bus and GPIO pins for your board.
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)
display = SSD1306_I2C(128, 64, i2c)

# BEGIN LOPAKA GENERATED CODE
def drawMain(display):
    # Frame
    display.rect(0, 0, 128, 64, 1)
    # Title
    display.text("MicroPython", 16, 12, 1)
    # Divider
    display.line(8, 26, 120, 26, 1)
    display.show()
# END LOPAKA GENERATED CODE

display.fill(0)
drawMain(display)

Call display.fill(0) before drawMain(display) whenever you redraw dynamic values so old pixels are cleared.

Note on SSD1309 displays ​

Many 2.42" SSD1309 128×64 OLED modules accept standard SSD1306 I2C/SPI initialization commands and work directly with the same code. If your SSD1309 module uses a dedicated driver, ensure it subclasses or exposes framebuf and implements polygon() if your screen uses triangles or polygons.


Part 2: CircuitPython (displayio) ​

Before you start (CircuitPython) ​

You need:

  • A board running CircuitPython with a working display object (either built-in board.DISPLAY or initialized via fourwire.FourWire / i2cdisplaybus.I2CDisplayBus).
  • Required libraries copied to /lib on your CIRCUITPY drive from the Adafruit CircuitPython Library Bundle:
    • adafruit_display_text (label, and bitmap_label if any text layer is rotated)
    • adafruit_display_shapes (line, rect, roundrect, circle, triangle)

Step 1: Create and design the CircuitPython project ​

  1. Open lopaka.app, create a project, and choose CircuitPython.
  2. Set Screen Size to match your display resolution.
  3. Design your screen using full 24-bit RGB colors (0xRRGGBB):
Lopaka layerGenerated CircuitPython displayio output
LineLine(x1, y1, x2, y2, color=0xRRGGBB)
RectangleRect(x, y, w, h, [fill=...,] outline=0xRRGGBB)
Rounded rectangleRoundRect(x, y, w, h, r, [fill=...,] outline=0xRRGGBB)
CircleCircle(x, y, r, [fill=...,] outline=0xRRGGBB)
TriangleTriangle(x1, y1, x2, y2, x3, y3, [fill=...,] outline=0xRRGGBB)
PolygonHelper function appending closed Line(...) shapes to the group
Text (0°)label.Label(terminalio.FONT, text=..., color=..., [background_color=...])
Text (90/180/270°)bitmap_label.Label(terminalio.FONT, ..., label_direction="DWR" / "UPD" / "UPR")
Paint, icon, imagedisplayio.OnDiskBitmap("/image_<Name>_bits.bmp") + displayio.TileGrid()

Ellipses are not supported by adafruit_display_shapes in Lopaka's CircuitPython generator.

Step 2: Export .bmp image assets ​

CircuitPython loads images directly from the CIRCUITPY flash filesystem rather than embedding Python byte arrays. Open Code Settings → Images, download every image_<Name>_bits.bmp file, and copy them to the root of your CIRCUITPY drive (or adjust the "/..." path in OnDiskBitmap). You can also generate standalone OnDiskBitmap + .bmp files in the free Image Converter.

Step 3: Integrate into code.py ​

python
import board
import displayio
import terminalio
from adafruit_display_text import label
from adafruit_display_shapes.rect import Rect
from adafruit_display_shapes.roundrect import RoundRect

# Use board.DISPLAY on boards with a built-in screen (PyPortal, Clue, Feather TFT)
# or initialize your own FourWire / I2CDisplayBus display here.
display = board.DISPLAY

# BEGIN LOPAKA GENERATED CODE
# Init display
Main = displayio.Group()
display.root_group = Main

def drawMain():
    # Border
    Border = RoundRect(4, 4, 232, 127, 8, outline=0x00FF88)
    Main.append(Border)
    # Title
    Title = label.Label(terminalio.FONT, text="CircuitPython", color=0xFFFFFF)
    Title.x = 20
    Title.y = 28
    Title.scale = 2
    Main.append(Title)
# END LOPAKA GENERATED CODE

drawMain()
while True:
    pass

Call drawMain() once (not inside while True:), because displayio retains every object appended to the Group. To update live text, keep a reference to the Label object and assign Title.text = "New value".

Code Settings reference ​

SettingMicroPythonCircuitPythonEffect
Wrapper functionOnOnEmits def draw{ScreenName}(display): (MPy) or def draw{ScreenName}(): (CPy)
Comments, layer titlesOnOnAdds # <layer name> comments (and PerfecXX driver URL on MicroPython)
Declare imagesOnOnEmits bytearray / load_pbm (MPy) or OnDiskBitmap (CPy)
Include image filesOff—Switches MicroPython from inline bytearray to .pbm file loading
Declare variablesOnOnEmits top-level Python variables for properties toggled as dynamic

Lopaka handles vs. you handle ​

Lopaka handlesYou handle
Visual layout, coordinates, and Python draw callsFlashing MicroPython or CircuitPython firmware
MONO_HLSB bytearray/.pbm and .bmp assetsCopying ssd1306.py, /lib modules, and .pbm/.bmp files
displayio.Group, TileGrid, and label rotationI2C/SPI/FourWire bus setup and display initialization
Dynamic Python variable declarationsClearing/updating state, buttons, sensors, and main loop

Troubleshooting ​

AttributeError: 'SSD1306_I2C' object has no attribute 'framebuf' (MicroPython) ​

Replace the stock ssd1306.py on your board with PerfecXX MicroPython-SSD1306, which exposes display.framebuf and display.polygon.

ImportError: no module named 'adafruit_display_shapes' (CircuitPython) ​

Download the matching version of the Adafruit CircuitPython Library Bundle for your CircuitPython major version and copy adafruit_display_shapes and adafruit_display_text into /lib on CIRCUITPY.

OSError: [Errno 2] ENOENT on .bmp or .pbm file ​

Check that the downloaded .bmp (CircuitPython) or .pbm (MicroPython) file sits in the exact path referenced in code ("/image_<Name>_bits.bmp" or "image_<Name>_bits.pbm").

CircuitPython runs out of memory after a few updates ​

Do not call drawMain() repeatedly inside while True:. Each call appends new displayio objects to the group. Build the group once and mutate .text, .x, .y, or .fill on existing objects.

FAQ ​

Which SSD1306 driver does Lopaka's MicroPython output require? ​

Lopaka targets the PerfecXX MicroPython-SSD1306 driver (ssd1306.py), which exposes display.framebuf for ellipses and adds display.polygon() for triangles and polygons. The stock micropython-lib ssd1306.py works for basic lines, rectangles, text, and blit(), but raises AttributeError on display.framebuf.ellipse() and display.polygon().

Does Lopaka work with SSD1309 OLED displays in MicroPython? ​

Yes. SSD1309 128×64 OLEDs share the same 1-bit framebuffer model and command set as SSD1306, or you can pass any SSD1309 driver instance that implements line, rect, fill_rect, text, blit, show, framebuf.ellipse, and polygon.

How do I clear or switch screens in CircuitPython displayio? ​

CircuitPython displayio uses a scene graph rather than a pixel clear command. Assign a new displayio.Group() to display.root_group to switch screens, or call group.pop() / clear items before rebuilding.

How are images exported for MicroPython vs. CircuitPython? ​

MicroPython exports 1-bit MONO_HLSB images either as inline bytearray(b'...') literals or as downloadable .pbm files loaded with load_pbm(). CircuitPython exports downloadable .bmp files loaded from the board's CIRCUITPY filesystem with displayio.OnDiskBitmap() and displayio.TileGrid().

Can I use custom fonts in MicroPython or CircuitPython projects in Lopaka? ​

Lopaka's MicroPython platform uses the built-in 8×8 Petme font (framebuf.text) and CircuitPython uses terminalio.FONT (previewed with Terminus BDF). Custom font upload is not available on either Python platform in Lopaka, though you can swap terminalio.FONT for a loaded BDF/PCF font in your CircuitPython script.

Where should I learn more? ​