Expand description
A framebuffer that stores pixels as raw bytes, suitable for direct display transmission.
This module provides RawFrameBuf
, a DrawTarget
implementation that allows
embedded-graphics
primitives to be rendered directly into an in-memory byte buffer.
This is a common pattern for “off-screen rendering,” where a complete frame is prepared
in a buffer before being sent to the display in a single, efficient operation (e.g., via DMA).
The framebuffer is generic over a color type C
and a buffer backend BUF
. The key
component is the IntoRawBytes
trait, which defines how a given PixelColor
is
converted into its byte representation. This allows the framebuffer to automatically
handle different color formats (like Rgb565
, Rgb888
, etc.) without needing
the user to specify the bytes-per-pixel manually.
§Usage with an Async Display Driver
This framebuffer is ideal for use with asynchronous display drivers, like an async fork of mipidsi
.
The typical workflow is:
- Allocate a buffer large enough for one full frame (often on the heap using
alloc
). - Create a
RawFrameBuf
inside a new scope, wrapping a mutable slice of the buffer. - Use
embedded-graphics
commands to draw a complete scene to theRawFrameBuf
. - Once the scope ends,
RawFrameBuf
is dropped, releasing its borrow on the buffer. - The now-populated buffer is passed to an async method on the display driver to be rendered.
§Example
use embedded_graphics::pixelcolor::Rgb565;
use embedded_graphics::prelude::*;
use embedded_graphics::primitives::{Circle, PrimitiveStyle};
use lcd_async::raw_framebuf::{RawFrameBuf, IntoRawBytes};
const WIDTH: usize = 64;
const HEIGHT: usize = 64;
const FRAME_SIZE: usize = WIDTH * HEIGHT * 2; // Rgb565 = 2 bytes per pixel
// Create a static buffer
let mut frame_buffer = [0u8; FRAME_SIZE];
// Create the framebuffer
let mut fbuf = RawFrameBuf::<Rgb565, _>::new(&mut frame_buffer[..], WIDTH, HEIGHT);
// Draw a scene to the in-memory framebuffer
fbuf.clear(Rgb565::BLACK).unwrap();
Circle::new(Point::new(32, 32), 20)
.into_styled(PrimitiveStyle::with_fill(Rgb565::RED))
.draw(&mut fbuf)
.unwrap();
// The frame_buffer now contains the rendered frame data
assert_eq!(fbuf.width(), WIDTH);
assert_eq!(fbuf.height(), HEIGHT);
Structs§
- RawFrame
Buf - A framebuffer that writes pixel data directly into a raw byte buffer.
Traits§
- Into
RawBytes - A trait for converting a
PixelColor
into its raw byte representation. - RawBuffer
Backend Mut - A trait for abstracting over a mutable byte buffer.