remove read_typed macro
This commit is contained in:
parent
6d89245f2c
commit
31d1df64ef
22
src/b15f.rs
22
src/b15f.rs
|
@ -6,7 +6,7 @@ use std::{process::Command, time::Duration, fmt::Debug, thread::sleep};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use serialport::SerialPort;
|
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 {
|
macro_rules! log {
|
||||||
($text: literal, $($arg:tt)*) => (println!(concat!("[B15F] ", $text), $($arg)*));
|
($text: literal, $($arg:tt)*) => (println!(concat!("[B15F] ", $text), $($arg)*));
|
||||||
|
@ -150,8 +150,8 @@ impl B15F {
|
||||||
|
|
||||||
self.usart.write(build_request![request, reversed])?;
|
self.usart.write(build_request![request, reversed])?;
|
||||||
|
|
||||||
let aw: u8 = read_typed!(self.usart, u8);
|
let aw = read_sized::<1>(&mut self.usart)?;
|
||||||
if aw != B15F::MSG_OK {
|
if aw[0] != B15F::MSG_OK {
|
||||||
return Err(format!("Setting Port {} failed", PORT).into());
|
return Err(format!("Setting Port {} failed", PORT).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,8 +193,8 @@ impl B15F {
|
||||||
self.usart.clear(serialport::ClearBuffer::Input)?;
|
self.usart.clear(serialport::ClearBuffer::Input)?;
|
||||||
self.usart.write(build_request![request])?;
|
self.usart.write(build_request![request])?;
|
||||||
|
|
||||||
let aw: u8 = read_typed!(self.usart, u8);
|
let aw = read_sized::<1>(&mut self.usart)?;
|
||||||
Ok(u8::reverse_bits(aw))
|
Ok(u8::reverse_bits(aw[0]))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Yields information about the installed firmware on the B15
|
/// Yields information about the installed firmware on the B15
|
||||||
|
@ -247,9 +247,9 @@ impl B15F {
|
||||||
data_count[0] -= 1;
|
data_count[0] -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
let aw: u8 = read_typed!(self.usart, u8);
|
let aw = read_sized::<1>(&mut self.usart)?;
|
||||||
if aw != B15F::MSG_OK {
|
if aw[0] != B15F::MSG_OK {
|
||||||
return Err(format!("Board info is faulty: code {}", aw).into());
|
return Err(format!("Board info is faulty: code {}", aw[0]).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(info)
|
Ok(info)
|
||||||
|
@ -279,8 +279,10 @@ impl B15F {
|
||||||
|
|
||||||
self.usart.write(build_request!(Request::IntTest, dummy & 0xFF, dummy >> 8))?;
|
self.usart.write(build_request!(Request::IntTest, dummy & 0xFF, dummy >> 8))?;
|
||||||
|
|
||||||
let aw: u16 = read_typed!(self.usart, u16);
|
let aw = read_sized::<2>(&mut self.usart)?;
|
||||||
if aw != dummy * 3 {
|
|
||||||
|
let result = u16::from_le_bytes(aw);
|
||||||
|
if result != dummy * 3 {
|
||||||
return Err("Int conversion failed".into());
|
return Err("Int conversion failed".into());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
18
src/usart.rs
18
src/usart.rs
|
@ -1,24 +1,6 @@
|
||||||
use serialport::SerialPort;
|
use serialport::SerialPort;
|
||||||
use crate::error::Error;
|
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> {
|
pub fn read_sized<const N: usize> (usart: &mut Box<dyn SerialPort>) -> Result<[u8; N], Error> {
|
||||||
let mut buf: [u8; N] = [0; N];
|
let mut buf: [u8; N] = [0; N];
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue