fix vec len, add moving transparent line example

This commit is contained in:
Vinzenz Schroeter 2024-05-11 18:28:57 +02:00
parent 02bac1e173
commit 35ae1f20ce
5 changed files with 81 additions and 3 deletions

10
examples/Cargo.lock generated
View file

@ -219,6 +219,16 @@ dependencies = [
"servicepoint2", "servicepoint2",
] ]
[[package]]
name = "moving_transparent_line"
version = "0.1.0"
dependencies = [
"clap",
"env_logger",
"log",
"servicepoint2",
]
[[package]] [[package]]
name = "num" name = "num"
version = "0.4.3" version = "0.4.3"

View file

@ -4,5 +4,6 @@ members = [
"announce", "announce",
"game_of_life", "game_of_life",
"moving_line", "moving_line",
"moving_transparent_line",
"random_brightness", "random_brightness",
] ]

View file

@ -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"

View file

@ -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<u8> = 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));
}
}
}

View file

@ -1,5 +1,5 @@
/// A vector of bits /// A vector of bits
#[derive(Debug, Clone)] #[derive(Clone)]
pub struct BitVec { pub struct BitVec {
data: Vec<u8>, data: Vec<u8>,
} }
@ -40,7 +40,7 @@ impl BitVec {
} }
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.data.len() self.data.len() * 8
} }
fn get_indexes(&self, index: usize) -> (usize, u8) { fn get_indexes(&self, index: usize) -> (usize, u8) {
@ -56,3 +56,12 @@ impl Into<Vec<u8>> for BitVec {
self.data 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()
}
}