feat(writer/reader): move write and read methods to SoftSerialWriter and SoftSerialReader traits + impl these traits
a
This commit is contained in:
parent
46837a68e4
commit
6defd8963f
56
src/lib.rs
56
src/lib.rs
|
@ -67,21 +67,6 @@ where
|
||||||
while P::is_low() {}
|
while P::is_low() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_bytes(&self, transmit_data: &[u8]) {
|
|
||||||
for byte in transmit_data {
|
|
||||||
self.write_byte(*byte);
|
|
||||||
self.sync_transmitter();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_bytes(&self, recieve_buffer: &mut [u8]) {
|
|
||||||
for byte in recieve_buffer {
|
|
||||||
if let Ok(data) = self.read_byte() {
|
|
||||||
*byte = data;
|
|
||||||
}
|
|
||||||
self.sync_reciever();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<P> SoftSerial<P> for HalfDuplexSerial<P>
|
impl<P> SoftSerial<P> for HalfDuplexSerial<P>
|
||||||
|
@ -138,6 +123,11 @@ where
|
||||||
while P::is_low() {}
|
while P::is_low() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub trait SoftSerialWriter<P, T>
|
||||||
|
where
|
||||||
|
P: PinOps + StaticPinOps,
|
||||||
|
{
|
||||||
#[inline(never)]
|
#[inline(never)]
|
||||||
fn write_byte(&self, data: u8) {
|
fn write_byte(&self, data: u8) {
|
||||||
let (mut data, mut parity_bit) = (data, 0);
|
let (mut data, mut parity_bit) = (data, 0);
|
||||||
|
@ -167,6 +157,25 @@ where
|
||||||
delay_us(SERIAL_DELAY);
|
delay_us(SERIAL_DELAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn write_bytes(&self, transmit_data: T);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<P> SoftSerialWriter<P, &[u8]> for HalfDuplexSerial<P>
|
||||||
|
where
|
||||||
|
P: PinOps + StaticPinOps,
|
||||||
|
{
|
||||||
|
fn write_bytes(&self, transmit_data: &[u8]) {
|
||||||
|
for byte in transmit_data {
|
||||||
|
self.write_byte(*byte);
|
||||||
|
self.sync_transmitter();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait SoftSerialReader<P, T>
|
||||||
|
where
|
||||||
|
P: PinOps + StaticPinOps,
|
||||||
|
{
|
||||||
#[inline(never)]
|
#[inline(never)]
|
||||||
fn read_byte(&self) -> ReadByteResult {
|
fn read_byte(&self) -> ReadByteResult {
|
||||||
let (mut data, mut reciever_parity_bit) = (0, 0);
|
let (mut data, mut reciever_parity_bit) = (0, 0);
|
||||||
|
@ -202,4 +211,21 @@ where
|
||||||
|
|
||||||
Ok(data)
|
Ok(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn read_bytes(&self, recieve_data: T);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<P> SoftSerialReader<P, &mut [u8]> for HalfDuplexSerial<P>
|
||||||
|
where
|
||||||
|
P: PinOps + StaticPinOps,
|
||||||
|
{
|
||||||
|
fn read_bytes(&self, recieve_data: &mut [u8]) {
|
||||||
|
for byte in recieve_data {
|
||||||
|
if let Ok(data) = self.read_byte() {
|
||||||
|
*byte = data;
|
||||||
|
}
|
||||||
|
self.sync_reciever();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue