Browse Source

Add chars mode.

master
Juergen Stuber 1 year ago
parent
commit
c1be631dcb
2 changed files with 92 additions and 1 deletions
  1. 4
    1
      README.txt
  2. 88
    0
      src/bin/chars/main.rs

+ 4
- 1
README.txt View File

@@ -1,5 +1,8 @@
1 1
 cnoise - colored noise, sets random pixels in selected colors
2
-    paramater is not used yet
2
+    parameter is not used yet
3
+
4
+chars - display characters randomly
5
+    parameter is not used yet
3 6
 
4 7
 bimood - color cycling mood lamp with two complementary colors
5 8
     parameter is the seconds for one cycle through the colors

+ 88
- 0
src/bin/chars/main.rs View File

@@ -0,0 +1,88 @@
1
+use std::env::args;
2
+use std::fs::File;
3
+use std::io::stdout;
4
+use std::io::Read;
5
+use std::io::Write;
6
+use std::iter::repeat_with;
7
+use std::thread::sleep;
8
+use std::time::Duration;
9
+
10
+use rand::thread_rng;
11
+use rand::Rng;
12
+
13
+use pixelfoo::color::Color;
14
+
15
+type Frame = Vec<Vec<Color>>;
16
+
17
+fn send<T: Write>(w: &mut T, f: &Frame) -> std::io::Result<()> {
18
+    for l in f {
19
+        for c in l {
20
+            w.write_all(&c.rgb())?;
21
+        }
22
+    }
23
+    w.flush()
24
+}
25
+
26
+fn pick_color<R>(c0: Color, c1: Color, rng: &mut R) -> Color
27
+where
28
+    R: Rng,
29
+{
30
+    if rng.gen::<f64>() < 0.5 {
31
+        c0.interpolate(c1, rng.gen::<f64>().powf(2.0))
32
+    } else {
33
+        Color::black()
34
+    }
35
+}
36
+
37
+fn main() -> std::io::Result<()> {
38
+    let args = args().collect::<Vec<_>>();
39
+    eprintln!("executing {}", args[0]);
40
+
41
+    let x_size = args[1].parse::<usize>().unwrap();
42
+    let y_size = args[2].parse::<usize>().unwrap();
43
+    eprintln!("screen size {}x{}", x_size, y_size);
44
+
45
+    let mut f = File::open("apps/pet-2.bin")?;
46
+    let mut buffer = Vec::new();
47
+    f.read_to_end(&mut buffer)?;
48
+
49
+    let t_frame = 0.040; // s
50
+    let delay = Duration::new(0, (1_000_000_000.0 * t_frame) as u32);
51
+
52
+    let c0 = Color::new(0, 138, 170);
53
+    let c1 = Color::new(0, 160, 95);
54
+
55
+    let mut rng = thread_rng();
56
+
57
+    let mut frame = repeat_with(|| {
58
+        repeat_with(|| pick_color(c0, c1, &mut rng))
59
+            .take(x_size)
60
+            .collect::<Vec<_>>()
61
+    })
62
+    .take(y_size)
63
+    .collect::<Vec<_>>();
64
+    loop {
65
+        let x = rng.gen_range(0, x_size / 8);
66
+        let y = rng.gen_range(0, y_size / 8);
67
+        let color = pick_color(c0, c1, &mut rng);
68
+        let start_index = rng.gen_range(0, buffer.len() / 8) * 8;
69
+        for i in start_index..(start_index + 8) {
70
+            let b = buffer[i];
71
+            for j in 0..8 {
72
+                frame[y][x] = {
73
+                    if b & (1 << j) != 0 {
74
+                        color
75
+                    } else {
76
+                        Color::black()
77
+                    }
78
+                };
79
+            }
80
+        }
81
+
82
+        let mut buf = Vec::with_capacity(x_size * y_size * 3);
83
+        send(&mut buf, &frame)?;
84
+        stdout().write_all(&buf)?;
85
+        stdout().flush()?;
86
+        sleep(delay);
87
+    }
88
+}

Loading…
Cancel
Save