Trait embedded_graphics::Drawing
source · pub trait Drawing<C>where
C: PixelColor + Clone,{
// Required method
fn draw<T>(&mut self, item: T)
where T: IntoIterator<Item = Pixel<C>>;
}Expand description
To use this crate in a driver, Drawing must be implemented. This allows display drivers to
support all embedded_graphics objects through the draw() method.
Note that you should also implement SizedDrawing if the display supports partial updates.
Here’s an example for an imaginary display that has a 64x64px framebuffer of 8 bit values that communicates over a (simplified) SPI interface:
use embedded_graphics::prelude::*;
use embedded_graphics::Drawing;
use embedded_graphics::egcircle;
/// A fake display 64px x 64px where each pixel is stored as a single `u8`
struct ExampleDisplay {
framebuffer: [u8; 64 * 64],
iface: SPI1,
}
impl ExampleDisplay {
/// Send buffer to the display
pub fn flush(&self) -> Result<(), ()> {
self.iface.send_bytes(&self.framebuffer)
}
}
impl Drawing<u8> for ExampleDisplay {
/// Draw any item that can produce an iterator of `Pixel`s that have a colour defined as a `u8`
fn draw<T>(&mut self, item: T)
where
T: IntoIterator<Item = Pixel<u8>>,
{
for Pixel(coord, color) in item {
// Place an (x, y) pixel at the right index in the framebuffer
let index = coord[0] + (coord[1] * 64);
self.framebuffer[index as usize] = color;
}
}
}
fn main() {
let mut display = ExampleDisplay {
framebuffer: [0; 4096],
iface: SPI1
};
// Draw a circle centered around `(32, 32)` with a radius of `10` and a stroke of `1u8`
display.draw(egcircle!((32, 32), 10, stroke = Some(1u8)));
// Update the display
display.flush().expect("Failed to send data to display");
}Required Methods§
sourcefn draw<T>(&mut self, item: T)where
T: IntoIterator<Item = Pixel<C>>,
fn draw<T>(&mut self, item: T)where
T: IntoIterator<Item = Pixel<C>>,
Draw an object from an iterator over its pixels
Object Safety§
This trait is not object safe.