remove read_typed macro

This commit is contained in:
Robert 2022-12-18 18:51:18 +01:00
parent 6d89245f2c
commit 31d1df64ef
2 changed files with 12 additions and 28 deletions

View file

@ -6,7 +6,7 @@ use std::{process::Command, time::Duration, fmt::Debug, thread::sleep};
use rand::Rng;
use serialport::SerialPort;
use crate::{assert::assert_in_range, error::Error, request::Request, usart::read_sized, build_request, read_typed};
use crate::{assert::assert_in_range, error::Error, request::Request, usart::read_sized, build_request};
macro_rules! log {
($text: literal, $($arg:tt)*) => (println!(concat!("[B15F] ", $text), $($arg)*));
@ -150,8 +150,8 @@ impl B15F {
self.usart.write(build_request![request, reversed])?;
let aw: u8 = read_typed!(self.usart, u8);
if aw != B15F::MSG_OK {
let aw = read_sized::<1>(&mut self.usart)?;
if aw[0] != B15F::MSG_OK {
return Err(format!("Setting Port {} failed", PORT).into());
}
@ -193,8 +193,8 @@ impl B15F {
self.usart.clear(serialport::ClearBuffer::Input)?;
self.usart.write(build_request![request])?;
let aw: u8 = read_typed!(self.usart, u8);
Ok(u8::reverse_bits(aw))
let aw = read_sized::<1>(&mut self.usart)?;
Ok(u8::reverse_bits(aw[0]))
}
/// Yields information about the installed firmware on the B15
@ -247,9 +247,9 @@ impl B15F {
data_count[0] -= 1;
}
let aw: u8 = read_typed!(self.usart, u8);
if aw != B15F::MSG_OK {
return Err(format!("Board info is faulty: code {}", aw).into());
let aw = read_sized::<1>(&mut self.usart)?;
if aw[0] != B15F::MSG_OK {
return Err(format!("Board info is faulty: code {}", aw[0]).into());
}
Ok(info)
@ -279,8 +279,10 @@ impl B15F {
self.usart.write(build_request!(Request::IntTest, dummy & 0xFF, dummy >> 8))?;
let aw: u16 = read_typed!(self.usart, u16);
if aw != dummy * 3 {
let aw = read_sized::<2>(&mut self.usart)?;
let result = u16::from_le_bytes(aw);
if result != dummy * 3 {
return Err("Int conversion failed".into());
}

View file

@ -1,24 +1,6 @@
use serialport::SerialPort;
use crate::error::Error;
/// Reads from a USART connecction and casts the result into the specified type
///
/// # Errors
/// This macro may throw an `error::Error` when reading from the connection fails.
/// Due to the implementation of this macro, an erroneous result is returned early
/// with the `?` operator, and not unwrapped.
///
/// # Unsafe
/// This macro makes use of `std::mem::transmute`.
#[macro_export]
macro_rules! read_typed {
($usart: expr, $T: ty) => {
unsafe {
std::mem::transmute(read_sized::<{ std::mem::size_of::<$T>() }>(&mut $usart)?)
}
};
}
pub fn read_sized<const N: usize> (usart: &mut Box<dyn SerialPort>) -> Result<[u8; N], Error> {
let mut buf: [u8; N] = [0; N];