use crate::{Command, SerializationError};
pub const ADDRESS: u8 = 0x70;
#[derive(Debug, Clone, Copy)]
pub enum Commands {
SetGate { output: u8, state: bool },
PlayNote { output: u8, pitch: i16, volume: i16 },
}
impl Command for Commands {
const MAX_LENGTH: usize = 6;
fn to_bytes<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a [u8], SerializationError> {
match *self {
Self::SetGate { output, state } => {
if buffer.len() < 3 {
return Err(SerializationError::BufferTooSmall);
}
buffer[0] = 0x01;
buffer[1] = output;
buffer[2] = state as u8;
Ok(&buffer[..3])
}
Self::PlayNote {
output,
pitch,
volume,
} => {
if buffer.len() < 6 {
return Err(SerializationError::BufferTooSmall);
}
let pitch_bytes = pitch.to_be_bytes();
let volume_bytes = volume.to_be_bytes();
buffer[0] = 0x08;
buffer[1] = output;
buffer[2] = pitch_bytes[0];
buffer[3] = pitch_bytes[1];
buffer[4] = volume_bytes[0];
buffer[5] = volume_bytes[1];
Ok(&buffer[..6])
}
}
}
}