move examples into package (include them in published crate)

This commit is contained in:
Vinzenz Schroeter 2024-05-26 13:55:19 +02:00
parent 5514f60c28
commit c92493fad1
22 changed files with 26 additions and 165 deletions

View file

@ -26,3 +26,8 @@ compression_bzip2 = ["dep:bzip2"]
compression_lzma = ["dep:rust-lzma"]
compression_zstd = ["dep:zstd"]
all_compressions = ["compression_zlib", "compression_bzip2", "compression_lzma", "compression_zstd"]
[dev-dependencies]
# for examples
clap = { version = "4.5", features = ["derive"] }
rand = "0.8"

View file

@ -0,0 +1,43 @@
use clap::Parser;
use servicepoint::{ByteGrid, Command, Connection, Grid, Origin};
#[derive(Parser, Debug)]
struct Cli {
#[arg(short, long, default_value = "localhost:2342")]
destination: String,
#[arg(short, long, num_args = 1.., value_delimiter = '\n')]
text: Vec<String>,
#[arg(short, long, default_value_t = true)]
clear: bool,
}
/// example: `cargo run -- --text "Hallo,
/// CCCB"`
fn main() {
let mut cli = Cli::parse();
if cli.text.is_empty() {
cli.text.push("Hello, CCCB!".to_string());
}
let connection = Connection::open(&cli.destination).unwrap();
if cli.clear {
connection.send(Command::Clear).unwrap();
}
let max_width = cli.text.iter().map(|t| t.len()).max().unwrap();
let mut chars = ByteGrid::new(max_width, cli.text.len());
for y in 0..cli.text.len() {
let row = &cli.text[y];
for (x, char) in row.chars().enumerate() {
let char = char.try_into().expect("invalid input char");
chars.set(x, y, char);
}
}
connection
.send(Command::Cp437Data(Origin(0, 0), chars))
.unwrap();
}

View file

@ -0,0 +1,89 @@
use std::thread;
use clap::Parser;
use rand::{distributions, Rng};
use servicepoint::*;
#[derive(Parser, Debug)]
struct Cli {
#[arg(short, long, default_value = "localhost:2342")]
destination: String,
#[arg(short, long, default_value_t = 0.5f64)]
probability: f64,
}
fn main() {
let cli = Cli::parse();
let connection = Connection::open(&cli.destination).unwrap();
let mut field = make_random_field(cli.probability);
loop {
connection
.send(Command::BitmapLinearWin(
Origin(0, 0),
field.clone(),
CompressionCode::Lzma,
))
.expect("could not send");
thread::sleep(FRAME_PACING);
field = iteration(field);
}
}
fn iteration(field: PixelGrid) -> PixelGrid {
let mut next = field.clone();
for x in 0..field.width() {
for y in 0..field.height() {
let old_state = field.get(x, y);
let neighbors = count_neighbors(&field, x as i32, y as i32);
let new_state = matches!(
(old_state, neighbors),
(true, 2) | (true, 3) | (false, 3)
);
next.set(x, y, new_state);
}
}
next
}
fn count_neighbors(field: &PixelGrid, x: i32, y: i32) -> i32 {
let mut count = 0;
for nx in x - 1..=x + 1 {
for ny in y - 1..=y + 1 {
if nx == x && ny == y {
continue; // the cell itself does not count
}
if nx < 0
|| ny < 0
|| nx >= field.width() as i32
|| ny >= field.height() as i32
{
continue; // pixels outside the grid do not count
}
if !field.get(nx as usize, ny as usize) {
continue; // dead cells do not count
}
count += 1;
}
}
count
}
fn make_random_field(probability: f64) -> PixelGrid {
let mut field = PixelGrid::max_sized();
let mut rng = rand::thread_rng();
let d = distributions::Bernoulli::new(probability).unwrap();
for x in 0..field.width() {
for y in 0..field.height() {
field.set(x, y, rng.sample(d));
}
}
field
}

View file

@ -0,0 +1,32 @@
use std::thread;
use clap::Parser;
use servicepoint::*;
#[derive(Parser, Debug)]
struct Cli {
#[arg(short, long, default_value = "localhost:2342")]
destination: String,
}
fn main() {
let connection = Connection::open(Cli::parse().destination).unwrap();
let mut pixels = PixelGrid::max_sized();
for x_offset in 0..usize::MAX {
pixels.fill(false);
for y in 0..PIXEL_HEIGHT {
pixels.set((y + x_offset) % PIXEL_WIDTH, y, true);
}
connection
.send(Command::BitmapLinearWin(
Origin(0, 0),
pixels.clone(),
CompressionCode::Lzma,
))
.unwrap();
thread::sleep(FRAME_PACING);
}
}

View file

@ -0,0 +1,60 @@
use std::time::Duration;
use clap::Parser;
use rand::Rng;
use servicepoint::Command::{BitmapLinearWin, Brightness, CharBrightness};
use servicepoint::*;
#[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() {
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);
let command =
BitmapLinearWin(Origin(0, 0), filled_grid, CompressionCode::Lzma);
connection.send(command).expect("send failed");
}
// set all pixels to the same random brightness
let mut rng = rand::thread_rng();
connection.send(Brightness(rng.gen())).unwrap();
// continuously update random windows to new random brightness
loop {
let min_size = 1;
let x = rng.gen_range(0..TILE_WIDTH - min_size);
let y = rng.gen_range(0..TILE_HEIGHT - min_size);
let w = rng.gen_range(min_size..=TILE_WIDTH - x);
let h = rng.gen_range(min_size..=TILE_HEIGHT - y);
let origin = Origin(x, y);
let mut luma = ByteGrid::new(w, h);
for y in 0..h {
for x in 0..w {
luma.set(x, y, rng.gen());
}
}
connection.send(CharBrightness(origin, luma)).unwrap();
std::thread::sleep(wait_duration);
}
}

View file

@ -0,0 +1,43 @@
use std::thread;
use std::time::Duration;
use clap::Parser;
use servicepoint::*;
#[derive(Parser, Debug)]
struct Cli {
#[arg(short, long, default_value = "localhost:2342")]
destination: String,
#[arg(short, long = "duration-ms", default_value_t = 5000)]
time: u64,
}
fn main() {
let cli = Cli::parse();
let sleep_duration = Duration::max(
FRAME_PACING,
Duration::from_millis(cli.time / PIXEL_WIDTH as u64),
);
let connection = Connection::open(cli.destination).unwrap();
let mut enabled_pixels = PixelGrid::new(PIXEL_WIDTH, PIXEL_HEIGHT);
enabled_pixels.fill(true);
for x_offset in 0..PIXEL_WIDTH {
for y in 0..PIXEL_HEIGHT {
enabled_pixels.set(x_offset % PIXEL_WIDTH, y, false);
}
// this works because the pixel grid has max size
let pixel_data: Vec<u8> = enabled_pixels.clone().into();
let bit_vec = BitVec::from(&*pixel_data);
connection
.send(Command::BitmapLinearAnd(0, bit_vec, CompressionCode::Lzma))
.unwrap();
thread::sleep(sleep_duration);
}
}