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",
]
[[package]]
name = "moving_transparent_line"
version = "0.1.0"
dependencies = [
"clap",
"env_logger",
"log",
"servicepoint2",
]
[[package]]
name = "num"
version = "0.4.3"

View file

@ -4,5 +4,6 @@ members = [
"announce",
"game_of_life",
"moving_line",
"moving_transparent_line",
"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));
}
}
}