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 screenC
- 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>
impl<const R: usize, const C: usize> LcdScreen<R, C>
Sourcepub fn new(
sdl_context: &Sdl,
title: &str,
on_color: Color,
off_color: Color,
dot_width: u32,
dot_height: u32,
) -> Result<LcdScreen<R, C>, LcdError>
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
- AnSdl
context objecttitle
- The title of the window containing the screenon_color
- AColor
object representing the color of a dot when it is ‘on’off_color
- AColor
object representing the color of a dot when it is ‘off’dot_width
- The width of a dot on the screen in pixelsdot_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
LcdError::Video
when there is an error initializing the SDL video subsystemLcdError::WindowBuild
when there is an error building the windowLcdError::CanvasBuild
when there is an error building the window canvasLcdError::WindowWidth
when the total window width, in pixels, would exceedi32::MAX
LcdError::WindowHeight
when the total window width, in pixels, would exceedi32::MAX
Sourcepub fn draw_bitmap<'a, BM: Into<&'a Bitmap<C, R>>>(
&mut self,
bm: BM,
) -> Result<(), LcdError>
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
- ABitmap
, 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more