A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.rs/embedded-graphics/latest/embedded_graphics/draw_target/trait.DrawTarget.html below:

DrawTarget in embedded_graphics::draw_target - Rust

pub trait DrawTarget: Dimensions {
    type Color: PixelColor;
    type Error;

    // Required method
    fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
       where I: IntoIterator<Item = Pixel<Self::Color>>;

    // Provided methods
    fn fill_contiguous<I>(
        &mut self,
        area: &Rectangle,
        colors: I,
    ) -> Result<(), Self::Error>
       where I: IntoIterator<Item = Self::Color> { ... }
    fn fill_solid(
        &mut self,
        area: &Rectangle,
        color: Self::Color,
    ) -> Result<(), Self::Error> { ... }
    fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error> { ... }
}
Expand description

A target for embedded-graphics drawing operations.

The DrawTarget trait is used to add embedded-graphics support to a display driver or similar targets like framebuffers or image files. Targets are required to at least implement the draw_iter method and the Dimensions trait. All other methods provide default implementations which use these methods internally.

Because the default implementations cannot use features specific to the target hardware they can be overridden to improve performance. These target specific implementations might, for example, use hardware accelerated drawing operations provided by a display controller or specialized hardware modules in a microcontroller.

Note that some displays require a “flush” operation to write changes from a framebuffer to the display. See docs associated with the chosen display driver for details on how to update the display.

§Examples §Minimum implementation

In this example DrawTarget is implemented for an an imaginary 64px x 64px 8-bit grayscale display that is connected using a simplified SPI interface. Because the hardware doesn’t support any acceleration only the draw_iter method and OriginDimensions trait need to be implemented.

To reduce the overhead caused by communicating with the display for each drawing operation the display driver uses and framebuffer to store the pixel data in memory. This way all drawing operations can be executed in local memory and the actual display is only updated on demand by calling the flush method.

Because all drawing operations are using a local framebuffer no communication error can occur while they are executed and the Error type can be set to core::convert::Infallible.

use core::convert::TryInto;
use embedded_graphics::{
    pixelcolor::{Gray8, GrayColor},
    prelude::*,
    primitives::{Circle, PrimitiveStyle},
};

#[derive(Debug)]
struct CommError;

struct ExampleDisplay {
    framebuffer: [u8; 64 * 64],

    iface: SPI1,
}

impl ExampleDisplay {
    pub fn flush(&self) -> Result<(), CommError> {
        self.iface.send_bytes(&self.framebuffer)
    }
}

impl DrawTarget for ExampleDisplay {
    type Color = Gray8;
    type Error = core::convert::Infallible;

    fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
    where
        I: IntoIterator<Item = Pixel<Self::Color>>,
    {
        for Pixel(coord, color) in pixels.into_iter() {
            if let Ok((x @ 0..=63, y @ 0..=63)) = coord.try_into() {
                let index: u32 = x + y * 64;
                self.framebuffer[index as usize] = color.luma();
            }
        }

        Ok(())
    }
}

impl OriginDimensions for ExampleDisplay {
    fn size(&self) -> Size {
        Size::new(64, 64)
    }
}

let mut display = ExampleDisplay {
    framebuffer: [0; 4096],
    iface: SPI1,
};

let circle = Circle::new(Point::new(22, 22), 20)
    .into_styled(PrimitiveStyle::with_stroke(Gray8::WHITE, 1));

circle.draw(&mut display)?;

display.flush().unwrap();
§Hardware acceleration - solid rectangular fill

This example uses an imaginary display with 16bpp RGB565 colors and hardware support for filling of rectangular areas with a solid color. A real display controller that supports this operation is the SSD1331 with it’s “Draw Rectangle” (22h) command which this example is loosely based on.

To leverage this feature in a DrawTarget, the default implementation of fill_solid can be overridden by a custom implementation. Instead of drawing individual pixels, this target specific version will only send a single command to the display controller in one transaction. Because the command size is independent of the filled area, all fill_solid calls will only transmit 8 bytes to the display, which is far less then what is required to transmit each pixel color inside the filled area.

use core::convert::TryInto;
use embedded_graphics::{
    pixelcolor::{raw::RawU16, Rgb565, RgbColor},
    prelude::*,
    primitives::{Circle, Rectangle, PrimitiveStyle, PrimitiveStyleBuilder},
};

#[derive(Debug)]
struct CommError;

struct ExampleDisplay {
    iface: SPI1,
}

impl ExampleDisplay {
    pub fn set_pixel(&self, x: u32, y: u32, color: u16) -> Result<(), CommError> {
        Ok(())
    }

    pub fn send_commands(&self, commands: &[u8]) -> Result<(), CommError> {
        Ok(())
    }
}

impl DrawTarget for ExampleDisplay {
    type Color = Rgb565;
    type Error = CommError;

    fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
    where
        I: IntoIterator<Item = Pixel<Self::Color>>,
    {
        for Pixel(coord, color) in pixels.into_iter() {
            if let Ok((x @ 0..=63, y @ 0..=63)) = coord.try_into() {
                self.set_pixel(x, y, RawU16::from(color).into_inner())?;
            }
        }

        Ok(())
    }

    fn fill_solid(&mut self, area: &Rectangle, color: Self::Color) -> Result<(), Self::Error> {
        let area = area.intersection(&self.bounding_box());

        let bottom_right = if let Some(bottom_right) = area.bottom_right() {
            bottom_right
        } else {
            return Ok(());
        };

        self.send_commands(&[
            0x22,
            area.top_left.x as u8,
            area.top_left.y as u8,
            bottom_right.x as u8,
            bottom_right.y as u8,
            color.r(),
            color.g(),
            color.b(),
        ])
    }
}

impl OriginDimensions for ExampleDisplay {
    fn size(&self) -> Size {
        Size::new(64, 64)
    }
}

let mut display = ExampleDisplay { iface: SPI1 };

Rectangle::new(Point::new(20, 20), Size::new(50, 40))
    .into_styled(
        PrimitiveStyleBuilder::new()
            .stroke_color(Rgb565::RED)
            .stroke_width(5)
            .fill_color(Rgb565::GREEN)
            .build(),
    )
    .draw(&mut display)?;

Circle::new(Point::new(5, 5), 10)
    .into_styled(
        PrimitiveStyleBuilder::new()
            .stroke_color(Rgb565::MAGENTA)
            .stroke_width(1)
            .fill_color(Rgb565::CYAN)
            .build(),
    )
    .draw(&mut display)?;
Source

The pixel color type the targetted display supports.

Source

Error type to return when a drawing operation fails.

This error is returned if an error occurred during a drawing operation. This mainly applies to drivers that need to communicate with the display for each drawing operation, where a communication error can occur. For drivers that use an internal framebuffer where drawing operations can never fail, core::convert::Infallible can instead be used as the Error type.

Source

Draw individual pixels to the display without a defined order.

Due to the unordered nature of the pixel iterator, this method is likely to be the slowest drawing method for a display that writes data to the hardware immediately. If possible, the other methods in this trait should be implemented to improve performance when rendering more contiguous pixel patterns.

Source

Fill a given area with an iterator providing a contiguous stream of pixel colors.

Use this method to fill an area with contiguous, non-transparent pixel colors. Pixel coordinates are iterated over from the top left to the bottom right corner of the area in row-first order. The provided iterator must provide pixel color values based on this ordering to produce correct output.

As seen in the example below, the PointsIter::points method can be used to get an iterator over all points in the provided area.

The provided iterator is not required to provide width * height pixels to completely fill the area. In this case, fill_contiguous should return without error.

This method should not attempt to draw any pixels that fall outside the drawable area of the target display. The area argument can be clipped to the drawable area using the Rectangle::intersection method.

The default implementation of this method delegates to draw_iter.

§Examples

This is an example implementation of fill_contiguous that delegates to draw_iter. This delegation behaviour is undesirable in a real application as it will be as slow as the default trait implementation, however is shown here for demonstration purposes.

The example demonstrates the usage of Rectangle::intersection on the passed area argument to only draw visible pixels. If there is no intersection between area and the display area, no pixels will be drawn.

use embedded_graphics::{
    pixelcolor::{Gray8, GrayColor},
    prelude::*,
    primitives::{ContainsPoint, Rectangle},
};

struct ExampleDisplay;

impl DrawTarget for ExampleDisplay {
    type Color = Gray8;
    type Error = core::convert::Infallible;

    fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
    where
        I: IntoIterator<Item = Pixel<Self::Color>>,
    {
        Ok(())
    }

    fn fill_contiguous<I>(&mut self, area: &Rectangle, colors: I) -> Result<(), Self::Error>
    where
        I: IntoIterator<Item = Self::Color>,
    {
        let drawable_area = area.intersection(&self.bounding_box());

        if drawable_area.size != Size::zero() {
            self.draw_iter(
                area.points()
                    .zip(colors)
                    .filter(|(pos, _color)| drawable_area.contains(*pos))
                    .map(|(pos, color)| Pixel(pos, color)),
            )
        } else {
            Ok(())
        }
    }
}

impl OriginDimensions for ExampleDisplay {
    fn size(&self) -> Size {
        Size::new(64, 64)
    }
}
Source

Fill a given area with a solid color.

If the target display provides optimized hardware commands for filling a rectangular area of the display with a solid color, this method should be overridden to use those commands to improve performance.

The default implementation of this method calls fill_contiguous with an iterator that repeats the given color for every point in area.

Source

Fill the entire display with a solid color.

If the target hardware supports a more optimized way of filling the entire display with a solid color, this method should be overridden to use those commands.

The default implementation of this method delegates to fill_solid to fill the bounding_box returned by the Dimensions implementation.

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4