From 35ae1f20ce52e36c6fbb94c74d183dda0cc1a0af Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 11 May 2024 18:28:57 +0200 Subject: [PATCH] fix vec len, add moving transparent line example --- examples/Cargo.lock | 10 +++++ examples/Cargo.toml | 3 +- examples/moving_transparent_line/Cargo.toml | 11 +++++ examples/moving_transparent_line/src/main.rs | 47 ++++++++++++++++++++ src/bit_vec.rs | 13 +++++- 5 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 examples/moving_transparent_line/Cargo.toml create mode 100644 examples/moving_transparent_line/src/main.rs diff --git a/examples/Cargo.lock b/examples/Cargo.lock index b2eb944..4deacdb 100644 --- a/examples/Cargo.lock +++ b/examples/Cargo.lock @@ -219,6 +219,16 @@ dependencies = [ "servicepoint2", ] +[[package]] +name = "moving_transparent_line" +version = "0.1.0" +dependencies = [ + "clap", + "env_logger", + "log", + "servicepoint2", +] + [[package]] name = "num" version = "0.4.3" diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 20a2220..76d1d8b 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -4,5 +4,6 @@ members = [ "announce", "game_of_life", "moving_line", + "moving_transparent_line", "random_brightness", -] \ No newline at end of file +] diff --git a/examples/moving_transparent_line/Cargo.toml b/examples/moving_transparent_line/Cargo.toml new file mode 100644 index 0000000..c38f62f --- /dev/null +++ b/examples/moving_transparent_line/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "moving_transparent_line" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +servicepoint2 = { path = "../.." } +clap = { version = "4.5.4", features = ["derive"] } +env_logger = "0.11.3" +log = "0.4.21" diff --git a/examples/moving_transparent_line/src/main.rs b/examples/moving_transparent_line/src/main.rs new file mode 100644 index 0000000..01a3604 --- /dev/null +++ b/examples/moving_transparent_line/src/main.rs @@ -0,0 +1,47 @@ +use std::thread; +use std::time::Duration; + +use clap::Parser; + +use servicepoint2::{BitVec, Connection, PIXEL_HEIGHT, PIXEL_WIDTH, PixelGrid}; +use servicepoint2::Command::{BitmapLinearOr, BitmapLinearXor}; + +#[derive(Parser, Debug)] +struct Cli { + #[arg(short, long, default_value = "localhost:2342")] + destination: String, +} + +fn main() { + env_logger::builder() + .filter_level(log::LevelFilter::Debug) + .init(); + + let connection = Connection::open(Cli::parse().destination).unwrap(); + + loop { + let mut last = BitVec::new(PIXEL_WIDTH as usize * PIXEL_HEIGHT as usize); + for x_offset in 0..PIXEL_WIDTH as usize { + let mut pixels = PixelGrid::max_sized(); + pixels.fill(false); + + for y in 0..PIXEL_HEIGHT as usize { + pixels.set((y + x_offset) % PIXEL_WIDTH as usize, y, true); + } + + // this works because the pixel grid has max size + let pixel_data: Vec = pixels.into(); + let bit_vec = BitVec::load(&*pixel_data); + + // remove pixels from last iteration + connection.send(BitmapLinearXor(0, last)).unwrap(); + // reduces dropped packages + thread::sleep(Duration::from_millis(1)); + // add pixels from this iteration + connection.send(BitmapLinearOr(0, bit_vec.clone())).unwrap(); + + last = bit_vec; + thread::sleep(Duration::from_millis(1000)); + } + } +} diff --git a/src/bit_vec.rs b/src/bit_vec.rs index 3a8de07..524fbec 100644 --- a/src/bit_vec.rs +++ b/src/bit_vec.rs @@ -1,5 +1,5 @@ /// A vector of bits -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct BitVec { data: Vec, } @@ -40,7 +40,7 @@ impl BitVec { } pub fn len(&self) -> usize { - self.data.len() + self.data.len() * 8 } fn get_indexes(&self, index: usize) -> (usize, u8) { @@ -56,3 +56,12 @@ impl Into> for BitVec { self.data } } + +impl std::fmt::Debug for BitVec { + fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fmt.debug_struct("BitVec") + .field("len", &self.len()) + .field("data", &self.data) + .finish() + } +}