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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use bitflags::bitflags;

#[derive(Debug, Clone)]
/// Device control
pub struct Descriptor {
    /// Unique identifier
    pub id: u32,
    /// Name
    pub name: String,
    /// State type
    pub typ: Type,
    /// State flags
    pub flags: Flags,
}

impl Descriptor {
    /// Returns true if the control value can be read
    pub fn readable(&self) -> bool {
        self.flags & Flags::READ == Flags::READ
    }

    /// Returns true if the control value can be written
    pub fn writable(&self) -> bool {
        self.flags & Flags::WRITE == Flags::WRITE
    }
}

#[derive(Debug, Clone)]
/// Device control type
pub enum Type {
    /// Stateless controls
    Stateless,
    /// On/Off switch
    Boolean,
    /// Numerical control
    Number {
        /// Valid value range (inclusive on both ends)
        range: (f64, f64),
        /// Valid range step size
        step: f32,
    },
    /// String control
    String,
    /// Bit field
    Bitmask,
    /// Menu containing an arbitrary number of items
    Menu(Vec<MenuItem>),
}

#[derive(Debug, Clone)]
/// Device control menu item
pub enum MenuItem {
    /// String value
    String(String),
    /// Numerical value
    Number(f64),
}

bitflags! {
    /// Control state flags
    pub struct Flags: u32 {
        /// No flags are set
        const NONE                  = 0x000;
        /// Value can be read
        const READ                  = 0x001;
        /// Value can be written
        const WRITE                 = 0x002;
    }
}

#[derive(Debug, Clone)]
/// Device control state
pub enum State {
    /* Stateless controls */
    None,

    /* Single value controls */
    String(String),
    Boolean(bool),
    Number(f64),
}