embedded_fps/lib.rs
1//! [![sponsor-us]](https://github.com/sponsors/LechevSpace) [![github]](https://github.com/LechevSpace/embedded-fps) [![crates-io]](https://crates.io/crates/embedded-fps)
2//!
3//! Frames Per Second counter for embedded devices.
4//!
5//! Create an [`FPS`] struct by passing the `MAX_FPS` (maximum frames per seconds)
6//! that you expect to hit and a [`embedded_time::Clock`] implementation.
7//!
8//! # Examples
9//!
10//! ## Frames Per Second with a simple for-loop
11//!
12//! You can also run this example from the `examples` directory using:
13//!
14//! `cargo run --features=std --example fps_counter`
15//!
16//! ```
17//! use embedded_fps::{FPS, StdClock};
18//! use std::{thread::sleep, time::Duration};
19//!
20//! let std_clock = StdClock::default();
21//! let mut fps_counter = FPS::<10, _>::new(std_clock);
22//!
23//! for i in 0..20 {
24//! // sleep for 125 milliseconds
25//! // this will give us 8 FPS
26//! sleep(Duration::from_millis(125));
27//!
28//! let fps = fps_counter.tick();
29//! println!("Frames per second: {fps}")
30//! }
31//! ```
32//!
33//! ## Frames Per Second with `embedded-graphics`
34//!
35//! This crate is suitable for usage with the [`embedded-graphics`] crate
36//! when you want to know, log or even show the frames per second of a
37//! display with an embedded device.
38//!
39//! Note: This example requires [`embedded-graphics-simulator`] and `SDL2` installed
40//! on your machine.
41//!
42//! Refer to the [`embedded-graphics-simulator` documentation][simulator-docs]
43//! for detailed instructions.
44//!
45//! You can also run this example from the `examples` directory using:
46//!
47//! `cargo run --features=std --example fps_counter`
48//!
49//! ```no_run
50//! use embedded_fps::{StdClock, FPS};
51//! use embedded_graphics::{
52//! draw_target::DrawTarget,
53//! mono_font::{ascii::FONT_10X20, MonoTextStyle},
54//! pixelcolor::BinaryColor,
55//! prelude::{Point, Size},
56//! text::Text,
57//! Drawable,
58//! };
59//! use embedded_graphics_simulator::{
60//! OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
61//! };
62//!
63//! pub const DISPLAY_360P: Size = Size::new(480, 360);
64//!
65//! fn main() -> Result<(), core::convert::Infallible> {
66//! let mut display = SimulatorDisplay::<BinaryColor>::new(DISPLAY_360P);
67//!
68//! let output_settings = OutputSettingsBuilder::new().scale(1).build();
69//! let mut window = Window::new("FPS using embedded-graphics & simulator", &output_settings);
70//!
71//! // starts the StdClock
72//! // `200` MAX_FPS is more than enough since `SimulatorDisplay`
73//! // doesn't reach more than `15` FPS when using `BinaryColor`.
74//! let mut fps_counter = FPS::<200, StdClock>::default();
75//! // create an initial value for FPS
76//! let mut fps = 0;
77//!
78//! 'running: loop {
79//! display.clear(BinaryColor::Off)?;
80//!
81//! let character_style = MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
82//! let fps_position = Point::new(20, 30);
83//!
84//! Text::new(&format!("FPS: {fps}"), fps_position, character_style).draw(&mut display)?;
85//!
86//! window.update(&display);
87//!
88//! if window.events().any(|e| e == SimulatorEvent::Quit) {
89//! break 'running Ok(());
90//! }
91//!
92//! // tick and update the FPS at the end of the loop
93//! fps = fps_counter.tick();
94//! }
95//! }
96//! ```
97//!
98//! # Crate features
99//!
100//! - `std` - enables [`StdClock`] - a [`Clock`] implementation using [`std`] for usage on a host machine.
101//!
102//! [`StdClock`]: crate::StdClock
103//! [`embedded-graphics`]: https://crates.io/crates/embedded-graphics
104//! [`embedded-graphics-simulator`]: https://crates.io/crates/embedded-graphics-simulator
105//! [simulator-docs]: https://docs.rs/embedded-graphics-simulator/latest/embedded_graphics_simulator/#setup
106//! [github]: https://img.shields.io/badge/github-3873AD?style=for-the-badge&labelColor=555555&logo=github
107//! [crates-io]: https://img.shields.io/crates/v/embedded-fps?logo=rust&style=for-the-badge
108//! [sponsor-us]: https://img.shields.io/github/sponsors/LechevSpace?color=bf3989&label=Sponsor%20us&style=for-the-badge&logoColor=bf3989&logo=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPHN2ZyBoZWlnaHQ9IjE2IiB2aWV3Qm94PSIwIDAgMTYgMTYiIHZlcnNpb249IjEuMSIgd2lkdGg9IjE2IiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgogICAgPHBhdGggZmlsbD0iI2JmMzk4OSIgZmlsbC1ydWxlPSJldmVub2RkIiBkPSJNNC4yNSAyLjVjLTEuMzM2IDAtMi43NSAxLjE2NC0yLjc1IDMgMCAyLjE1IDEuNTggNC4xNDQgMy4zNjUgNS42ODJBMjAuNTY1IDIwLjU2NSAwIDAwOCAxMy4zOTNhMjAuNTYxIDIwLjU2MSAwIDAwMy4xMzUtMi4yMTFDMTIuOTIgOS42NDQgMTQuNSA3LjY1IDE0LjUgNS41YzAtMS44MzYtMS40MTQtMy0yLjc1LTMtMS4zNzMgMC0yLjYwOS45ODYtMy4wMjkgMi40NTZhLjc1Ljc1IDAgMDEtMS40NDIgMEM2Ljg1OSAzLjQ4NiA1LjYyMyAyLjUgNC4yNSAyLjV6TTggMTQuMjVsLS4zNDUuNjY2LS4wMDItLjAwMS0uMDA2LS4wMDMtLjAxOC0uMDFhNy42NDMgNy42NDMgMCAwMS0uMzEtLjE3IDIyLjA3NSAyMi4wNzUgMCAwMS0zLjQzNC0yLjQxNEMyLjA0NSAxMC43MzEgMCA4LjM1IDAgNS41IDAgMi44MzYgMi4wODYgMSA0LjI1IDEgNS43OTcgMSA3LjE1MyAxLjgwMiA4IDMuMDIgOC44NDcgMS44MDIgMTAuMjAzIDEgMTEuNzUgMSAxMy45MTQgMSAxNiAyLjgzNiAxNiA1LjVjMCAyLjg1LTIuMDQ1IDUuMjMxLTMuODg1IDYuODE4YTIyLjA4IDIyLjA4IDAgMDEtMy43NDQgMi41ODRsLS4wMTguMDEtLjAwNi4wMDNoLS4wMDJMOCAxNC4yNXptMCAwbC4zNDUuNjY2YS43NTIuNzUyIDAgMDEtLjY5IDBMOCAxNC4yNXoiPjwvcGF0aD4KPC9zdmc%2BCg%3D%3D
109
110// Rustdoc lints
111#![deny(rustdoc::broken_intra_doc_links)]
112// Rustc lints
113#![deny(
114 missing_debug_implementations,
115 missing_docs,
116 rust_2018_idioms,
117 unreachable_pub,
118 unused_imports
119)]
120// adds `#[no_std]` attribute if the `std` feature is not enabled
121#![cfg_attr(not(feature = "std"), no_std)]
122#![cfg_attr(docsrs, feature(doc_cfg))]
123
124pub use fps::{FPS, Error};
125
126mod fps;
127
128#[cfg(feature = "std")]
129#[doc(inline)]
130pub use std_clock::StdClock;
131
132#[cfg(feature = "std")]
133mod std_clock;