use anyhow::{ Result, }; use crate::improv::{ IMPROV_HEADER, }; pub fn find_begin_of_improv_packet(buffer: &Vec) -> Result { let mut improv_header_char: usize = 0; for (i, b) in buffer.iter().enumerate() { if b == &IMPROV_HEADER[improv_header_char] { improv_header_char += 1; if improv_header_char >= IMPROV_HEADER.len() { return Ok(i - (IMPROV_HEADER.len() - 1)); } } else { improv_header_char = 0; if b == &IMPROV_HEADER[improv_header_char] { improv_header_char += 1; } } } return Err(String::from("Improv header not found")); } pub struct SerialInterface { interface: Box, } impl SerialInterface { pub fn new(path: &str, baud_rate: u32) -> Result { let interface = tokio_serial::new(path, baud_rate).open().unwrap(); return Ok(Self { interface: interface, }); } pub fn send_bytes(&mut self, packet_bytes: &[u8]) { self.interface.write(packet_bytes).unwrap(); } pub fn recv_bytes(&mut self) -> Result> { let mut buffer: Vec = Vec::new(); self.interface.read_to_end(&mut buffer); let improv_packet_offset = find_begin_of_improv_packet(&buffer).unwrap(); let improv_packet_end = improv_packet_offset + 10 + >::into(buffer[improv_packet_offset+8]) return Ok(buffer[improv_packet_offset..improv_packet_end].to_vec()); } }