pub mod baseline;
pub mod extended;
pub mod service_mode;
pub use baseline::*;
pub use extended::*;
pub use service_mode::*;
use crate::Error;
use bitvec::prelude::*;
pub type Result<T> = core::result::Result<T, Error>;
struct Preamble(BitArr!(for 14, in u8, Msb0));
const MAX_BITS: usize = 15 + 4 * 9 + 1;
pub type SerialiseBuffer = BitArr!(for MAX_BITS, in u8, Msb0);
fn serialise(data: &[u8], buf: &mut SerialiseBuffer) -> Result<usize> {
let required_bits = 15 + data.len() * 9 + 1;
if required_bits > MAX_BITS {
return Err(Error::TooLong);
}
buf[0..16].copy_from_bitslice([0xff, 0xfe].view_bits::<Msb0>());
let mut pos: usize = 15;
for byte in data {
buf.set(pos, false); pos += 1;
buf[pos..pos + 8].copy_from_bitslice([*byte].view_bits::<Msb0>());
pos += 8;
}
buf.set(pos, true); pos += 1;
Ok(pos)
}
#[cfg(test)]
mod test {
use super::*;
pub fn print_chunks(buf: &SerialiseBuffer, limit: usize) {
println!("Preamble: {}", &buf[..15]);
let mut offset = 15;
while offset < limit - 1 {
println!(
"[{}] Chunk: {}-{:08b}",
offset,
buf[offset] as u8,
&buf[offset + 1..offset + 9]
);
offset += 9;
}
println!("Stop bit: {}", buf[offset] as u8);
}
}