Files
avr-webee/src/webee.rs

112 lines
2.6 KiB
Rust

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<USART: UsartOps<Atmega, RX, TX>, RX, TX>
{
pub webee: Usart<USART, RX, TX>,
}
#[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<USART, RX, TX>
Webee<
USART,
Pin<Input, RX>,
Pin<Output, TX>
>
where
USART: UsartOps<Atmega, Pin<Input, RX>, Pin<Output, TX>>,
RX: PinOps,
TX: PinOps
{
pub fn new(device: USART, rx: Pin<Input, RX>, tx: Pin<Output, TX>) -> 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<u8, 128> {
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<u8, 128>{
let frame = [0x5A, 0xAA, cmd as u8];
self.send(&frame)
}
pub fn query_parameter(&mut self) {
}
pub fn recv(&mut self) -> Vec<u8, 128> {
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<u8, 128>) -> &'static str {
// let mut result = String::new();
// for byte in vec.iter() {
// result = format!("{}", byte);
// }
//
// return result.as_str();
// }
}