Crates.io | simulate-lcd |
lib.rs | simulate-lcd |
version | 0.1.2 |
source | src |
created_at | 2023-03-31 00:27:09.385168 |
updated_at | 2023-03-31 01:28:21.266904 |
description | A simple library to simulate monochrome dot-matrix displays, such as monochrome LCD screens. |
homepage | |
repository | https://github.com/simonvarey/simulate-lcd |
max_upload_size | |
id | 825844 |
size | 42,499 |
A simple library to simulate monochrome dot-matrix displays, such as monochrome LCD screens.
use std::{thread::sleep, time::Duration};
use rand::{thread_rng, Rng};
use sdl2::event::Event;
use simulate_lcd::{Bitmap, LcdScreen}
use simulate_lcd::{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 Demo: 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 { .. } => 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::new(0, NANOS_PER_SEC / 60));
}
}
More examples can be found in the examples folder.
LcdScreen
is the main type provided by this crate. To create new simulated screen window with R
rows and C
columns of dots, use the function LcdScreen::<R, C>::new
, with the following parameters:
sdl_context
: an Sdl
context objecttitle
: the window titleon_color
: the color of a dot when it is 'on'. e.g. (near) black on a backlight LCD screenoff_color
: the color of a dot when it is 'off'. e.g. green on a green backlight LCD screendot_width
: the width of a dot in pixels of the actual windowdot_height
: the height of a dot in pixels of the actual windowThe screen will disappear as soon as the LcdScreen
object is dropped, including at the end of the scope it was created. Use a loop, or some other device, to stop the screen object from being dropped.
New images can be drawn to the screen using the draw_bitmap
method. draw_bitmap
takes any object which can be converted into a [[
bool
;
C
];
R
]
array. Each true
in this row-major array represents a dot that is 'on'. simulate-lcd offers Bitmap<C, R>
as a convenient alias for [[
bool
;
C
];
R
]
.
The 'on' and 'off' colors of the screen are sdl2::pixels::Color
objects. They can be created from RGB values with the sdl2::pixels::Color::RGB
function. simulate-lcd offers the LCD_DARK_GREEN
and LCD_LIGHT_GREEN
constants from simulating green backlight LCD screens.
simulate-lcd is built around the sdl2 crate. A new LcdScreen
requires an Sdl
context object created by the sdl2::init()
function. Note that sdl2 may require further setup than just adding the crate. See the sdl2 README for details.
Licensed under the Apache License, Version 2.0 (LICENSE or http://www.apache.org/licenses/LICENSE-2.0).