feat: remove complex type casting

This commit is contained in:
doryan 2025-06-01 18:20:24 +04:00
parent e915deb1d5
commit 68c1ff13eb
2 changed files with 82 additions and 86 deletions

View File

@ -107,24 +107,60 @@ where
} }
} }
pub trait SoftSerialWriter<P, T> pub trait SoftSerialWriter<P, T>: SoftSerialByteWriter<P>
where where
P: PinOps + StaticPinOps, P: PinOps + StaticPinOps,
{ {
fn write_byte(&self, data: u8);
fn write_bytes(&self, transmit_data: T); fn write_bytes(&self, transmit_data: T);
} }
pub trait SoftSerialReader<P, T> pub trait SoftSerialReader<P, T>: SoftSerialByteReader<P>
where where
P: PinOps + StaticPinOps, P: PinOps + StaticPinOps,
{ {
fn read_byte(&self) -> ReadByteResult;
fn read_bytes(&self, recieve_data: T); fn read_bytes(&self, recieve_data: T);
} }
#[inline(always)] pub trait SoftSerialByteWriter<P>
pub(crate) fn _priv_read_byte<P: PinOps + StaticPinOps>() -> ReadByteResult { where
P: PinOps + StaticPinOps,
{
#[inline(never)]
fn write_byte(&self, transmit_data: u8) {
let (mut data, mut parity_bit) = (transmit_data, 0);
for _ in 0..8 {
if data & MSB == 0 {
P::set_high();
parity_bit ^= 0;
} else {
P::set_low();
parity_bit ^= 1;
}
delay_us(SERIAL_DELAY);
data <<= 1;
}
// Hamming code and CRC are very weightful and slow, so I use simple parity check
if parity_bit == 0 {
P::set_high();
} else {
P::set_low();
}
delay_us(SERIAL_DELAY);
}
}
pub trait SoftSerialByteReader<P>
where
P: PinOps + StaticPinOps,
{
#[inline(never)]
fn read_byte(&self) -> ReadByteResult {
let (mut data, mut reciever_parity_bit) = (0, 0); let (mut data, mut reciever_parity_bit) = (0, 0);
delay_cycles(FIRST_ENTRY_READING); delay_cycles(FIRST_ENTRY_READING);
@ -158,48 +194,18 @@ pub(crate) fn _priv_read_byte<P: PinOps + StaticPinOps>() -> ReadByteResult {
Ok(data) Ok(data)
} }
#[inline(always)]
pub(crate) fn _priv_write_bytes<P: PinOps + StaticPinOps>(data: u8) {
let (mut data, mut parity_bit) = (data, 0);
for _ in 0..8 {
if data & MSB == 0 {
P::set_high();
parity_bit ^= 0;
} else {
P::set_low();
parity_bit ^= 1;
} }
delay_us(SERIAL_DELAY); impl<P: PinOps + StaticPinOps> SoftSerialByteWriter<P> for HalfDuplexSerial<P> {}
impl<P: PinOps + StaticPinOps> SoftSerialByteReader<P> for HalfDuplexSerial<P> {}
data <<= 1;
}
// Hamming code and CRC are very weightful and slow, so I use simple parity check
if parity_bit == 0 {
P::set_high();
} else {
P::set_low();
}
delay_us(SERIAL_DELAY);
}
impl<P> SoftSerialWriter<P, &[u8]> for HalfDuplexSerial<P> impl<P> SoftSerialWriter<P, &[u8]> for HalfDuplexSerial<P>
where where
P: PinOps + StaticPinOps, P: PinOps + StaticPinOps,
{ {
#[inline(never)]
fn write_byte(&self, data: u8) {
_priv_write_bytes::<P>(data);
}
fn write_bytes(&self, transmit_data: &[u8]) { fn write_bytes(&self, transmit_data: &[u8]) {
for byte in transmit_data { for byte in transmit_data {
<Self as SoftSerialWriter<P, &[u8]>>::write_byte(self, *byte); self.write_byte(*byte);
self.sync_transmitter(); self.sync_transmitter();
} }
} }
@ -209,14 +215,9 @@ impl<P> SoftSerialReader<P, &mut [u8]> for HalfDuplexSerial<P>
where where
P: PinOps + StaticPinOps, P: PinOps + StaticPinOps,
{ {
#[inline(never)]
fn read_byte(&self) -> ReadByteResult {
_priv_read_byte::<P>()
}
fn read_bytes(&self, recieve_data: &mut [u8]) { fn read_bytes(&self, recieve_data: &mut [u8]) {
for byte in recieve_data { for byte in recieve_data {
if let Ok(data) = <Self as SoftSerialReader<P, &mut [u8]>>::read_byte(self) { if let Ok(data) = self.read_byte() {
*byte = data; *byte = data;
} }
self.sync_reciever(); self.sync_reciever();

View File

@ -4,7 +4,10 @@
use arduino_hal::port::PinOps; use arduino_hal::port::PinOps;
use static_pins::StaticPinOps; use static_pins::StaticPinOps;
use crate::{HalfDuplexSerial, SoftSerialReader, SoftSerialWriter}; use crate::{
HalfDuplexSerial, SoftSerialByteReader, SoftSerialByteWriter, SoftSerialReader,
SoftSerialWriter,
};
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct RingBuffer<const N: usize> { pub struct RingBuffer<const N: usize> {
@ -14,11 +17,17 @@ pub struct RingBuffer<const N: usize> {
tail: usize, tail: usize,
} }
impl<const N: usize> Default for RingBuffer<N> {
fn default() -> Self {
Self::new()
}
}
impl<const N: usize> RingBuffer<N> { impl<const N: usize> RingBuffer<N> {
#[inline] #[inline]
pub const fn new() -> Self { pub const fn new() -> Self {
if N.is_power_of_two() { if N.is_power_of_two() {
RingBuffer { Self {
buf: [0; N], buf: [0; N],
mask: N - 1, mask: N - 1,
head: 0, head: 0,
@ -104,21 +113,13 @@ impl<const N: usize, P> SoftSerialWriter<P, &mut RingBuffer<N>> for HalfDuplexSe
where where
P: PinOps + StaticPinOps, P: PinOps + StaticPinOps,
{ {
#[inline(never)]
fn write_byte(&self, data: u8) {
crate::_priv_write_bytes::<P>(data);
}
#[inline(never)] #[inline(never)]
fn write_bytes(&self, transmit_data: &mut RingBuffer<N>) { fn write_bytes(&self, transmit_data: &mut RingBuffer<N>) {
<Self as SoftSerialWriter<P, &mut RingBuffer<N>>>::write_byte( self.write_byte(transmit_data.len() as u8);
self,
transmit_data.len() as u8,
);
for byte in transmit_data { for byte in transmit_data {
self.sync_transmitter(); self.sync_transmitter();
<Self as SoftSerialWriter<P, &mut RingBuffer<N>>>::write_byte(self, byte); self.write_byte(byte);
} }
} }
} }
@ -127,21 +128,15 @@ impl<const N: usize, P> SoftSerialReader<P, &mut RingBuffer<N>> for HalfDuplexSe
where where
P: PinOps + StaticPinOps, P: PinOps + StaticPinOps,
{ {
#[inline(never)]
fn read_byte(&self) -> crate::ReadByteResult {
crate::_priv_read_byte::<P>()
}
#[inline(never)] #[inline(never)]
fn read_bytes(&self, recieve_data: &mut RingBuffer<N>) { fn read_bytes(&self, recieve_data: &mut RingBuffer<N>) {
let byte = <Self as SoftSerialReader<P, &mut RingBuffer<N>>>::read_byte(self); let byte = self.read_byte();
if let Ok(len) = byte { if let Ok(len) = byte {
for _ in 0..len { for _ in 0..len {
self.sync_reciever(); self.sync_reciever();
if let Ok(byte) = <Self as SoftSerialReader<P, &mut RingBuffer<N>>>::read_byte(self) if let Ok(byte) = self.read_byte() {
{
recieve_data.push(byte).unwrap_or(()); recieve_data.push(byte).unwrap_or(());
} }
} }