pub fn run_tx(&mut self) -> Result<WaitingForAckNack, Error<TXO::Error, RXI::Error>> {
        let mut result = WaitingForAckNack::No;
        self.tx_state = match self.tx_state {
            TxState::Idle => {
                // Do nothing
                if self.tx_buffer.len() != 0 {
                    TxState::SendingDelimiterStart {
                        payload: Payload::IFrame,
                    }
                } else if let Some(frame) = self.sframe_pending.take() {
                    TxState::SendingDelimiterStart {
                        payload: Payload::SFrame(frame),
                    }
                } else {
                    TxState::Idle
                }
            }
            TxState::SendingDelimiterStart { payload } => {
                self.writer_write(0x00)?;
                TxState::SendingCobsHeader { payload }
            }
            TxState::SendingCobsHeader { payload } => {
                // Count how many bytes up to the first zero byte.
                // And send that number
                let num = match payload {
                    Payload::IFrame => self.cobs_find_zero(&self.tx_buffer),
                    Payload::SFrame(frame) => self.cobs_find_zero(frame),
                };
                self.writer_write(num as u8 + 1)?;
                TxState::SendingPayload { payload, sent: 0 }
            }
            TxState::SendingPayload { payload, sent } => {
                // Send the complete frame
                let source = match payload {
                    Payload::IFrame => &self.tx_buffer,
                    Payload::SFrame(frame) => frame,
                };
                let len = source.len();
                let mut b = source[sent];
                if b == 0 {
                    // Can't send zeros - send gap to next zero instead
                    let num = self.cobs_find_zero(&source[sent + 1..]);
                    b = num as u8 + 1;
                }
                self.writer_write(b)?;
                let new_sent = sent + 1;
                if new_sent == len {
                    TxState::SendingDelimiterEnd { payload }
                } else {
                    TxState::SendingPayload {
                        payload,
                        sent: new_sent,
                    }
                }
            }
            TxState::SendingDelimiterEnd { payload } => {
                self.writer_write(0x00)?;
                match payload {
                    Payload::IFrame => TxState::WaitingForAckNack { num_polls: 0 },
                    Payload::SFrame { .. } => TxState::Idle,
                }
            }
            TxState::WaitingForAckNack { num_polls } => {
                if num_polls >= self.poll_limit {
                    // Poll N times for ack/nack, else retry
                    TxState::SendingDelimiterStart {
                        payload: Payload::IFrame,
                    }
                } else {
                    result = WaitingForAckNack::Yes;
                    TxState::WaitingForAckNack {
                        num_polls: num_polls + 1,
                    }
                }
            }
        };
        Ok(result)
    }
/// Pumps the TX state machine. Returns `true` if it makes sense to call this function again right away.
/// Returns `false` if we're stuck waiting for an ack and you should wait a while before trying again.
Pumps the TX state machine. Returns `true` if it makes sense to call this function again right away.
Returns `false` if we're stuck waiting for an ack and you should wait a while before trying again.
Pumps the TX state machine . Returns ` true ` if it makes sense to call this function again right away . Returns ` false ` if we ' re stuck waiting for an ack and you should wait a while before trying again .
pub fn run_tx(&mut self) -> Result<WaitingForAckNack, Error<TXO::Error, RXI::Error>> {
        let mut result = WaitingForAckNack::No;
        self.tx_state = match self.tx_state {
            TxState::Idle => {
                // Do nothing
                if self.tx_buffer.len() != 0 {
                    TxState::SendingDelimiterStart {
                        payload: Payload::IFrame,
                    }
                } else if let Some(frame) = self.sframe_pending.take() {
                    TxState::SendingDelimiterStart {
                        payload: Payload::SFrame(frame),
                    }
                } else {
                    TxState::Idle
                }
            }
            TxState::SendingDelimiterStart { payload } => {
                self.writer_write(0x00)?;
                TxState::SendingCobsHeader { payload }
            }
            TxState::SendingCobsHeader { payload } => {
                // Count how many bytes up to the first zero byte.
                // And send that number
                let num = match payload {
                    Payload::IFrame => self.cobs_find_zero(&self.tx_buffer),
                    Payload::SFrame(frame) => self.cobs_find_zero(frame),
                };
                self.writer_write(num as u8 + 1)?;
                TxState::SendingPayload { payload, sent: 0 }
            }
            TxState::SendingPayload { payload, sent } => {
                // Send the complete frame
                let source = match payload {
                    Payload::IFrame => &self.tx_buffer,
                    Payload::SFrame(frame) => frame,
                };
                let len = source.len();
                let mut b = source[sent];
                if b == 0 {
                    // Can't send zeros - send gap to next zero instead
                    let num = self.cobs_find_zero(&source[sent + 1..]);
                    b = num as u8 + 1;
                }
                self.writer_write(b)?;
                let new_sent = sent + 1;
                if new_sent == len {
                    TxState::SendingDelimiterEnd { payload }
                } else {
                    TxState::SendingPayload {
                        payload,
                        sent: new_sent,
                    }
                }
            }
            TxState::SendingDelimiterEnd { payload } => {
                self.writer_write(0x00)?;
                match payload {
                    Payload::IFrame => TxState::WaitingForAckNack { num_polls: 0 },
                    Payload::SFrame { .. } => TxState::Idle,
                }
            }
            TxState::WaitingForAckNack { num_polls } => {
                if num_polls >= self.poll_limit {
                    // Poll N times for ack/nack, else retry
                    TxState::SendingDelimiterStart {
                        payload: Payload::IFrame,
                    }
                } else {
                    result = WaitingForAckNack::Yes;
                    TxState::WaitingForAckNack {
                        num_polls: num_polls + 1,
                    }
                }
            }
        };
        Ok(result)
    }
pub fn run_tx ( & mut self ) -> Result < WaitingForAckNack , Error < TXO :: Error , RXI :: Error > > { let mut result = WaitingForAckNack :: No ; self . tx_state = match self . tx_state { TxState :: Idle => { // Do nothing if self . tx_buffer . len ( ) != 0 { TxState :: SendingDelimiterStart { payload : Payload :: IFrame , } } else if let Some ( frame ) = self . sframe_pending . take ( ) { TxState :: SendingDelimiterStart { payload : Payload :: SFrame ( frame ) , } } else { TxState :: Idle } } TxState :: SendingDelimiterStart { payload } => { self . writer_write ( 0x00 ) ? ; TxState :: SendingCobsHeader { payload } } TxState :: SendingCobsHeader { payload } => { // Count how many bytes up to the first zero byte. // And send that number let num = match payload { Payload :: IFrame => self . cobs_find_zero ( & self . tx_buffer ) , Payload :: SFrame ( frame ) => self . cobs_find_zero ( frame ) , } ; self . writer_write ( num as u8 + 1 ) ? ; TxState :: SendingPayload { payload , sent : 0 } } TxState :: SendingPayload { payload , sent } => { // Send the complete frame let source = match payload { Payload :: IFrame => & self . tx_buffer , Payload :: SFrame ( frame ) => frame , } ; let len = source . len ( ) ; let mut b = source [ sent ] ; if b == 0 { // Can't send zeros - send gap to next zero instead let num = self . cobs_find_zero ( & source [ sent + 1 .. ] ) ; b = num as u8 + 1 ; } self . writer_write ( b ) ? ; let new_sent = sent + 1 ; if new_sent == len { TxState :: SendingDelimiterEnd { payload } } else { TxState :: SendingPayload { payload , sent : new_sent , } } } TxState :: SendingDelimiterEnd { payload } => { self . writer_write ( 0x00 ) ? ; match payload { Payload :: IFrame => TxState :: WaitingForAckNack { num_polls : 0 } , Payload :: SFrame { .. } => TxState :: Idle , } } TxState :: WaitingForAckNack { num_polls } => { if num_polls >= self . poll_limit { // Poll N times for ack/nack, else retry TxState :: SendingDelimiterStart { payload : Payload :: IFrame , } } else { result = WaitingForAckNack :: Yes ; TxState :: WaitingForAckNack { num_polls : num_polls + 1 , } } } } ; Ok ( result ) }
Pumps the TX state machine.
Pumps the TX state machine .
&mut self
&mut self