1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use RequestBuffer;
use crate::;
/// A HackRF operating in receive mode.
///
/// To receive data, first take a HackRF peripheral and call
/// [`HackRf::start_rx`], or use [`Receive::new`] with it.
///
/// Next, call [`submit`][Receive::submit] to queue up requests, stopping when
/// there are enough pending requests. `libhackrf` queues up 1 MiB of data, or
/// 524288 samples. You'll probably want something similar, with the number of
/// pending requests informed by your chosen transfer block size.
///
/// Actual reception is done with [`next_complete`][Receive::next_complete],
/// which will panic if there are no pending requests. The number of pending
/// requests can always be checked with [`pending`][Receive::pending].
///
/// When finished receiving, call [`stop`][Receive::stop] to cancel all
/// remaining transactions and switch the HackRF off again.
///
/// Putting it all together, here's an example receive program that writes all
/// data to a file:
///
///
/// ```no_run
/// use std::sync::{Arc, atomic};
///
/// use anyhow::Result;
/// use tokio::io::AsyncWriteExt;
/// use waverave_hackrf::Buffer;
/// #[tokio::main]
/// async fn main() -> Result<()> {
/// // Set up the ctrl-c handler
/// let ctrlc_rx = Arc::new(atomic::AtomicBool::new(false));
/// let ctrlc_tx = ctrlc_rx.clone();
/// tokio::spawn(async move {
/// tokio::signal::ctrl_c().await.unwrap();
/// ctrlc_tx.store(true, atomic::Ordering::Release);
/// });
///
/// // Open up a file for buffered writing.
/// let mut args = std::env::args();
/// args.next();
/// let file_name = args.next().unwrap_or_else(|| String::from("./rx.bin"));
/// let mut file = tokio::fs::File::create(&file_name).await?;
///
/// // Open up the HackRF
/// let hackrf = waverave_hackrf::open_hackrf()?;
///
/// // Configure: 20MHz sample rate, turn on the RF amp, set IF & BB gains to 16 dB,
/// // and tune to 915 MHz.
/// hackrf.set_amp_enable(true).await?;
/// hackrf.set_sample_rate(20e6).await?;
/// hackrf.set_lna_gain(16).await?;
/// hackrf.set_vga_gain(16).await?;
/// hackrf.set_freq(915_000_000).await?;
///
/// // Start receiving, in bursts of 8192 samples
/// let mut hackrf_rx = hackrf.start_rx(8192).await.map_err(|e| e.err)?;
///
/// // Separate the file writer from the sample reader with a separate task
/// let (data_send, mut data_recv) = tokio::sync::mpsc::unbounded_channel::<Buffer>();
/// let file_writer = tokio::spawn(async move {
/// loop {
/// let Some(buf) = data_recv.recv().await else {
/// break;
/// };
/// file.write_all_buf(&mut buf.bytes()).await?;
/// }
/// file.flush().await?;
/// Ok::<(), anyhow::Error>(())
/// });
///
/// // Queue up 64 transfers immediately, then start retrieving them until we
/// // get a ctrl-c.
/// for _ in 0..64 {
/// hackrf_rx.submit();
/// }
/// loop {
/// if ctrlc_rx.load(atomic::Ordering::Acquire) {
/// break;
/// }
/// if data_send.send(hackrf_rx.next_complete().await?).is_err() {
/// break;
/// }
/// hackrf_rx.submit();
/// }
///
/// // Stop receiving
/// hackrf_rx.stop().await?;
/// drop(data_send);
///
/// // Wait for file writer task to close up shop
/// file_writer.await??;
///
/// Ok(())
/// }
/// ```