1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use std::{fmt, mem};

use crate::fraction::Fraction;
use crate::parameters::Capabilities;
use crate::v4l_sys::*;

#[derive(Debug, Copy, Clone)]
/// Output parameters (single-planar)
pub struct Parameters {
    pub capabilities: Capabilities,
    pub interval: Fraction,
}

impl Parameters {
    pub fn new(frac: Fraction) -> Self {
        Parameters {
            capabilities: Capabilities::from(0),
            interval: frac,
        }
    }

    pub fn with_fps(fps: u32) -> Self {
        Parameters {
            capabilities: Capabilities::from(0),
            interval: Fraction::new(1, fps),
        }
    }
}

impl fmt::Display for Parameters {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "capabilities : {}", self.capabilities)?;
        writeln!(f, "interval     : {} [s]", self.interval)?;
        Ok(())
    }
}

impl From<v4l2_outputparm> for Parameters {
    fn from(params: v4l2_outputparm) -> Self {
        Self {
            capabilities: Capabilities::from(params.capability),
            interval: Fraction::from(params.timeperframe),
        }
    }
}

impl From<Parameters> for v4l2_outputparm {
    fn from(parameters: Parameters) -> Self {
        Self {
            capability: parameters.capabilities.into(),
            timeperframe: parameters.interval.into(),
            ..unsafe { mem::zeroed() }
        }
    }
}