#include <sched.h>
#include <values.h>
#include <iomanip>
int printUsage(
string name,
int N,
string chip,
int chCnt,
float T,
float fs,
int periodCount) {
cout<<name<<" : An application to stream input from IIO devices to file."<<endl;
cout<<"Usage:"<<endl;
cout<<"\t "<<name<<" [options] outFileName"<<endl;
cout<<"\t -p : The number of samples to read each time from the IIO devices : (-p "<<N<<")"<<endl;
cout<<"\t -C : The name of the chip to look for in the available IIO devices : (-C "<<chip<<")"<<endl;
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;
cout<<"\t -t : The duration to sample for : (-t "<<T<<")"<<endl;
cout<<"\t -f : The sample rate to use : (-f "<<fixed<<setprecision(2)<<fs<<")"<<endl;
cout<<"\t -n : The number of periods : (-n "<<periodCount<<")"<<endl;
cout<<resetiosflags(ios::showbase);
cout<<"The known output file extensions (output file formats) are the following :"<<endl;
for (int i=0; i<formats.size(); i++)
cout<<formats[i]<<' ';
cout<<endl;
return 0;
}
int main(
int argc,
char *argv[]) {
struct sched_param param;
param.sched_priority = 96;
if (sched_setscheduler(0, SCHED_FIFO, & param) == -1) {
perror("sched_setscheduler");
return -1;
}
string chip("AD7476A");
int chCnt=MAXINT;
float T=40.;
float fs=1.e6;
int periodCount=3;
int i=0;
string help;
if (op.
getArg<
string>(
"h", argc, argv, help, i=0)!=0)
return printUsage(argv[0], N, chip, chCnt, T, fs, periodCount);
if (op.
getArg<
string>(
"help", argc, argv, help, i=0)!=0)
return printUsage(argv[0], N, chip, chCnt, T, fs, periodCount);
if (argc<2)
return printUsage(argv[0], N, chip, chCnt, T, fs, periodCount);
if (op.
getArg<
int>(
"p", argc, argv, N, i=0)!=0)
;
if (op.
getArg<
string>(
"C", argc, argv, chip, i=0)!=0)
;
if (op.
getArg<
int>(
"i", argc, argv, chCnt, i=0)!=0)
;
if (op.
getArg<
float>(
"t", argc, argv, T, i=0)!=0)
;
if (op.
getArg<
float>(
"f", argc, argv, fs, i=0)!=0)
;
if (op.
getArg<
int>(
"n", argc, argv, periodCount, i=0)!=0)
;
iio.BlockBuffer::resize(periodCount);
cout<<"Number of samples p="<<N<<"\nNumber of channels available i="<<chCnt<<"\nChip name C="<<chip<<endl;
cout<<"Reading n="<<periodCount<<" period buffers of p samples each"<<endl;
cout<<"Duration t="<<T<<"\nSample rate f="<<fs<<endl;
cout<<endl;
cout<<"Reading "<<M<<" times "<<N<<" samples, resulting in a processing time of "<<M*N/1e6<<" or "<<M*N<<" samples per channel."<<endl;
int colCnt=(int)ceil((float)chCnt/(float)iio[0].getChCnt());
int ret=sox.
openWrite(argv[argc-1], fs, colCnt, MAXSHORT);
return ret;
Eigen::Matrix<unsigned short, Eigen::Dynamic, Eigen::Dynamic> data;
data.resize(M*N*2, colCnt);
return ret;
int expectedWriteCnt=N*chCnt;
return ret;
return ret;
}
return ret;
struct timespec start, stop, lockStart, lockStop;
if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) {
cout<<"clock start get time error"<<endl;
exit(-1);
}
double durations[M];
for (int i=0; i<M; i++) {
if( clock_gettime( CLOCK_REALTIME, &lockStart) == -1 ) {
cout<<"clock lockStart get time error"<<endl;
exit(-1);
}
Eigen::Array<short unsigned, Eigen::Dynamic, Eigen::Dynamic> *b=iio.
getFullBuffer();
if (!b){
cout<<"main : Error : couldn't get a valid full buffer\n";
usleep(1000);
} else {
data.block(i*N*2, 0, N*2, b->cols())=*b;
}
if( clock_gettime( CLOCK_REALTIME, &lockStop) == -1 ) {
cout<<"clock lockStop get time error"<<endl;
exit(-1);
}
durations[i] = 1.e3*( lockStop.tv_sec - lockStart.tv_sec ) + (double)( lockStop.tv_nsec - lockStart.tv_nsec )/1.e6;
cout<<"Lock duration "<<durations[i]<<" ms\n";
}
if( clock_gettime( CLOCK_REALTIME, &stop) == -1 ) {
cout<<"clock stop get time error"<<endl;
exit(-1);
}
int written=sox.
write(data);
ofstream outputd("/tmp/durations.txt");
for (int i=0; i<M; i++)
outputd<<durations[i]<<'\n';
outputd.close();
double duration = 1.e3*( stop.tv_sec - start.tv_sec ) + (double)( stop.tv_nsec - start.tv_nsec )/1.e6;
cout<<"Duration "<<duration<<" ms\n";
double expected = (float)(N*M)/1.e6*1.e3;
cout<<"Expected "<<expected<<" ms\n";
cout<<"Difference "<<duration-expected<<" ms"<<endl;
return 0;
}