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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use crate::utils::byteutil;

/// A property value of a single earbud
pub trait BudProperty {
    type Item;

    /// Get the corresponding value of a byte based on the
    /// side of the earbud (left/right)
    fn side_val(val: u8, side: Side) -> u8 {
        match side {
            Side::Left => byteutil::value_of_left(val),
            Side::Right => byteutil::value_of_right(val),
        }
    }

    /// Get the property item decoded based on
    /// the side and msg byte
    fn value(val: u8, side: Side) -> Self::Item {
        Self::decode(Self::side_val(val, side))
    }

    /// Decode the value. Returns a property variant
    fn decode(val: u8) -> Self::Item;

    /// Needs to be implemented to send a
    /// property variant inside a msg payload
    fn encode(&self) -> u8;
}

/// The side of an earbud.
#[derive(Debug, PartialEq)]
pub enum Side {
    Left,
    Right,
}

impl From<bool> for Side {
    fn from(inp_side: bool) -> Side {
        if !inp_side {
            Side::Right
        } else {
            Side::Left
        }
    }
}

/// Where an earbud is placed in the
/// physical real life world
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Placement {
    InOpenCase,
    Outside,
    Ear,
    InCloseCase,
    Undetected,
}

/// Placement comes inside some payloads so
/// define the decode() here
impl BudProperty for Placement {
    type Item = Placement;

    fn decode(val: u8) -> Placement {
        match val {
            1 => Placement::Ear,
            2 => Placement::Outside,
            3 => Placement::InOpenCase,
            4 => Placement::InCloseCase,
            _ => Placement::Undetected,
        }
    }

    fn encode(&self) -> u8 {
        match *self {
            Placement::Ear => 1,
            Placement::Outside => 2,
            Placement::InOpenCase => 3,
            Placement::InCloseCase => 4,
            Placement::Undetected => 0,
        }
    }
}

/// Which option is set for holding the touchpad
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum TouchpadOption {
    NoiseCanceling,
    VoiceCommand,
    Volume,
    Spotify,
    Undetected,
}

impl BudProperty for TouchpadOption {
    type Item = TouchpadOption;

    fn encode(&self) -> u8 {
        match *self {
            TouchpadOption::VoiceCommand => 1,
            TouchpadOption::NoiseCanceling => 2,
            TouchpadOption::Volume => 3,
            TouchpadOption::Spotify => 4,
            TouchpadOption::Undetected => 0,
        }
    }

    fn decode(val: u8) -> TouchpadOption {
        match val {
            1 => TouchpadOption::VoiceCommand,
            2 => TouchpadOption::NoiseCanceling,
            3 => TouchpadOption::Volume,
            4 => TouchpadOption::Spotify,
            _ => TouchpadOption::Undetected,
        }
    }
}

/// The selected EqualizerType
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum EqualizerType {
    Normal,
    BassBoost,
    Soft,
    Dynamic,
    Clear,
    TrebleBoost,
    Undetected,
}

impl BudProperty for EqualizerType {
    type Item = EqualizerType;

    fn decode(val: u8) -> EqualizerType {
        match val {
            0 => EqualizerType::Normal,
            1 => EqualizerType::BassBoost,
            2 => EqualizerType::Soft,
            3 => EqualizerType::Dynamic,
            4 => EqualizerType::Clear,
            5 => EqualizerType::TrebleBoost,
            _ => EqualizerType::Undetected,
        }
    }

    fn encode(&self) -> u8 {
        match *self {
            EqualizerType::Normal => 0,
            EqualizerType::BassBoost => 2,
            EqualizerType::Soft => 2,
            EqualizerType::Dynamic => 3,
            EqualizerType::Clear => 4,
            EqualizerType::TrebleBoost => 5,
            EqualizerType::Undetected => 10,
        }
    }
}