~caolan/bramley

a30138cb5e44d5924fb7f7086f9e5a5d3d238656 — Caolan McMahon 1 year, 4 months ago 8e32e2a master
draw character preview on black background
4 files changed, 46 insertions(+), 3 deletions(-)

M drawing/src/lib.rs
M menu/src/main.rs
M shutdown-complete/src/main.rs
M shutdown-start/src/main.rs
M drawing/src/lib.rs => drawing/src/lib.rs +36 -1
@@ 29,6 29,14 @@ pub fn size_of_text(font: &Font, font_size: f32, lines: &[&str]) -> (usize, usiz
    (max_line_width as usize, height)
}

/// Returns the inverse pixel color to the one provided
pub fn invert_pixel(pixel: Pixel) -> Pixel {
    match pixel {
        Pixel::Black => Pixel::White,
        Pixel::White => Pixel::Black,
    }
}

pub trait MemoryDisplayBufferExt {
    /// Renders lines of text using the provided Font at the given x/y
    /// coordinates.


@@ 39,6 47,17 @@ pub trait MemoryDisplayBufferExt {
        lines: &[&str],
        x: usize,
        y: u8,
        color: Pixel,
    );

    /// Draws filled rectangle at given coordinates.
    fn draw_rect_filled(
        &mut self,
        x: usize,
        y: u8,
        width: usize,
        height: u8,
        color: Pixel,
    );
}



@@ 50,6 69,7 @@ impl MemoryDisplayBufferExt for MemoryDisplayBuffer {
        lines: &[&str],
        x: usize,
        mut y: u8,
        color: Pixel,
    ) {
        let line_height: u8 = font_size.ceil() as u8;
        for line in lines {


@@ 67,7 87,7 @@ impl MemoryDisplayBufferExt for MemoryDisplayBuffer {
                            gx as usize + x + bounding_box.min.x as usize,
                            gy as u8 + y + bounding_box.min.y as u8,
                            // Turn the coverage into an alpha value
                            if gv >= 0.5 {Pixel::Black} else { Pixel::White },
                            if gv >= 0.5 { color } else { invert_pixel(color) },
                        );
                    });
                }


@@ 75,4 95,19 @@ impl MemoryDisplayBufferExt for MemoryDisplayBuffer {
            y += line_height;
        }
    }

    fn draw_rect_filled(
        &mut self,
        x: usize,
        y: u8,
        width: usize,
        height: u8,
        color: Pixel,
    ) {
        for j in y..(y + height) {
            for i in x..(x + width) {
                self.set_pixel(i, j, color);
            }
        }
    }
}

M menu/src/main.rs => menu/src/main.rs +6 -0
@@ 91,7 91,9 @@ async fn main() {
            &[&text],
            10,
            10,
            Pixel::Black,
        );
        // TODO: why is this called twice?
        let (_, text_bottom) = size_of_text(
            &font,
            font_size,


@@ 175,11 177,15 @@ fn draw_carat(
    for i in 0..height {
        buffer.set_pixel(x, y + i, Pixel::Black);
    }

    let (tw, th) = size_of_text(font, font_size, &[preview]);
    buffer.draw_rect_filled(x, y, tw, th as u8, Pixel::Black);
    buffer.draw_text(
        font,
        font_size,
        &[preview],
        x,
        y,
        Pixel::White,
    );
}

M shutdown-complete/src/main.rs => shutdown-complete/src/main.rs +2 -1
@@ 1,4 1,4 @@
use rpi_memory_display::{MemoryDisplay, MemoryDisplayBuffer};
use rpi_memory_display::{MemoryDisplay, MemoryDisplayBuffer, Pixel};
use rppal::spi::{Bus, SlaveSelect};
use rusttype::Font;
use drawing::{size_of_text, MemoryDisplayBufferExt};


@@ 39,6 39,7 @@ fn main() {
        lines,
        x,
        y,
        Pixel::Black,
    );
    display.update(&buffer).unwrap();
}

M shutdown-start/src/main.rs => shutdown-start/src/main.rs +2 -1
@@ 1,4 1,4 @@
use rpi_memory_display::{MemoryDisplay, MemoryDisplayBuffer};
use rpi_memory_display::{MemoryDisplay, MemoryDisplayBuffer, Pixel};
use rppal::spi::{Bus, SlaveSelect};
use rusttype::Font;
use drawing::{size_of_text, MemoryDisplayBufferExt};


@@ 41,6 41,7 @@ fn main() {
        lines,
        x,
        y,
        Pixel::Black,
    );
    display.update(&buffer).unwrap();
}