Skip to content

Commit

Permalink
video pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
tuna-f1sh committed Dec 5, 2024
1 parent 7a032c0 commit 126ef59
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
29 changes: 24 additions & 5 deletions src/function/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! Frame::new(640, 360, vec![15, 30, 60, 120], Format::Mjpeg),
//! Frame::new(1280, 720, vec![30, 60], Format::Mjpeg),
//! Frame::new(1920, 1080, vec![30], Format::Mjpeg),
//! ]).build();
//! ]);
//!
//! let udc = default_udc().expect("cannot get UDC");
//! let reg =
Expand Down Expand Up @@ -184,8 +184,8 @@ impl UvcFrame {
}

/// Create a new UVC frame with the specified properties.
pub fn new(width: u32, height: u32, format: Format, intervals: Vec<u32>) -> Self {
Self { width, height, intervals, color_matching: None, format }
pub fn new(width: u32, height: u32, format: Format, intervals: impl IntoIterator<Item = u32>) -> Self {
Self { width, height, intervals: intervals.into_iter().collect(), color_matching: None, format }
}
}

Expand Down Expand Up @@ -217,6 +217,24 @@ impl UvcBuilder {
let dir = FunctionDir::new();
(Uvc { dir: dir.clone() }, Handle::new(UvcFunction { builder: self, dir }))
}

/// Add a frame to builder
pub fn add_frame<F>(&mut self, frame: F)
where
UvcFrame: From<F>,
{
self.frames.push(frame.into());
}

/// UVC builder with frames
#[must_use]
pub fn with_frames<F>(mut self, frames: impl IntoIterator<Item = F>) -> Self
where
UvcFrame: From<F>,
{
self.frames = frames.into_iter().map(UvcFrame::from).collect();
self
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -339,12 +357,13 @@ impl Uvc {
}

/// Creates a new USB Video Class (UVC) with the specified frames.
pub fn new<F>(frames: Vec<F>) -> UvcBuilder
pub fn new<F>(frames: impl IntoIterator<Item = F>) -> (Uvc, Handle)
where
UvcFrame: From<F>,
{
let frames = frames.into_iter().map(UvcFrame::from).collect();
UvcBuilder { frames, ..Default::default() }
let builder = UvcBuilder { frames, ..Default::default() };
builder.build()
}

/// Access to registration status.
Expand Down
2 changes: 1 addition & 1 deletion tests/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use usb_gadget::function::video::{ColorMatching, Format, Frame, Uvc};
fn video() {
init();

let mut builder = Uvc::new(vec![
let mut builder = Uvc::builder().with_frames(vec![
Frame::new(640, 360, vec![15, 30, 60, 120], Format::Yuyv),
Frame::new(640, 360, vec![15, 30, 60, 120], Format::Mjpeg),
Frame::new(1280, 720, vec![30, 60], Format::Mjpeg),
Expand Down

0 comments on commit 126ef59

Please sign in to comment.