diff --git a/hostcontrol/src/camera.rs b/hostcontrol/src/camera.rs index ceaa38e..6d1d480 100644 --- a/hostcontrol/src/camera.rs +++ b/hostcontrol/src/camera.rs @@ -1,32 +1,31 @@ use anyhow::{Context, Result}; use image::RgbImage; -use nokhwa::Camera as NokhwaCamera; use nokhwa::pixel_format::RgbFormat; use nokhwa::utils::{CameraIndex, RequestedFormat, RequestedFormatType}; +use nokhwa::Camera as NokhwaCamera; pub struct Camera { camera: NokhwaCamera, } -impl Camera { +impl Camera { pub fn new(camera_index: u32) -> Result { - let index = CameraIndex::Index(camera_index); + let index = CameraIndex::Index(camera_index); let format = RequestedFormat::new::(RequestedFormatType::AbsoluteHighestResolution); - let mut camera = NokhwaCamera::new(index, format).context("could not find/access webcam")?; + let mut camera = + NokhwaCamera::new(index, format).context("could not find/access webcam")?; camera.open_stream().context("failed to open stream")?; - Ok(Self { - camera, - }) + Ok(Self { camera }) } pub fn capture(&mut self) -> Result { - for _ in 0..5 { - // Hack to have camera flush frames in buffer. - let _ = self.camera.frame(); + for _ in 0..5 { + // Hack to have camera flush frames in buffer. + let _ = self.camera.frame(); } let frame = self.camera.frame().context("Could not capture image")?; - println!("snap!"); + println!("snap!"); Ok(frame.decode_image::()?) } } diff --git a/hostcontrol/src/main.rs b/hostcontrol/src/main.rs index c418eab..bdccb94 100644 --- a/hostcontrol/src/main.rs +++ b/hostcontrol/src/main.rs @@ -1,15 +1,15 @@ use clap::{Parser, ValueEnum}; -use std::path::PathBuf; use image::RgbImage; +use std::path::PathBuf; +mod camera; mod gcode_stage; mod openflexure_stage; -mod camera; mod stage_io; +use crate::camera::Camera; use crate::stage_io::StageIO; use crate::xy_stage::XYStage; -use crate::camera::Camera; mod xy_stage; @@ -33,7 +33,7 @@ struct CliArgs { camera_index: u32, /// Directory all captured images are stored. - #[arg(long, value_name="out-dir")] + #[arg(long, value_name = "out-dir")] output_directory: PathBuf, } @@ -47,10 +47,8 @@ enum Backend { GCode, } -fn store_image(number: u32, pos: nalgebra::Vector3, dir: &PathBuf, - img: &RgbImage) { - let img_file = - &dir.join(format!("img-{:05}-{:0},{:0}.png", number, pos[0], pos[1])); +fn store_image(number: u32, pos: nalgebra::Vector3, dir: &PathBuf, img: &RgbImage) { + let img_file = &dir.join(format!("img-{:05}-{:0},{:0}.png", number, pos[0], pos[1])); img.save(img_file).unwrap(); } @@ -60,14 +58,14 @@ fn main() { let args = CliArgs::parse(); if !args.output_directory.is_dir() { - panic!("--out-dir needs to be an existing directory"); + panic!("--out-dir needs to be an existing directory"); } let origin = nalgebra::vector![0.0, 0.0, 0.0]; // Picture source let mut cam = Camera::new(args.camera_index).unwrap(); - cam.capture().unwrap(); // Warm up camera. + cam.capture().unwrap(); // Warm up camera. // Movement let stage_io = StageIO::new(&args.stage_device, args.tty_speed).unwrap(); @@ -85,8 +83,10 @@ fn main() { max_xy[2] = 0.0; for n in 0..10 { - stage.move_absolute_cartesian(max_xy * n as f64 / 10.0).unwrap(); - store_image(n, origin, &args.output_directory, &cam.capture().unwrap()); + stage + .move_absolute_cartesian(max_xy * n as f64 / 10.0) + .unwrap(); + store_image(n, origin, &args.output_directory, &cam.capture().unwrap()); } }