Add Webee stuff
This commit is contained in:
122
src/main.rs
122
src/main.rs
@@ -1,27 +1,123 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
mod webee;
|
||||
use webee::{Webee, Vector};
|
||||
|
||||
use arduino_hal::Peripherals;
|
||||
use arduino_hal::prelude::*;
|
||||
use panic_halt as _;
|
||||
|
||||
|
||||
#[arduino_hal::entry]
|
||||
fn main() -> ! {
|
||||
let dp = arduino_hal::Peripherals::take().unwrap();
|
||||
let dp = Peripherals::take().unwrap();
|
||||
let pins = arduino_hal::pins!(dp);
|
||||
let status = pins.d13.into_output();
|
||||
let mut serial = arduino_hal::default_serial!(dp, pins, 57600);
|
||||
let rx = pins.d2;
|
||||
|
||||
/*
|
||||
* For examples (and inspiration), head to
|
||||
*
|
||||
* https://github.com/Rahix/avr-hal/tree/main/examples
|
||||
*
|
||||
* NOTE: Not all examples were ported to all boards! There is a good chance though, that code
|
||||
* for a different board can be adapted for yours. The Arduino Uno currently has the most
|
||||
* examples available.
|
||||
*/
|
||||
let mut test = Webee::new(
|
||||
dp.USART1,
|
||||
pins.d19,
|
||||
pins.d18.into_output()
|
||||
);
|
||||
|
||||
let mut led = pins.d13.into_output();
|
||||
// let mut webee = Usart::new(
|
||||
// dp.USART1,
|
||||
// pins.d19,
|
||||
// pins.d18.into_output(),
|
||||
// 38400.into_baudrate(),
|
||||
// );
|
||||
|
||||
// ufmt::uwriteln!(&mut serial, "Hello, World!").void_unwrap();
|
||||
// ufmt::uwriteln!(&mut serial, "RX ready: {}", rx.is_high()).void_unwrap();
|
||||
// delay_ms(1000);
|
||||
// ufmt::uwriteln!(&mut serial, "RX ready: {}", rx.is_high()).void_unwrap();
|
||||
|
||||
// let webee = Webee::new(dp.USART1, pins.d19, pins.d18);
|
||||
|
||||
|
||||
// let mut test = webee.write(0xFF);
|
||||
// while let Err(_) = test {
|
||||
// test = webee.write(0xFF);
|
||||
// ufmt::uwriteln!(&mut serial, "initializing..").void_unwrap();
|
||||
// delay_ms(500);
|
||||
// }
|
||||
ufmt::uwriteln!(&mut serial, "Webee initialized!").void_unwrap();
|
||||
// delay_ms(1000);
|
||||
|
||||
// let mut init = true;
|
||||
// while init {
|
||||
// match webee.write(0) {
|
||||
// Ok(_) => {
|
||||
// webee.flush();
|
||||
// ufmt::uwriteln!(&mut serial, "Webee initialized!").void_unwrap();
|
||||
// init = false;
|
||||
// }
|
||||
// Err(_) => {
|
||||
// ufmt::uwriteln!(&mut serial, "initializing..").void_unwrap();
|
||||
// delay_ms(500);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// ufmt::uwriteln!(&mut serial, "RX ready: {}", rx.is_high()).void_unwrap();
|
||||
let role : Vector<u8> = test.send(vector!([0x5a, 0xaa, 0xb1]));
|
||||
ufmt::uwriteln!(&mut serial, "Role: {}", role);
|
||||
|
||||
// for byte in [0x5a, 0xaa, 0xb1].iter() {
|
||||
// webee.write_byte(*byte);
|
||||
// ufmt::uwriteln!(&mut serial, "Wrote byte: {:X}", *byte).void_unwrap();
|
||||
// }
|
||||
//
|
||||
// ufmt::uwrite!(&mut serial, "Role:").void_unwrap();
|
||||
// let mut result: u8 = webee.read_byte();
|
||||
//
|
||||
// while result != 0xFF {
|
||||
// ufmt::uwrite!(&mut serial, " {:02X}", result).void_unwrap();
|
||||
// result = webee.read_byte();
|
||||
// }
|
||||
|
||||
loop {
|
||||
led.toggle();
|
||||
arduino_hal::delay_ms(1000);
|
||||
let b = nb::block!(serial.read()).void_unwrap();
|
||||
|
||||
ufmt::uwriteln!(&mut serial, "Got {:X}!", b).void_unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
// fn readBytesUntil()
|
||||
|
||||
// struct Webee<USART, RX, TX> {
|
||||
// serial: Usart<USART, RX, TX>,
|
||||
// }
|
||||
//
|
||||
// impl<USART, RX, TX> Webee<USART, RX, TX> {
|
||||
// pub fn new(usart: USART, rx: RX, tx: TX) -> Self {
|
||||
// Webee {
|
||||
// serial: crate::Usart::new(usart, rx, tx, Baudrate::new(38400)),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// struct Webee<'a> {
|
||||
// serial: &'a usart
|
||||
// }
|
||||
//
|
||||
// impl Webee {
|
||||
// fn new(serial: usart) -> Self {
|
||||
// Self {
|
||||
// serial
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// fn send_cmd(&mut self, cmd: u8) -> ! {
|
||||
// let frame: [u8; 3] = [0x55, 0xAA, cmd];
|
||||
// self.serial.write(&frame)
|
||||
// }
|
||||
//
|
||||
// fn recv(&mut self) -> ! {
|
||||
// self.serial.read()
|
||||
// }
|
||||
// }
|
||||
|
||||
44
src/webee.rs
Normal file
44
src/webee.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use arduino_hal::Usart;
|
||||
use arduino_hal::usart::UsartOps;
|
||||
use arduino_hal::prelude::*;
|
||||
use embedded_hal::serial::{Read};
|
||||
|
||||
mod vector;
|
||||
pub use vector::Vector;
|
||||
|
||||
pub struct Webee<USART, RX, TX> {
|
||||
webee: Usart<USART, RX, TX>,
|
||||
}
|
||||
|
||||
enum SEND_CMD {
|
||||
|
||||
}
|
||||
|
||||
impl<USART, RX, TX> Webee<USART, RX, TX> {
|
||||
const STOP : u8 = 0xFF;
|
||||
|
||||
pub fn new(device: USART, rx: RX, tx: TX) -> Self {
|
||||
Self {
|
||||
webee: Usart::new(device, rx, tx, 38400.into_baudrate())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send(&mut self, data: Vector<u8>) -> Vector<u8> {
|
||||
for byte in data {
|
||||
self.webee.write_byte(byte);
|
||||
}
|
||||
|
||||
return self.recv();
|
||||
}
|
||||
|
||||
fn recv(&mut self) -> Vector<u8> {
|
||||
const buffer : Vector<u8> = Vector::new(0, &[]);
|
||||
let byte : u8 = self.webee.read_byte();
|
||||
|
||||
while byte != self.STOP {
|
||||
buffer.push(byte);
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
19
src/webee/vector.rs
Normal file
19
src/webee/vector.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
pub struct Vector<'a, T> {
|
||||
size: u16,
|
||||
array: &'a [T]
|
||||
}
|
||||
|
||||
impl<'a, T> Vector<'a, T> {
|
||||
pub fn new(size: u16, array: &[T]) -> Self {
|
||||
Self {
|
||||
size, array
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! vector {
|
||||
($array:expr) => {
|
||||
Vector::new($array.len(), $array);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user