Struct LcdScreen

Source
pub struct LcdScreen<const R: usize, const C: usize> { /* private fields */ }
Expand description

A simulated LCD dot-matrix screen.

The screen has R rows and C columns of dots. Note: The number of rows and columns of dots for the screen is specified as a const parameter on the type of the screen, rather than as an argument to the constructor function new.

§Parameters

  • R - The number of rows of dots of the screen
  • C - The number of columns of dots of the screen

§Examples

use std::{thread::sleep, time::Duration};

use rand::{thread_rng, Rng};
use sdl2::{event::Event, keyboard::Keycode};
use simulate_lcd::{Bitmap, LcdScreen, LCD_DARK_GREEN, LCD_LIGHT_GREEN};

const NANOS_PER_SEC: u64 = 1_000_000_000;

fn main() {
    let sdl_context = sdl2::init().unwrap();
    let mut screen = LcdScreen::<64, 96>::new(
        &sdl_context,
        "LCD Example: Random",
        LCD_DARK_GREEN,
        LCD_LIGHT_GREEN,
        10,
        10,
     )
     .unwrap();

     let mut event_pump = sdl_context.event_pump().unwrap();
     'running: loop {
        for event in event_pump.poll_iter() {
            match event {
                Event::Quit { .. }
                | Event::KeyDown {
                    keycode: Some(Keycode::Escape),
                    ..
                } => break 'running,
                _ => {}
             }
         }
         let mut rng = thread_rng();

         let random_bits: Vec<[bool; 96]> = (0..64).map(|_| rng.gen()).collect();

         screen.draw_bitmap(&random_bits.try_into().unwrap()).unwrap();

         sleep(Duration::from_nanos(NANOS_PER_SEC / 60));
     }
 }

Implementations§

Source§

impl<const R: usize, const C: usize> LcdScreen<R, C>

Source

pub fn new( sdl_context: &Sdl, title: &str, on_color: Color, off_color: Color, dot_width: u32, dot_height: u32, ) -> Result<LcdScreen<R, C>, LcdError>

Creates a simulated LCD screen.

Note: The number of rows and columns of dots for a screen is specified as a const parameter on the type of the screen, rather than as an argument to this function.

§Arguments
  • sdl_context - An Sdl context object
  • title - The title of the window containing the screen
  • on_color - A Color object representing the color of a dot when it is ‘on’
  • off_color - A Color object representing the color of a dot when it is ‘off’
  • dot_width - The width of a dot on the screen in pixels
  • dot_height - The height of a dot on the screen in pixels
§Examples
let mut screen = LcdScreen::<64, 96>::new(
        &sdl_context,
        "LCD Example: Blank",
        LCD_DARK_GREEN,
        LCD_LIGHT_GREEN,
        10,
        10,
     )
     .unwrap();
§Errors
Source

pub fn draw_bitmap<'a, BM: Into<&'a Bitmap<C, R>>>( &mut self, bm: BM, ) -> Result<(), LcdError>

Draws a bitmap to a simulated LCD screen.

§Arguments
  • bm - A Bitmap, or something that can be converted into a bitmap, to write to the LCD screen
§Examples
let mut screen = LcdScreen::<2, 2>::new(
        &sdl_context,
        "LCD Example: Checkerboard",
        LCD_DARK_GREEN,
        LCD_LIGHT_GREEN,
        100,
        100,
     )
     .unwrap();

screen.draw_bitmap(&[[true, false], [false, true]]);
§Errors
  • LcdError::Fill when there is an error filling one of the dots with the relevant color

Auto Trait Implementations§

§

impl<const R: usize, const C: usize> Freeze for LcdScreen<R, C>

§

impl<const R: usize, const C: usize> RefUnwindSafe for LcdScreen<R, C>

§

impl<const R: usize, const C: usize> !Send for LcdScreen<R, C>

§

impl<const R: usize, const C: usize> !Sync for LcdScreen<R, C>

§

impl<const R: usize, const C: usize> Unpin for LcdScreen<R, C>

§

impl<const R: usize, const C: usize> UnwindSafe for LcdScreen<R, C>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.