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]
mod webee;
use webee::{Webee, Vector};
use webee::{Webee};
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::{delay_ms, Peripherals};
use arduino_hal::prelude::*;
use panic_halt as _;
use crate::webee::SimpleVec;
#[arduino_hal::entry]
fn main() -> ! {
let dp = Peripherals::take().unwrap();
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 test = Webee::new(
@@ -25,49 +25,23 @@ fn main() -> ! {
pins.d18.into_output()
);
// let mut webee = Usart::new(
// dp.USART1,
// pins.d19,
// pins.d18.into_output(),
// 38400.into_baudrate(),
// );
ufmt::uwriteln!(&mut serial, "Webee initialized: {}", webee_rx.is_high()).void_unwrap();
delay_ms(1000);
// 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 x = SimpleVec::from_slice(&[0x5a, 0xaa, 0xb1]);
ufmt::uwrite!(&mut serial, "Vector ({}): ", x.len());
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 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 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);
let role = test.send(&[0x5a, 0xaa, 0xb1]);
ufmt::uwrite!(&mut serial, "Role: ");
for byte in role.items() {
ufmt::uwrite!(&mut serial, "{:02X}", byte);
}
// for byte in [0x5a, 0xaa, 0xb1].iter() {
// webee.write_byte(*byte);

View File

@@ -1,16 +1,11 @@
use arduino_hal::clock::Clock;
use arduino_hal::hal::Atmega;
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::prelude::*;
use avr_device::atmega2560::usart0::ucsr0c::USBS0_A::STOP1;
use embedded_hal::serial::{Read};
mod vector;
pub use vector::Vector;
mod simple_vector;
pub use simple_vector::SimpleVec;
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 {
self.webee.write_byte(byte);
self.webee.write_byte(*byte);
}
return self.recv();
}
fn recv(&mut self) -> Vector<u8> {
let mut buffer: Vector<u8> = Vector::new(0, &mut []);
let byte: u8 = self.webee.read_byte();
fn recv(&mut self) -> SimpleVec {
let mut byte: u8 = self.webee.read_byte();
let mut buffer = SimpleVec::new();
while byte != STOP {
buffer.push(byte);
byte = self.webee.read_byte();
}
return buffer;

View File

@@ -1,8 +1,6 @@
#[derive(Copy, Clone, Debug)]
pub struct SimpleVec {
items: [VecItem; 128]
items: [VecItem; 128],
}
@@ -23,27 +21,21 @@ impl VecItem {
}
impl SimpleVec {
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 {
let mut len = 0;
for i in &self.items {
if i.indexed { len = len + 1 };
};
len
}
pub fn push(&mut self, value: u8) {
let index = if self.len() < 0 { self.len() + 1 } else { 0 };
let vec_item = VecItem {
value,
indexed: true,
@@ -51,21 +43,19 @@ impl SimpleVec {
let mut items = self.items;
{
let (left, right) = items.split_at_mut(self.len());
right[index] = vec_item;
let (_left, right) = items.split_at_mut(self.len());
right[0] = vec_item;
}
self.items = items;
}
pub fn pop(&mut self) {
let index = if self.len() > 0 { self.len() - 1 } else { 0 };
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();
}
@@ -76,32 +66,11 @@ impl SimpleVec {
self.items.map(|i| i.value)
}
pub fn from_slice(slice: &[u8]) -> Self {
let mut simple_array = Self::new();
for i in slice {
simple_array.push(*i);
};
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 ufmt::{Formatter, uDisplay, uWrite};
pub struct Vector<'a, T> {
pub struct SimpleVector<T> {
size: usize,
array: &'a mut [T]
array: [T]
}
impl<'a, T> Vector<'a, T> {
pub fn new(size: usize, array: &'a mut [T]) -> Self {
impl<T> SimpleVector<T> {
pub fn new(size: usize, array: [T]) -> Self {
Self {
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;
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 {
todo!()
}