Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/als/webcam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>,
video: usize,
sleep_ms: u64,
}

impl Webcam {
pub fn new(webcam_tx: Sender<u64>, video: usize) -> Self {
Self { webcam_tx, video }
pub fn new(webcam_tx: Sender<u64>, video: usize, sleep_ms: Option<u64>) -> 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) {
Expand All @@ -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<u8>, usize), Box<dyn Error>> {
Expand Down Expand Up @@ -123,6 +129,30 @@ mod tests {
(als, webcam_tx)
}

#[test]
fn test_sleep_ms_is_custom_value_when_present_in_config() -> Result<(), Box<dyn Error>> {
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<dyn Error>> {
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<dyn Error>> {
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<dyn Error>> {
let (als, _) = setup();
Expand Down
1 change: 1 addition & 0 deletions src/config/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub enum Als {
},
Webcam {
video: usize,
sleep_ms: Option<u64>,
thresholds: HashMap<u64, String>,
},
None,
Expand Down
1 change: 1 addition & 0 deletions src/config/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub enum Als {
},
Webcam {
video: usize,
sleep_ms: Option<u64>,
thresholds: HashMap<String, String>,
},
None,
Expand Down
7 changes: 6 additions & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@ fn parse() -> Result<app::Config, toml::de::Error> {
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 {
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down