[][src]Trait embedded_graphics::Drawing

pub trait Drawing<C> where
    C: PixelColor
{ fn draw<T>(&mut self, item: T)
    where
        T: IntoIterator<Item = Pixel<C>>
; }

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;
use embedded_graphics::pixelcolor::{Gray8, GrayColor};

/// 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<Gray8> for ExampleDisplay {
    /// Draw any item that can produce an iterator of `Pixel`s that have a colour defined as a `Gray8`
    fn draw<T>(&mut self, item: T)
    where
        T: IntoIterator<Item = Pixel<Gray8>>,
    {
        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.luma();
        }
    }
}

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 white stroke
    display.draw(egcircle!((32, 32), 10, stroke = Some(Gray8::WHITE)));

    // Update the display
    display.flush().expect("Failed to send data to display");
}

Required methods

fn draw<T>(&mut self, item: T) where
    T: IntoIterator<Item = Pixel<C>>, 

Draw an object from an iterator over its pixels

Loading content...

Implementors

Loading content...