Skip to content

Conversation

@andrewleech
Copy link
Contributor

@andrewleech andrewleech commented May 20, 2025

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

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 6 times, most recently from f691ae9 to 73a8cf9 Compare May 20, 2025 03:22
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]>
@codecov
Copy link

codecov bot commented May 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.54%. Comparing base (79abdad) to head (5b0456f).
Report is 68 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #17328   +/-   ##
=======================================
  Coverage   98.54%   98.54%           
=======================================
  Files         169      169           
  Lines       21898    21909   +11     
=======================================
+ Hits        21580    21591   +11     
  Misses        318      318           

☔ 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
Copy link
Contributor

peterhinch commented Oct 16, 2025

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
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.

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.

4 participants