Vector type hell

This commit is contained in:
2023-09-18 09:39:01 +02:00
parent d84f4ba08f
commit 6f6ce8b314
3 changed files with 56 additions and 24 deletions

View File

@@ -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<u8> = test.send(vector!([0x5a, 0xaa, 0xb1]));
let data: [u8; 3] = [0x5a, 0xaa, 0xb1];
let role : Vector<u8> = test.send(Vector::new(3, data.as_mut_slice()));
ufmt::uwriteln!(&mut serial, "Role: {}", role);
// for byte in [0x5a, 0xaa, 0xb1].iter() {

View File

@@ -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<USART: UsartOps<Atmega, RX, TX>, RX, TX>
webee: Usart<USART, RX, TX>,
}
enum SEND_CMD {}
enum SendCmd {}
const STOP: u8 = 0xFF;
impl<USART, RX, TX>
Webee<
USART,
port::Pin<Input, RX>,
port::Pin<Output, TX>
Pin<Input, RX>,
Pin<Output, TX>
>
where
USART: UsartOps<Atmega, port::Pin<Input, RX>, port::Pin<Output, TX>>,
RX: port::PinOps,
TX: port::PinOps
USART: UsartOps<Atmega, Pin<Input, RX>, Pin<Output, TX>>,
RX: PinOps,
TX: PinOps
{
const STOP: u8 = 0xFF;
pub fn new(device: USART, rx: port::Pin<Input, RX>, tx: port::Pin<Output, TX>) -> Self {
pub fn new(device: USART, rx: Pin<Input, RX>, tx: Pin<Output, TX>) -> Self {
Self {
webee: Usart::new(device, rx, tx, 38400.into_baudrate())
}
@@ -50,10 +48,10 @@ where
}
fn recv(&mut self) -> Vector<u8> {
const buffer: Vector<u8> = Vector::new(0, &[]);
let mut buffer: Vector<u8> = Vector::new(0, &mut []);
let byte: u8 = self.webee.read_byte();
while byte != self.STOP {
while byte != STOP {
buffer.push(byte);
}

View File

@@ -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<Self::Item> {
todo!()
}
}
impl<'a, T> uDisplay for Vector<'a, T> {
fn fmt<W>(&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)
};
}
}