Fixed SimpleVector push

This commit is contained in:
2023-09-28 21:51:38 +02:00
parent 4d45ebba5b
commit df53777a80
4 changed files with 41 additions and 102 deletions

View File

@@ -2,21 +2,21 @@
#![no_main] #![no_main]
mod webee; mod webee;
use webee::{Webee, Vector}; use webee::{Webee};
use arduino_hal::Peripherals;
use arduino_hal::{port::{Pin, mode::AnyInput}}; use arduino_hal::{delay_ms, Peripherals};
use arduino_hal::hal::port::PD2;
use arduino_hal::port::mode::{Floating, Input};
use arduino_hal::prelude::*; use arduino_hal::prelude::*;
use panic_halt as _; use panic_halt as _;
use crate::webee::SimpleVec;
#[arduino_hal::entry] #[arduino_hal::entry]
fn main() -> ! { fn main() -> ! {
let dp = Peripherals::take().unwrap(); let dp = Peripherals::take().unwrap();
let pins = arduino_hal::pins!(dp); let pins = arduino_hal::pins!(dp);
let status = pins.d13.into_output(); let _status = pins.d13.into_output();
let webee_rx = pins.d2;
let mut serial = arduino_hal::default_serial!(dp, pins, 57600); let mut serial = arduino_hal::default_serial!(dp, pins, 57600);
let mut test = Webee::new( let mut test = Webee::new(
@@ -25,49 +25,23 @@ fn main() -> ! {
pins.d18.into_output() pins.d18.into_output()
); );
// let mut webee = Usart::new( ufmt::uwriteln!(&mut serial, "Webee initialized: {}", webee_rx.is_high()).void_unwrap();
// dp.USART1, delay_ms(1000);
// 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(); let x = SimpleVec::from_slice(&[0x5a, 0xaa, 0xb1]);
// delay_ms(1000); ufmt::uwrite!(&mut serial, "Vector ({}): ", x.len());
// ufmt::uwriteln!(&mut serial, "RX ready: {}", rx.is_high()).void_unwrap(); for byte in &x.items()[..x.len()] {
ufmt::uwrite!(&mut serial, "{:02X} ", *byte);
}
ufmt::uwrite!(&mut serial, "\n");
}
// let webee = Webee::new(dp.USART1, pins.d19, pins.d18); let role = test.send(&[0x5a, 0xaa, 0xb1]);
ufmt::uwrite!(&mut serial, "Role: ");
for byte in role.items() {
// let mut test = webee.write(0xFF); ufmt::uwrite!(&mut serial, "{:02X}", byte);
// 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 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() { // for byte in [0x5a, 0xaa, 0xb1].iter() {
// webee.write_byte(*byte); // webee.write_byte(*byte);

View File

@@ -1,16 +1,11 @@
use arduino_hal::clock::Clock;
use arduino_hal::hal::Atmega; use arduino_hal::hal::Atmega;
use arduino_hal::port::{Pin, PinOps}; use arduino_hal::port::{Pin, PinOps};
use arduino_hal::port::mode::{Input, Output, PullUp}; use arduino_hal::port::mode::{Input, Output};
use arduino_hal::usart::{Usart, UsartOps}; use arduino_hal::usart::{Usart, UsartOps};
use arduino_hal::prelude::*; use arduino_hal::prelude::*;
use avr_device::atmega2560::usart0::ucsr0c::USBS0_A::STOP1;
use embedded_hal::serial::{Read}; mod simple_vector;
pub use simple_vector::SimpleVec;
mod vector;
pub use vector::Vector;
pub struct Webee<USART: UsartOps<Atmega, RX, TX>, RX, TX> pub struct Webee<USART: UsartOps<Atmega, RX, TX>, RX, TX>
@@ -39,20 +34,21 @@ where
} }
} }
pub fn send(&mut self, data: Vector<u8>) -> Vector<u8> { pub fn send(&mut self, data: &[u8]) -> SimpleVec {
for byte in data { for byte in data {
self.webee.write_byte(byte); self.webee.write_byte(*byte);
} }
return self.recv(); return self.recv();
} }
fn recv(&mut self) -> Vector<u8> { fn recv(&mut self) -> SimpleVec {
let mut buffer: Vector<u8> = Vector::new(0, &mut []); let mut byte: u8 = self.webee.read_byte();
let byte: u8 = self.webee.read_byte(); let mut buffer = SimpleVec::new();
while byte != STOP { while byte != STOP {
buffer.push(byte); buffer.push(byte);
byte = self.webee.read_byte();
} }
return buffer; return buffer;

View File

@@ -1,8 +1,6 @@
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub struct SimpleVec { pub struct SimpleVec {
items: [VecItem; 128] items: [VecItem; 128],
} }
@@ -23,27 +21,21 @@ impl VecItem {
} }
impl SimpleVec { impl SimpleVec {
pub fn new() -> Self { pub fn new() -> Self {
Self { items: [VecItem { value: 0, indexed: false }; 128] } Self { items: [VecItem { value: 0, indexed: false }; 128] }
} }
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
let mut len = 0; let mut len = 0;
for i in &self.items { for i in &self.items {
if i.indexed { len = len + 1 }; if i.indexed { len = len + 1 };
}; };
len len
} }
pub fn push(&mut self, value: u8) { pub fn push(&mut self, value: u8) {
let index = if self.len() < 0 { self.len() + 1 } else { 0 };
let vec_item = VecItem { let vec_item = VecItem {
value, value,
indexed: true, indexed: true,
@@ -51,21 +43,19 @@ impl SimpleVec {
let mut items = self.items; let mut items = self.items;
{ {
let (left, right) = items.split_at_mut(self.len()); let (_left, right) = items.split_at_mut(self.len());
right[index] = vec_item; right[0] = vec_item;
} }
self.items = items; self.items = items;
} }
pub fn pop(&mut self) { pub fn pop(&mut self) {
let index = if self.len() > 0 { self.len() - 1 } else { 0 }; let index = if self.len() > 0 { self.len() - 1 } else { 0 };
let mut items = self.items; let mut items = self.items;
{ {
let (left, right) = items.split_at_mut(self.len()); let (left, _right) = items.split_at_mut(self.len());
left[index] = VecItem::new(); left[index] = VecItem::new();
} }
@@ -76,32 +66,11 @@ impl SimpleVec {
self.items.map(|i| i.value) self.items.map(|i| i.value)
} }
pub fn from_slice(slice: &[u8]) -> Self { pub fn from_slice(slice: &[u8]) -> Self {
let mut simple_array = Self::new(); let mut simple_array = Self::new();
for i in slice { for i in slice {
simple_array.push(*i); simple_array.push(*i);
}; };
simple_array simple_array
} }
}
fn main() {
let mut myarr = SimpleVec::new();
myarr.push(15);
println!("{:?}", myarr.items());
myarr.push(122);
println!("{:?}", myarr.items());
myarr.pop();
println!("{:?}", myarr.items());
let mut anotherarr = SimpleVec::from_slice(&[1,2,3]);
println!("{:?}", anotherarr);
} }

View File

@@ -1,13 +1,13 @@
use avr_device::atmega2560::TC0; use avr_device::atmega2560::TC0;
use ufmt::{Formatter, uDisplay, uWrite}; use ufmt::{Formatter, uDisplay, uWrite};
pub struct Vector<'a, T> { pub struct SimpleVector<T> {
size: usize, size: usize,
array: &'a mut [T] array: [T]
} }
impl<'a, T> Vector<'a, T> { impl<T> SimpleVector<T> {
pub fn new(size: usize, array: &'a mut [T]) -> Self { pub fn new(size: usize, array: [T]) -> Self {
Self { Self {
size, array size, array
} }
@@ -28,7 +28,7 @@ impl<'a, T> Vector<'a, T> {
} }
} }
impl<'a, T> Iterator for Vector<'a, T> { impl<'a, T> Iterator for SimpleVector<'a, T> {
type Item = T; type Item = T;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
@@ -36,7 +36,7 @@ impl<'a, T> Iterator for Vector<'a, T> {
} }
} }
impl<'a, T> uDisplay for Vector<'a, T> { impl<'a, T> uDisplay for SimpleVector<'a, T> {
fn fmt<W>(&self, _: &mut Formatter<'_, W>) -> Result<(), W::Error> where W: uWrite + ?Sized { fn fmt<W>(&self, _: &mut Formatter<'_, W>) -> Result<(), W::Error> where W: uWrite + ?Sized {
todo!() todo!()
} }