diff --git a/src/als/webcam.rs b/src/als/webcam.rs index 0845b7a..1d53b28 100644 --- a/src/als/webcam.rs +++ b/src/als/webcam.rs @@ -14,15 +14,21 @@ use v4l::{Device, FourCC}; const DEFAULT_LUX: u64 = 100; const WAITING_SLEEP_MS: u64 = 2000; +const MIN_WAITING_SLEEP_MS: u64 = 1000; pub struct Webcam { webcam_tx: Sender, video: usize, + sleep_ms: u64, } impl Webcam { - pub fn new(webcam_tx: Sender, video: usize) -> Self { - Self { webcam_tx, video } + pub fn new(webcam_tx: Sender, video: usize, sleep_ms: Option) -> Self { + Self { + webcam_tx, + video, + sleep_ms: sleep_ms.filter(|&s| s >= MIN_WAITING_SLEEP_MS).unwrap_or(WAITING_SLEEP_MS), + } } pub fn run(&mut self) { @@ -40,7 +46,7 @@ impl Webcam { .expect("Unable to send new webcam lux value, channel is dead"); }; - thread::sleep(Duration::from_millis(WAITING_SLEEP_MS)); + thread::sleep(Duration::from_millis(self.sleep_ms)); } fn frame(&mut self) -> Result<(Vec, usize), Box> { @@ -123,6 +129,30 @@ mod tests { (als, webcam_tx) } + #[test] + fn test_sleep_ms_is_custom_value_when_present_in_config() -> Result<(), Box> { + let (webcam_tx, _) = mpsc::channel(); + let webcam = Webcam::new(webcam_tx, 0, Some(10000)); + assert_eq!(10000, webcam.sleep_ms); + Ok(()) + } + + #[test] + fn test_sleep_ms_is_default_value_when_not_present_in_config() -> Result<(), Box> { + let (webcam_tx, _) = mpsc::channel(); + let webcam = Webcam::new(webcam_tx, 0, None); + assert_eq!(WAITING_SLEEP_MS, webcam.sleep_ms); + Ok(()) + } + + #[test] + fn test_sleep_ms_is_default_value_when_invalid_in_config() -> Result<(), Box> { + let (webcam_tx, _) = mpsc::channel(); + let webcam = Webcam::new(webcam_tx, 0, Some(MIN_WAITING_SLEEP_MS - 1)); + assert_eq!(WAITING_SLEEP_MS, webcam.sleep_ms); + Ok(()) + } + #[test] fn test_get_raw_returns_default_value_when_no_data_from_webcam() -> Result<(), Box> { let (als, _) = setup(); diff --git a/src/config/app.rs b/src/config/app.rs index ce61290..bdcb0ec 100644 --- a/src/config/app.rs +++ b/src/config/app.rs @@ -17,6 +17,7 @@ pub enum Als { }, Webcam { video: usize, + sleep_ms: Option, thresholds: HashMap, }, None, diff --git a/src/config/file.rs b/src/config/file.rs index 6667da2..28bbece 100644 --- a/src/config/file.rs +++ b/src/config/file.rs @@ -20,6 +20,7 @@ pub enum Als { }, Webcam { video: usize, + sleep_ms: Option, thresholds: HashMap, }, None, diff --git a/src/config/mod.rs b/src/config/mod.rs index 00d035e..3a76760 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -64,8 +64,13 @@ fn parse() -> Result { path, thresholds: parse_als_thresholds(thresholds), }, - file::Als::Webcam { video, thresholds } => app::Als::Webcam { + file::Als::Webcam { video, + sleep_ms, + thresholds, + } => app::Als::Webcam { + video, + sleep_ms, thresholds: parse_als_thresholds(thresholds), }, file::Als::Time { thresholds } => app::Als::Time { diff --git a/src/main.rs b/src/main.rs index 8b011ce..e629da5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -112,12 +112,12 @@ fn main() { .expect("Unable to initialize ALS IIO sensor"), ), config::Als::Time { thresholds } => Box::new(als::time::Als::new(thresholds)), - config::Als::Webcam { video, thresholds } => Box::new({ + config::Als::Webcam { video, thresholds, sleep_ms } => Box::new({ let (webcam_tx, webcam_rx) = mpsc::channel(); std::thread::Builder::new() .name("als-webcam".to_string()) .spawn(move || { - als::webcam::Webcam::new(webcam_tx, video).run(); + als::webcam::Webcam::new(webcam_tx, video, sleep_ms).run(); }) .expect("Unable to start thread: als-webcam"); als::webcam::Als::new(webcam_rx, thresholds)