Compare commits
No commits in common. "6991e5611e6e9ae7aef5d5acc0345531c9dc41c2" and "46837a68e4c0591bf6f2158436df2f97709356a5" have entirely different histories.
6991e5611e
...
46837a68e4
97
src/lib.rs
97
src/lib.rs
|
@ -46,8 +46,49 @@ where
|
|||
_pin: PhantomData {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn poll(&self) -> PollResult {
|
||||
pub trait SoftSerial<P>
|
||||
where
|
||||
P: PinOps + StaticPinOps,
|
||||
{
|
||||
fn poll(&self) -> PollResult;
|
||||
fn response(&self);
|
||||
|
||||
fn sync_reciever(&self);
|
||||
fn sync_transmitter(&self);
|
||||
|
||||
fn write_byte(&self, data: u8);
|
||||
fn read_byte(&self) -> ReadByteResult;
|
||||
|
||||
#[inline]
|
||||
fn finish_write(&self) {
|
||||
P::into_pull_up_input();
|
||||
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>
|
||||
where
|
||||
P: PinOps + StaticPinOps,
|
||||
{
|
||||
fn poll(&self) -> PollResult {
|
||||
P::into_output();
|
||||
delay_cycles(1);
|
||||
P::into_pull_up_input();
|
||||
|
@ -69,7 +110,7 @@ where
|
|||
PollResult::Ok(())
|
||||
}
|
||||
|
||||
pub fn response(&self) {
|
||||
fn response(&self) {
|
||||
P::into_output_high();
|
||||
|
||||
delay_us(FIRST_HALF_SERIAL_DELAY);
|
||||
|
@ -82,32 +123,21 @@ where
|
|||
}
|
||||
|
||||
#[inline(never)]
|
||||
pub fn sync_transmitter(&self) {
|
||||
fn sync_transmitter(&self) {
|
||||
P::into_output();
|
||||
|
||||
delay_us(SERIAL_DELAY / 2);
|
||||
delay_us(SERIAL_DELAY);
|
||||
|
||||
P::set_high();
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
pub fn sync_reciever(&self) {
|
||||
fn sync_reciever(&self) {
|
||||
while P::is_high() {}
|
||||
|
||||
while P::is_low() {}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn reset(&self) {
|
||||
P::into_pull_up_input();
|
||||
while P::is_low() {}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait SoftSerialWriter<P, T>
|
||||
where
|
||||
P: PinOps + StaticPinOps,
|
||||
{
|
||||
#[inline(never)]
|
||||
fn write_byte(&self, data: u8) {
|
||||
let (mut data, mut parity_bit) = (data, 0);
|
||||
|
@ -137,25 +167,6 @@ where
|
|||
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)]
|
||||
fn read_byte(&self) -> ReadByteResult {
|
||||
let (mut data, mut reciever_parity_bit) = (0, 0);
|
||||
|
@ -191,20 +202,4 @@ where
|
|||
|
||||
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