From bfaa0eca2fa1377600b5ff52be0145793c36394d Mon Sep 17 00:00:00 2001 From: devdesk Date: Fri, 23 Feb 2024 01:48:11 +0200 Subject: [PATCH] pulse threshold --- src/stream.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/stream.rs b/src/stream.rs index 333a63e..5648f2e 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -2,6 +2,7 @@ use crate::offline::{Header, HDR_SIZE}; use bracket_color::prelude::*; use clap::Parser; use dotenv::dotenv; +use std::time::SystemTime; use std::{ io::Write, sync::{Arc, Mutex}, @@ -9,6 +10,11 @@ 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 { @@ -87,6 +93,10 @@ fn main(streamer: Arc>) -> anyhow::Result<()> { const WIDTH: usize = 288; const HEIGHT: usize = 384; println!("reading cutoff"); + let start = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_secs_f64(); let cutoff = streamer.lock().unwrap().cutoff; let greyscale = !args.temperature || cutoff.is_none(); let fourcc_repr = if greyscale { @@ -143,7 +153,13 @@ 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 cutoff = streamer.lock().unwrap().cutoff.unwrap_or(0.0); + 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; // 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 {