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
| Feature | MicroPython (framebuf) | CircuitPython (displayio) |
|---|---|---|
| Color model | Monochrome (1 white, 0 black) | Full 24-bit RGB (0xRRGGBB) |
| Rendering model | Immediate pixel buffer + display.show() | Retained displayio.Group scene graph |
| Built-in font | Fixed 8×8 (display.text, Petme 8×8) | terminalio.FONT (integer scale, 0/90/180/270°) |
| Rounded corners | No | Yes (RoundRect) |
| Images | Inline bytearray or .pbm file | External .bmp via displayio.OnDiskBitmap |
| Typical displays | SSD1306, SSD1309, SH1106 monochrome OLEDs | ST7789, 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
- Open lopaka.app, create a project, and choose Micropython.
- Set Screen Size to match your OLED (for example
128 × 64). - Draw with the supported monochrome layers:
| Lopaka layer | Generated MicroPython output |
|---|---|
| Line | display.line(x1, y1, x2, y2, color) |
| Rectangle | display.rect() / display.fill_rect() |
| Ellipse | display.framebuf.ellipse(x, y, rx, ry, color, fill) |
| Triangle | display.polygon([[x1, y1], [x2, y2], [x3, y3]], color, fill) |
| Polygon | display.polygon([[x1, y1], ...], color, fill) |
| Text | display.text("Hello", x, y, color) (fixed 8×8 font) |
| Paint, icon, image | framebuf.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 filesoff): Lopaka embeds each 1-bit MSB-first icon directly asimage_<Name>_bits = bytearray(b'\x...')and wraps it in aframebuf.FrameBuffer(..., framebuf.MONO_HLSB)fordisplay.blit(). - External files (
Include image fileson): Lopaka emits a compactload_pbm(path)helper and loads each downloadedimage_<Name>_bits.pbmfile from your board's flash filesystem. Download every.pbmfrom Code Settings → Images and copy them next tomain.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
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
displayobject (either built-inboard.DISPLAYor initialized viafourwire.FourWire/i2cdisplaybus.I2CDisplayBus). - Required libraries copied to
/libon yourCIRCUITPYdrive from the Adafruit CircuitPython Library Bundle:adafruit_display_text(label, andbitmap_labelif any text layer is rotated)adafruit_display_shapes(line,rect,roundrect,circle,triangle)
Step 1: Create and design the CircuitPython project
- Open lopaka.app, create a project, and choose CircuitPython.
- Set Screen Size to match your display resolution.
- Design your screen using full 24-bit RGB colors (
0xRRGGBB):
| Lopaka layer | Generated CircuitPython displayio output |
|---|---|
| Line | Line(x1, y1, x2, y2, color=0xRRGGBB) |
| Rectangle | Rect(x, y, w, h, [fill=...,] outline=0xRRGGBB) |
| Rounded rectangle | RoundRect(x, y, w, h, r, [fill=...,] outline=0xRRGGBB) |
| Circle | Circle(x, y, r, [fill=...,] outline=0xRRGGBB) |
| Triangle | Triangle(x1, y1, x2, y2, x3, y3, [fill=...,] outline=0xRRGGBB) |
| Polygon | Helper 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, image | displayio.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
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:
passCall 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
| Setting | MicroPython | CircuitPython | Effect |
|---|---|---|---|
| Wrapper function | On | On | Emits def draw{ScreenName}(display): (MPy) or def draw{ScreenName}(): (CPy) |
| Comments, layer titles | On | On | Adds # <layer name> comments (and PerfecXX driver URL on MicroPython) |
| Declare images | On | On | Emits bytearray / load_pbm (MPy) or OnDiskBitmap (CPy) |
| Include image files | Off | — | Switches MicroPython from inline bytearray to .pbm file loading |
| Declare variables | On | On | Emits top-level Python variables for properties toggled as dynamic |
Lopaka handles vs. you handle
| Lopaka handles | You handle |
|---|---|
| Visual layout, coordinates, and Python draw calls | Flashing MicroPython or CircuitPython firmware |
MONO_HLSB bytearray/.pbm and .bmp assets | Copying ssd1306.py, /lib modules, and .pbm/.bmp files |
displayio.Group, TileGrid, and label rotation | I2C/SPI/FourWire bus setup and display initialization |
| Dynamic Python variable declarations | Clearing/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.