diff --git a/examples/live.rs b/examples/live.rs index bd711b1..d09a742 100644 --- a/examples/live.rs +++ b/examples/live.rs @@ -1,9 +1,31 @@ +use clap::Parser; use dotenv::dotenv; use std::io::Write; use thermaldecoder::{Header, HDR_SIZE}; use v4l::video::Output; +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct Args { + #[arg(short, long, default_value_t = false)] + temperature: bool, + #[arg(short, long, default_value = "/dev/video0")] + device: String, +} + +fn pixel_to_celcius(x: u16) -> u16 { + let x: f64 = x.into(); + let x = x / 256.0; + let t = (-1.665884e-08) * x.powf(4.) + + (1.347094e-05) * x.powf(3.) + + (-4.396264e-03) * x.powf(2.) + + (9.506939e-01) * x + + (-6.353247e+01); + (t * 256.0) as u16 +} + fn main() -> anyhow::Result<()> { + let args = Args::parse(); dotenv().ok(); let device = match std::env::var("THERMALCAM_IFACE=enp1s0f0") { Ok(d) => { @@ -21,9 +43,7 @@ fn main() -> anyhow::Result<()> { // get the default Device println!("Using device {}", device.name); - let output = std::env::args() - .nth(1) - .expect("required output file argument"); + let output = args.device; println!("Using output v4l2loopback device {}", output); const WIDTH: usize = 288; @@ -73,8 +93,12 @@ fn main() -> anyhow::Result<()> { // possibly limitation of current v4l2loopback or v4l rust wrapper or libv4l2 let mut swapped = [0u8; FRAME_LEN]; for i in 0..FRAME_LEN / 2 { - swapped[i * 2] = frame[i * 2 + 1]; - swapped[i * 2 + 1] = frame[i * 2]; + let mut pixel = u16::from_be_bytes([frame[i * 2], frame[i * 2 + 1]]); + if args.temperature { + pixel = pixel_to_celcius(pixel); + } + let pixel_swapped = pixel.to_le_bytes(); + swapped[i * 2..i * 2 + 2].copy_from_slice(&pixel_swapped); } out.write_all(&swapped[..])?; } diff --git a/run_live.sh b/run_live.sh index 5f6fe36..f59fb97 100755 --- a/run_live.sh +++ b/run_live.sh @@ -10,4 +10,4 @@ TARGET=./target/release/examples/live # setcap does not work yet (EPERM on socket AF_PACKET) # sudo setcap cap_net_raw,cap_net_admin=eip $TARGET #sudo strace -f -o live.strace $TARGET /dev/video0 -sudo $TARGET /dev/video0 +sudo RUST_BACKTRACE=full $TARGET "$@" diff --git a/run_live_debug.sh b/run_live_debug.sh new file mode 100755 index 0000000..2663480 --- /dev/null +++ b/run_live_debug.sh @@ -0,0 +1,13 @@ +#!/bin/bash +cd $(dirname $0) + +set -e + +# Python works but stutters +#sudo ./venv/bin/python ./decode.py --live +cargo build --example live +TARGET=./target/debug/examples/live +# setcap does not work yet (EPERM on socket AF_PACKET) +# sudo setcap cap_net_raw,cap_net_admin=eip $TARGET +#sudo strace -f -o live.strace $TARGET /dev/video0 +sudo RUST_BACKTRACE=full $TARGET "$@"