Expand description
Frames Per Second counter for embedded devices.
Create an FPS
struct by passing the MAX_FPS
(maximum frames per seconds)
that you expect to hit and a embedded_time::Clock
implementation.
§Examples
§Frames Per Second with a simple for-loop
You can also run this example from the examples
directory using:
cargo run --features=std --example fps_counter
use embedded_fps::{FPS, StdClock};
use std::{thread::sleep, time::Duration};
let std_clock = StdClock::default();
let mut fps_counter = FPS::<10, _>::new(std_clock);
for i in 0..20 {
// sleep for 125 milliseconds
// this will give us 8 FPS
sleep(Duration::from_millis(125));
let fps = fps_counter.tick();
println!("Frames per second: {fps}")
}
§Frames Per Second with embedded-graphics
This crate is suitable for usage with the embedded-graphics
crate
when you want to know, log or even show the frames per second of a
display with an embedded device.
Note: This example requires embedded-graphics-simulator
and SDL2
installed
on your machine.
Refer to the embedded-graphics-simulator
documentation
for detailed instructions.
You can also run this example from the examples
directory using:
cargo run --features=std --example fps_counter
use embedded_fps::{StdClock, FPS};
use embedded_graphics::{
draw_target::DrawTarget,
mono_font::{ascii::FONT_10X20, MonoTextStyle},
pixelcolor::BinaryColor,
prelude::{Point, Size},
text::Text,
Drawable,
};
use embedded_graphics_simulator::{
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
pub const DISPLAY_360P: Size = Size::new(480, 360);
fn main() -> Result<(), core::convert::Infallible> {
let mut display = SimulatorDisplay::<BinaryColor>::new(DISPLAY_360P);
let output_settings = OutputSettingsBuilder::new().scale(1).build();
let mut window = Window::new("FPS using embedded-graphics & simulator", &output_settings);
// starts the StdClock
// `200` MAX_FPS is more than enough since `SimulatorDisplay`
// doesn't reach more than `15` FPS when using `BinaryColor`.
let mut fps_counter = FPS::<200, StdClock>::default();
// create an initial value for FPS
let mut fps = 0;
'running: loop {
display.clear(BinaryColor::Off)?;
let character_style = MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
let fps_position = Point::new(20, 30);
Text::new(&format!("FPS: {fps}"), fps_position, character_style).draw(&mut display)?;
window.update(&display);
if window.events().any(|e| e == SimulatorEvent::Quit) {
break 'running Ok(());
}
// tick and update the FPS at the end of the loop
fps = fps_counter.tick();
}
}