ledcat 0.2.0

Control lots of LED's over lots of protocols
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#[macro_use]
mod util;
mod color;
mod device;
mod driver;
mod input;

use crate::color::*;
use crate::device::*;
use crate::driver::*;
use crate::input::geometry::*;
use crate::input::*;
use std::collections::BTreeMap;
use std::env;
use std::error::Error;
use std::fmt;
use std::fs;
use std::io;
use std::iter;
use std::path::PathBuf;
use std::process;
use std::sync::mpsc;
use std::thread;
use std::time::{Duration, Instant};

fn main() -> Result<(), Box<dyn Error>> {
    let mut cli = clap::App::new(env!("CARGO_PKG_NAME"))
        .version(env!("CARGO_PKG_VERSION"))
        .author(env!("CARGO_PKG_AUTHORS"))
        .about(env!("CARGO_PKG_DESCRIPTION"))
        .arg(clap::Arg::with_name("output")
            .short("o")
            .long("output")
            .takes_value(true)
            .default_value("-")
            .help("The output file to write to. Use - for stdout."))
        .arg(clap::Arg::with_name("input")
            .short("i")
            .long("input")
            .takes_value(true)
            .min_values(1)
            .multiple(true)
            .default_value("-")
            .help("The inputs to read from. Read the manual for how inputs are read and \
                   prioritized."))
        .arg(clap::Arg::with_name("exit")
            .short("e")
            .long("exit")
            .takes_value(true)
            .possible_values(&["never", "one", "all"])
            .default_value("all")
            .help("Set the exit condition. \"one\" and \"all\" indicate the number of files that \
                should be closed to trigger"))
        .arg(clap::Arg::with_name("clear-timeout")
            .long("clear-timeout")
            .takes_value(true)
            .validator(regex_validator!(r"^[1-9]\d*$"))
            .conflicts_with("framerate")
            .help("Sets a timeout in milliseconds after which partially read frames are deleted. \
                   If a framerate is set, a timeout is calculated automatically."))
        .arg(clap::Arg::with_name("geometry")
            .short("g")
            .long("geometry")
            .alias("num-pixels")
            .takes_value(true)
            .default_value("env")
            .validator(|val| {
                if val == "env" {
                    return Ok(())
                }
                match val.parse::<Dimensions>() {
                    Ok(_) => Ok(()),
                    Err(err) => Err(err),
                }
            })
            .help("Specify the size of the display. Can be either a number for 1D, WxH for 2D, or\
                  \"env\" to load the LEDCAT_GEOMETRY environment variable."))
        .arg(clap::Arg::with_name("transpose")
            .short("t")
            .long("transpose")
            .takes_value(true)
            .min_values(1)
            .multiple(true)
            .possible_values(&["reverse", "zigzag_x", "zigzag_y", "mirror_x", "mirror_y"])
            .help("Apply one or more transpositions to the output"))
        .arg(clap::Arg::with_name("color-correction")
            .short("c")
            .long("color-correction")
            .takes_value(true)
            .possible_values(&["none", "srgb"])
            .help("Override the default color correction. The default is determined per device."))
        .arg(clap::Arg::with_name("dim")
            .long("dim")
            .takes_value(true)
            .default_value("1.0")
            .validator(|v| {
                let f = v.parse::<f32>()
                    .map_err(|e| format!("{}", e))?;
                if (0.0..=1.0).contains(&f) {
                    Ok(())
                } else {
                    Err(format!("dim value out of range: {}", f))
                }
            })
            .help("Apply a global grayscale before the collor correction. The value should be \
                   between 0 and 1.0 inclusive"))
        .arg(clap::Arg::with_name("driver")
            .long("driver")
            .takes_value(true)
            .help("The driver to use for the output. If this is not specified, the driver is \
                   automaticaly detected based on the output"))
        .arg(clap::Arg::with_name("serial-baudrate")
            .long("serial-baudrate")
            .takes_value(true)
            .validator(regex_validator!(r"^[1-9]\d*$"))
            .default_value("1152000")
            .help("If serial is used as driver, use this to set the baudrate"))
        .arg(clap::Arg::with_name("framerate")
            .short("f")
            .long("framerate")
            .takes_value(true)
            .validator(regex_validator!(r"^[1-9]\d*$"))
            .help("Limit the number of frames per second"))
        .arg(clap::Arg::with_name("single-frame")
            .short("1")
            .long("one")
            .conflicts_with("framerate")
            .help("Send a single frame to the output and exit"));

    let mut device_constructors = BTreeMap::new();
    for (command, from_command) in device::devices() {
        device_constructors.insert(command.get_name().to_string(), from_command);
        cli = cli.subcommand(command);
    }

    let matches = cli.clone().get_matches();
    let (sub_name, sub_matches) = matches.subcommand();
    if sub_name.is_empty() {
        let mut out = io::stderr();
        cli.write_help(&mut out).unwrap();
        eprintln!();
        process::exit(1);
    }

    let gargs = GlobalArgs {
        output_file: {
            let output = matches.value_of("output").unwrap();
            PathBuf::from(match output {
                "-" => "/dev/stdout",
                out => out,
            })
        },
        // Don't require the display geomtry to be set just yet, a non-outputting subcommand may
        // not need it anyway.
        dimensions: {
            let env = env::var("LEDCAT_GEOMETRY");
            match matches.value_of("geometry").unwrap() {
                "env" => match env.as_ref().map(|e| e.as_str()) {
                    Err(_) | Ok("") => None,
                    Ok(e) => Some(e),
                },
                v => Some(v),
            }
            .and_then(|v| v.parse().ok())
        },
    };
    let output: Box<dyn Output> = {
        let from_command = device_constructors[sub_name](sub_matches.unwrap(), &gargs)?;
        match from_command {
            FromCommand::Device(dev) => {
                let driver_name = matches
                    .value_of("driver")
                    .or_else(|| driver::detect(&gargs.output_file))
                    .map(str::to_string)
                    .unwrap_or_else(|| "none".to_string());
                let output: Box<dyn io::Write + Send> = match driver_name.as_str() {
                    "none" => Box::new(
                        fs::OpenOptions::new()
                            .write(true)
                            .open(&gargs.output_file)
                            .unwrap(),
                    ),
                    "spidev" => {
                        let conf = dev
                            .spidev_config()
                            .ok_or(driver::Error::DeviceNotSupported)?;
                        Box::new(spidev::open(&gargs.output_file, conf).unwrap())
                    }
                    "serial" => {
                        let baudrate = matches
                            .value_of("serial-baudrate")
                            .unwrap()
                            .parse::<u32>()
                            .unwrap();
                        Box::new(serial::open(&gargs.output_file, baudrate).unwrap())
                    }
                    d => return Err(GenericError::new(format!("unknown driver {}", d)).into()),
                };
                Box::new((dev, output))
            }
            FromCommand::Output(output) => output,
            FromCommand::SubcommandHandled => return Ok(()),
        }
    };
    let dimensions = gargs.dimensions()?;

    let transposition = match matches.values_of("transpose") {
        Some(v) => transposition_table(&dimensions, v),
        None => transposition_table(&dimensions, iter::empty()),
    }?;
    assert_eq!(dimensions.size(), transposition.len());

    let color_correction = matches
        .value_of("color-correction")
        .and_then(|name| match name {
            "none" => Some(Correction::none()),
            "srgb" => Some(Correction::srgb(255, 255, 255)),
            _ => None,
        })
        .unwrap_or_else(|| output.color_correction());
    let dim = (matches.value_of("dim").unwrap().parse::<f32>().unwrap() * 255.0).round() as u8;

    let frame_interval = matches
        .value_of("framerate")
        .map(|fps| Duration::from_secs(1) / fps.parse::<u32>().unwrap());
    let single_frame = matches.is_present("single-frame");

    let input = {
        let exit_condition = {
            match matches.value_of("exit") {
                Some("never") => select::ExitCondition::Never,
                Some("one") => select::ExitCondition::OneClosed,
                Some("all") | None => select::ExitCondition::AllClosed,
                Some(e) => {
                    return Err(
                        GenericError::new(format!("invalid value for --exit: {}", e)).into(),
                    )
                }
            }
        };
        let files = matches
            .values_of("input")
            .unwrap()
            .map(|f| match f {
                "-" => "/dev/stdin",
                f => f,
            })
            .collect();
        let clear_timeout = frame_interval.map(|t| t * 2).unwrap_or_else(|| {
            let ms = matches
                .value_of("clear-timeout")
                .map(|v| v.parse::<u32>().unwrap())
                .unwrap_or(100);
            Duration::from_millis(ms as u64)
        });
        select::Reader::from_files(
            files,
            dimensions.size() * 3,
            exit_condition,
            Some(clear_timeout),
        )
        .unwrap()
    };

    let _ = pipe_frames(
        input,
        output,
        transposition,
        color_correction,
        dim,
        single_frame,
        frame_interval,
    );
    Ok(())
}

fn pipe_frames(
    mut input: impl io::Read + Send + 'static,
    mut dev: impl Output + 'static,
    transposition: Vec<usize>,
    correction: Correction,
    dim: u8,
    single_frame: bool,
    frame_interval: Option<Duration>,
) -> io::Result<()> {
    let (err_tx, err_rx) = mpsc::channel();
    macro_rules! try_or_send {
        ($tx:expr, $expression:expr) => {
            match $expression {
                Ok(val) => val,
                Err(err) => {
                    $tx.send(Err(err)).unwrap();
                    return;
                }
            }
        };
    }

    let local_err_tx = err_tx.clone();
    let num_pixels = transposition.len();
    let (input_tx, input_rx) = mpsc::sync_channel(1);
    thread::spawn(move || {
        loop {
            // Read a full frame into a buffer. This prevents half frames being written to a
            // potentially timing sensitive output if the input blocks and lets us apply the
            // transpositions.
            let mut bin_buffer = vec![0; num_pixels * 3];
            try_or_send!(local_err_tx, input.read_exact(&mut bin_buffer));
            input_tx.send(bin_buffer).unwrap();
            if single_frame {
                break;
            }
        }
    });

    let (map_tx, map_rx) = mpsc::sync_channel(1);
    thread::spawn(move || {
        for bin_buffer in input_rx.into_iter() {
            let mut buffer = vec![Pixel { r: 0, g: 0, b: 0 }; transposition.len()];
            for (transpose_mapped, bin) in transposition.iter().zip(bin_buffer.chunks(3)) {
                // Load the pixel.
                let pix = Pixel {
                    r: bin[0],
                    g: bin[1],
                    b: bin[2],
                };
                // Apply dimming.
                let pix = {
                    let dim16 = u16::from(dim);
                    Pixel {
                        r: ((u16::from(pix.r) * dim16) / 0xff) as u8,
                        g: ((u16::from(pix.g) * dim16) / 0xff) as u8,
                        b: ((u16::from(pix.b) * dim16) / 0xff) as u8,
                    }
                };
                // Apply color correction.
                let pix = correction.correct(pix);
                // Apply transposition and store the pixel in the output buffer.
                buffer[*transpose_mapped] = pix;
            }
            map_tx.send(buffer).unwrap();
        }
    });

    thread::spawn(move || loop {
        let start = Instant::now();

        let buffer = match map_rx.recv() {
            Ok(v) => v,
            Err(_) => break,
        };
        try_or_send!(err_tx, dev.output_frame(&buffer));

        if let Some(interval) = frame_interval {
            let el = start.elapsed();
            if interval >= el {
                thread::sleep(interval - el);
            }
        }
    });

    match err_rx.recv() {
        Ok(err) => err,
        Err(mpsc::RecvError) => Ok(()),
    }
}

fn transposition_table<'a>(
    dimensions: &Dimensions,
    operations: impl Iterator<Item = &'a str>,
) -> Result<Vec<usize>, String> {
    let transpositions = operations
        .map(|name| map_transposition(dimensions, name))
        .collect::<Result<Vec<_>, _>>()?;
    Ok((0..dimensions.size())
        .map(|index| transpositions.transpose(index))
        .collect())
}

fn map_transposition(
    dimensions: &Dimensions,
    name: &str,
) -> Result<Box<dyn Transposition>, String> {
    match (name, *dimensions) {
        ("reverse", dim) => Ok(Box::new(Reverse { length: dim.size() })),
        ("zigzag_x", Dimensions::Two(w, h)) | ("zigzag_y", Dimensions::Two(w, h)) => {
            Ok(Box::new(Zigzag {
                width: w,
                height: h,
                major_axis: match name.chars().last().unwrap() {
                    'x' => Axis::X,
                    'y' => Axis::Y,
                    _ => unreachable!(),
                },
            }))
        }
        ("mirror_x", Dimensions::Two(w, h)) | ("mirror_y", Dimensions::Two(w, h)) => {
            Ok(Box::new(Mirror {
                width: w,
                height: h,
                axis: match name.chars().last().unwrap() {
                    'x' => Axis::X,
                    'y' => Axis::Y,
                    _ => unreachable!(),
                },
            }))
        }
        (name, Dimensions::One(_)) => Err(format!("{} requires 2D geometry to be specified", name)),
        (name, _) => Err(format!("unknown transposition: {}", name)),
    }
}

#[derive(Debug)]
struct GenericError {
    msg: String,
}

impl GenericError {
    fn new(msg: impl Into<String>) -> Self {
        Self { msg: msg.into() }
    }
}

impl fmt::Display for GenericError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.msg)
    }
}

impl Error for GenericError {}