lasp/src/lasp/device/lasp_daqconfig.cpp

138 lines
3.5 KiB
C++

#include "lasp_daqconfig.h"
#include "lasp_deviceinfo.h"
#include <algorithm>
#include <cassert>
#define MAX_DEV_COUNT_PER_API 20
using std::vector;
vector<DaqApi> DaqApi::getAvailableApis() {
vector<DaqApi> apis;
apis.resize(6);
#if LASP_HAS_ULDAQ == 1
apis.at(uldaqapi.apicode) = uldaqapi;
#endif
#if LASP_HAS_RTAUDIO == 1
apis.at(rtaudioAlsaApi.apicode) = rtaudioAlsaApi;
apis.at(rtaudioPulseaudioApi.apicode) = rtaudioPulseaudioApi;
apis.at(rtaudioWasapiApi.apicode) = rtaudioWasapiApi;
apis.at(rtaudioDsApi.apicode) = rtaudioDsApi;
apis.at(rtaudioAsioApi.apicode) = rtaudioAsioApi;
#endif
return apis;
}
DaqConfiguration::DaqConfiguration(const DeviceInfo &device) {
api = device.api;
device_name = device.device_name;
inchannel_config.resize(device.ninchannels);
outchannel_config.resize(device.noutchannels);
us i = 0;
for(auto& inch: inchannel_config) {
inch.name = "Unnamed input channel " + std::to_string(i);
i++;
}
i = 0;
for(auto& outch: outchannel_config) {
outch.name = "Unnamed output channel " + std::to_string(i);
i++;
}
sampleRateIndex = device.prefSampleRateIndex;
dataTypeIndex = device.prefDataTypeIndex;
framesPerBlockIndex = device.prefFramesPerBlockIndex;
monitorOutput = false;
assert(match(device));
}
bool DaqConfiguration::match(const DeviceInfo &dev) const {
return (dev.device_name == device_name && dev.api == api);
}
int DaqConfiguration::getHighestInChannel() const {
for (int i = inchannel_config.size() - 1; i > -1; i--) {
if (inchannel_config.at(i).enabled)
return i;
}
return -1;
}
int DaqConfiguration::getHighestOutChannel() const {
for (us i = outchannel_config.size() - 1; i >= 0; i--) {
if (outchannel_config.at(i).enabled)
return i;
}
return -1;
}
int DaqConfiguration::getLowestInChannel() const {
for (us i = 0; i < inchannel_config.size(); i++) {
if (inchannel_config.at(i).enabled)
return i;
}
return -1;
}
int DaqConfiguration::getLowestOutChannel() const {
for (us i = 0; i < outchannel_config.size(); i++) {
if (outchannel_config.at(i).enabled)
return i;
}
return -1;
}
#include "toml++/toml.h"
#include <sstream>
toml::table daqChannelToTOML(const DaqChannel& ch) {
toml::table tbl;
tbl.emplace("enabled", ch.enabled);
tbl.emplace("name", ch.name);
tbl.emplace("sensitivity", ch.sensitivity);
tbl.emplace("IEPEEnabled", ch.IEPEEnabled);
tbl.emplace("ACCouplingMode", ch.ACCouplingMode);
tbl.emplace("rangeIndex", ch.rangeIndex);
tbl.emplace("qty", static_cast<int>(ch.qty));
tbl.emplace("digitalHighpassCutOn", ch.digitalHighPassCutOn);
return tbl;
}
string DaqConfiguration::toTOML() const {
toml::table apitbl{{"name", api.apiname},
{"code", api.apicode},
{"subcode", api.api_specific_subcode}};
toml::table tbl{{"daqapi", apitbl}};
tbl.emplace("device_name", device_name);
tbl.emplace("sampleRateIndex", sampleRateIndex);
tbl.emplace("dataTypeIndex", dataTypeIndex);
tbl.emplace("framesPerBlockIndex", framesPerBlockIndex);
tbl.emplace("monitorOutput", monitorOutput);
toml::array inchannel_config_tbl;
for(const auto& ch: inchannel_config) {
inchannel_config_tbl.emplace_back(daqChannelToTOML(ch));
}
tbl.emplace("inchannel_config", inchannel_config_tbl);
toml::array outchannel_config_tbl;
for(const auto& ch: outchannel_config) {
outchannel_config_tbl.emplace_back(daqChannelToTOML(ch));
}
tbl.emplace("outchannel_config", outchannel_config_tbl);
std::stringstream str;
str << tbl;
return str.str();
}