]>
code.octet-stream.net Git - m17rt/blob - m17app/src/rtlsdr.rs
3 process
::{Child
, Command
, Stdio
},
4 sync
::{mpsc
::SyncSender
, Mutex
},
9 soundmodem
::{InputSource
, SoundmodemEvent
},
15 rtlfm
: Mutex
<Option
<Child
>>,
19 pub fn new(device_index
: usize, frequency_mhz
: f32) -> Result
<Self, M17Error
> {
23 rtlfm
: Mutex
::new(None
),
28 impl InputSource
for RtlSdr
{
29 fn start(&self, tx
: SyncSender
<SoundmodemEvent
>) {
30 // TODO: error handling
31 let mut cmd
= Command
::new("rtl_fm")
36 &format
!("{:.6}M", self.freq
uency
_mhz
),
38 &self.device_index
.to_string(),
42 .stdout(Stdio
::piped())
45 let mut stdout
= cmd
.stdout
.take().unwrap
();
46 let mut buf
= [0u8; 1024];
47 let mut leftover
: Option
<u8> = None
;
48 std
::thread
::spawn(move || {
49 while let Ok(n
) = stdout
.read(&mut buf
) {
50 let mut start_idx
= 0;
51 let mut samples
= vec
![];
52 if let Some(left
) = leftover
{
54 samples
.push(i16::from_le_bytes([left
, buf
[0]]));
59 for sample
in buf
[start_idx
..n
].chunks(2) {
60 if sample
.len() == 2 {
61 samples
.push(i16::from_le_bytes([sample
[0], sample
[1]]))
63 leftover
= Some(sample
[0]);
67 .send(SoundmodemEvent
::BasebandInput(samples
.into
()))
74 *self.rtlfm
.lock().unwrap
() = Some(cmd
);
78 if let Some(mut process
) = self.rtlfm
.lock().unwrap
().take() {
79 let _
= process
.kill();