Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,43 @@ pub fn get_device_list() -> Result<Vec<DevInfo>> {

Ok(devs)
}

/// Gets the closest frequency using the given range.
pub fn get_nearest_frequency(frequency: u64, range: Range) -> u64 {
let desired_frequency = frequency as f64;
let multiplier = (desired_frequency - range.min) / range.step;

let integer_multiplier = multiplier.round();

(range.min + (integer_multiplier * range.step)) as u64
}

/// Gets the closest gain using the given range.
pub fn get_nearest_gain(gain: Gain, range: Range) -> Gain {
let desired_gain = f64::from(gain);
let multiplier = (desired_gain - range.min) / range.step;

let integer_multiplier = multiplier.round();

(range.min + (integer_multiplier * range.step)) as Gain
}

#[cfg(test)]
mod tests {
use crate::{get_nearest_frequency, Range};

#[test]
fn test_get_nearest_frequency() {
let range = Range {
min: 70_000_005.0,
max: 6e9,
step: 2.0,
};

let set_freq = 433_000_000;

let actual_freq = get_nearest_frequency(set_freq, range);

assert_eq!(actual_freq, 433_000_001);
}
}
2 changes: 1 addition & 1 deletion src/types/range.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::sys::*;

/// Range struct to represent `bladerf_range`
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub struct Range {
pub min: f64,
pub max: f64,
Expand Down
59 changes: 41 additions & 18 deletions tests/hardware_any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use std::{thread, time::Duration};

use bladerf::{
BladeRF, BladeRfAny, ChannelLayoutRx, ComplexI12, ComplexI16, Error, Result, RxChannel,
StreamConfig,
get_nearest_frequency, get_nearest_gain, BladeRF, BladeRfAny, Channel, ChannelLayoutRx,
ComplexI12, ComplexI16, Error, Result, RxChannel, StreamConfig,
};
use serial_test::serial;

Expand Down Expand Up @@ -89,13 +89,16 @@ fn print_fpga_version() -> Result<()> {
#[test]
#[serial]
fn get_set_sample_rate() -> Result<()> {
const CHANNEL: Channel = Channel::Rx0;

let device = BladeRfAny::open_first()?;
let actual_rate = device.set_sample_rate(bladerf::Channel::Rx0, 1_000_000)?;
let getter_rate = device.get_sample_rate(bladerf::Channel::Rx0)?;

let actual_rate = device.set_sample_rate(CHANNEL, 1_000_000)?;
let getter_rate = device.get_sample_rate(CHANNEL)?;
assert_eq!(actual_rate, getter_rate);

let actual_rate = device.set_sample_rate(bladerf::Channel::Rx0, 2_000_000)?;
let getter_rate = device.get_sample_rate(bladerf::Channel::Rx0)?;
let actual_rate = device.set_sample_rate(CHANNEL, 2_000_000)?;
let getter_rate = device.get_sample_rate(CHANNEL)?;
assert_eq!(actual_rate, getter_rate);
Ok(())
}
Expand All @@ -121,13 +124,15 @@ fn get_set_rx_mux() -> Result<()> {
#[test]
#[serial]
fn get_set_bandwidth() -> Result<()> {
const CHANNEL: Channel = Channel::Rx0;

let device = BladeRfAny::open_first()?;
let actual_bandwidth = device.set_bandwidth(bladerf::Channel::Rx0, 1_000_000)?;
let getter_bandwidth = device.get_bandwidth(bladerf::Channel::Rx0)?;
let actual_bandwidth = device.set_bandwidth(CHANNEL, 1_000_000)?;
let getter_bandwidth = device.get_bandwidth(CHANNEL)?;
assert_eq!(actual_bandwidth, getter_bandwidth);

let actual_bandwidth = device.set_bandwidth(bladerf::Channel::Rx0, 2_000_000)?;
let getter_bandwidth = device.get_bandwidth(bladerf::Channel::Rx0)?;
let actual_bandwidth = device.set_bandwidth(CHANNEL, 2_000_000)?;
let getter_bandwidth = device.get_bandwidth(CHANNEL)?;
assert_eq!(actual_bandwidth, getter_bandwidth);
Ok(())
}
Expand All @@ -137,15 +142,23 @@ fn get_set_bandwidth() -> Result<()> {
#[test]
#[serial]
fn get_set_frequency() -> Result<()> {
const CHANNEL: Channel = Channel::Rx0;
let device = BladeRfAny::open_first()?;

let frequency_range = device.get_frequency_range(CHANNEL)?;

let set_frequency = 433_500_000;
device.set_frequency(bladerf::Channel::Rx0, set_frequency)?;
let getter_frequency = device.get_frequency(bladerf::Channel::Rx0)?;
let set_frequency = get_nearest_frequency(set_frequency, frequency_range);

device.set_frequency(CHANNEL, set_frequency)?;
let getter_frequency = device.get_frequency(CHANNEL)?;
assert_eq!(set_frequency, getter_frequency);

let set_frequency = 915_000_000;
device.set_frequency(bladerf::Channel::Rx0, set_frequency)?;
let getter_frequency = device.get_frequency(bladerf::Channel::Rx0)?;
let set_frequency = get_nearest_frequency(set_frequency, frequency_range);

device.set_frequency(CHANNEL, set_frequency)?;
let getter_frequency = device.get_frequency(CHANNEL)?;
assert_eq!(set_frequency, getter_frequency);
Ok(())
}
Expand Down Expand Up @@ -173,15 +186,25 @@ fn get_set_loopback() -> Result<()> {
#[test]
#[serial]
fn get_set_gain() -> Result<()> {
const CHANNEL: Channel = Channel::Rx0;

let device = BladeRfAny::open_first()?;

let gain_range = device.get_gain_range(CHANNEL)?;

let set_gain = 20;
device.set_gain(bladerf::Channel::Rx0, set_gain)?;
let getter_gain = device.get_gain(bladerf::Channel::Rx0)?;
let set_gain = get_nearest_gain(set_gain, gain_range);

device.set_gain(CHANNEL, set_gain)?;
let getter_gain = device.get_gain(CHANNEL)?;
assert_eq!(set_gain, getter_gain);

let set_gain = 40;
device.set_gain(bladerf::Channel::Rx0, set_gain)?;
let getter_gain = device.get_gain(bladerf::Channel::Rx0)?;
let set_gain = get_nearest_gain(set_gain, gain_range);

device.set_gain(CHANNEL, set_gain)?;
let getter_gain = device.get_gain(CHANNEL)?;

assert_eq!(set_gain, getter_gain);
Ok(())
}
Expand Down