use arduino_hal::delay_ms; use arduino_hal::hal::Atmega; use arduino_hal::port::{Pin, PinOps}; use arduino_hal::port::mode::{Input, Output}; use arduino_hal::usart::{Usart, UsartOps}; use arduino_hal::prelude::*; use heapless::Vec; pub struct Webee, RX, TX> { pub webee: Usart, } #[repr(u8)] pub enum SendCmd { Role = 0xB1, PanId = 0xB2, Channel = 0xB3, Baudrate = 0xB4, TransmitPower = 0xB5, LocalMulticastNumber = 0xB6, TargetShortAddress = 0xB7, TargetMulticastNumber = 0xB8, AllDataTransmission = 0xB9, MAC = 0xBA, LocalShortAddress = 0xBB, ZigbeeNetworkKey = 0xBC } impl SendCmd where { pub fn to_str(&self) -> &'static str { match self { SendCmd::Role => "Role", SendCmd::PanId => "PAN ID", SendCmd::Channel => "Channel", SendCmd::Baudrate => "Baudrate", SendCmd::TransmitPower => "Transmit Power", SendCmd::MAC => "MAC", SendCmd::ZigbeeNetworkKey => "Zigbee Network Key", _ => "not_implemented" } } } impl Webee< USART, Pin, Pin > where USART: UsartOps, Pin>, RX: PinOps, TX: PinOps { pub fn new(device: USART, rx: Pin, tx: Pin) -> Self { let instance = Self { webee: Usart::new(device, rx, tx, 38400.into_baudrate()) }; delay_ms(1000); instance } pub fn send(&mut self, data: &[u8]) -> Vec { for byte in data.iter() { self.webee.write_byte(*byte); } return self.recv(); } pub fn void_send(&mut self, data: &[u8]) { for byte in data.iter() { self.webee.write_byte(*byte); } } pub fn send_cmd(&mut self, cmd: SendCmd) -> Vec{ let frame = [0x5A, 0xAA, cmd as u8]; self.send(&frame) } pub fn query_parameter(&mut self) { } pub fn recv(&mut self) -> Vec { let mut buffer = Vec::new(); // Read first byte in blocking mode to wait until data is available buffer.push(self.webee.read_byte()).unwrap(); delay_ms(10); while let Ok(byte) = self.webee.read() { buffer.push(byte).unwrap(); } return buffer; } // pub fn vec_to_string(vec: Vec) -> &'static str { // let mut result = String::new(); // for byte in vec.iter() { // result = format!("{}", byte); // } // // return result.as_str(); // } }