diff --git a/src/main.rs b/src/main.rs index 16da565..7ffb5d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,9 @@ mod webee; use webee::{Webee, Vector}; use arduino_hal::Peripherals; +use arduino_hal::{port::{Pin, mode::AnyInput}}; +use arduino_hal::hal::port::PD2; +use arduino_hal::port::mode::{Floating, Input}; use arduino_hal::prelude::*; use panic_halt as _; @@ -15,11 +18,10 @@ fn main() -> ! { 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; let mut test = Webee::new( dp.USART1, - pins.d19, + pins.d19.forget_imode(), pins.d18.into_output() ); @@ -63,7 +65,8 @@ fn main() -> ! { // } // ufmt::uwriteln!(&mut serial, "RX ready: {}", rx.is_high()).void_unwrap(); - let role : Vector = test.send(vector!([0x5a, 0xaa, 0xb1])); + let data: [u8; 3] = [0x5a, 0xaa, 0xb1]; + let role : Vector = test.send(Vector::new(3, data.as_mut_slice())); ufmt::uwriteln!(&mut serial, "Role: {}", role); // for byte in [0x5a, 0xaa, 0xb1].iter() { diff --git a/src/webee.rs b/src/webee.rs index 8fe68a8..39a9f62 100644 --- a/src/webee.rs +++ b/src/webee.rs @@ -1,12 +1,10 @@ use arduino_hal::clock::Clock; use arduino_hal::hal::Atmega; -use arduino_hal::port; -use arduino_hal::port::mode::{Input, Output}; -// use arduino_hal::port; -// use arduino_hal::port::mode::{Input, Output}; +use arduino_hal::port::{Pin, PinOps}; +use arduino_hal::port::mode::{Input, Output, PullUp}; use arduino_hal::usart::{Usart, UsartOps}; use arduino_hal::prelude::*; -use avr_device::atmega2560::{USART1, USART2, USART3}; +use avr_device::atmega2560::usart0::ucsr0c::USBS0_A::STOP1; use embedded_hal::serial::{Read}; @@ -20,22 +18,22 @@ pub struct Webee, RX, TX> webee: Usart, } -enum SEND_CMD {} +enum SendCmd {} + +const STOP: u8 = 0xFF; impl Webee< USART, - port::Pin, - port::Pin + Pin, + Pin > where - USART: UsartOps, port::Pin>, - RX: port::PinOps, - TX: port::PinOps + USART: UsartOps, Pin>, + RX: PinOps, + TX: PinOps { - const STOP: u8 = 0xFF; - - pub fn new(device: USART, rx: port::Pin, tx: port::Pin) -> Self { + pub fn new(device: USART, rx: Pin, tx: Pin) -> Self { Self { webee: Usart::new(device, rx, tx, 38400.into_baudrate()) } @@ -50,10 +48,10 @@ where } fn recv(&mut self) -> Vector { - const buffer: Vector = Vector::new(0, &[]); + let mut buffer: Vector = Vector::new(0, &mut []); let byte: u8 = self.webee.read_byte(); - while byte != self.STOP { + while byte != STOP { buffer.push(byte); } diff --git a/src/webee/vector.rs b/src/webee/vector.rs index e5aca05..04fc297 100644 --- a/src/webee/vector.rs +++ b/src/webee/vector.rs @@ -1,19 +1,50 @@ +use avr_device::atmega2560::TC0; +use ufmt::{Formatter, uDisplay, uWrite}; + pub struct Vector<'a, T> { - size: u16, - array: &'a [T] + size: usize, + array: &'a mut [T] } impl<'a, T> Vector<'a, T> { - pub fn new(size: u16, array: &[T]) -> Self { + pub fn new(size: usize, array: &'a mut [T]) -> Self { Self { size, array } } + + pub fn push(&mut self, byte: T) { + let array : &[T]; + + if self.size > 0 { + array = &mut self.array[0..self.size]; + array[self.size] = byte; + } else { + array = &mut [byte]; + } + + self.array = array; + self.size = self.size + 1; + } +} + +impl<'a, T> Iterator for Vector<'a, T> { + type Item = T; + + fn next(&mut self) -> Option { + todo!() + } +} + +impl<'a, T> uDisplay for Vector<'a, T> { + fn fmt(&self, _: &mut Formatter<'_, W>) -> Result<(), W::Error> where W: uWrite + ?Sized { + todo!() + } } #[macro_export] macro_rules! vector { ($array:expr) => { - Vector::new($array.len(), $array); + Vector::new(&mut $array.len(), $array) }; -} \ No newline at end of file +}