26 #define FP_TYPE unsigned short int 31 int printUsage(
string name,
int N,
string chip,
int chCnt,
float T,
float fs) {
32 cout<<name<<
" : An application to stream input from IIO devices to file."<<endl;
34 cout<<
"\t "<<name<<
" [options] outFileName"<<endl;
35 cout<<
"\t -p : The number of samples to read each time from the IIO devices : (-p"<<N<<
")"<<endl;
36 cout<<
"\t -C : The name of the chip to look for in the available IIO devices : (-C "<<chip<<
")"<<endl;
37 cout<<
"\t -i : The number of channels to open, if the available number is less, then it is reduced to the available : (-i"<<chCnt<<
")"<<endl;
38 cout<<
"\t -t : The duration to sample for : (-t"<<T<<
")"<<endl;
39 cout<<
"\t -f : The sample rate to use : (-f"<<fixed<<setprecision(2)<<fs<<
")"<<endl;
40 cout<<resetiosflags(ios::showbase);
44 cout<<
"The known output file extensions (output file formats) are the following :"<<endl;
45 for (
int i=0; i<formats.size(); i++)
46 cout<<formats[i]<<
' ';
51 int main(
int argc,
char *argv[]) {
54 string chip(
"AD7476A");
63 if (op.
getArg<
string>(
"h", argc, argv, help, i=0)!=0)
64 return printUsage(argv[0], N, chip, chCnt, T, fs);
65 if (op.
getArg<
string>(
"help", argc, argv, help, i=0)!=0)
66 return printUsage(argv[0], N, chip, chCnt, T, fs);
68 return printUsage(argv[0], N, chip, chCnt, T, fs);
70 if (op.
getArg<
int>(
"p", argc, argv, N, i=0)!=0)
73 if (op.
getArg<
string>(
"C", argc, argv, chip, i=0)!=0)
76 if (op.
getArg<
int>(
"i", argc, argv, chCnt, i=0)!=0)
79 if (op.
getArg<
float>(
"t", argc, argv, T, i=0)!=0)
82 if (op.
getArg<
float>(
"f", argc, argv, fs, i=0)!=0)
95 cout<<
"Number of samples p="<<N<<
"\nNumber of channels available i="<<chCnt<<
"\nChip name C="<<chip<<endl;
96 cout<<
"Duration t="<<T<<
"\nSample rate f="<<fs<<endl;
99 cout<<
"Reading "<<M<<
" times "<<N<<
" samples, resulting in a processing time of "<<M*N/1e6<<
" or "<<M*N<<
" samples per channel."<<endl;
103 int ret=sox.
openWrite(argv[argc-1], fs, chCnt/iio[0].getChCnt(), MAXSHORT);
107 Eigen::Matrix<unsigned short, Eigen::Dynamic, Eigen::Dynamic> data;
108 data.resize(M*N*2, 2);
113 int expectedWriteCnt=N*chCnt;
132 struct timespec start, stop, lockStart, lockStop;
134 if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) {
135 cout<<
"clock start get time error"<<endl;
157 for (
int i=0; i<M; i++) {
158 if( clock_gettime( CLOCK_REALTIME, &lockStart) == -1 ) {
159 cout<<
"clock lockStart get time error"<<endl;
201 if( clock_gettime( CLOCK_REALTIME, &lockStop) == -1 ) {
202 cout<<
"clock lockStop get time error"<<endl;
205 durations[i] = 1.e3*( lockStop.tv_sec - lockStart.tv_sec ) + (
double)( lockStop.tv_nsec - lockStart.tv_nsec )/1.e6;
206 cout<<
"Lock duration "<<durations[i]<<
" ms\n";
216 if( clock_gettime( CLOCK_REALTIME, &stop) == -1 ) {
217 cout<<
"clock stop get time error"<<endl;
224 int written=sox.
write(data);
226 ofstream outputd(
"/tmp/durations.txt");
227 for (
int i=0; i<M; i++)
228 outputd<<durations[i]<<
'\n';
231 double duration = 1.e3*( stop.tv_sec - start.tv_sec ) + (
double)( stop.tv_nsec - start.tv_nsec )/1.e6;
232 cout<<
"Duration "<<duration<<
" ms\n";
234 double expected = (float)(N*M)/1.e6*1.e3;
235 cout<<
"Expected "<<expected<<
" ms\n";
237 cout<<
"Difference "<<duration-expected<<
" ms"<<endl;
virtual int openWrite(const string &fileName, double fs, int channels, double maxVal)
bool bufFull
Indicates when the read has completed and a buffer is full.
int getArg(string key, int argc, char *argv[], TYPE &ret, int i)
Eigen::Array< unsigned short int, Eigen::Dynamic, Eigen::Dynamic > * getFullBuffer()
int setSampleCountChannelCount(uint N, uint ch)
int printUsage(string name, int N, string chip, int chCnt, float T, float fs, int periodCount)
vector< string > availableFormats(void)
int findDevicesByChipName(std::string chipName)
#define NO_ERROR
There is no error.
int main(int argc, char *argv[])
virtual int run(int priority=0)
virtual int write(const vector< vector< FP_TYPE_ > > &audioData)