ssd1315 0.3.0

SSD1315 OLED driver.
Documentation
use core::convert::Infallible;

use embedded_graphics_core::Pixel;
use embedded_graphics_core::draw_target::DrawTarget;
use embedded_graphics_core::geometry::{OriginDimensions, Size};
use embedded_graphics_core::pixelcolor::BinaryColor;
use embedded_graphics_core::pixelcolor::raw::ToBytes;

use crate::{DISPLAY_HEIGHT, DISPLAY_WIDTH, DrawFromRaw, Ssd1315};

impl<DI> OriginDimensions for Ssd1315<DI> {
    fn size(&self) -> Size {
        Size::new(DISPLAY_WIDTH as u32, DISPLAY_HEIGHT as u32)
    }
}

impl<DI> DrawTarget for Ssd1315<DI> {
    type Color = BinaryColor;
    type Error = 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() {
            let x = coord.x as usize;
            let y = coord.y as usize;

            if (0..DISPLAY_WIDTH).contains(&x) && (0..DISPLAY_HEIGHT).contains(&y) {
                let byte = &mut self.buffer[y / 8][x];
                let bit = 1 << (y % 8);
                *byte = (*byte & !bit) | (color.to_le_bytes()[0] << (y % 8));
            } else {
                // Pixels outside the buffer can be safely omitted.
                continue;
            }
        }

        Ok(())
    }
}

impl DrawFromRaw for [[u8; DISPLAY_WIDTH]; DISPLAY_HEIGHT / 8] {
    /// Draw raw image.
    fn draw_from_raw<DI>(&self, instance: &mut Ssd1315<DI>) {
        instance.buffer.clone_from(self)
    }
}