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
use super::{channel::Channel, duty_cycle::DutyCycle, panning::Panning};

/// Tone flags.
#[cfg_attr(not(target_family = "wasm"), derive(PartialEq, Eq, Debug))]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
#[derive(Clone, Copy)]
pub struct ToneFlags {
    channel: Channel,
    #[cfg_attr(feature = "serde", serde(default))]
    duty_cycle: DutyCycle,
    #[cfg_attr(feature = "serde", serde(default))]
    panning: Panning,
}

impl ToneFlags {
    /// Create a tone flags builder.
    pub const fn builder() -> ToneFlagsBuilder {
        ToneFlagsBuilder::new()
    }

    /// Build new default tone flags.
    pub const fn new() -> Self {
        Self {
            channel: Channel::Pulse1,
            duty_cycle: DutyCycle::C12_5,
            panning: Panning::Center,
        }
    }
}

impl Default for ToneFlags {
    fn default() -> Self {
        Self::new()
    }
}

/// Tone flags builder.
pub struct ToneFlagsBuilder {
    flags: ToneFlags,
}

impl ToneFlagsBuilder {
    /// Create a tone flags builder.
    pub const fn new() -> Self {
        Self {
            flags: ToneFlags::new(),
        }
    }

    /// Set a channel.
    pub const fn with_channel(mut self, channel: Channel) -> Self {
        self.flags.channel = channel;
        self
    }

    /// Set a duty cycle value.
    pub const fn with_duty_cycle(mut self, duty_cycle: DutyCycle) -> Self {
        self.flags.duty_cycle = duty_cycle;
        self
    }

    /// Set panning.
    pub const fn with_panning(mut self, panning: Panning) -> Self {
        self.flags.panning = panning;
        self
    }

    /// Build the tone flags.
    pub const fn build(self) -> ToneFlags {
        self.flags
    }
}

impl Default for ToneFlagsBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl From<ToneFlags> for u16 {
    fn from(value: ToneFlags) -> Self {
        value.channel as u16 | ((value.duty_cycle as u16) << 2) | ((value.panning as u16) << 4)
    }
}

impl From<u16> for ToneFlags {
    fn from(value: u16) -> Self {
        let channel = Channel::from((value & 0b11) as u8);
        let duty_cycle = DutyCycle::from(((value >> 2) & 0b1100) as u8);
        let panning = Panning::from(((value >> 4) & 0b110000) as u8);

        Self {
            channel,
            duty_cycle,
            panning,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn flags_to_u16() {
        assert_eq!(
            u16::from(ToneFlags {
                channel: Channel::Pulse1,
                duty_cycle: DutyCycle::C12_5,
                panning: Panning::Center
            }),
            0
        );
        assert_eq!(
            u16::from(ToneFlags {
                channel: Channel::Pulse2,
                duty_cycle: DutyCycle::C75,
                panning: Panning::Right
            }),
            0x231
        );
    }

    #[test]
    fn flags_from_u16() {
        assert_eq!(
            ToneFlags::from(0),
            ToneFlags {
                channel: Channel::Pulse1,
                duty_cycle: DutyCycle::C12_5,
                panning: Panning::Center
            }
        );
        assert_eq!(
            ToneFlags::from(0x231),
            ToneFlags {
                channel: Channel::Pulse2,
                duty_cycle: DutyCycle::C75,
                panning: Panning::Right
            }
        );
    }
}