Skip to content

Latest commit

 

History

History
90 lines (62 loc) · 10.5 KB

framebuf.md

File metadata and controls

90 lines (62 loc) · 10.5 KB

FrameBuffer and user-defined display drivers

Status: P0 initialization and raw hardware transfer blocks implemented in the first batch; drawing expansion remains planned. The first SSD1306 block-built example and bus trace regression tests are added; ST7735 and hardware verification remain pending. Design agreed 2026-11-30.

Objective and acceptance boundary

The goal is not a catalogue of display controllers. Expose the native MicroPython graphics and hardware primitives so that users can implement their own display driver in blocks. As a concrete acceptance test, reconstruct the behavior of both existing bundled drivers (SSD1306 over I2C and ST7735 over SPI) using ordinary blocks, without the bundled driver-definition blocks or handwritten driver Python. Compare against the existing implementations. A program built from blocks may use functions and composition instead of subclassing if it achieves equivalent observable behavior; reusable classes and inheritance are desirable for reproducing the original object interface.

Keep driver definition, object creation, drawing, and hardware refresh independent. Drawing works with both memory-only FrameBuffers and compatible display objects. A plain FrameBuffer has no show(); refreshing a device is driver or application logic. Do not inject controller code, bus setup, clearing or refresh from drawing blocks. Preserve the two current driver blocks as convenient optional components.

First implementation batch

Implemented: FrameBuffer constructor using a caller-owned buffer and optional stride, all seven pixel format constants, reusable Pin objects, hardware SPI creation/write/deinit, and native I2C writeto/writevto buffer transfers. Existing I2C convenience write and existing display drivers are unchanged. Drawing expansion, SoftSPI, additional SPI read methods, generic class support, and the ST7735 block-built driver example remain follow-up work. No additional display driver is introduced by these blocks.

P0 — native buffer and graphics foundation

Reference: MicroPython framebuf. Check installed firmware versions before claiming API coverage.

Operation Native API Current / next
Allocate writable bytes bytearray(n) Existing conversion block; verify numeric allocation and raw byte literals
Construct framebuffer framebuf.FrameBuffer(buffer, width, height, format[, stride]) Implemented; caller-owned writable buffer; optional stride in pixels
Pixel format MONO_VLSB, MONO_HLSB, MONO_HMSB, GS2_HMSB, GS4_HMSB, GS8, RGB565 Implemented native constants
Fill fill(c) Reuse existing clear/fill
Pixel read and write pixel(x,y), pixel(x,y,c) Add read; reuse write
Text text(s,x,y[,c]) Reuse existing text; native 8×8 font
Lines line(x1,y1,x2,y2,c), hline(x,y,w,c), vline(x,y,h,c) Add
Rectangle rect(x,y,w,h,c[,f]) Add optional fill; check older fill_rect compatibility
Ellipse / circle ellipse(x,y,xr,yr,c[,f,m]) Add fill and optional quadrant mask; circle uses equal radii
Polygon poly(x,y,coords,c[,f]) Add; coords are a native integer array, e.g. array('h', ...)
Scroll scroll(xstep,ystep) Add; no implicit clearing of exposed pixels
Composite blit(src,x,y[,key,palette]) Add; transparency and native palette mapping

Use plain Python objects and selectable variables, defaulting to display for existing screen workflows. Consider an explicit buffer receiver for generic expressions. Do not duplicate drawing APIs under individual board or driver groups. A constructor should not silently allocate a second buffer; the raw buffer must remain accessible for hardware transfer. Support buffer size and stride correctly: bit-packed formats have layout-specific padding. Examples with tight stride: 128×64 MONO_VLSB = 1024 bytes; 160×128 RGB565 = 40960 bytes; 320×240 RGB565 = 153600 bytes. RGB565 memory byte order and controller color/byte order must be verified separately.

For blit, support a FrameBuffer source first, then verify the tuple/list buffer descriptor accepted by the target firmware. Palette height is one pixel and width is the number of source colors; transparency with a palette compares the mapped color. Do not implement a second graphics library in Python for methods missing on older firmware; document the version boundary.

P0 — driver-building primitives (audit and fill actual gaps)

FrameBuffer is only the drawing layer. These primitives are necessary to reproduce both existing drivers.

Capability Required operations Current / gap
Raw data bytes/bytearray construction from numeric sequences, indexing, slicing, bit shifts/masks, concatenation, memoryview Conversion/index/bitwise blocks exist; memoryview added to native conversion. SSD1306 example exercises bytearray allocation, bytes from a list and raw vector writes.
I2C create with pins/frequency, writeto, writevto Create/write and direct buffer/vectored writes implemented; verify on hardware
SPI create with bus, baudrate, polarity, phase, SCK/MOSI and optional MISO; write Hardware SPI creation/write/deinit implemented; SoftSPI and read methods pending
GPIO reusable Pin objects, OUT mode, initial value, value(0/1) Reusable Pin creation/value read/write implemented; verify on hardware
Timing sleep_ms Existing timing blocks; confirm exact units and reset delays
Python composition variables, functions/parameters, loops, conditions, returns, generic calls, attributes Existing generic calls and data blocks cover much; audit class definition, inheritance, init, self, super and methods for full object parity
Raw buffer transfer write existing bytearray or memoryview directly Verify no unintended encoding, copies, or reallocation

Reuse existing generic Python blocks where sufficient. Add the smallest missing native capability, not controller-specific command wrappers. Keep controller register sequences in example projects or optional driver definitions. If classes are not yet exposed, first demonstrate functional parity with a framebuffer plus explicit initialize/flush functions, then add reusable class support if needed for exact constructor/show interface parity. Do not claim class parity based only on a working drawing demo.

P1 — reconstruct the existing drivers in blocks

SSD1306 / Matrix:bit

Import the first block-built driver. The sample uses two ordinary procedures, ssd1306_init and ssd1306_show, a caller-owned bytearray, a native FrameBuffer, I2C raw writes, and shared drawing blocks. It uses no bundled driver blocks, external library, or handwritten program. The screen size/address/bus are explicit; 128×64 is the default, and 128×32 is covered by the bus trace test. Generic class syntax is not required for this first composition-based example. It does not implement the original class interface or its .show() method: refresh is the explicit user-defined procedure. Real hardware verification is still pending.

  • Allocate MONO_VLSB buffer and construct FrameBuffer with 128×64 and 128×32 variants.
  • Create I2C with configurable bus, SCL/SDA, frequency and address (current Matrix:bit preset: bus 0, SCL 22, SDA 23, 0x3C).
  • Reproduce the existing command initialization, including height-dependent multiplex and COM settings.
  • Flush: set column/page windows, send control byte and framebuffer using writeto/writevto. Confirm 1024-byte or 512-byte payload as appropriate.
  • Verify drawing, clear, pixel read, text, shapes and bitmap transfer without implicit refresh.

ST7735 / XiaoMiao

  • Allocate RGB565 buffer and construct FrameBuffer for 160×128 landscape and 128×160 portrait.
  • Reproduce SPI initialization, CS/DC/RES outputs, reset delays, and command/data helper.
  • Reproduce controller initialization sequence, orientation selection and address windows; write the framebuffer to GRAM.
  • Preserve configurable pins/baudrate/dimensions; compare with current XiaoMiao preset (SPI 2, 20 MHz, SCK 18, MOSI 23, CS 5, DC 4, RES 19).
  • Verify actual panel offsets, rotation, red/green/blue order and RGB565 byte order on hardware. Existing implementation is a baseline, not proof of hardware correctness.

Create two importable example projects that use no bundled SSD1306/ST7735 driver-definition blocks, with no raw handwritten Python escape hatch. The primary criterion is equivalent initialization and refresh behavior, not matching Python source character-for-character.

P2 — additional devices and usability

After both reference drivers are reproducible, consider ST7789, SH1106, ILI9341 and GC9A01 according to available hardware. Reuse the same drawing blocks; do not force text LCDs, e-paper or unrelated board-native APIs into FrameBuffer. Consider memory-constrained partial buffers, shared buses, panel offsets and additional pixel formats as actual use cases require. Keep driver delivery (embedded blocks, external module, firmware) replaceable without changing graphics APIs.

Verification

  • Generator tests: native signatures, optional argument omission, constants, variable handling, deterministic output, no driver injection or hidden refresh. Compile Python as a syntax check only.
  • Functional tests on MicroPython: memory-only drawing, pixel readback, fill/line/shape, scroll, transparent/palette blit, format/stride and raw buffer mutation. Check firmware-specific API availability.
  • Bus trace or fake-bus tests: command/data bytes and ordering, reset/delays, address windows, exact payload size and contents; compare equivalent programs with bundled drivers, including boundary dimensions and orientation.
  • Physical acceptance: run both block-built programs on Matrix:bit and XiaoMiao (or equivalent known panels), confirm display output, colors, orientation and refresh. Check memory usage and multiple instances when wiring permits.
  • Done means both drivers are reconstructible without relying on bundled driver code, with results documented; CI alone does not prove hardware parity. The first implementation batch is recorded above; full driver reconstruction and hardware verification are still pending.

Reuse decision (2026-11-30)

See reuse design. Visual class definitions and custom blocks are deferred. Function-based driver examples remain the near-term acceptance path; exact class API parity is a later consideration. A future Python file workflow may provide another way to reuse drivers without changing drawing APIs.