Skip to content

Commit

Permalink
some code review
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Aug 1, 2024
1 parent 14c41ee commit f6df82a
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 58 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ jobs:
name: rf24-rs docs
path: target/doc
- run: rustup component add llvm-tools-preview
- name: Install cargo-nextest and cargo-llvm-cov
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@v2
with:
tool: cargo-llvm-cov,cargo-binstall
tool: cargo-llvm-cov
- name: Run tests
run: cargo llvm-cov --lcov --output-path lcov.info
- uses: codecov/codecov-action@v4
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This is a pure-rust driver for the nRF24L01 wireless transceivers.
This project aims to support the [embedded rust][embedded-rs] ecosystem.
This includes but is not limited to Linux on RPi. Other points of interest:
- [crates.io for embedded-hal crates][crates-hal]
- [awesome embedded rust][awesome-hal]
- the [awesome embedded rust][awesome-hal] list
- the [embedded-hal][eh] framework

## Goals
Expand Down
24 changes: 2 additions & 22 deletions src/radio/rf24/auto_ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,35 +67,15 @@ where

fn write_ack_payload(&mut self, pipe: u8, buf: &[u8]) -> Result<bool, Self::AutoAckErrorType> {
if self._ack_payloads_enabled && pipe <= 5 {
let len = {
let buf_len = buf.len();
if buf_len > 32 {
32usize
} else {
buf_len
}
};
let len = buf.len().min(32);
self.spi_write_buf(commands::W_ACK_PAYLOAD | pipe, &buf[..len])?;
return Ok(0 == self._status & 1);
}
Ok(false)
}

fn set_auto_retries(&mut self, delay: u8, count: u8) -> Result<(), Self::AutoAckErrorType> {
let out = {
if count > 15 {
15
} else {
count
}
} | ({
if delay > 15 {
15
} else {
delay
}
} << 4);
self.spi_write_byte(registers::SETUP_RETR, out)
self.spi_write_byte(registers::SETUP_RETR, count.min(15) | (delay.min(15) << 4))
}
}

Expand Down
9 changes: 1 addition & 8 deletions src/radio/rf24/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ where
/// The nRF24L01 support 126 channels. The specified `channel` is
/// clamped to the range [0, 125].
fn set_channel(&mut self, channel: u8) -> Result<(), Self::ChannelErrorType> {
let ch = {
if channel > 125 {
125
} else {
channel
}
};
self.spi_write_byte(registers::RF_CH, ch)
self.spi_write_byte(registers::RF_CH, channel.min(125))
}

/// See also [`RF24::set_channel()`].
Expand Down
15 changes: 6 additions & 9 deletions src/radio/rf24/pa_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,17 @@ where
0 => Ok(PaLevel::MIN),
1 => Ok(PaLevel::LOW),
2 => Ok(PaLevel::HIGH),
3 => Ok(PaLevel::MAX),
_ => unreachable!(),
_ => Ok(PaLevel::MAX),
}
}

fn set_pa_level(&mut self, pa_level: PaLevel) -> Result<(), Self::PaLevelErrorType> {
let pa_bin = 1
| ({
match pa_level {
PaLevel::MIN => 0u8,
PaLevel::LOW => 1u8,
PaLevel::HIGH => 2u8,
PaLevel::MAX => 3u8,
}
| (match pa_level {
PaLevel::MIN => 0u8,
PaLevel::LOW => 1u8,
PaLevel::HIGH => 2u8,
PaLevel::MAX => 3u8,
} << 1);
self.spi_read(1, registers::RF_SETUP)?;
let out = self._buf[1] & !(3 << 1) | pa_bin;
Expand Down
7 changes: 1 addition & 6 deletions src/radio/rf24/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ where
if pipe < 2 {
// Clamp the address length used: min(self._addr_length, address.len());
// This means that we only write the bytes that were passed
let width = if address.len() < self._addr_length as usize {
address.len()
} else {
self._addr_length as usize
};
let width = address.len().min(self._addr_length as usize);

// If this is pipe 0, cache the address. This is needed because
// open_writing_pipe() will overwrite the pipe 0 address, so
Expand Down Expand Up @@ -70,7 +66,6 @@ where
2 => 0,
3 => 1,
4 => 2,
5 => 3,
_ => 3,
};
self.spi_write_byte(registers::SETUP_AW, width)?;
Expand Down
11 changes: 1 addition & 10 deletions src/radio/rf24/radio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,7 @@ where
/// The nRF24L01 will repeatedly use the last byte from the last
/// payload even when [`RF24::read()`] is called with an empty RX FIFO.
fn read(&mut self, buf: &mut [u8], len: u8) -> Result<(), Self::RadioErrorType> {
let buf_len = {
let max_len = buf.len() as u8;
if len > max_len {
max_len
} else if len > 32 {
32u8
} else {
len
}
};
let buf_len = (buf.len() as u8).min(len).min(32);
if buf_len == 0 {
return Ok(());
}
Expand Down

0 comments on commit f6df82a

Please sign in to comment.