mirror of
https://github.com/cccb/servicepoint.git
synced 2025-01-18 18:10:14 +01:00
fix vec len, add moving transparent line example
This commit is contained in:
parent
02bac1e173
commit
35ae1f20ce
10
examples/Cargo.lock
generated
10
examples/Cargo.lock
generated
|
@ -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"
|
||||
|
|
|
@ -4,5 +4,6 @@ members = [
|
|||
"announce",
|
||||
"game_of_life",
|
||||
"moving_line",
|
||||
"moving_transparent_line",
|
||||
"random_brightness",
|
||||
]
|
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));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/// A vector of bits
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Clone)]
|
||||
pub struct BitVec {
|
||||
data: Vec<u8>,
|
||||
}
|
||||
|
@ -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<Vec<u8>> 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()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue