diff --git a/docs/cli.md b/docs/cli.md index f307bd1..74a261e 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -98,6 +98,11 @@ You can display available information about the current state of the camera with t4l info ``` +## Verbose + +With the flag `--verbose` you can enable verbose logging. +You will see the communication with the camera in the console. + ## Auto-Completion If you use the cli more often, it might be useful to enable auto-completion for the commands. diff --git a/src/cli/main.rs b/src/cli/main.rs index bbcddc3..1cb75c7 100644 --- a/src/cli/main.rs +++ b/src/cli/main.rs @@ -9,6 +9,8 @@ use tiny4linux::{AIMode, Camera, OBSBotWebCam}; struct Args { #[command(subcommand)] subcommand: Command, + #[arg(short, long, help = "Turns the debug logging on", global = true)] + verbose: bool, } #[derive(Subcommand)] @@ -98,28 +100,30 @@ enum ExposureArg { fn main() { let args = Args::parse(); - let camera = Camera::new("OBSBOT Tiny 2").ok(); + let mut camera = Camera::new("OBSBOT Tiny 2").ok(); if camera.is_none() { println!("Camera could not be found. Please check the connection of the camera."); return; } + if args.verbose { + camera.as_mut().unwrap().set_debugging(true); + } + + let camera = camera.unwrap(); + match &args.subcommand { - Command::Turn { action } => evaluate_sleep_arg(action.clone(), camera.unwrap()), - Command::Sleep => evaluate_sleep_arg(Option::from(OnOffArg::Off), camera.unwrap()), - Command::Wake => evaluate_sleep_arg(Option::from(OnOffArg::On), camera.unwrap()), - Command::Tracking { tracking_mode } => { - evaluate_tracking_arg(tracking_mode.clone(), camera.unwrap()) - } - Command::Speed { speed } => evaluate_speed_arg(speed.clone(), camera.unwrap()), - Command::Preset { position_id } => evaluate_preset_arg(*position_id, camera.unwrap()), - Command::Hdr { hdr_mode } => evaluate_hdr_arg(hdr_mode.clone(), camera.unwrap()), - Command::Exposure { exposure_mode } => { - evaluate_exposure_arg(exposure_mode.clone(), camera.unwrap()) - } + Command::Turn { action } => evaluate_sleep_arg(action.clone(), camera), + Command::Sleep => evaluate_sleep_arg(Option::from(OnOffArg::Off), camera), + Command::Wake => evaluate_sleep_arg(Option::from(OnOffArg::On), camera), + Command::Tracking { tracking_mode } => evaluate_tracking_arg(tracking_mode.clone(), camera), + Command::Speed { speed } => evaluate_speed_arg(speed.clone(), camera), + Command::Preset { position_id } => evaluate_preset_arg(*position_id, camera), + Command::Hdr { hdr_mode } => evaluate_hdr_arg(hdr_mode.clone(), camera), + Command::Exposure { exposure_mode } => evaluate_exposure_arg(exposure_mode.clone(), camera), Command::Info => { - let info = camera.unwrap().get_status(); + let info = camera.get_status(); if info.is_err() { println!( diff --git a/src/gui/main.rs b/src/gui/main.rs index 8e25288..97ef7a9 100644 --- a/src/gui/main.rs +++ b/src/gui/main.rs @@ -117,6 +117,8 @@ impl MainPanel { } Message::ChangeDebugging(new_mode) => { self.debugging_on = new_mode; + let mutable_camera = self.camera.as_mut().unwrap(); + mutable_camera.set_debugging(new_mode); Task::none() } Message::TextInput(s) => { diff --git a/src/lib.rs b/src/lib.rs index fd9557b..657c865 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,7 @@ pub enum Error { #[derive(Debug)] pub struct Camera { handle: usbio::CameraHandle, + debugging: bool, } pub struct CameraStatus { @@ -223,6 +224,7 @@ pub trait OBSBotWebCam { fn set_hdr_mode(&self, mode: bool) -> Result<(), Error>; fn set_exposure_mode(&self, mode: ExposureMode) -> Result<(), Error>; fn set_exposure_mode_type(&self, mode: ExposureModeType) -> Result<(), Error>; + fn set_debugging(&mut self, debugging: bool); } impl OBSBotWebCam for Camera { @@ -422,12 +424,17 @@ impl OBSBotWebCam for Camera { Ok(()) } + + fn set_debugging(&mut self, debugging: bool) { + self.set_debugging(debugging); + } } impl Camera { pub fn new(hint: &str) -> Result { Ok(Self { handle: usbio::open_camera(hint)?, + debugging: false, }) } @@ -439,6 +446,11 @@ impl Camera { let mut data: [u8; 60] = [0u8; 60]; self.get_cur(0x2, 0x6, &mut data) .map_err(|x| Error::USBIOError(x.0))?; + + if self.debugging { + println!("Current state: {:?} {:}", data, hex::encode(&data)); + } + Ok(CameraStatus::decode(&data)) } @@ -464,13 +476,13 @@ impl Camera { .map_err(|e| Error::USBIOError(e.0)) } - fn get_cur(&self, unit: u8, selector: u8, data: &mut [u8]) -> Result<(), errno::Errno> { + fn get_cur(&self, unit: u8, selector: u8, data: &mut [u8]) -> Result<(), Errno> { // always call get_len first match self.get_len(unit, selector) { Ok(size) => { if data.len() < size { println!("Got size {}", size); - return Err(errno::Errno(1)); + return Err(Errno(1)); } } Err(err) => return Err(err), @@ -483,18 +495,20 @@ impl Camera { } } - fn set_cur(&self, unit: u8, selector: u8, data: &mut [u8]) -> Result<(), errno::Errno> { + fn set_cur(&self, unit: u8, selector: u8, data: &mut [u8]) -> Result<(), Errno> { match self.get_len(unit, selector) { Ok(size) => { if data.len() > size { println!("Got size {}", size); - return Err(errno::Errno(1)); + return Err(Errno(1)); } } Err(err) => return Err(err), }; - println!("{:} {:} {:}", unit, selector, hex::encode(&data)); + if self.debugging { + println!("{:} {:} {:}", unit, selector, hex::encode(&data)); + } match self.io(unit, selector, usbio::UVC_SET_CUR, data) { Ok(_) => Ok(()), @@ -514,6 +528,10 @@ impl Camera { fn io(&self, unit: u8, selector: u8, query: u8, data: &mut [u8]) -> Result<(), Errno> { self.handle.io(unit, selector, query, data) } + + fn set_debugging(&mut self, debugging: bool) { + self.debugging = debugging + } } pub struct Command02 { diff --git a/src/usbio.rs b/src/usbio.rs index b0e8296..232c159 100644 --- a/src/usbio.rs +++ b/src/usbio.rs @@ -16,7 +16,7 @@ pub trait UvcUsbIo { } #[derive(Debug)] -pub struct CameraHandle(std::fs::File); +pub struct CameraHandle(File); impl From for CameraHandle { fn from(file: File) -> Self { @@ -106,7 +106,7 @@ pub struct v4l2_capability { } impl v4l2_capability { - fn new(dev: &std::fs::File) -> Result { + fn new(dev: &File) -> Result { let mut query = [v4l2_capability { ..Default::default() }];