servicepoint-binding-uniffi/examples/random_brightness/src/main.rs

70 lines
1.9 KiB
Rust
Raw Normal View History

2024-05-11 17:34:52 +02:00
use std::time::Duration;
2024-05-11 23:28:08 +02:00
2024-05-11 17:34:52 +02:00
use clap::Parser;
use rand::Rng;
2024-05-11 23:28:08 +02:00
2024-05-16 23:18:43 +02:00
use servicepoint2::{
ByteGrid, CompressionCode, Connection, Origin, PixelGrid, TILE_HEIGHT,
TILE_WIDTH,
};
2024-05-11 23:28:08 +02:00
use servicepoint2::Command::{BitmapLinearWin, Brightness, CharBrightness};
2024-05-11 17:34:52 +02:00
#[derive(Parser, Debug)]
struct Cli {
#[arg(short, long, default_value = "localhost:2342")]
destination: String,
#[arg(short, long, default_value_t = true)]
enable_all: bool,
#[arg(short, long, default_value_t = 100, allow_negative_numbers = false)]
wait_ms: u64,
}
fn main() {
2024-05-12 13:51:41 +02:00
env_logger::init();
2024-05-11 17:34:52 +02:00
let cli = Cli::parse();
let connection = Connection::open(cli.destination).unwrap();
let wait_duration = Duration::from_millis(cli.wait_ms);
// put all pixels in on state
if cli.enable_all {
let mut filled_grid = PixelGrid::max_sized();
filled_grid.fill(true);
2024-05-16 23:18:43 +02:00
let command = BitmapLinearWin(
Origin::top_left(),
filled_grid,
CompressionCode::Lzma,
);
connection.send(command.into()).expect("send failed");
2024-05-11 17:34:52 +02:00
}
// set all pixels to the same random brightness
let mut rng = rand::thread_rng();
connection.send(Brightness(rng.gen()).into()).unwrap();
2024-05-11 17:34:52 +02:00
// continuously update random windows to new random brightness
loop {
let min_size = 1;
let x: u16 = rng.gen_range(0..TILE_WIDTH - min_size);
let y: u16 = rng.gen_range(0..TILE_HEIGHT - min_size);
let w: u16 = rng.gen_range(min_size..=TILE_WIDTH - x);
let h: u16 = rng.gen_range(min_size..=TILE_HEIGHT - y);
let origin = Origin(x, y);
let mut luma = ByteGrid::new(w as usize, h as usize);
for y in 0..h as usize {
for x in 0..w as usize {
luma.set(x, y, rng.gen());
}
}
2024-05-16 23:18:43 +02:00
connection
.send(CharBrightness(origin, luma).into())
.unwrap();
2024-05-11 17:34:52 +02:00
std::thread::sleep(wait_duration);
}
}