diff --git a/library/src/radio/rf24/radio.rs b/library/src/radio/rf24/radio.rs index e01f86a..4a481f3 100644 --- a/library/src/radio/rf24/radio.rs +++ b/library/src/radio/rf24/radio.rs @@ -536,6 +536,7 @@ mod test { let pin_expectations = [ PinTransaction::set(PinState::Low), PinTransaction::set(PinState::High), + PinTransaction::set(PinState::Low), ]; let mut pin_mock = PinMock::new(&pin_expectations); @@ -549,7 +550,7 @@ mod test { } let spi_expectations = spi_test_expects![ - // flush_tx() of artifact ACK payloads + // flush_tx() (vec![commands::FLUSH_TX], vec![0xEu8]), // clear_status_flags() ( @@ -563,11 +564,25 @@ mod test { (buf.to_vec(), vec![0u8; 33]), // spoof a tx_ds event from a NOP write (vec![commands::NOP], vec![0xE | mnemonics::MASK_TX_DS]), + // flush_tx() + (vec![commands::FLUSH_TX], vec![0xEu8]), + // clear_status_flags() + ( + vec![ + registers::STATUS | commands::W_REGISTER, + mnemonics::MASK_MAX_RT | mnemonics::MASK_RX_DR | mnemonics::MASK_TX_DS, + ], + vec![0xFu8, 0u8], + ), ]; let mut spi_mock = SpiMock::new(&spi_expectations); let mut radio = RF24::new(pin_mock.clone(), spi_mock.clone(), delay_mock); let payload = [0x55; 8]; assert!(radio.send(&payload, false).unwrap()); + // again using simulated full TX FIFO + assert!(!radio.send(&payload, false).unwrap()); + radio._config_reg |= 1; // simulate RX mode + assert!(!radio.send(&payload, false).unwrap()); spi_mock.done(); pin_mock.done(); } @@ -651,6 +666,8 @@ mod test { let mut spi_mock = SpiMock::new(&spi_expectations); let mut radio = RF24::new(pin_mock.clone(), spi_mock.clone(), delay_mock); assert!(radio.resend().unwrap()); + radio._config_reg |= 1; // simulate RX mode + assert!(!radio.resend().unwrap()); spi_mock.done(); pin_mock.done(); } diff --git a/library/src/types.rs b/library/src/types.rs index 8cc51e9..3177fb7 100644 --- a/library/src/types.rs +++ b/library/src/types.rs @@ -187,3 +187,107 @@ impl Display for StatusFlags { ) } } + +#[cfg(test)] +mod test { + use crate::StatusFlags; + + use super::{CrcLength, DataRate, FifoState, PaLevel}; + extern crate std; + use std::{format, string::String}; + + fn display_crc(param: CrcLength, expected: String) -> bool { + format!("{param}") == expected + } + + #[test] + fn crc_8bit() { + assert!(display_crc(CrcLength::Bit8, String::from("8 bit"))); + } + + #[test] + fn crc_16bit() { + assert!(display_crc(CrcLength::Bit16, String::from("16 bit"))); + } + + #[test] + fn crc_disable() { + assert!(display_crc(CrcLength::Disabled, String::from("disabled"))); + } + + fn display_fifo_state(param: FifoState, expected: String) -> bool { + format!("{param}") == expected + } + + #[test] + fn fifo_state_empty() { + assert!(display_fifo_state(FifoState::Empty, String::from("Empty"))); + } + + #[test] + fn fifo_state_full() { + assert!(display_fifo_state(FifoState::Full, String::from("Full"))); + } + + #[test] + fn fifo_state_occupied() { + assert!(display_fifo_state( + FifoState::Occupied, + String::from("Occupied") + )); + } + + fn display_data_rate(param: DataRate, expected: String) -> bool { + format!("{param}") == expected + } + + #[test] + fn data_rate_1mbps() { + assert!(display_data_rate(DataRate::Mbps1, String::from("1 Mbps"))); + } + + #[test] + fn data_rate_2mbps() { + assert!(display_data_rate(DataRate::Mbps2, String::from("2 Mbps"))); + } + + #[test] + fn data_rate_250kbps() { + assert!(display_data_rate( + DataRate::Kbps250, + String::from("250 Kbps") + )); + } + + fn display_pa_level(param: PaLevel, expected: String) -> bool { + format!("{param}") == expected + } + + #[test] + fn pa_level_min() { + assert!(display_pa_level(PaLevel::Min, String::from("Min"))); + } + + #[test] + fn pa_level_low() { + assert!(display_pa_level(PaLevel::Low, String::from("Low"))); + } + + #[test] + fn pa_level_high() { + assert!(display_pa_level(PaLevel::High, String::from("High"))); + } + + #[test] + fn pa_level_max() { + assert!(display_pa_level(PaLevel::Max, String::from("Max"))); + } + + #[test] + fn display_flags() { + assert_eq!( + format!("{}", StatusFlags::default()), + String::from("StatusFlags rx_dr: false, tx_ds: false, tx_df: false") + ); + } +}