diff --git a/src/stream.rs b/src/stream.rs index 5648f2e..7fe044e 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -10,11 +10,6 @@ use std::{ }; use v4l::video::Output; -const MIN_CUTOFF: f64 = 26.0; -const MAX_CUTOFF: f64 = 36.0; -const MID_CUTOFF: f64 = (MIN_CUTOFF + MAX_CUTOFF) / 2.0; -const RANGE_CUTOFF: f64 = MAX_CUTOFF - MIN_CUTOFF; - #[derive(Parser, Debug)] #[command(version, about, long_about = None)] struct Args { @@ -58,13 +53,17 @@ fn rgb_to_u8s(rgb: &RGB) -> (u8, u8, u8) { } pub(crate) struct Streamer { - pub(crate) cutoff: Option, + pub(crate) min_cutoff: f64, + pub(crate) max_cutoff: f64, + pub(crate) freq_hz: f64, } pub(crate) fn initialize() -> Arc> { let args = Args::parse(); Arc::new(Mutex::new(Streamer { - cutoff: args.red_cutoff, + min_cutoff: args.red_cutoff.unwrap_or(26.), + max_cutoff: args.red_cutoff.unwrap_or(26.) + 10.0, + freq_hz: 1.0, })) } @@ -97,8 +96,7 @@ fn main(streamer: Arc>) -> anyhow::Result<()> { .duration_since(SystemTime::UNIX_EPOCH) .unwrap() .as_secs_f64(); - let cutoff = streamer.lock().unwrap().cutoff; - let greyscale = !args.temperature || cutoff.is_none(); + let greyscale = !args.temperature; let fourcc_repr = if greyscale { [ b'Y', // | 0b10000000 @@ -153,13 +151,16 @@ fn main(streamer: Arc>) -> anyhow::Result<()> { { if len == FRAME_LEN { // read once per frame, can make it lower if need be - // let cutoff = streamer.lock().unwrap().cutoff.unwrap_or(0.0); + let state = streamer.lock().unwrap(); + let mid = (state.min_cutoff + state.max_cutoff) / 2.0; + let range = state.max_cutoff - state.min_cutoff; + let hz = state.freq_hz; let now = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .unwrap() .as_secs_f64(); let dt = now - start; - let cutoff = MID_CUTOFF + f64::sin(dt / 2.0) * 0.5 * RANGE_CUTOFF; + let cutoff = mid + f64::sin(dt / hz) * 0.5 * range; // swap the bytes, we are using LE, not BE, 16 bit grayscale // possibly limitation of current v4l2loopback or v4l rust wrapper or libv4l2 for i in 0..FRAME_LEN / 2 {