fix vec len, add moving transparent line example
This commit is contained in:
		
							parent
							
								
									02bac1e173
								
							
						
					
					
						commit
						35ae1f20ce
					
				
					 5 changed files with 81 additions and 3 deletions
				
			
		
							
								
								
									
										11
									
								
								examples/moving_transparent_line/Cargo.toml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								examples/moving_transparent_line/Cargo.toml
									
										
									
									
									
										Normal 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"
 | 
			
		||||
							
								
								
									
										47
									
								
								examples/moving_transparent_line/src/main.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								examples/moving_transparent_line/src/main.rs
									
										
									
									
									
										Normal 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));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue