505 lines
16 KiB
C++
Raw Normal View History

2021-02-10 21:35:56 +01:00
#include <imgui.h>
#include <spdlog/spdlog.h>
#include <module.h>
#include <gui/gui.h>
#include <signal_path/signal_path.h>
#include <core.h>
#include <gui/style.h>
#include <config.h>
#include <options.h>
#include <sdrplay_api.h>
2021-02-10 21:35:56 +01:00
#define CONCAT(a, b) ((std::string(a) + b).c_str())
SDRPP_MOD_INFO {
/* Name: */ "sdrplay_source",
/* Description: */ "Airspy source module for SDR++",
/* Author: */ "Ryzerth",
/* Version: */ 0, 1, 0,
/* Max instances */ 1
};
ConfigManager config;
2021-02-26 16:52:54 +01:00
unsigned int sampleRates[] = {
2000000,
3000000,
4000000,
5000000,
6000000,
7000000,
8000000,
9000000,
10000000
};
const char* sampleRatesTxt =
"2MHz\0"
"3MHz\0"
"4MHz\0"
"5MHz\0"
"6MHz\0"
"7MHz\0"
"8MHz\0"
"9MHz\0"
"10MHz\0";
sdrplay_api_Bw_MHzT bandwidths[] = {
sdrplay_api_BW_0_200,
sdrplay_api_BW_0_300,
sdrplay_api_BW_0_600,
sdrplay_api_BW_1_536,
sdrplay_api_BW_5_000,
sdrplay_api_BW_6_000,
sdrplay_api_BW_7_000,
sdrplay_api_BW_8_000,
};
const char* bandwidthsTxt =
"200KHz\0"
"300KHz\0"
"600KHz\0"
"1.536MHz\0"
"5MHz\0"
"6MHz\0"
"7MHz\0"
"8MHz\0"
"Auto\0";
sdrplay_api_Bw_MHzT preferedBandwidth[] = {
sdrplay_api_BW_5_000,
sdrplay_api_BW_5_000,
sdrplay_api_BW_5_000,
sdrplay_api_BW_5_000,
sdrplay_api_BW_6_000,
sdrplay_api_BW_7_000,
sdrplay_api_BW_8_000,
sdrplay_api_BW_8_000,
sdrplay_api_BW_8_000
};
2021-02-10 21:35:56 +01:00
class SDRPlaySourceModule : public ModuleManager::Instance {
public:
SDRPlaySourceModule(std::string name) {
this->name = name;
2021-02-11 22:53:00 +01:00
// Init callbacks
2021-02-12 22:44:29 +01:00
cbFuncs.EventCbFn = eventCB;
cbFuncs.StreamACbFn = streamCB;
cbFuncs.StreamBCbFn = streamCB;
2021-02-11 22:53:00 +01:00
sdrplay_api_Open();
2021-02-26 16:52:54 +01:00
sampleRate = 2000000.0;
srId = 0;
bandwidth = sdrplay_api_BW_5_000;
bandwidthId = 8;
2021-02-10 21:35:56 +01: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;
refresh();
2021-02-11 22:53:00 +01:00
selectFirst();
2021-02-12 22:44:29 +01:00
// if (sampleRateList.size() > 0) {
// sampleRate = sampleRateList[0];
// }
2021-02-10 21:35:56 +01:00
// Select device from config
// config.aquire();
// std::string devSerial = config.conf["device"];
// config.release();
// selectByString(devSerial);
// core::setInputSampleRate(sampleRate);
sigpath::sourceManager.registerSource("SDRplay", &handler);
}
~SDRPlaySourceModule() {
sdrplay_api_Close();
2021-02-10 21:35:56 +01:00
}
void enable() {
enabled = true;
}
void disable() {
enabled = false;
}
bool isEnabled() {
return enabled;
}
void refresh() {
devList.clear();
devListTxt = "";
sdrplay_api_DeviceT devArr[128];
unsigned int numDev = 0;
sdrplay_api_GetDevices(devArr, &numDev, 128);
for (unsigned int i = 0; i < numDev; i++) {
2021-02-11 22:53:00 +01:00
devList.push_back(devArr[i]);
2021-02-12 22:44:29 +01:00
switch (devArr[i].hwVer) {
case SDRPLAY_RSP1_ID:
devListTxt += "RSP1 "; devListTxt += devArr[i].SerNo;
break;
case SDRPLAY_RSP1A_ID:
devListTxt += "RSP1A "; devListTxt += devArr[i].SerNo;
break;
case SDRPLAY_RSP2_ID:
devListTxt += "RSP2 "; devListTxt += devArr[i].SerNo;
break;
case SDRPLAY_RSPduo_ID:
devListTxt += "RSPduo "; devListTxt += devArr[i].SerNo;
break;
case SDRPLAY_RSPdx_ID:
devListTxt += "RSPdx "; devListTxt += devArr[i].SerNo;
break;
default:
devListTxt += "Unknown "; devListTxt += devArr[i].SerNo;
break;
}
devListTxt += '\0';
}
2021-02-10 21:35:56 +01:00
}
2021-02-11 22:53:00 +01:00
void selectFirst() {
if (devList.size() == 0) { return; }
selectDev(devList[0]);
}
2021-02-26 16:52:54 +01:00
void selectById(int id) {
selectDev(devList[id]);
}
2021-02-11 22:53:00 +01:00
void selectDev(sdrplay_api_DeviceT dev) {
openDev = dev;
sdrplay_api_ErrT err;
2021-02-26 16:52:54 +01:00
if (deviceOpen) {
sdrplay_api_Uninit(openDev.dev);
sdrplay_api_ReleaseDevice(&openDev);
}
2021-02-11 22:53:00 +01:00
err = sdrplay_api_SelectDevice(&openDev);
if (err != sdrplay_api_Success) {
const char* errStr = sdrplay_api_GetErrorString(err);
spdlog::error("Could not select RSP device: {0}", errStr);
2021-02-14 22:23:30 +01:00
deviceOpen = false;
2021-02-11 22:53:00 +01:00
return;
}
err = sdrplay_api_GetDeviceParams(openDev.dev, &openDevParams);
if (err != sdrplay_api_Success) {
const char* errStr = sdrplay_api_GetErrorString(err);
spdlog::error("Could not get device params for RSP device: {0}", errStr);
2021-02-14 22:23:30 +01:00
deviceOpen = false;
2021-02-11 22:53:00 +01:00
return;
}
2021-02-12 22:44:29 +01:00
err = sdrplay_api_Init(openDev.dev, &cbFuncs, this);
if (err != sdrplay_api_Success) {
const char* errStr = sdrplay_api_GetErrorString(err);
spdlog::error("Could not init RSP device: {0}", errStr);
2021-02-14 22:23:30 +01:00
deviceOpen = false;
2021-02-12 22:44:29 +01:00
return;
}
2021-02-11 22:53:00 +01:00
2021-02-12 22:44:29 +01:00
deviceOpen = true;
2021-02-11 22:53:00 +01:00
}
2021-02-10 21:35:56 +01:00
private:
std::string getBandwdithScaled(double bw) {
char buf[1024];
if (bw >= 1000000.0) {
sprintf(buf, "%.1lfMHz", bw / 1000000.0);
}
else if (bw >= 1000.0) {
sprintf(buf, "%.1lfKHz", bw / 1000.0);
}
else {
sprintf(buf, "%.1lfHz", bw);
}
return std::string(buf);
}
static void menuSelected(void* ctx) {
SDRPlaySourceModule* _this = (SDRPlaySourceModule*)ctx;
core::setInputSampleRate(_this->sampleRate);
spdlog::info("SDRPlaySourceModule '{0}': Menu Select!", _this->name);
}
static void menuDeselected(void* ctx) {
SDRPlaySourceModule* _this = (SDRPlaySourceModule*)ctx;
spdlog::info("SDRPlaySourceModule '{0}': Menu Deselect!", _this->name);
}
static void start(void* ctx) {
SDRPlaySourceModule* _this = (SDRPlaySourceModule*)ctx;
if (_this->running) {
return;
}
// Do start procedure here
2021-02-12 22:44:29 +01:00
sdrplay_api_ErrT err;
2021-02-20 15:27:43 +01:00
_this->bufferIndex = 0;
_this->bufferSize = 8000000 / 200;
2021-02-26 16:52:54 +01:00
_this->openDevParams->devParams->fsFreq.fsHz = _this->sampleRate;
_this->openDevParams->rxChannelA->tunerParams.bwType = _this->bandwidth;
2021-02-12 22:44:29 +01:00
_this->openDevParams->rxChannelA->tunerParams.rfFreq.rfHz = _this->freq;
2021-02-26 16:52:54 +01:00
_this->openDevParams->rxChannelA->tunerParams.gain.gRdB = _this->gain;
_this->openDevParams->rxChannelA->tunerParams.gain.LNAstate = _this->lnaGain;
2021-02-14 22:23:30 +01:00
_this->openDevParams->rxChannelA->ctrlParams.agc.enable = sdrplay_api_AGC_DISABLE;
2021-02-26 16:52:54 +01:00
// RSP1A Options
_this->openDevParams->devParams->rsp1aParams.rfNotchEnable = _this->fmNotch;
_this->openDevParams->devParams->rsp1aParams.rfNotchEnable = _this->dabNotch;
_this->openDevParams->rxChannelA->rsp1aTunerParams.biasTEnable = _this->biasT;
2021-02-12 22:44:29 +01:00
sdrplay_api_Update(_this->openDev.dev, _this->openDev.tuner, sdrplay_api_Update_Dev_Fs, sdrplay_api_Update_Ext1_None);
sdrplay_api_Update(_this->openDev.dev, _this->openDev.tuner, sdrplay_api_Update_Tuner_BwType, sdrplay_api_Update_Ext1_None);
sdrplay_api_Update(_this->openDev.dev, _this->openDev.tuner, sdrplay_api_Update_Tuner_Frf, sdrplay_api_Update_Ext1_None);
sdrplay_api_Update(_this->openDev.dev, _this->openDev.tuner, sdrplay_api_Update_Tuner_Gr, sdrplay_api_Update_Ext1_None);
2021-02-14 22:23:30 +01:00
sdrplay_api_Update(_this->openDev.dev, _this->openDev.tuner, sdrplay_api_Update_Ctrl_Agc, sdrplay_api_Update_Ext1_None);
2021-02-10 21:35:56 +01:00
2021-02-26 16:52:54 +01:00
// RSP1A Options
sdrplay_api_Update(_this->openDev.dev, _this->openDev.tuner, sdrplay_api_Update_Rsp1a_RfNotchControl, sdrplay_api_Update_Ext1_None);
sdrplay_api_Update(_this->openDev.dev, _this->openDev.tuner, sdrplay_api_Update_Rsp1a_RfDabNotchControl, sdrplay_api_Update_Ext1_None);
sdrplay_api_Update(_this->openDev.dev, _this->openDev.tuner, sdrplay_api_Update_Rsp1a_BiasTControl, sdrplay_api_Update_Ext1_None);
2021-02-26 16:52:54 +01:00
2021-02-10 21:35:56 +01:00
_this->running = true;
spdlog::info("SDRPlaySourceModule '{0}': Start!", _this->name);
}
static void stop(void* ctx) {
SDRPlaySourceModule* _this = (SDRPlaySourceModule*)ctx;
if (!_this->running) {
return;
}
_this->running = false;
_this->stream.stopWriter();
// Stop procedure here
_this->stream.clearWriteStop();
spdlog::info("SDRPlaySourceModule '{0}': Stop!", _this->name);
}
static void tune(double freq, void* ctx) {
SDRPlaySourceModule* _this = (SDRPlaySourceModule*)ctx;
if (_this->running) {
_this->openDevParams->rxChannelA->tunerParams.rfFreq.rfHz = freq;
2021-02-12 22:44:29 +01:00
sdrplay_api_Update(_this->openDev.dev, _this->openDev.tuner, sdrplay_api_Update_Tuner_Frf, sdrplay_api_Update_Ext1_None);
2021-02-10 21:35:56 +01:00
}
_this->freq = freq;
spdlog::info("SDRPlaySourceModule '{0}': Tune: {1}!", _this->name, freq);
}
static void menuHandler(void* ctx) {
SDRPlaySourceModule* _this = (SDRPlaySourceModule*)ctx;
float menuWidth = ImGui::GetContentRegionAvailWidth();
if (_this->running) { style::beginDisabled(); }
ImGui::SetNextItemWidth(menuWidth);
2021-02-12 22:44:29 +01:00
if (ImGui::Combo(CONCAT("##sdrplay_dev", _this->name), &_this->devId, _this->devListTxt.c_str())) {
2021-02-26 16:52:54 +01:00
_this->selectById(_this->devId);
// Save config
}
ImGui::SetNextItemWidth(menuWidth);
if (ImGui::Combo(CONCAT("##sdrplay_sr", _this->name), &_this->srId, sampleRatesTxt)) {
_this->sampleRate = sampleRates[_this->srId];
if (_this->bandwidthId == 8) {
_this->bandwidth = preferedBandwidth[_this->srId];
}
2021-02-26 16:52:54 +01:00
core::setInputSampleRate(_this->sampleRate);
// Save config
2021-02-14 22:23:30 +01:00
}
2021-02-15 14:55:48 +01:00
if (_this->running) { style::endDisabled(); }
2021-02-26 16:52:54 +01:00
ImGui::SetNextItemWidth(menuWidth);
if (ImGui::Combo(CONCAT("##sdrplay_bw", _this->name), &_this->bandwidthId, bandwidthsTxt)) {
_this->bandwidth = (_this->bandwidthId == 8) ? preferedBandwidth[_this->srId] : bandwidths[_this->bandwidthId];
if (_this->running) {
_this->openDevParams->rxChannelA->tunerParams.bwType = _this->bandwidth;
sdrplay_api_Update(_this->openDev.dev, _this->openDev.tuner, sdrplay_api_Update_Tuner_BwType, sdrplay_api_Update_Ext1_None);
}
// Save config
}
2021-02-14 22:23:30 +01:00
if (_this->deviceOpen) {
2021-03-06 19:23:03 +01:00
ImGui::PushItemWidth(menuWidth - ImGui::CalcTextSize("LNA Gain").x - 10);
ImGui::Text("LNA Gain");
ImGui::SameLine();
float pos = ImGui::GetCursorPosX();
if (ImGui::SliderInt(CONCAT("##sdrplay_lna_gain", _this->name), &_this->lnaGain, 9, 0, "")) {
_this->openDevParams->rxChannelA->tunerParams.gain.LNAstate = _this->lnaGain;
sdrplay_api_Update(_this->openDev.dev, _this->openDev.tuner, sdrplay_api_Update_Tuner_Gr, sdrplay_api_Update_Ext1_None);
}
ImGui::Text("IF Gain");
ImGui::SameLine();
ImGui::SetCursorPosX(pos);
if (ImGui::SliderInt(CONCAT("##sdrplay_gain", _this->name), &_this->gain, 59, 20, "")) {
_this->openDevParams->rxChannelA->tunerParams.gain.gRdB = _this->gain;
sdrplay_api_Update(_this->openDev.dev, _this->openDev.tuner, sdrplay_api_Update_Tuner_Gr, sdrplay_api_Update_Ext1_None);
}
ImGui::PopItemWidth();
2021-02-14 22:23:30 +01:00
switch (_this->openDev.hwVer) {
case SDRPLAY_RSP1_ID:
_this->RSP1Menu(menuWidth);
break;
case SDRPLAY_RSP1A_ID:
_this->RSP1AMenu(menuWidth);
break;
case SDRPLAY_RSP2_ID:
_this->RSP2Menu(menuWidth);
break;
case SDRPLAY_RSPduo_ID:
_this->RSPduoMenu(menuWidth);
break;
case SDRPLAY_RSPdx_ID:
_this->RSPdxMenu(menuWidth);
break;
default:
_this->RSPUnsupportedMenu(menuWidth);
break;
}
}
else {
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "No device available");
2021-02-15 14:55:48 +01:00
}
2021-02-10 21:35:56 +01:00
}
2021-02-14 22:23:30 +01:00
void RSP1Menu(float menuWidth) {
2021-03-06 19:23:03 +01:00
2021-02-14 22:23:30 +01:00
}
void RSP1AMenu(float menuWidth) {
2021-02-26 16:52:54 +01:00
if (ImGui::Checkbox("FM Notch", &fmNotch)) {
openDevParams->devParams->rsp1aParams.rfNotchEnable = fmNotch;
sdrplay_api_Update(openDev.dev, openDev.tuner, sdrplay_api_Update_Rsp1a_RfNotchControl, sdrplay_api_Update_Ext1_None);
}
if (ImGui::Checkbox("DAB Notch", &dabNotch)) {
openDevParams->devParams->rsp1aParams.rfNotchEnable = dabNotch;
sdrplay_api_Update(openDev.dev, openDev.tuner, sdrplay_api_Update_Rsp1a_RfDabNotchControl, sdrplay_api_Update_Ext1_None);
}
if (ImGui::Checkbox("Bias-T", &biasT)) {
openDevParams->rxChannelA->rsp1aTunerParams.biasTEnable = biasT;
sdrplay_api_Update(openDev.dev, openDev.tuner, sdrplay_api_Update_Rsp1a_BiasTControl, sdrplay_api_Update_Ext1_None);
}
2021-02-14 22:23:30 +01:00
}
void RSP2Menu(float menuWidth) {
2021-03-06 19:23:03 +01:00
2021-02-14 22:23:30 +01:00
}
void RSPduoMenu(float menuWidth) {
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "Device currently unsupported");
}
void RSPdxMenu(float menuWidth) {
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "Device currently unsupported");
}
void RSPUnsupportedMenu(float menuWidth) {
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "Device currently unsupported");
}
2021-02-10 21:35:56 +01:00
2021-02-12 22:44:29 +01:00
static void streamCB(short *xi, short *xq, sdrplay_api_StreamCbParamsT *params,
2021-02-11 22:53:00 +01:00
unsigned int numSamples, unsigned int reset, void *cbContext) {
2021-02-12 22:44:29 +01:00
SDRPlaySourceModule* _this = (SDRPlaySourceModule*)cbContext;
if (!_this->running) { return; }
for (int i = 0; i < numSamples; i++) {
2021-02-20 15:27:43 +01:00
int id = _this->bufferIndex++;
_this->stream.writeBuf[id].i = (float)xq[i] / 32768.0f;
_this->stream.writeBuf[id].q = (float)xi[i] / 32768.0f;
if (_this->bufferIndex >= _this->bufferSize) {
_this->stream.swap(_this->bufferSize);
_this->bufferIndex = 0;
}
2021-02-12 22:44:29 +01:00
}
2021-02-11 22:53:00 +01:00
}
2021-02-12 22:44:29 +01:00
static void eventCB(sdrplay_api_EventT eventId, sdrplay_api_TunerSelectT tuner,
2021-02-11 22:53:00 +01:00
sdrplay_api_EventParamsT *params, void *cbContext) {
2021-02-12 22:44:29 +01:00
SDRPlaySourceModule* _this = (SDRPlaySourceModule*)cbContext;
2021-02-11 22:53:00 +01:00
}
2021-02-10 21:35:56 +01:00
std::string name;
bool enabled = true;
dsp::stream<dsp::complex_t> stream;
double sampleRate;
SourceManager::SourceHandler handler;
bool running = false;
double freq;
2021-02-12 22:44:29 +01:00
bool deviceOpen = false;
2021-02-10 21:35:56 +01:00
2021-02-12 22:44:29 +01:00
sdrplay_api_CallbackFnsT cbFuncs;
2021-02-11 22:53:00 +01:00
sdrplay_api_DeviceT openDev;
2021-02-11 22:53:00 +01:00
sdrplay_api_DeviceParamsT * openDevParams;
2021-02-26 16:52:54 +01:00
sdrplay_api_Bw_MHzT bandwidth;
int bandwidthId = 0;
2021-02-10 21:35:56 +01:00
int devId = 0;
int srId = 0;
2021-02-26 16:52:54 +01:00
int lnaGain = 9;
int gain = 59;
2021-02-20 15:27:43 +01:00
int bufferSize = 0;
int bufferIndex = 0;
2021-02-15 14:55:48 +01:00
2021-02-26 16:52:54 +01:00
// RSP1A Options
bool fmNotch = false;
bool dabNotch = false;
bool biasT = false;
2021-02-26 16:52:54 +01:00
2021-02-11 22:53:00 +01:00
std::vector<sdrplay_api_DeviceT> devList;
2021-02-10 21:35:56 +01:00
std::string devListTxt;
};
MOD_EXPORT void _INIT_() {
json def = json({});
def["devices"] = json({});
def["device"] = "";
config.setPath(options::opts.root + "/sdrplay_config.json");
config.load(def);
config.enableAutoSave();
}
MOD_EXPORT ModuleManager::Instance* _CREATE_INSTANCE_(std::string name) {
return new SDRPlaySourceModule(name);
}
MOD_EXPORT void _DELETE_INSTANCE_(ModuleManager::Instance* instance) {
delete (SDRPlaySourceModule*)instance;
}
MOD_EXPORT void _END_() {
config.disableAutoSave();
config.save();
}