headless!
This commit is contained in:
82
src/webee.rs
82
src/webee.rs
@@ -1,10 +1,11 @@
|
||||
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;
|
||||
use heapless::{String, Vec};
|
||||
|
||||
// mod simple_vector;
|
||||
// pub use simple_vector::SimpleVec;
|
||||
@@ -15,9 +16,35 @@ pub struct Webee<USART: UsartOps<Atmega, RX, TX>, RX, TX>
|
||||
pub webee: Usart<USART, RX, TX>,
|
||||
}
|
||||
|
||||
enum SendCmd {}
|
||||
#[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
|
||||
}
|
||||
|
||||
const STOP: u8 = 0xFF;
|
||||
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",
|
||||
_ => "not_implemented"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<USART, RX, TX>
|
||||
Webee<
|
||||
@@ -31,47 +58,56 @@ where
|
||||
TX: PinOps
|
||||
{
|
||||
pub fn new(device: USART, rx: Pin<Input, RX>, tx: Pin<Output, TX>) -> Self {
|
||||
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 {
|
||||
for byte in data.iter() {
|
||||
self.webee.write_byte(*byte);
|
||||
}
|
||||
|
||||
return self.recv();
|
||||
}
|
||||
|
||||
pub fn send_void(&mut self, data: &[u8]) {
|
||||
for byte in data {
|
||||
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 byte: u8 = self.webee.read_byte();
|
||||
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 byte != STOP && !buffer.is_full() {
|
||||
while let Ok(byte) = self.webee.read() {
|
||||
buffer.push(byte).unwrap();
|
||||
byte = self.webee.read_byte();
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
pub fn recv_debug(&mut self, callback: fn(u8)) -> Vec<u8, 128> {
|
||||
let mut byte: u8 = self.webee.read_byte();
|
||||
callback(byte);
|
||||
let mut buffer = Vec::new();
|
||||
|
||||
while byte != STOP && !buffer.is_full() {
|
||||
buffer.push(byte).unwrap();
|
||||
byte = self.webee.read_byte();
|
||||
}
|
||||
|
||||
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();
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user