Skip to content

extmod/modframebuf: Add attributes for width, height, format and stride. - #17328

Open
andrewleech wants to merge 1 commit into
micropython:masterfrom
andrewleech:framebuf_attr
Open

andrewleech wants to merge 1 commit into
micropython:masterfrom
andrewleech:framebuf_attr

Conversation

@andrewleech

@andrewleech andrewleech commented May 20, 2025 •

Copy link
Copy Markdown
Contributor

Summary

This PR adds direct attribute access to the FrameBuffer class by implementing an attribute handler in the C code. Users can now access the width, height, format, and stride properties directly from
framebuffer objects using standard attribute syntax (e.g., fb.width, fb.height, etc.).

This makes it easier to work with framebuffers in applications where these properties need to be accessed frequently, eliminating the need to store and track these values separately. It's particularly
useful when passing framebuffers between different functions or modules.

Prompted by https://github.com/orgs/micropython/discussions/17266

Testing

I've added a comprehensive test suite in tests/extmod/framebuf_attr.py that validates all the framebuffer attributes with all supported formats:

  • MONO_VLSB, MONO_HLSB, MONO_HMSB
  • RGB565
  • GS2_HMSB, GS4_HMSB, GS8

The test also checks various dimensions (8x4, 16x8, 128x64) and verifies that attempting to access non-existent attributes raises an AttributeError as expected.

The implementation was tested on the Unix port, and since this change is contained within the framebuf module and doesn't interact with hardware-specific features, it should work consistently across all
ports that support the framebuf module.


🤖 Generated with Claude Code

@github-actions

Copy link
Copy Markdown

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:  +304 +0.036% standard[incl +64(data)]
      stm32:  +112 +0.029% PYBV10
     mimxrt:  +104 +0.028% TEENSY40
        rp2:  +112 +0.012% RPI_PICO_W
       samd:  +108 +0.040% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:  +123 +0.027% VIRT_RV32

@dpgeorge dpgeorge added the extmod Relates to extmod/ directory in source label May 20, 2025
@andrewleech
andrewleech force-pushed the framebuf_attr branch 7 times, most recently from 73a8cf9 to 5b0456f Compare May 20, 2025 03:33
@codecov

codecov Bot commented May 20, 2025 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.46%. Comparing base (2dc2e30) to head (63acb87).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #17328   +/-   ##
=======================================
  Coverage   98.46%   98.46%           
=======================================
  Files         176      176           
  Lines       22784    22796   +12     
=======================================
+ Hits        22435    22447   +12     
  Misses        349      349           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@peterhinch

peterhinch commented Oct 16, 2025 •

Copy link
Copy Markdown
Contributor

Some display drivers enable rotation where width and height may be transposed.

My subclasses maintain .width and .height attributes which match the attributes of the displayed image. These are derived from args passed to the constructor (height, width, some means of specifying orientation).

To expand on this, where rotation is done in code there may be no problem if the framebuf is instantiated with height and width attributes matching the image. But some hardware (e.g. ST7789) performs rotation in hardware, so the framebuffer's dimensions may not match those of the image.

Arguably no action is required here with such subclasses overriding the base class attributes, but comments are welcome.

@dpgeorge

Copy link
Copy Markdown
Member

Some display drivers enable rotation where width and height may be transposed.

Rotation feels like a problem best solved at a layer above FrameBuffer.

Or at least right now FrameBuffer has no concept of rotation, maybe in the future it could but I'm not sure exactly how that would work. For now the width, height and stride attributes are those used in clipping calculations, so seem sensible to expose.

@projectgus
projectgus self-requested a review January 22, 2026 02:23
Implement an attr handler for FrameBuffer objects to allow direct access to
the width, height, format and stride properties, which makes it easier to
reuse framebuffers without needing to track their properties separately.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Signed-off-by: Andrew Leech <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

extmod Relates to extmod/ directory in source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants