Skip to content
Merged
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
5 changes: 5 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 18 additions & 14 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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!(
Expand Down
2 changes: 2 additions & 0 deletions src/gui/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
28 changes: 23 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub enum Error {
#[derive(Debug)]
pub struct Camera {
handle: usbio::CameraHandle,
debugging: bool,
}

pub struct CameraStatus {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Self, Error> {
Ok(Self {
handle: usbio::open_camera(hint)?,
debugging: false,
})
}

Expand All @@ -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))
}

Expand All @@ -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),
Expand All @@ -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(()),
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/usbio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub trait UvcUsbIo {
}

#[derive(Debug)]
pub struct CameraHandle(std::fs::File);
pub struct CameraHandle(File);

impl From<File> for CameraHandle {
fn from(file: File) -> Self {
Expand Down Expand Up @@ -106,7 +106,7 @@ pub struct v4l2_capability {
}

impl v4l2_capability {
fn new(dev: &std::fs::File) -> Result<Self, errno::Errno> {
fn new(dev: &File) -> Result<Self, Errno> {
let mut query = [v4l2_capability {
..Default::default()
}];
Expand Down