From 46c9174d3d1972d4e6fe3010b45e0f133ef2861f Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 17 May 2024 23:56:08 +0200 Subject: [PATCH] add suggested frame pacing --- examples/game_of_life/src/main.rs | 7 ++++--- examples/moving_line/src/main.rs | 7 +++---- examples/random_brightness/src/main.rs | 9 +++------ examples/wiping_clear/src/main.rs | 10 +++++++--- servicepoint2/src/lib.rs | 4 ++++ 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index faa44db..1b6c3f8 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -1,10 +1,11 @@ use std::thread; -use std::time::Duration; use clap::Parser; use rand::{distributions, Rng}; -use servicepoint2::{Command, CompressionCode, Connection, Origin, PixelGrid}; +use servicepoint2::{ + Command, CompressionCode, Connection, Origin, PixelGrid, FRAME_PACING, +}; #[derive(Parser, Debug)] struct Cli { @@ -32,7 +33,7 @@ fn main() { .into(), ) .expect("could not send"); - thread::sleep(Duration::from_millis(30)); + thread::sleep(FRAME_PACING); field = iteration(field); } } diff --git a/examples/moving_line/src/main.rs b/examples/moving_line/src/main.rs index 31b9a84..23e34d4 100644 --- a/examples/moving_line/src/main.rs +++ b/examples/moving_line/src/main.rs @@ -1,11 +1,10 @@ use std::thread; -use std::time::Duration; use clap::Parser; use servicepoint2::{ - Command, CompressionCode, Connection, Origin, PIXEL_HEIGHT, PIXEL_WIDTH, - PixelGrid, + Command, CompressionCode, Connection, Origin, PixelGrid, FRAME_PACING, + PIXEL_HEIGHT, PIXEL_WIDTH, }; #[derive(Parser, Debug)] @@ -36,6 +35,6 @@ fn main() { .into(), ) .unwrap(); - thread::sleep(Duration::from_millis(14)); + thread::sleep(FRAME_PACING); } } diff --git a/examples/random_brightness/src/main.rs b/examples/random_brightness/src/main.rs index a8a388f..3235390 100644 --- a/examples/random_brightness/src/main.rs +++ b/examples/random_brightness/src/main.rs @@ -3,11 +3,11 @@ use std::time::Duration; use clap::Parser; use rand::Rng; +use servicepoint2::Command::{BitmapLinearWin, Brightness, CharBrightness}; use servicepoint2::{ ByteGrid, CompressionCode, Connection, Origin, PixelGrid, TILE_HEIGHT, TILE_WIDTH, }; -use servicepoint2::Command::{BitmapLinearWin, Brightness, CharBrightness}; #[derive(Parser, Debug)] struct Cli { @@ -31,11 +31,8 @@ fn main() { let mut filled_grid = PixelGrid::max_sized(); filled_grid.fill(true); - let command = BitmapLinearWin( - Origin(0, 0), - filled_grid, - CompressionCode::Lzma, - ); + let command = + BitmapLinearWin(Origin(0, 0), filled_grid, CompressionCode::Lzma); connection.send(command.into()).expect("send failed"); } diff --git a/examples/wiping_clear/src/main.rs b/examples/wiping_clear/src/main.rs index bf44c67..5c9818e 100644 --- a/examples/wiping_clear/src/main.rs +++ b/examples/wiping_clear/src/main.rs @@ -4,8 +4,8 @@ use std::time::Duration; use clap::Parser; use servicepoint2::{ - BitVec, Command, CompressionCode, Connection, PixelGrid, PIXEL_HEIGHT, - PIXEL_WIDTH, + BitVec, Command, CompressionCode, Connection, PixelGrid, FRAME_PACING, + PIXEL_HEIGHT, PIXEL_WIDTH, }; #[derive(Parser, Debug)] @@ -19,7 +19,11 @@ struct Cli { fn main() { env_logger::init(); let cli = Cli::parse(); - let sleep_duration = Duration::from_millis(cli.time / PIXEL_WIDTH as u64); + + let sleep_duration = Duration::max( + FRAME_PACING, + Duration::from_millis(cli.time / PIXEL_WIDTH as u64), + ); let connection = Connection::open(cli.destination).unwrap(); diff --git a/servicepoint2/src/lib.rs b/servicepoint2/src/lib.rs index 2433693..8e028e3 100644 --- a/servicepoint2/src/lib.rs +++ b/servicepoint2/src/lib.rs @@ -5,6 +5,7 @@ pub use crate::compression_code::CompressionCode; pub use crate::connection::Connection; pub use crate::packet::{Header, Packet, Payload}; pub use crate::pixel_grid::PixelGrid; +use std::time::Duration; #[cfg(feature = "c_api")] pub use crate::c_slice::CByteSlice; @@ -32,3 +33,6 @@ pub const PIXEL_WIDTH: u16 = TILE_WIDTH * TILE_SIZE; pub const PIXEL_HEIGHT: u16 = TILE_HEIGHT * TILE_SIZE; /// pixel count on whole screen pub const PIXEL_COUNT: usize = PIXEL_WIDTH as usize * PIXEL_HEIGHT as usize; + +/// Actual hardware limit is around 28-29ms/frame. Rounded up for less dropped packets. +pub const FRAME_PACING: Duration = Duration::from_millis(30);