From c1be631dcb293067a1574118f1f2f080ec053420 Mon Sep 17 00:00:00 2001 From: Juergen Stuber Date: Mon, 31 Dec 2018 20:03:59 +0100 Subject: [PATCH] Add chars mode. --- README.txt | 5 ++- src/bin/chars/main.rs | 88 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 src/bin/chars/main.rs diff --git a/README.txt b/README.txt index 8a2f71c..62d6307 100644 --- a/README.txt +++ b/README.txt @@ -1,5 +1,8 @@ cnoise - colored noise, sets random pixels in selected colors - paramater is not used yet + parameter is not used yet + +chars - display characters randomly + parameter is not used yet bimood - color cycling mood lamp with two complementary colors parameter is the seconds for one cycle through the colors diff --git a/src/bin/chars/main.rs b/src/bin/chars/main.rs new file mode 100644 index 0000000..591588a --- /dev/null +++ b/src/bin/chars/main.rs @@ -0,0 +1,88 @@ +use std::env::args; +use std::fs::File; +use std::io::stdout; +use std::io::Read; +use std::io::Write; +use std::iter::repeat_with; +use std::thread::sleep; +use std::time::Duration; + +use rand::thread_rng; +use rand::Rng; + +use pixelfoo::color::Color; + +type Frame = Vec>; + +fn send(w: &mut T, f: &Frame) -> std::io::Result<()> { + for l in f { + for c in l { + w.write_all(&c.rgb())?; + } + } + w.flush() +} + +fn pick_color(c0: Color, c1: Color, rng: &mut R) -> Color +where + R: Rng, +{ + if rng.gen::() < 0.5 { + c0.interpolate(c1, rng.gen::().powf(2.0)) + } else { + Color::black() + } +} + +fn main() -> std::io::Result<()> { + let args = args().collect::>(); + eprintln!("executing {}", args[0]); + + let x_size = args[1].parse::().unwrap(); + let y_size = args[2].parse::().unwrap(); + eprintln!("screen size {}x{}", x_size, y_size); + + let mut f = File::open("apps/pet-2.bin")?; + let mut buffer = Vec::new(); + f.read_to_end(&mut buffer)?; + + let t_frame = 0.040; // s + let delay = Duration::new(0, (1_000_000_000.0 * t_frame) as u32); + + let c0 = Color::new(0, 138, 170); + let c1 = Color::new(0, 160, 95); + + let mut rng = thread_rng(); + + let mut frame = repeat_with(|| { + repeat_with(|| pick_color(c0, c1, &mut rng)) + .take(x_size) + .collect::>() + }) + .take(y_size) + .collect::>(); + loop { + let x = rng.gen_range(0, x_size / 8); + let y = rng.gen_range(0, y_size / 8); + let color = pick_color(c0, c1, &mut rng); + let start_index = rng.gen_range(0, buffer.len() / 8) * 8; + for i in start_index..(start_index + 8) { + let b = buffer[i]; + for j in 0..8 { + frame[y][x] = { + if b & (1 << j) != 0 { + color + } else { + Color::black() + } + }; + } + } + + let mut buf = Vec::with_capacity(x_size * y_size * 3); + send(&mut buf, &frame)?; + stdout().write_all(&buf)?; + stdout().flush()?; + sleep(delay); + } +}