### Example: RX Capture Setup Source: https://docs.rs/hackrfone/latest/hackrfone/struct.HackRfOne.html?search=u32+-%3E+bool Demonstrates the setup for receiving (RX) samples from the HackRF One, including setting sample rate, frequency, and gains, and spawning a sample capture thread. ```rust use hackrfone::{HackRfOne, UnknownMode, RxMode}; use std::thread; use std::sync::mpsc::{self, TryRecvError}; fn iq_to_cplx_f32(i: u8, q: u8) -> num_complex::Complex32 { let i_f = (i as f32 / 127.5) - 1.0; let q_f = (q as f32 / 127.5) - 1.0; num_complex::Complex32::new(i_f, q_f) } fn main() { let mut radio: HackRfOne = HackRfOne::new().expect("Failed to open HackRF One"); const FC: u64 = 915_000_000; const FS: u32 = 10_000_000; const DIV: u32 = 2; radio .set_sample_rate(FS * DIV, DIV) .expect("Failed to set sample rate"); radio.set_freq(FC).expect("Failed to set frequency"); radio .set_amp_enable(false) .expect("Failed to disable amplifier"); radio .set_antenna_enable(0) .expect("Failed to disable antenna"); radio.set_lna_gain(20).expect("Failed to set LNA gain"); radio.set_vga_gain(32).expect("Failed to set VGA gain"); let mut radio: HackRfOne = radio.into_rx_mode().expect("Failed to enter RX mode"); let (data_tx, data_rx) = mpsc::channel(); let (exit_tx, exit_rx) = mpsc::channel(); let sample_thread = thread::Builder::new() .name("sample".to_string()) .spawn(move || -> Result<(), hackrfone::Error> { println!("Spawned sample thread"); loop { let samples: Vec = radio.rx()?; data_tx .send(samples) .expect("Failed to send buffer from sample thread"); match exit_rx.try_recv() { Ok(_) => { radio.stop_rx()?; return Ok(()); } Err(TryRecvError::Disconnected) => { println!("Main thread disconnected"); return Ok(()); } Err(TryRecvError::Empty) => {} } } }) .expect("Failed to spawn sample thread"); const NUM_SAMPLES: usize = 1024 * 1024; let mut capture_buf: Vec = Vec::with_capacity(NUM_SAMPLES); loop { match data_rx.try_recv() { Ok(buf) => buf.chunks_exact(2).for_each(|iq| { capture_buf.push(iq_to_cplx_f32(iq[0], iq[1])); }), Err(TryRecvError::Disconnected) => { println!("Sample thread disconnected"); break; } Err(TryRecvError::Empty) => {} // Continue waiting } // ... do signal processing with capture buf in the loop // ... or wait for the buffer to fill and do processing outside if capture_buf.len() >= NUM_SAMPLES { break; } } println!("Shutting down sample thread"); if let Err(e) = exit_tx.send(()) { println!("Failed to send exit event (receiver disconnected): {}", e); } sample_thread .join() .expect("Failed to join sample thread") .expect("Sample thread returned an error"); println!("Done"); } ``` -------------------------------- ### Example: RX - Basic Radio Reception Setup Source: https://docs.rs/hackrfone/latest/hackrfone/struct.HackRfOne.html?search=u32+-%3E+bool This example demonstrates setting up a HackRF One for receiving, configuring sample rate, frequency, and gains, then entering RX mode and setting up a sample capture thread. ```rust fn main() { let mut radio: HackRfOne = HackRfOne::new().expect("Failed to open HackRF One"); const FC: u64 = 915_000_000; const FS: u32 = 10_000_000; const DIV: u32 = 2; radio .set_sample_rate(FS * DIV, DIV) .expect("Failed to set sample rate"); radio.set_freq(FC).expect("Failed to set frequency"); radio .set_amp_enable(false) .expect("Failed to disable amplifier"); radio .set_antenna_enable(0) .expect("Failed to disable antenna"); radio.set_lna_gain(20).expect("Failed to set LNA gain"); radio.set_vga_gain(32).expect("Failed to set VGA gain"); let mut radio: HackRfOne = radio.into_rx_mode().expect("Failed to enter RX mode"); let (data_tx, data_rx) = mpsc::channel(); let (exit_tx, exit_rx) = mpsc::channel(); let sample_thread = thread::Builder::new() .name("sample".to_string()) .spawn(move || -> Result<(), hackrfone::Error> { println!("Spawned sample thread"); loop { let samples: Vec = radio.rx()?; data_tx .send(samples) .expect("Failed to send buffer from sample thread"); match exit_rx.try_recv() { Ok(_) => { radio.stop_rx()?; return Ok(()); } Err(TryRecvError::Disconnected) => { println!("Main thread disconnected"); return Ok(()); } Err(TryRecvError::Empty) => {} } } }) .expect("Failed to spawn sample thread"); const NUM_SAMPLES: usize = 1024 * 1024; let mut capture_buf: Vec = Vec::with_capacity(NUM_SAMPLES); loop { match data_rx.try_recv() { Ok(buf) => buf.chunks_exact(2).for_each(|iq| { capture_buf.push(iq_to_cplx_f32(iq[0], iq[1])); }), Err(TryRecvError::Disconnected) => { println!("Sample thread disconnected"); break; } Err(TryRecvError::Empty) => {} } // ... do signal processing with capture buf in the loop // ... or wait for the buffer to fill and do processing outside if capture_buf.len() >= NUM_SAMPLES { break; } } println!("Shutting down sample thread"); if let Err(e) = exit_tx.send(()) { println!("Failed to send exit event (receiver disconnected): {}", e); } sample_thread .join() .expect("Failed to join sample thread") .expect("Sample thread returned an error"); println!("Done"); } ``` -------------------------------- ### Example Usage in rx.rs Source: https://docs.rs/hackrfone/latest/hackrfone/struct.HackRfOne.html?search=u32+-%3E+bool This example demonstrates setting the sample rate, frequency, and amplifier settings before entering RX mode. It also includes setup for sample capture threads and buffer management. ```rust 13fn main() { 14 let mut radio: HackRfOne = HackRfOne::new().expect("Failed to open HackRF One"); 15 16 const FC: u64 = 915_000_000; 17 const FS: u32 = 10_000_000; 18 const DIV: u32 = 2; 19 radio 20 .set_sample_rate(FS * DIV, DIV) 21 .expect("Failed to set sample rate"); 22 radio.set_freq(FC).expect("Failed to set frequency"); 23 radio 24 .set_amp_enable(false) 25 .expect("Failed to disable amplifier"); 26 radio 27 .set_antenna_enable(0) 28 .expect("Failed to disable antenna"); 29 radio.set_lna_gain(20).expect("Failed to set LNA gain"); 30 radio.set_vga_gain(32).expect("Failed to set VGA gain"); 31 let mut radio: HackRfOne = radio.into_rx_mode().expect("Failed to enter RX mode"); 32 33 let (data_tx, data_rx) = mpsc::channel(); 34 let (exit_tx, exit_rx) = mpsc::channel(); 35 36 let sample_thread = thread::Builder::new() 37 .name("sample".to_string()) 38 .spawn(move || -> Result<(), hackrfone::Error> { 39 println!("Spawned sample thread"); 40 41 loop { 42 let samples: Vec = radio.rx()?; 43 data_tx 44 .send(samples) 45 .expect("Failed to send buffer from sample thread"); 46 47 match exit_rx.try_recv() { 48 Ok(_) => { 49 radio.stop_rx()?; 50 return Ok(()); 51 } 52 Err(TryRecvError::Disconnected) => { 53 println!("Main thread disconnected"); 54 return Ok(()); 55 } 56 Err(TryRecvError::Empty) => {} 57 } 58 } 59 }) 60 .expect("Failed to spawn sample thread"); 61 62 const NUM_SAMPLES: usize = 1024 * 1024; 63 let mut capture_buf: Vec = Vec::with_capacity(NUM_SAMPLES); 64 65 loop { 66 match data_rx.try_recv() { 67 Ok(buf) => buf.chunks_exact(2).for_each(|iq| { 68 capture_buf.push(iq_to_cplx_f32(iq[0], iq[1])); 69 }), 70 Err(TryRecvError::Disconnected) => { 71 println!("Sample thread disconnected"); 72 break; 73 } 74 Err(TryRecvError::Empty) => {} 75 } 76 77 // ... do signal processing with capture buf in the loop 78 79 // ... or wait for the buffer to fill and do processing outside 80 if capture_buf.len() >= NUM_SAMPLES { 81 break; 82 } 83 } 84 85 println!("Shutting down sample thread"); 86 87 if let Err(e) = exit_tx.send(()) { 88 println!("Failed to send exit event (receiver disconnected): {}", e); 89 } 90 91 sample_thread 92 .join() 93 .expect("Failed to join sample thread") 94 .expect("Sample thread returned an error"); 95 96 println!("Done"); 97} ``` -------------------------------- ### Full RX Example with Configuration Source: https://docs.rs/hackrfone/latest/hackrfone/struct.HackRfOne.html?search=std%3A%3Avec Demonstrates a comprehensive setup for receiving radio signals, including setting sample rate, frequency, amplifier, antenna, and gains, followed by sample capture in a separate thread. ```rust use hackrfone::{HackRfOne, UnknownMode, RxMode}; use std::sync::mpsc::{self, TryRecvError}; use std::thread; fn main() { let mut radio: HackRfOne = HackRfOne::new().expect("Failed to open HackRF One"); const FC: u64 = 915_000_000; const FS: u32 = 10_000_000; const DIV: u32 = 2; radio .set_sample_rate(FS * DIV, DIV) .expect("Failed to set sample rate"); radio.set_freq(FC).expect("Failed to set frequency"); radio .set_amp_enable(false) .expect("Failed to disable amplifier"); radio .set_antenna_enable(0) .expect("Failed to disable antenna"); radio.set_lna_gain(20).expect("Failed to set LNA gain"); radio.set_vga_gain(32).expect("Failed to set VGA gain"); let mut radio: HackRfOne = radio.into_rx_mode().expect("Failed to enter RX mode"); let (data_tx, data_rx) = mpsc::channel(); let (exit_tx, exit_rx) = mpsc::channel(); let sample_thread = thread::Builder::new() .name("sample".to_string()) .spawn(move || -> Result<(), hackrfone::Error> { println!("Spawned sample thread"); loop { let samples: Vec = radio.rx()?; data_tx .send(samples) .expect("Failed to send buffer from sample thread"); match exit_rx.try_recv() { Ok(_) => { radio.stop_rx()?; return Ok(()); } Err(TryRecvError::Disconnected) => { println!("Main thread disconnected"); return Ok(()); } Err(TryRecvError::Empty) => {} } } }) .expect("Failed to spawn sample thread"); const NUM_SAMPLES: usize = 1024 * 1024; let mut capture_buf: Vec = Vec::with_capacity(NUM_SAMPLES); loop { match data_rx.try_recv() { Ok(buf) => buf.chunks_exact(2).for_each(|iq| { capture_buf.push(iq_to_cplx_f32(iq[0], iq[1])); }), Err(TryRecvError::Disconnected) => { println!("Sample thread disconnected"); break; } Err(TryRecvError::Empty) => {} } // ... do signal processing with capture buf in the loop // ... or wait for the buffer to fill and do processing outside if capture_buf.len() >= NUM_SAMPLES { break; } } println!("Shutting down sample thread"); if let Err(e) = exit_tx.send(()) { println!("Failed to send exit event (receiver disconnected): {}", e); } sample_thread .join() .expect("Failed to join sample thread") .expect("Sample thread returned an error"); println!("Done"); } fn iq_to_cplx_f32(i: u8, q: u8) -> Complex32 { Complex32::new(i as f32, q as f32) } ``` -------------------------------- ### Full RX Example with Threading Source: https://docs.rs/hackrfone/latest/hackrfone/struct.HackRfOne.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E A comprehensive example demonstrating continuous reception in a separate thread and stopping it gracefully. It includes setup, sample collection, and thread management. ```rust use hackrfone::{HackRfOne, RxMode, UnknownMode}; use std::thread; use std::sync::mpsc::{self, TryRecvError}; fn main() { let mut radio: HackRfOne = HackRfOne::new().expect("Failed to open HackRF One"); const FC: u64 = 915_000_000; const FS: u32 = 10_000_000; const DIV: u32 = 2; radio .set_sample_rate(FS * DIV, DIV) .expect("Failed to set sample rate"); radio.set_freq(FC).expect("Failed to set frequency"); radio .set_amp_enable(false) .expect("Failed to disable amplifier"); radio .set_antenna_enable(0) .expect("Failed to disable antenna"); radio.set_lna_gain(20).expect("Failed to set LNA gain"); radio.set_vga_gain(32).expect("Failed to set VGA gain"); let mut radio: HackRfOne = radio.into_rx_mode().expect("Failed to enter RX mode"); let (data_tx, data_rx) = mpsc::channel(); let (exit_tx, exit_rx) = mpsc::channel(); let sample_thread = thread::Builder::new() .name("sample".to_string()) .spawn(move || -> Result<(), hackrfone::Error> { println!("Spawned sample thread"); loop { let samples: Vec = radio.rx()?; data_tx .send(samples) .expect("Failed to send buffer from sample thread"); match exit_rx.try_recv() { Ok(_) => { radio.stop_rx()?; return Ok(()); } Err(TryRecvError::Disconnected) => { println!("Main thread disconnected"); return Ok(()); } Err(TryRecvError::Empty) => {} } } }) .expect("Failed to spawn sample thread"); const NUM_SAMPLES: usize = 1024 * 1024; let mut capture_buf: Vec = Vec::with_capacity(NUM_SAMPLES); loop { match data_rx.try_recv() { Ok(buf) => buf.chunks_exact(2).for_each(|iq| { capture_buf.push(iq_to_cplx_f32(iq[0], iq[1])); }), Err(TryRecvError::Disconnected) => { println!("Sample thread disconnected"); break; } Err(TryRecvError::Empty) => {} } // ... do signal processing with capture buf in the loop // ... or wait for the buffer to fill and do processing outside if capture_buf.len() >= NUM_SAMPLES { break; } } println!("Shutting down sample thread"); if let Err(e) = exit_tx.send(()) { println!("Failed to send exit event (receiver disconnected): {}", e); } sample_thread .join() .expect("Failed to join sample thread") .expect("Sample thread returned an error"); println!("Done"); } fn iq_to_cplx_f32(i: u8, q: u8) -> Complex32 { // Placeholder for actual conversion logic Complex32::new(i as f32, q as f32) } // Dummy Complex32 struct for compilation #[derive(Debug, Clone, Copy)] struct Complex32 { re: f32, im: f32 } impl Complex32 { fn new(re: f32, im: f32) -> Self { Complex32 { re, im } } } ``` -------------------------------- ### Example: Info - HackRF One Details Source: https://docs.rs/hackrfone/latest/hackrfone/struct.HackRfOne.html?search=u32+-%3E+bool A basic example demonstrating how to initialize a HackRF One device and print its board ID, version, and device version. ```rust fn main() { let radio: HackRfOne = HackRfOne::new().expect("Failed to open HackRF One"); println!("Board ID: {:?}", radio.board_id()); println!("Version: {:?}", radio.version()); println!("Device version: {:?}", radio.device_version()); } ``` -------------------------------- ### Full RX Example with HackRF One Source: https://docs.rs/hackrfone/latest/hackrfone/struct.HackRfOne.html A comprehensive example demonstrating the setup and usage of HackRF One for receiving radio signals. It includes setting sample rate, frequency, amplifier, antenna, gains, and then proceeds to spawn a thread for continuous data capture. ```rust fn main() { let mut radio: HackRfOne = HackRfOne::new().expect("Failed to open HackRF One"); const FC: u64 = 915_000_000; const FS: u32 = 10_000_000; const DIV: u32 = 2; radio .set_sample_rate(FS * DIV, DIV) .expect("Failed to set sample rate"); radio.set_freq(FC).expect("Failed to set frequency"); radio .set_amp_enable(false) .expect("Failed to disable amplifier"); radio .set_antenna_enable(0) .expect("Failed to disable antenna"); radio.set_lna_gain(20).expect("Failed to set LNA gain"); radio.set_vga_gain(32).expect("Failed to set VGA gain"); let mut radio: HackRfOne = radio.into_rx_mode().expect("Failed to enter RX mode"); let (data_tx, data_rx) = mpsc::channel(); let (exit_tx, exit_rx) = mpsc::channel(); let sample_thread = thread::Builder::new() .name("sample".to_string()) .spawn(move || -> Result<(), hackrfone::Error> { println!("Spawned sample thread"); loop { let samples: Vec = radio.rx()?; data_tx .send(samples) .expect("Failed to send buffer from sample thread"); match exit_rx.try_recv() { Ok(_) => { radio.stop_rx()?; return Ok(()); } Err(TryRecvError::Disconnected) => { println!("Main thread disconnected"); return Ok(()); } Err(TryRecvError::Empty) => {} } } }) .expect("Failed to spawn sample thread"); const NUM_SAMPLES: usize = 1024 * 1024; let mut capture_buf: Vec = Vec::with_capacity(NUM_SAMPLES); loop { match data_rx.try_recv() { Ok(buf) => buf.chunks_exact(2).for_each(|iq| { capture_buf.push(iq_to_cplx_f32(iq[0], iq[1])); }), Err(TryRecvError::Disconnected) => { println!("Sample thread disconnected"); break; } Err(TryRecvError::Empty) => {} } // ... do signal processing with capture buf in the loop // ... or wait for the buffer to fill and do processing outside if capture_buf.len() >= NUM_SAMPLES { break; } } println!("Shutting down sample thread"); if let Err(e) = exit_tx.send(()) { println!("Failed to send exit event (receiver disconnected): {}", e); } sample_thread .join() .expect("Failed to join sample thread") .expect("Sample thread returned an error"); println!("Done"); } ``` -------------------------------- ### RX Example with iq_to_cplx_f32 Source: https://docs.rs/hackrfone/latest/hackrfone/fn.iq_to_cplx_f32.html?search=u32+-%3E+bool This comprehensive example demonstrates setting up a HackRF One device for reception, capturing samples in a separate thread, and processing these samples into complex f32 numbers using `iq_to_cplx_f32`. It includes channel communication for thread synchronization and buffer management. ```rust use hackrfone::{HackRfOne, RxMode, UnknownMode, iq_to_cplx_f32}; use std::sync::mpsc; use std::thread; fn main() { let mut radio: HackRfOne = HackRfOne::new().expect("Failed to open HackRF One"); const FC: u64 = 915_000_000; const FS: u32 = 10_000_000; const DIV: u32 = 2; radio .set_sample_rate(FS * DIV, DIV) .expect("Failed to set sample rate"); radio.set_freq(FC).expect("Failed to set frequency"); radio .set_amp_enable(false) .expect("Failed to disable amplifier"); radio .set_antenna_enable(0) .expect("Failed to disable antenna"); radio.set_lna_gain(20).expect("Failed to set LNA gain"); radio.set_vga_gain(32).expect("Failed to set VGA gain"); let mut radio: HackRfOne = radio.into_rx_mode().expect("Failed to enter RX mode"); let (data_tx, data_rx) = mpsc::channel(); let (exit_tx, exit_rx) = mpsc::channel(); let sample_thread = thread::Builder::new() .name("sample".to_string()) .spawn(move || -> Result<(), hackrfone::Error> { println!("Spawned sample thread"); loop { let samples: Vec = radio.rx()?; data_tx .send(samples) .expect("Failed to send buffer from sample thread"); match exit_rx.try_recv() { Ok(_) => { radio.stop_rx()?; return Ok(()); } Err(mpsc::TryRecvError::Disconnected) => { println!("Main thread disconnected"); return Ok(()); } Err(mpsc::TryRecvError::Empty) => {} } } }) .expect("Failed to spawn sample thread"); const NUM_SAMPLES: usize = 1024 * 1024; let mut capture_buf: Vec> = Vec::with_capacity(NUM_SAMPLES); loop { match data_rx.try_recv() { Ok(buf) => buf.chunks_exact(2).for_each(|iq| { capture_buf.push(iq_to_cplx_f32(iq[0], iq[1])); }), Err(mpsc::TryRecvError::Disconnected) => { println!("Sample thread disconnected"); break; } Err(mpsc::TryRecvError::Empty) => {} } // ... do signal processing with capture buf in the loop // ... or wait for the buffer to fill and do processing outside if capture_buf.len() >= NUM_SAMPLES { break; } } println!("Shutting down sample thread"); if let Err(e) = exit_tx.send(()) { println!("Failed to send exit event (receiver disconnected): {}", e); } sample_thread .join() .expect("Failed to join sample thread") .expect("Sample thread returned an error"); println!("Done"); } ``` -------------------------------- ### Example: Read Board ID and Version Source: https://docs.rs/hackrfone/latest/hackrfone/struct.HackRfOne.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E A simple example demonstrating how to read the board ID and firmware version from a HackRF One device. ```rust 3fn main() { 4 let radio: HackRfOne = HackRfOne::new().expect("Failed to open HackRF One"); 5 println!("Board ID: {:?}", radio.board_id()); 6 println!("Version: {:?}", radio.version()); 7 println!("Device version: {:?}", radio.device_version()); 8} ``` -------------------------------- ### RX Example with iq_to_cplx_f32 in a sample thread Source: https://docs.rs/hackrfone/latest/hackrfone/fn.iq_to_cplx_f32.html?search= This comprehensive example from the repository's `rx.rs` file demonstrates setting up a HackRF One for reception, capturing samples in a separate thread, and converting them to `Complex` using `iq_to_cplx_f32` within the main loop. ```rust use hackrfone::{HackRfOne, RxMode, UnknownMode, iq_to_cplx_f32}; use std::sync::mpsc::{self, TryRecvError}; use std::thread; fn main() { let mut radio: HackRfOne = HackRfOne::new().expect("Failed to open HackRF One"); const FC: u64 = 915_000_000; const FS: u32 = 10_000_000; const DIV: u32 = 2; radio .set_sample_rate(FS * DIV, DIV) .expect("Failed to set sample rate"); radio.set_freq(FC).expect("Failed to set frequency"); radio .set_amp_enable(false) .expect("Failed to disable amplifier"); radio .set_antenna_enable(0) .expect("Failed to disable antenna"); radio.set_lna_gain(20).expect("Failed to set LNA gain"); radio.set_vga_gain(32).expect("Failed to set VGA gain"); let mut radio: HackRfOne = radio.into_rx_mode().expect("Failed to enter RX mode"); let (data_tx, data_rx) = mpsc::channel(); let (exit_tx, exit_rx) = mpsc::channel(); let sample_thread = thread::Builder::new() .name("sample".to_string()) .spawn(move || -> Result<(), hackrfone::Error> { println!("Spawned sample thread"); loop { let samples: Vec = radio.rx()?; data_tx .send(samples) .expect("Failed to send buffer from sample thread"); match exit_rx.try_recv() { Ok(_) => { radio.stop_rx()?; return Ok(()); } Err(TryRecvError::Disconnected) => { println!("Main thread disconnected"); return Ok(()); } Err(TryRecvError::Empty) => {} } } }) .expect("Failed to spawn sample thread"); const NUM_SAMPLES: usize = 1024 * 1024; let mut capture_buf: Vec = Vec::with_capacity(NUM_SAMPLES); loop { match data_rx.try_recv() { Ok(buf) => buf.chunks_exact(2).for_each(|iq| { capture_buf.push(iq_to_cplx_f32(iq[0], iq[1])); }), Err(TryRecvError::Disconnected) => { println!("Sample thread disconnected"); break; } Err(TryRecvError::Empty) => {} } // ... do signal processing with capture buf in the loop // ... or wait for the buffer to fill and do processing outside if capture_buf.len() >= NUM_SAMPLES { break; } } println!("Shutting down sample thread"); if let Err(e) = exit_tx.send(()) { println!("Failed to send exit event (receiver disconnected): {}", e); } sample_thread .join() .expect("Failed to join sample thread") .expect("Sample thread returned an error"); println!("Done"); } ``` -------------------------------- ### Full Example: RX Mode with Amplifier Disabled Source: https://docs.rs/hackrfone/latest/hackrfone/struct.HackRfOne.html?search= Demonstrates setting up the HackRF One for RX mode, including disabling the amplifier, setting sample rate, frequency, gains, and starting a sample thread for data reception. ```rust use hackrfone::{HackRfOne, UnknownMode, RxMode}; use std::sync::mpsc; use std::thread; use std::sync::mpsc::TryRecvError; fn iq_to_cplx_f32(i: u8, q: u8) -> num_complex::Complex32 { let i_f = (i as f32 - 127.5) / 128.0; let q_f = (q as f32 - 127.5) / 128.0; num_complex::Complex32::new(i_f, q_f) } fn main() { let mut radio: HackRfOne = HackRfOne::new().expect("Failed to open HackRF One"); const FC: u64 = 915_000_000; const FS: u32 = 10_000_000; const DIV: u32 = 2; radio .set_sample_rate(FS * DIV, DIV) .expect("Failed to set sample rate"); radio.set_freq(FC).expect("Failed to set frequency"); radio .set_amp_enable(false) .expect("Failed to disable amplifier"); radio .set_antenna_enable(0) .expect("Failed to disable antenna"); radio.set_lna_gain(20).expect("Failed to set LNA gain"); radio.set_vga_gain(32).expect("Failed to set VGA gain"); let mut radio: HackRfOne = radio.into_rx_mode().expect("Failed to enter RX mode"); let (data_tx, data_rx) = mpsc::channel(); let (exit_tx, exit_rx) = mpsc::channel(); let sample_thread = thread::Builder::new() .name("sample".to_string()) .spawn(move || -> Result<(), hackrfone::Error> { println!("Spawned sample thread"); loop { let samples: Vec = radio.rx()?; data_tx .send(samples) .expect("Failed to send buffer from sample thread"); match exit_rx.try_recv() { Ok(_) => { radio.stop_rx()?; return Ok(()); } Err(TryRecvError::Disconnected) => { println!("Main thread disconnected"); return Ok(()); } Err(TryRecvError::Empty) => {} } } }) .expect("Failed to spawn sample thread"); const NUM_SAMPLES: usize = 1024 * 1024; let mut capture_buf: Vec = Vec::with_capacity(NUM_SAMPLES); loop { match data_rx.try_recv() { Ok(buf) => buf.chunks_exact(2).for_each(|iq| { capture_buf.push(iq_to_cplx_f32(iq[0], iq[1])); }), Err(TryRecvError::Disconnected) => { println!("Sample thread disconnected"); break; } Err(TryRecvError::Empty) => {} } // ... do signal processing with capture buf in the loop // ... or wait for the buffer to fill and do processing outside if capture_buf.len() >= NUM_SAMPLES { break; } } println!("Shutting down sample thread"); if let Err(e) = exit_tx.send(()) { println!("Failed to send exit event (receiver disconnected): {}", e); } sample_thread .join() .expect("Failed to join sample thread") .expect("Sample thread returned an error"); println!("Done"); } ``` -------------------------------- ### RX Example with Amplifier Disabled Source: https://docs.rs/hackrfone/latest/hackrfone/struct.HackRfOne.html?search=std%3A%3Avec This example demonstrates setting up the HackRF One for reception, including disabling the amplifier, setting frequency and sample rate, and then entering a loop to capture samples. ```rust use hackrfone::{HackRfOne, RxMode, UnknownMode}; use std::sync::mpsc; use std::sync::mpsc::TryRecvError; use std::thread; fn main() { let mut radio: HackRfOne = HackRfOne::new().expect("Failed to open HackRF One"); const FC: u64 = 915_000_000; const FS: u32 = 10_000_000; const DIV: u32 = 2; radio .set_sample_rate(FS * DIV, DIV) .expect("Failed to set sample rate"); radio.set_freq(FC).expect("Failed to set frequency"); radio .set_amp_enable(false) .expect("Failed to disable amplifier"); radio .set_antenna_enable(0) .expect("Failed to disable antenna"); radio.set_lna_gain(20).expect("Failed to set LNA gain"); radio.set_vga_gain(32).expect("Failed to set VGA gain"); let mut radio: HackRfOne = radio.into_rx_mode().expect("Failed to enter RX mode"); let (data_tx, data_rx) = mpsc::channel(); let (exit_tx, exit_rx) = mpsc::channel(); let sample_thread = thread::Builder::new() .name("sample".to_string()) .spawn(move || -> Result<(), hackrfone::Error> { println!("Spawned sample thread"); loop { let samples: Vec = radio.rx()?; data_tx .send(samples) .expect("Failed to send buffer from sample thread"); match exit_rx.try_recv() { Ok(_) => { radio.stop_rx()?; return Ok(()); } Err(TryRecvError::Disconnected) => { println!("Main thread disconnected"); return Ok(()); } Err(TryRecvError::Empty) => {} } } }) .expect("Failed to spawn sample thread"); const NUM_SAMPLES: usize = 1024 * 1024; let mut capture_buf: Vec = Vec::with_capacity(NUM_SAMPLES); loop { match data_rx.try_recv() { Ok(buf) => buf.chunks_exact(2).for_each(|iq| { capture_buf.push(iq_to_cplx_f32(iq[0], iq[1])); }), Err(TryRecvError::Disconnected) => { println!("Sample thread disconnected"); break; } Err(TryRecvError::Empty) => {} } // ... do signal processing with capture buf in the loop // ... or wait for the buffer to fill and do processing outside if capture_buf.len() >= NUM_SAMPLES { break; } } println!("Shutting down sample thread"); if let Err(e) = exit_tx.send(()) { println!("Failed to send exit event (receiver disconnected): {}", e); } sample_thread .join() .expect("Failed to join sample thread") .expect("Sample thread returned an error"); println!("Done"); } fn iq_to_cplx_f32(i: u8, q: u8) -> Complex32 { Complex32::new(i as f32, q as f32) } ``` -------------------------------- ### Full RX Example with HackRF One Source: https://docs.rs/hackrfone/latest/hackrfone/struct.HackRfOne.html?search=u32+-%3E+bool This example demonstrates setting up the HackRF One for reception, including configuring sample rate, frequency, and gains, then entering RX mode and capturing samples in a separate thread. It shows how to manage the sample capture and signal the thread to exit. ```rust 13fn main() { 14 let mut radio: HackRfOne = HackRfOne::new().expect("Failed to open HackRF One"); 15 16 const FC: u64 = 915_000_000; 17 const FS: u32 = 10_000_000; 18 const DIV: u32 = 2; 19 radio 20 .set_sample_rate(FS * DIV, DIV) 21 .expect("Failed to set sample rate"); 22 radio.set_freq(FC).expect("Failed to set frequency"); 23 radio 24 .set_amp_enable(false) 25 .expect("Failed to disable amplifier"); 26 radio 27 .set_antenna_enable(0) 28 .expect("Failed to disable antenna"); 29 radio.set_lna_gain(20).expect("Failed to set LNA gain"); 30 radio.set_vga_gain(32).expect("Failed to set VGA gain"); 31 let mut radio: HackRfOne = radio.into_rx_mode().expect("Failed to enter RX mode"); 32 33 let (data_tx, data_rx) = mpsc::channel(); 34 let (exit_tx, exit_rx) = mpsc::channel(); 35 36 let sample_thread = thread::Builder::new() 37 .name("sample".to_string()) 38 .spawn(move || -> Result<(), hackrfone::Error> { 39 println!("Spawned sample thread"); 40 41 loop { 42 let samples: Vec = radio.rx()?; 43 data_tx 44 .send(samples) 45 .expect("Failed to send buffer from sample thread"); 46 47 match exit_rx.try_recv() { 48 Ok(_) => { 49 radio.stop_rx()?; 50 return Ok(()); 51 } 52 Err(TryRecvError::Disconnected) => { 53 println!("Main thread disconnected"); 54 return Ok(()); 55 } 56 Err(TryRecvError::Empty) => {} 57 } 58 } 59 }) 60 .expect("Failed to spawn sample thread"); 61 62 const NUM_SAMPLES: usize = 1024 * 1024; 63 let mut capture_buf: Vec = Vec::with_capacity(NUM_SAMPLES); 64 65 loop { 66 match data_rx.try_recv() { 67 Ok(buf) => buf.chunks_exact(2).for_each(|iq| { 68 capture_buf.push(iq_to_cplx_f32(iq[0], iq[1])); 69 }), 70 Err(TryRecvError::Disconnected) => { 71 println!("Sample thread disconnected"); 72 break; 73 } 74 Err(TryRecvError::Empty) => {} 75 } 76 77 // ... do signal processing with capture buf in the loop 78 79 // ... or wait for the buffer to fill and do processing outside 80 if capture_buf.len() >= NUM_SAMPLES { 81 break; 82 } 83 } 84 85 println!("Shutting down sample thread"); 86 87 if let Err(e) = exit_tx.send(()) { 88 println!("Failed to send exit event (receiver disconnected): {}", e); 89 } 90 91 sample_thread 92 .join() 93 .expect("Failed to join sample thread") 94 .expect("Sample thread returned an error"); 95 96 println!("Done"); 97} ``` -------------------------------- ### Configure HackRF One for RX and Set LNA Gain Source: https://docs.rs/hackrfone/latest/hackrfone/struct.HackRfOne.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E This example shows a comprehensive setup for receiving radio signals, including setting the sample rate, frequency, disabling amplifiers and antenna, and setting both LNA and VGA gain before entering RX mode. It's part of a larger example demonstrating radio reception and data capture. ```rust use hackrfone::{HackRfOne, UnknownMode, RxMode}; use std::sync::mpsc::{self, TryRecvError}; use std::thread; fn main() { let mut radio: HackRfOne = HackRfOne::new().expect("Failed to open HackRF One"); const FC: u64 = 915_000_000; const FS: u32 = 10_000_000; const DIV: u32 = 2; radio .set_sample_rate(FS * DIV, DIV) .expect("Failed to set sample rate"); radio.set_freq(FC).expect("Failed to set frequency"); radio .set_amp_enable(false) .expect("Failed to disable amplifier"); radio .set_antenna_enable(0) .expect("Failed to disable antenna"); radio.set_lna_gain(20).expect("Failed to set LNA gain"); radio.set_vga_gain(32).expect("Failed to set VGA gain"); let mut radio: HackRfOne = radio.into_rx_mode().expect("Failed to enter RX mode"); let (data_tx, data_rx) = mpsc::channel(); let (exit_tx, exit_rx) = mpsc::channel(); let sample_thread = thread::Builder::new() .name("sample".to_string()) .spawn(move || -> Result<(), hackrfone::Error> { println!("Spawned sample thread"); loop { let samples: Vec = radio.rx()?; data_tx .send(samples) .expect("Failed to send buffer from sample thread"); match exit_rx.try_recv() { Ok(_) => { radio.stop_rx()?; return Ok(()); } Err(TryRecvError::Disconnected) => { println!("Main thread disconnected"); return Ok(()); } Err(TryRecvError::Empty) => {} } } }) .expect("Failed to spawn sample thread"); const NUM_SAMPLES: usize = 1024 * 1024; let mut capture_buf: Vec = Vec::with_capacity(NUM_SAMPLES); loop { match data_rx.try_recv() { Ok(buf) => buf.chunks_exact(2).for_each(|iq| { capture_buf.push(iq_to_cplx_f32(iq[0], iq[1])); }), Err(TryRecvError::Disconnected) => { println!("Sample thread disconnected"); break; } Err(TryRecvError::Empty) => {} // Ignore empty } // ... do signal processing with capture buf in the loop // ... or wait for the buffer to fill and do processing outside if capture_buf.len() >= NUM_SAMPLES { break; } } println!("Shutting down sample thread"); if let Err(e) = exit_tx.send(()) { println!("Failed to send exit event (receiver disconnected): {}", e); } sample_thread .join() .expect("Failed to join sample thread") .expect("Sample thread returned an error"); println!("Done"); } // Helper function assumed to be defined elsewhere for iq_to_cplx_f32 fn iq_to_cplx_f32(i: u8, q: u8) -> Complex32 { // Placeholder implementation Complex32::new((i as f32 - 127.5) / 127.5, (q as f32 - 127.5) / 127.5) } // Dummy Complex32 struct for compilation #[derive(Debug, Clone, Copy)] struct Complex32 { re: f32, im: f32, } impl Complex32 { fn new(re: f32, im: f32) -> Self { Complex32 { re, im } } } ``` -------------------------------- ### Example Calling a Documented Function Source: https://docs.rs/hackrfone/latest/scrape-examples-help.html An example demonstrating how to call a documented function from another crate. This code would be scraped and included in the documentation of `a_func`. ```rust // examples/ex.rs fn main() { a_crate::a_func(); } ```