-
Notifications
You must be signed in to change notification settings - Fork 4
/
fcddirect.cpp
75 lines (73 loc) · 2.29 KB
/
fcddirect.cpp
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
#include <SoapySDR/Device.hpp>
#include <SoapySDR/Modules.hpp>
#include <stdio.h>
#include <signal.h>
static volatile bool done=false;
void trap(int sig)
{
done = true;
}
int main(int argc, char **argv)
{
// find our device
const char *config = "driver=fcdpp";
if (argc>1)
config = argv[1];
SoapySDR::loadModules();
auto device = SoapySDR::Device::make(config);
if (!device)
return 1;
// get a native stream
double fullScale;
std::string fmt = device->getNativeStreamFormat(SOAPY_SDR_RX, 0, fullScale);
std::vector<size_t> chans;
SoapySDR::Kwargs args;
auto stream = device->setupStream(SOAPY_SDR_RX, fmt);
const size_t period = device->getStreamMTU(stream);
const size_t rate = device->getSampleRate(SOAPY_SDR_RX, 0);
fprintf(stderr, "device period=%d rate=%d\n", (int)period, (int)rate);
// check direct buffers are available
if (device->getNumDirectAccessBuffers(stream)!=1)
return 2;
// go!
if (device->activateStream(stream)!=0)
return 3;
fputs("Reading samples, Ctrl-C to stop..\n", stderr);
signal(SIGINT, trap);
int rv = 0;
int tot = 0;
int cnt = 0;
int tmo = 0;
char spin[4]={ '-', '\\', '|', '/' };
while (!done) {
// map the buffer (may wait/timeout)
size_t handle;
const void *buf;
int flg;
long long timeNs;
long long timeout = (long long)period*1000000L*2L/(long long)rate;
// timeout calcualted as twice expected MTU period in usecs
int rv = device->acquireReadBuffer(stream, handle, &buf, flg, timeNs, (long)timeout);
if (rv>0) {
// process the buffer (we simply emit to stdout)
fwrite(buf, 4, rv, stdout);
// release the buffer
device->releaseReadBuffer(stream, handle);
tot += rv;
} else if (SOAPY_SDR_TIMEOUT==rv) {
// count, go round again =)
++tmo;
continue;
} else {
break;
}
fprintf(stderr, "\r%c ", spin[cnt%4]);
fflush(stderr);
++cnt;
}
fprintf(stderr, "processed %d frames, with %d timeouts\n", tot, tmo);
device->deactivateStream(stream);
device->closeStream(stream);
SoapySDR::Device::unmake(device);
return rv<0 ? 4 : 0;
}