Run cargo fmt

This commit is contained in:
Henner Zeller 2026-03-08 20:38:48 +01:00
parent a6077d755e
commit 2da9567679
2 changed files with 22 additions and 23 deletions

View file

@ -1,32 +1,31 @@
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use image::RgbImage; use image::RgbImage;
use nokhwa::Camera as NokhwaCamera;
use nokhwa::pixel_format::RgbFormat; use nokhwa::pixel_format::RgbFormat;
use nokhwa::utils::{CameraIndex, RequestedFormat, RequestedFormatType}; use nokhwa::utils::{CameraIndex, RequestedFormat, RequestedFormatType};
use nokhwa::Camera as NokhwaCamera;
pub struct Camera { pub struct Camera {
camera: NokhwaCamera, camera: NokhwaCamera,
} }
impl Camera { impl Camera {
pub fn new(camera_index: u32) -> Result<Self> { pub fn new(camera_index: u32) -> Result<Self> {
let index = CameraIndex::Index(camera_index); let index = CameraIndex::Index(camera_index);
let format = let format =
RequestedFormat::new::<RgbFormat>(RequestedFormatType::AbsoluteHighestResolution); RequestedFormat::new::<RgbFormat>(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")?; camera.open_stream().context("failed to open stream")?;
Ok(Self { Ok(Self { camera })
camera,
})
} }
pub fn capture(&mut self) -> Result<RgbImage> { pub fn capture(&mut self) -> Result<RgbImage> {
for _ in 0..5 { for _ in 0..5 {
// Hack to have camera flush frames in buffer. // Hack to have camera flush frames in buffer.
let _ = self.camera.frame(); let _ = self.camera.frame();
} }
let frame = self.camera.frame().context("Could not capture image")?; let frame = self.camera.frame().context("Could not capture image")?;
println!("snap!"); println!("snap!");
Ok(frame.decode_image::<RgbFormat>()?) Ok(frame.decode_image::<RgbFormat>()?)
} }
} }

View file

@ -1,15 +1,15 @@
use clap::{Parser, ValueEnum}; use clap::{Parser, ValueEnum};
use std::path::PathBuf;
use image::RgbImage; use image::RgbImage;
use std::path::PathBuf;
mod camera;
mod gcode_stage; mod gcode_stage;
mod openflexure_stage; mod openflexure_stage;
mod camera;
mod stage_io; mod stage_io;
use crate::camera::Camera;
use crate::stage_io::StageIO; use crate::stage_io::StageIO;
use crate::xy_stage::XYStage; use crate::xy_stage::XYStage;
use crate::camera::Camera;
mod xy_stage; mod xy_stage;
@ -33,7 +33,7 @@ struct CliArgs {
camera_index: u32, camera_index: u32,
/// Directory all captured images are stored. /// Directory all captured images are stored.
#[arg(long, value_name="out-dir")] #[arg(long, value_name = "out-dir")]
output_directory: PathBuf, output_directory: PathBuf,
} }
@ -47,10 +47,8 @@ enum Backend {
GCode, GCode,
} }
fn store_image(number: u32, pos: nalgebra::Vector3<f64>, dir: &PathBuf, fn store_image(number: u32, pos: nalgebra::Vector3<f64>, dir: &PathBuf, img: &RgbImage) {
img: &RgbImage) { let img_file = &dir.join(format!("img-{:05}-{:0},{:0}.png", number, pos[0], pos[1]));
let img_file =
&dir.join(format!("img-{:05}-{:0},{:0}.png", number, pos[0], pos[1]));
img.save(img_file).unwrap(); img.save(img_file).unwrap();
} }
@ -60,14 +58,14 @@ fn main() {
let args = CliArgs::parse(); let args = CliArgs::parse();
if !args.output_directory.is_dir() { 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]; let origin = nalgebra::vector![0.0, 0.0, 0.0];
// Picture source // Picture source
let mut cam = Camera::new(args.camera_index).unwrap(); let mut cam = Camera::new(args.camera_index).unwrap();
cam.capture().unwrap(); // Warm up camera. cam.capture().unwrap(); // Warm up camera.
// Movement // Movement
let stage_io = StageIO::new(&args.stage_device, args.tty_speed).unwrap(); let stage_io = StageIO::new(&args.stage_device, args.tty_speed).unwrap();
@ -85,8 +83,10 @@ fn main() {
max_xy[2] = 0.0; max_xy[2] = 0.0;
for n in 0..10 { for n in 0..10 {
stage.move_absolute_cartesian(max_xy * n as f64 / 10.0).unwrap(); stage
store_image(n, origin, &args.output_directory, &cam.capture().unwrap()); .move_absolute_cartesian(max_xy * n as f64 / 10.0)
.unwrap();
store_image(n, origin, &args.output_directory, &cam.capture().unwrap());
} }
} }