SDRPlusPlus/soapy_source/src/main.cpp

408 lines
12 KiB
C++
Raw Normal View History

2020-10-01 13:46:12 +02:00
#include <imgui.h>
#include <spdlog/spdlog.h>
2020-12-22 20:00:51 +01:00
#include <module.h>
2020-10-01 13:46:12 +02:00
#include <gui/gui.h>
#include <signal_path/signal_path.h>
2020-10-02 01:44:18 +02:00
#include <SoapySDR/Device.hpp>
#include <SoapySDR/Modules.hpp>
#include <SoapySDR/Logger.hpp>
#include <core.h>
#include <gui/style.h>
2020-12-22 14:50:26 +01:00
#include <options.h>
2020-10-01 13:46:12 +02:00
#define CONCAT(a, b) ((std::string(a) + b).c_str())
2020-12-08 04:36:37 +01:00
SDRPP_MOD_INFO {
/* Name: */ "soapy_source",
/* Description: */ "SoapySDR source module for SDR++",
/* Author: */ "Ryzerth",
/* Version: */ 0, 1, 5,
/* Max instances */ 1
2020-10-01 13:46:12 +02:00
};
ConfigManager config;
2020-12-08 04:36:37 +01:00
class SoapyModule : public ModuleManager::Instance {
2020-10-01 13:46:12 +02:00
public:
SoapyModule(std::string name) {
this->name = name;
//TODO: Make module tune on source select change (in sdrpp_core)
uiGains = new float[1];
refresh();
2020-10-02 01:44:18 +02:00
// Select default device
config.aquire();
std::string devName = config.conf["device"];
config.release();
selectDevice(devName);
2020-10-01 13:46:12 +02:00
handler.ctx = this;
handler.selectHandler = menuSelected;
handler.deselectHandler = menuDeselected;
handler.menuHandler = menuHandler;
handler.startHandler = start;
handler.stopHandler = stop;
handler.tuneHandler = tune;
handler.stream = &stream;
sigpath::sourceManager.registerSource("SoapySDR", &handler);
2020-10-01 13:46:12 +02:00
spdlog::info("SoapyModule '{0}': Instance created!", name);
}
2020-10-02 01:44:18 +02:00
2020-12-08 04:36:37 +01:00
~SoapyModule() {
spdlog::info("SoapyModule '{0}': Instance deleted!", name);
}
void enable() {
enabled = true;
}
void disable() {
enabled = false;
}
bool isEnabled() {
return enabled;
}
private:
void refresh() {
devList = SoapySDR::Device::enumerate();
txtDevList = "";
2020-10-05 18:20:27 +02:00
int i = 0;
for (auto& dev : devList) {
2020-10-05 18:20:27 +02:00
txtDevList += dev["label"] != "" ? dev["label"] : dev["driver"];
txtDevList += '\0';
2020-10-05 18:20:27 +02:00
i++;
}
2020-10-01 13:46:12 +02:00
}
2020-10-02 01:44:18 +02:00
void selectSampleRate(double samplerate) {
spdlog::info("Setting sample rate to {0}", samplerate);
2020-10-02 01:44:18 +02:00
if (sampleRates.size() == 0) {
devId = -1;
return;
}
bool found = false;
int i = 0;
for (auto& sr : sampleRates) {
if (sr == samplerate) {
srId = i;
sampleRate = sr;
found = true;
core::setInputSampleRate(sampleRate);
2020-10-02 01:44:18 +02:00
break;
}
i++;
}
if (!found) {
// Select default sample rate
selectSampleRate(sampleRates[0]);
}
}
void selectDevice(std::string name) {
if (devList.size() == 0) {
devId = -1;
return;
}
bool found = false;
int i = 0;
for (auto& args : devList) {
if (args["label"] == name) {
devArgs = args;
devId = i;
found = true;
break;
}
i++;
}
if (!found) {
// If device was not found, select default device instead
selectDevice(devList[0]["label"]);
return;
}
SoapySDR::Device* dev = SoapySDR::Device::make(devArgs);
gainList = dev->listGains(SOAPY_SDR_RX, channelId);
delete[] uiGains;
uiGains = new float[gainList.size()];
for (auto gain : gainList) {
gainRanges.push_back(dev->getGainRange(SOAPY_SDR_RX, channelId, gain));
}
sampleRates = dev->listSampleRates(SOAPY_SDR_RX, channelId);
2020-10-02 01:44:18 +02:00
txtSrList = "";
for (double sr : sampleRates) {
if (sr > 1.0e3 && sr <= 1.0e6) {
txtSrList += std::to_string((sr / 1.0e3)) + " kHz";
} else if (sr > 1.0e6) {
txtSrList += std::to_string((sr / 1.0e6)) + " MHz";
} else {
txtSrList += std::to_string((int) sr);
}
2020-10-02 01:44:18 +02:00
txtSrList += '\0';
}
hasAgc = dev->hasGainMode(SOAPY_SDR_RX, channelId);
2020-10-02 01:44:18 +02:00
SoapySDR::Device::unmake(dev);
config.aquire();
if (config.conf["devices"].contains(name)) {
int i = 0;
for (auto gain : gainList) {
if (config.conf["devices"][name]["gains"].contains(gain)) {
uiGains[i] = config.conf["devices"][name]["gains"][gain];
}
else {
uiGains[i] = gainRanges[i].minimum();
}
i++;
}
if (hasAgc && config.conf["devices"][name].contains("agc")) {
agc = config.conf["devices"][name]["agc"];
}
else {
agc = false;
}
if (config.conf["devices"][name].contains("sampleRate")) {
selectSampleRate(config.conf["devices"][name]["sampleRate"]);
}
else {
selectSampleRate(sampleRates[0]);
}
}
else {
int i = 0;
for (auto gain : gainList) {
uiGains[i] = gainRanges[i].minimum();
i++;
}
if (hasAgc) {
agc = false;
}
selectSampleRate(sampleRates[0]); // Select default
}
config.release();
2020-10-02 01:44:18 +02:00
}
void saveCurrent() {
json conf;
conf["sampleRate"] = sampleRate;
int i = 0;
for (auto gain : gainList) {
conf["gains"][gain] = uiGains[i];
i++;
}
if (hasAgc) {
conf["agc"] = agc;
}
config.aquire();
config.conf["devices"][devArgs["label"]] = conf;
config.release(true);
}
2020-10-01 13:46:12 +02:00
static void menuSelected(void* ctx) {
SoapyModule* _this = (SoapyModule*)ctx;
spdlog::info("SoapyModule '{0}': Menu Select!", _this->name);
2020-10-02 01:44:18 +02:00
if (_this->devList.size() == 0) {
return;
}
core::setInputSampleRate(_this->sampleRate);
2020-10-01 13:46:12 +02:00
}
static void menuDeselected(void* ctx) {
SoapyModule* _this = (SoapyModule*)ctx;
spdlog::info("SoapyModule '{0}': Menu Deselect!", _this->name);
}
static void start(void* ctx) {
SoapyModule* _this = (SoapyModule*)ctx;
2020-10-02 01:44:18 +02:00
_this->dev = SoapySDR::Device::make(_this->devArgs);
_this->dev->setSampleRate(SOAPY_SDR_RX, _this->channelId, _this->sampleRate);
int i = 0;
for (auto gain : _this->gainList) {
_this->dev->setGain(SOAPY_SDR_RX, _this->channelId, gain, _this->uiGains[i]);
i++;
}
if (_this->hasAgc) {
_this->dev->setGainMode(SOAPY_SDR_RX, _this->channelId, _this->agc);
}
_this->dev->setFrequency(SOAPY_SDR_RX, _this->channelId, _this->freq);
2020-10-02 01:44:18 +02:00
_this->devStream = _this->dev->setupStream(SOAPY_SDR_RX, "CF32");
_this->dev->activateStream(_this->devStream);
_this->running = true;
_this->workerThread = std::thread(_worker, _this);
2020-10-01 13:46:12 +02:00
spdlog::info("SoapyModule '{0}': Start!", _this->name);
}
static void stop(void* ctx) {
SoapyModule* _this = (SoapyModule*)ctx;
2020-10-02 01:44:18 +02:00
_this->running = false;
_this->dev->deactivateStream(_this->devStream);
_this->dev->closeStream(_this->devStream);
2020-11-02 03:57:44 +01:00
_this->stream.stopWriter();
_this->workerThread.join();
2020-11-02 03:57:44 +01:00
_this->stream.clearWriteStop();
SoapySDR::Device::unmake(_this->dev);
2020-10-01 13:46:12 +02:00
spdlog::info("SoapyModule '{0}': Stop!", _this->name);
}
static void tune(double freq, void* ctx) {
SoapyModule* _this = (SoapyModule*)ctx;
2020-10-02 01:44:18 +02:00
_this->freq = freq;
if (_this->running) {
_this->dev->setFrequency(SOAPY_SDR_RX, _this->channelId, freq);
2020-10-02 01:44:18 +02:00
}
2020-10-01 13:46:12 +02:00
spdlog::info("SoapyModule '{0}': Tune: {1}!", _this->name, freq);
}
static void menuHandler(void* ctx) {
SoapyModule* _this = (SoapyModule*)ctx;
2020-10-02 01:44:18 +02:00
// If no device is available, do not attempt to display menu
if (_this->devId < 0) {
return;
}
float menuWidth = ImGui::GetContentRegionAvailWidth();
if (_this->running) { style::beginDisabled(); }
2020-10-02 01:44:18 +02:00
ImGui::SetNextItemWidth(menuWidth);
if (ImGui::Combo(CONCAT("##_dev_select_", _this->name), &_this->devId, _this->txtDevList.c_str())) {
_this->selectDevice(_this->devList[_this->devId]["label"]);
config.aquire();
config.conf["device"] = _this->devList[_this->devId]["label"];
config.release(true);
2020-10-02 01:44:18 +02:00
}
2020-10-02 01:44:18 +02:00
if (ImGui::Combo(CONCAT("##_sr_select_", _this->name), &_this->srId, _this->txtSrList.c_str())) {
_this->selectSampleRate(_this->sampleRates[_this->srId]);
_this->saveCurrent();
}
2020-12-14 15:46:05 +01:00
2020-12-14 17:18:43 +01:00
ImGui::SameLine();
float refreshBtnWdith = menuWidth - ImGui::GetCursorPosX();
if (ImGui::Button(CONCAT("Refresh##_dev_select_", _this->name), ImVec2(refreshBtnWdith, 0))) {
_this->refresh();
_this->selectDevice(config.conf["device"]);
}
if (_this->running) { style::endDisabled(); }
float gainNameLen = 0;
float len;
for (auto gain : _this->gainList) {
len = ImGui::CalcTextSize((gain + " gain").c_str()).x;
if (len > gainNameLen) {
gainNameLen = len;
}
}
gainNameLen += 5.0f;
if (_this->hasAgc) {
if (ImGui::Checkbox((std::string("AGC##_agc_sel_") + _this->name).c_str(), &_this->agc)) {
if (_this->running) { _this->dev->setGainMode(SOAPY_SDR_RX, _this->channelId, _this->agc); }
// When disabled, reset the gains
if (!_this->agc) {
int i = 0;
for (auto gain : _this->gainList) {
_this->dev->setGain(SOAPY_SDR_RX, _this->channelId, gain, _this->uiGains[i]);
i++;
}
}
_this->saveCurrent();
}
}
int i = 0;
for (auto gain : _this->gainList) {
2020-10-22 02:47:10 +02:00
ImGui::Text("%s gain", gain.c_str());
ImGui::SameLine();
ImGui::SetCursorPosX(gainNameLen);
ImGui::SetNextItemWidth(menuWidth - gainNameLen);
if (ImGui::SliderFloat((gain + std::string("##_gain_sel_") + _this->name).c_str(), &_this->uiGains[i],
_this->gainRanges[i].minimum(), _this->gainRanges[i].maximum())) {
if (_this->running) {
_this->dev->setGain(SOAPY_SDR_RX, _this->channelId, gain, _this->uiGains[i]);
}
_this->saveCurrent();
}
i++;
2020-10-02 01:44:18 +02:00
}
}
static void _worker(SoapyModule* _this) {
int blockSize = _this->sampleRate / 200.0f;
int flags = 0;
long long timeMs = 0;
while (_this->running) {
int res = _this->dev->readStream(_this->devStream, (void**)&_this->stream.writeBuf, blockSize, flags, timeMs);
2020-10-02 01:44:18 +02:00
if (res < 1) {
continue;
2020-11-03 19:22:53 +01:00
}
if (!_this->stream.swap(res)) { return; }
2020-10-02 01:44:18 +02:00
}
2020-10-01 13:46:12 +02:00
}
std::string name;
2020-12-08 04:36:37 +01:00
bool enabled = true;
2020-10-01 13:46:12 +02:00
dsp::stream<dsp::complex_t> stream;
2020-10-02 01:44:18 +02:00
SoapySDR::Stream* devStream;
2020-10-01 13:46:12 +02:00
SourceManager::SourceHandler handler;
2020-10-02 01:44:18 +02:00
SoapySDR::KwargsList devList;
SoapySDR::Kwargs devArgs;
SoapySDR::Device* dev;
std::string txtDevList;
std::string txtSrList;
std::thread workerThread;
int devId = -1;
double freq;
double sampleRate;
bool running = false;
bool hasAgc = false;
bool agc = false;
2020-10-02 01:44:18 +02:00
std::vector<double> sampleRates;
int srId = -1;
float* uiGains;
int channelCount = 1;
int channelId = 0;
std::vector<std::string> gainList;
std::vector<SoapySDR::Range> gainRanges;
2020-10-01 13:46:12 +02:00
};
MOD_EXPORT void _INIT_() {
2020-12-22 14:50:26 +01:00
config.setPath(options::opts.root + "/soapy_source_config.json");
json defConf;
defConf["device"] = "";
defConf["devices"] = json({});
config.load(defConf);
config.enableAutoSave();
2020-10-01 13:46:12 +02:00
}
2020-12-08 04:36:37 +01:00
MOD_EXPORT ModuleManager::Instance* _CREATE_INSTANCE_(std::string name) {
2020-10-01 13:46:12 +02:00
return new SoapyModule(name);
}
2020-12-08 04:36:37 +01:00
MOD_EXPORT void _DELETE_INSTANCE_(ModuleManager::Instance* instance) {
2020-10-01 13:46:12 +02:00
delete (SoapyModule*)instance;
}
2020-12-08 04:36:37 +01:00
MOD_EXPORT void _END_() {
config.disableAutoSave();
config.save();
2020-10-01 13:46:12 +02:00
}