lasp/test/test_uldaq.cpp

94 lines
2.4 KiB
C++
Raw Normal View History

#include "lasp_cppdaq.h"
#include <chrono>
#include <iostream>
#include <cmath>
#include <thread>
using std::cout;
using std::cerr;
using std::endl;
int main() {
/* boolvec inChannels = {true, false, false, false}; */
boolvec inChannels = {true, true, false, false};
boolvec outChannels = {true};
double samplerate = 10000;
const us samplesPerBlock = 256;
DaqConfiguration config;
config.eninchannels = inChannels;
config.enoutchannels = outChannels;
config.inputIEPEEnabled = {false, false, false, false};
config.inputACCouplingMode = {false, false, false, false};
config.inputHighRange = {false, false, false, false};
config.sampleRateIndex = 0;
config.datatype = dtype_fl64;
config.monitorOutput = true;
config.inputIEPEEnabled = {false, false, false, false};
config.nFramesPerBlock = samplesPerBlock;
cout << "Inchannnels size: " <<config.eninchannels.size() << endl;
vector<DeviceInfo> devinfos = DaqDevices::getDeviceInfo();
DeviceInfo devinfo;
us i;
bool found = false;
for(i=0;i<devinfos.size();i++) {
if(devinfos[i].api == uldaqapi){
cout << string(devinfos[i]) << endl;
devinfo = devinfos[i];
found = true;
}
}
if(!found) {
throw runtime_error("Could not find UlDaq device");
}
Daq* daq = DaqDevices::createDevice(devinfo, config);
SafeQueue<void*> inqueue;
SafeQueue<void*> outqueue;
double totalTime = 5;
double t = 0;
double freq = 1000;
us nblocks = ((us) totalTime*samplerate/samplesPerBlock) + 10;
for(us i=0;i<nblocks;i++) {
double* data = static_cast<double*>(malloc(sizeof(double)*samplesPerBlock));
for(us sample=0;sample<samplesPerBlock;sample++) {
data[sample] = sin(2*M_PI*freq*t);
t+= 1.0/samplerate;
}
outqueue.enqueue(data);
}
daq->start(&inqueue, &outqueue);
std::this_thread::sleep_for(std::chrono::seconds((int) totalTime));
daq->stop();
while(!inqueue.empty()) {
double* buf = (double*) inqueue.dequeue();
for(us i=0;i<samplesPerBlock;i++) {
for(us ch=0;ch<daq->neninchannels();ch++) {
cout << buf[ch*samplesPerBlock+i] << " ";
}
cout << endl;
}
free(buf);
}
while(!outqueue.empty()){
void* dat = outqueue.dequeue();
free(dat);
}
return 0;
}