mirror of
https://github.com/AlexandreRouma/SDRPlusPlus.git
synced 2024-12-26 02:48:31 +01:00
Merge pull request #1301 from AlexandreRouma/new_plutosdr_enum
New plutosdr enum
This commit is contained in:
commit
f541328e5c
@ -8,6 +8,8 @@
|
||||
#include <iio.h>
|
||||
#include <ad9361.h>
|
||||
#include <utils/optionlist.h>
|
||||
#include <algorithm>
|
||||
#include <regex>
|
||||
|
||||
#define CONCAT(a, b) ((std::string(a) + b).c_str())
|
||||
|
||||
@ -26,61 +28,32 @@ public:
|
||||
PlutoSDRSourceModule(std::string name) {
|
||||
this->name = name;
|
||||
|
||||
// Load configuration
|
||||
config.acquire();
|
||||
if (config.conf.contains("IP")) {
|
||||
std::string _ip = config.conf["IP"];
|
||||
strcpy(&ip[3], _ip.c_str());
|
||||
}
|
||||
if (config.conf.contains("sampleRate")) {
|
||||
sampleRate = config.conf["sampleRate"];
|
||||
}
|
||||
if (config.conf.contains("bandwidth")) {
|
||||
bandwidth = config.conf["bandwidth"];
|
||||
}
|
||||
if (config.conf.contains("gainMode")) {
|
||||
gainMode = config.conf["gainMode"];
|
||||
}
|
||||
if (config.conf.contains("gain")) {
|
||||
gain = config.conf["gain"];
|
||||
}
|
||||
config.release();
|
||||
|
||||
// Define valid samplerates
|
||||
for (int sr = 1000000; sr <= 61440000; sr += 500000) {
|
||||
samplerates.define(sr, getBandwdithScaled(sr), sr);
|
||||
}
|
||||
samplerates.define(61440000, getBandwdithScaled(61440000.0), 61440000.0);
|
||||
|
||||
// Set samplerate ID
|
||||
if (samplerates.keyExists(sampleRate)) {
|
||||
srId = samplerates.keyId(sampleRate);
|
||||
}
|
||||
else {
|
||||
srId = 0;
|
||||
sampleRate = samplerates.value(srId);
|
||||
}
|
||||
|
||||
// Define valid bandwidths
|
||||
bandwidths.define(0, "Auto", 0);
|
||||
for (int bw = 1000000.0; bw <= 52000000; bw += 500000) {
|
||||
bandwidths.define(bw, getBandwdithScaled(bw), bw);
|
||||
}
|
||||
|
||||
// Set bandwidth ID
|
||||
if (bandwidths.keyExists(bandwidth)) {
|
||||
bwId = bandwidths.keyId(bandwidth);
|
||||
}
|
||||
else {
|
||||
bwId = 0;
|
||||
bandwidth = bandwidths.value(bwId);
|
||||
}
|
||||
|
||||
// Define gain modes
|
||||
gainModes.define(0, "Manual", "manual");
|
||||
gainModes.define(1, "Fast Attack", "fast_attack");
|
||||
gainModes.define(2, "Slow Attack", "slow_attack");
|
||||
gainModes.define(3, "Hybrid", "hybrid");
|
||||
gainModes.define("manual", "Manual", "manual");
|
||||
gainModes.define("fast_attack", "Fast Attack", "fast_attack");
|
||||
gainModes.define("slow_attack", "Slow Attack", "slow_attack");
|
||||
gainModes.define("hybrid", "Hybrid", "hybrid");
|
||||
|
||||
// Enumerate devices
|
||||
refresh();
|
||||
|
||||
// Select device
|
||||
config.acquire();
|
||||
devDesc = config.conf["device"];
|
||||
config.release();
|
||||
select(devDesc);
|
||||
|
||||
// Register source
|
||||
handler.ctx = this;
|
||||
@ -128,9 +101,157 @@ private:
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
void refresh() {
|
||||
// Clear device list
|
||||
devices.clear();
|
||||
|
||||
// Create scan context
|
||||
iio_scan_context* sctx = iio_create_scan_context(NULL, 0);
|
||||
if (!sctx) {
|
||||
flog::error("Failed get scan context");
|
||||
return;
|
||||
}
|
||||
|
||||
// Create parsing regexes
|
||||
std::regex backendRgx(".+(?=:)", std::regex::ECMAScript);
|
||||
std::regex modelRgx("\\(.+(?=\\),)", std::regex::ECMAScript);
|
||||
std::regex serialRgx("serial=[0-9A-Za-z]+", std::regex::ECMAScript);
|
||||
|
||||
// Enumerate devices
|
||||
iio_context_info** ctxInfoList;
|
||||
ssize_t count = iio_scan_context_get_info_list(sctx, &ctxInfoList);
|
||||
if (count < 0) {
|
||||
flog::error("Failed to enumerate contexts");
|
||||
return;
|
||||
}
|
||||
for (ssize_t i = 0; i < count; i++) {
|
||||
iio_context_info* info = ctxInfoList[i];
|
||||
std::string desc = iio_context_info_get_description(info);
|
||||
std::string duri = iio_context_info_get_uri(info);
|
||||
|
||||
// If the device is not a plutosdr, don't include it
|
||||
if (desc.find("PlutoSDR") == std::string::npos) {
|
||||
flog::warn("Ignored IIO device: [{}] {}", duri, desc);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Extract the backend
|
||||
std::string backend = "unknown";
|
||||
std::smatch backendMatch;
|
||||
if (std::regex_search(duri, backendMatch, backendRgx)) {
|
||||
backend = backendMatch[0];
|
||||
}
|
||||
|
||||
// Extract the model
|
||||
std::string model = "Unknown";
|
||||
std::smatch modelMatch;
|
||||
if (std::regex_search(desc, modelMatch, modelRgx)) {
|
||||
model = modelMatch[0];
|
||||
int parenthPos = model.find('(');
|
||||
if (parenthPos != std::string::npos) {
|
||||
model = model.substr(parenthPos+1);
|
||||
}
|
||||
}
|
||||
|
||||
// Extract the serial
|
||||
std::string serial = "unknown";
|
||||
std::smatch serialMatch;
|
||||
if (std::regex_search(desc, serialMatch, serialRgx)) {
|
||||
serial = serialMatch[0].str().substr(7);
|
||||
}
|
||||
|
||||
// Construct the device name
|
||||
std::string devName = '(' + backend + ") " + model + " [" + serial + ']';
|
||||
|
||||
// Save device
|
||||
devices.define(desc, devName, duri);
|
||||
}
|
||||
iio_context_info_list_free(ctxInfoList);
|
||||
|
||||
// Destroy scan context
|
||||
iio_scan_context_destroy(sctx);
|
||||
|
||||
#ifdef __ANDROID__
|
||||
// On Android, a default IP entry must be made (TODO: This is not ideal since the IP cannot be changed)
|
||||
const char* androidURI = "ip:192.168.2.1";
|
||||
const char* androidName = "Default (192.168.2.1)";
|
||||
devices.define(androidName, androidName, androidURI);
|
||||
#endif
|
||||
}
|
||||
|
||||
void select(const std::string& desc) {
|
||||
// If no device is available, give up
|
||||
if (devices.empty()) {
|
||||
devDesc.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
// If the device is not available, select the first one
|
||||
if (!devices.keyExists(desc)) {
|
||||
select(devices.key(0));
|
||||
}
|
||||
|
||||
// Update URI
|
||||
devDesc = desc;
|
||||
uri = devices.value(devices.keyId(desc));
|
||||
|
||||
// TODO: Enumerate capabilities
|
||||
|
||||
// Load defaults
|
||||
samplerate = 4000000;
|
||||
bandwidth = 0;
|
||||
gmId = 0;
|
||||
gain = -1.0f;
|
||||
|
||||
// Load device config
|
||||
config.acquire();
|
||||
if (config.conf["devices"][devDesc].contains("samplerate")) {
|
||||
samplerate = config.conf["devices"][devDesc]["samplerate"];
|
||||
}
|
||||
if (config.conf["devices"][devDesc].contains("bandwidth")) {
|
||||
bandwidth = config.conf["devices"][devDesc]["bandwidth"];
|
||||
}
|
||||
if (config.conf["devices"][devDesc].contains("gainMode")) {
|
||||
// Select given gain mode or default if invalid
|
||||
std::string gm = config.conf["devices"][devDesc]["gainMode"];
|
||||
if (gainModes.keyExists(gm)) {
|
||||
gmId = gainModes.keyId(gm);
|
||||
}
|
||||
else {
|
||||
gmId = 0;
|
||||
}
|
||||
}
|
||||
if (config.conf["devices"][devDesc].contains("gain")) {
|
||||
gain = config.conf["devices"][devDesc]["gain"];
|
||||
gain = std::clamp<int>(gain, -1.0f, 73.0f);
|
||||
}
|
||||
config.release();
|
||||
|
||||
// Update samplerate ID
|
||||
if (samplerates.keyExists(samplerate)) {
|
||||
srId = samplerates.keyId(samplerate);
|
||||
}
|
||||
else {
|
||||
srId = 0;
|
||||
samplerate = samplerates.value(srId);
|
||||
}
|
||||
|
||||
// Update bandwidth ID
|
||||
if (bandwidths.keyExists(bandwidth)) {
|
||||
bwId = bandwidths.keyId(bandwidth);
|
||||
}
|
||||
else {
|
||||
bwId = 0;
|
||||
bandwidth = bandwidths.value(bwId);
|
||||
}
|
||||
|
||||
// Update core samplerate
|
||||
core::setInputSampleRate(samplerate);
|
||||
}
|
||||
|
||||
static void menuSelected(void* ctx) {
|
||||
PlutoSDRSourceModule* _this = (PlutoSDRSourceModule*)ctx;
|
||||
core::setInputSampleRate(_this->sampleRate);
|
||||
core::setInputSampleRate(_this->samplerate);
|
||||
flog::info("PlutoSDRSourceModule '{0}': Menu Select!", _this->name);
|
||||
}
|
||||
|
||||
@ -143,10 +264,13 @@ private:
|
||||
PlutoSDRSourceModule* _this = (PlutoSDRSourceModule*)ctx;
|
||||
if (_this->running) { return; }
|
||||
|
||||
// If no device is selected, give up
|
||||
if (_this->devDesc.empty() || _this->uri.empty()) { return; }
|
||||
|
||||
// Open context
|
||||
_this->ctx = iio_create_context_from_uri(_this->ip);
|
||||
_this->ctx = iio_create_context_from_uri(_this->uri.c_str());
|
||||
if (_this->ctx == NULL) {
|
||||
flog::error("Could not open pluto");
|
||||
flog::error("Could not open pluto ({})", _this->uri);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -174,15 +298,15 @@ private:
|
||||
|
||||
// Configure RX channel
|
||||
iio_channel_attr_write(_this->rxChan, "rf_port_select", "A_BALANCED");
|
||||
iio_channel_attr_write_longlong(_this->rxLO, "frequency", round(_this->freq)); // Freq
|
||||
iio_channel_attr_write_bool(_this->rxChan, "filter_fir_en", true); // Digital filter
|
||||
iio_channel_attr_write_longlong(_this->rxChan, "sampling_frequency", round(_this->sampleRate)); // Sample rate
|
||||
iio_channel_attr_write_longlong(_this->rxChan, "hardwaregain", round(_this->gain)); // Gain
|
||||
iio_channel_attr_write(_this->rxChan, "gain_control_mode", _this->gainModes.value(_this->gainMode).c_str()); // Gain mode
|
||||
iio_channel_attr_write_longlong(_this->rxLO, "frequency", round(_this->freq)); // Freq
|
||||
iio_channel_attr_write_bool(_this->rxChan, "filter_fir_en", true); // Digital filter
|
||||
iio_channel_attr_write_longlong(_this->rxChan, "sampling_frequency", round(_this->samplerate)); // Sample rate
|
||||
iio_channel_attr_write_double(_this->rxChan, "hardwaregain", _this->gain); // Gain
|
||||
iio_channel_attr_write(_this->rxChan, "gain_control_mode", _this->gainModes.value(_this->gmId).c_str()); // Gain mode
|
||||
_this->setBandwidth(_this->bandwidth);
|
||||
|
||||
// Configure the ADC filters
|
||||
ad9361_set_bb_rate(_this->phy, round(_this->sampleRate));
|
||||
ad9361_set_bb_rate(_this->phy, round(_this->samplerate));
|
||||
|
||||
// Start worker thread
|
||||
_this->running = true;
|
||||
@ -214,7 +338,7 @@ private:
|
||||
_this->freq = freq;
|
||||
if (_this->running) {
|
||||
// Tune device
|
||||
iio_channel_attr_write_longlong(iio_device_find_channel(_this->phy, "altvoltage0", true), "frequency", round(freq));
|
||||
iio_channel_attr_write_longlong(_this->rxLO, "frequency", round(freq));
|
||||
}
|
||||
flog::info("PlutoSDRSourceModule '{0}': Tune: {1}!", _this->name, freq);
|
||||
}
|
||||
@ -223,22 +347,33 @@ private:
|
||||
PlutoSDRSourceModule* _this = (PlutoSDRSourceModule*)ctx;
|
||||
|
||||
if (_this->running) { SmGui::BeginDisabled(); }
|
||||
SmGui::LeftLabel("IP");
|
||||
SmGui::FillWidth();
|
||||
if (SmGui::InputText(CONCAT("##_pluto_ip_", _this->name), &_this->ip[3], 16)) {
|
||||
SmGui::ForceSync();
|
||||
if (SmGui::Combo("##plutosdr_dev_sel", &_this->devId, _this->devices.txt)) {
|
||||
_this->select(_this->devices.key(_this->devId));
|
||||
config.acquire();
|
||||
config.conf["IP"] = &_this->ip[3];
|
||||
config.conf["device"] = _this->devices.key(_this->devId);
|
||||
config.release(true);
|
||||
}
|
||||
|
||||
SmGui::LeftLabel("Samplerate");
|
||||
SmGui::FillWidth();
|
||||
if (SmGui::Combo(CONCAT("##_pluto_sr_", _this->name), &_this->srId, _this->samplerates.txt)) {
|
||||
_this->sampleRate = _this->samplerates.value(_this->srId);
|
||||
core::setInputSampleRate(_this->sampleRate);
|
||||
config.acquire();
|
||||
config.conf["sampleRate"] = _this->sampleRate;
|
||||
config.release(true);
|
||||
_this->samplerate = _this->samplerates.value(_this->srId);
|
||||
core::setInputSampleRate(_this->samplerate);
|
||||
if (!_this->devDesc.empty()) {
|
||||
config.acquire();
|
||||
config.conf["devices"][_this->devDesc]["samplerate"] = _this->samplerate;
|
||||
config.release(true);
|
||||
}
|
||||
}
|
||||
|
||||
// Refresh button
|
||||
SmGui::SameLine();
|
||||
SmGui::FillWidth();
|
||||
SmGui::ForceSync();
|
||||
if (SmGui::Button(CONCAT("Refresh##_pluto_refr_", _this->name))) {
|
||||
_this->refresh();
|
||||
_this->select(_this->devDesc);
|
||||
|
||||
}
|
||||
if (_this->running) { SmGui::EndDisabled(); }
|
||||
|
||||
@ -249,35 +384,41 @@ private:
|
||||
if (_this->running) {
|
||||
_this->setBandwidth(_this->bandwidth);
|
||||
}
|
||||
config.acquire();
|
||||
config.conf["bandwidth"] = _this->bandwidth;
|
||||
config.release(true);
|
||||
if (!_this->devDesc.empty()) {
|
||||
config.acquire();
|
||||
config.conf["devices"][_this->devDesc]["bandwidth"] = _this->bandwidth;
|
||||
config.release(true);
|
||||
}
|
||||
}
|
||||
|
||||
SmGui::LeftLabel("Gain Mode");
|
||||
SmGui::FillWidth();
|
||||
SmGui::ForceSync();
|
||||
if (SmGui::Combo(CONCAT("##_gainmode_select_", _this->name), &_this->gainMode, _this->gainModes.txt)) {
|
||||
if (SmGui::Combo(CONCAT("##_pluto_gainmode_select_", _this->name), &_this->gmId, _this->gainModes.txt)) {
|
||||
if (_this->running) {
|
||||
iio_channel_attr_write(_this->rxChan, "gain_control_mode", _this->gainModes.value(_this->gainMode).c_str());
|
||||
iio_channel_attr_write(_this->rxChan, "gain_control_mode", _this->gainModes.value(_this->gmId).c_str());
|
||||
}
|
||||
if (!_this->devDesc.empty()) {
|
||||
config.acquire();
|
||||
config.conf["devices"][_this->devDesc]["gainMode"] = _this->gainModes.key(_this->gmId);
|
||||
config.release(true);
|
||||
}
|
||||
config.acquire();
|
||||
config.conf["gainMode"] = _this->gainMode;
|
||||
config.release(true);
|
||||
}
|
||||
|
||||
SmGui::LeftLabel("PGA Gain");
|
||||
if (_this->gainMode) { SmGui::BeginDisabled(); }
|
||||
SmGui::LeftLabel("Gain");
|
||||
if (_this->gmId) { SmGui::BeginDisabled(); }
|
||||
SmGui::FillWidth();
|
||||
if (SmGui::SliderFloat(CONCAT("##_gain_select_", _this->name), &_this->gain, 0, 76)) {
|
||||
if (SmGui::SliderFloatWithSteps(CONCAT("##_pluto_gain__", _this->name), &_this->gain, -1.0f, 73.0f, 1.0f, SmGui::FMT_STR_FLOAT_DB_NO_DECIMAL)) {
|
||||
if (_this->running) {
|
||||
iio_channel_attr_write_longlong(_this->rxChan, "hardwaregain", round(_this->gain));
|
||||
iio_channel_attr_write_double(_this->rxChan, "hardwaregain", _this->gain);
|
||||
}
|
||||
if (!_this->devDesc.empty()) {
|
||||
config.acquire();
|
||||
config.conf["devices"][_this->devDesc]["gain"] = _this->gain;
|
||||
config.release(true);
|
||||
}
|
||||
config.acquire();
|
||||
config.conf["gain"] = _this->gain;
|
||||
config.release(true);
|
||||
}
|
||||
if (_this->gainMode) { SmGui::EndDisabled(); }
|
||||
if (_this->gmId) { SmGui::EndDisabled(); }
|
||||
}
|
||||
|
||||
void setBandwidth(int bw) {
|
||||
@ -285,17 +426,21 @@ private:
|
||||
iio_channel_attr_write_longlong(rxChan, "rf_bandwidth", bw);
|
||||
}
|
||||
else {
|
||||
iio_channel_attr_write_longlong(rxChan, "rf_bandwidth", std::min<int>(sampleRate, 52000000));
|
||||
iio_channel_attr_write_longlong(rxChan, "rf_bandwidth", std::min<int>(samplerate, 52000000));
|
||||
}
|
||||
}
|
||||
|
||||
static void worker(void* ctx) {
|
||||
PlutoSDRSourceModule* _this = (PlutoSDRSourceModule*)ctx;
|
||||
int blockSize = _this->sampleRate / 200.0f;
|
||||
int blockSize = _this->samplerate / 200.0f;
|
||||
|
||||
// Acquire channels
|
||||
iio_channel* rx0_i = iio_device_find_channel(_this->dev, "voltage0", 0);
|
||||
iio_channel* rx0_q = iio_device_find_channel(_this->dev, "voltage1", 0);
|
||||
if (!rx0_i || !rx0_q) {
|
||||
flog::error("Failed to acquire RX channels");
|
||||
return;
|
||||
}
|
||||
|
||||
// Start streaming
|
||||
iio_channel_enable(rx0_i);
|
||||
@ -315,6 +460,7 @@ private:
|
||||
|
||||
// Get buffer pointer
|
||||
int16_t* buf = (int16_t*)iio_buffer_first(rxbuf, rx0_i);
|
||||
if (!buf) { break; }
|
||||
|
||||
// Convert samples to CF32
|
||||
volk_16i_s32f_convert_32f((float*)_this->stream.writeBuf, buf, 32768.0f, blockSize * 2);
|
||||
@ -343,26 +489,42 @@ private:
|
||||
iio_channel* rxChan = NULL;
|
||||
bool running = false;
|
||||
|
||||
double freq;
|
||||
char ip[1024] = "ip:192.168.2.1";
|
||||
float sampleRate = 4000000;
|
||||
int bandwidth = 0;
|
||||
int gainMode = 0;
|
||||
float gain = 0;
|
||||
std::string devDesc = "";
|
||||
std::string uri = "";
|
||||
|
||||
double freq;
|
||||
int samplerate = 4000000;
|
||||
int bandwidth = 0;
|
||||
float gain = -1;
|
||||
|
||||
int devId = 0;
|
||||
int srId = 0;
|
||||
int bwId = 0;
|
||||
int gmId = 0;
|
||||
|
||||
OptionList<std::string, std::string> devices;
|
||||
OptionList<int, double> samplerates;
|
||||
OptionList<int, double> bandwidths;
|
||||
OptionList<int, std::string> gainModes;
|
||||
OptionList<std::string, std::string> gainModes;
|
||||
};
|
||||
|
||||
MOD_EXPORT void _INIT_() {
|
||||
json defConf = {};
|
||||
defConf["device"] = "";
|
||||
defConf["devices"] = {};
|
||||
config.setPath(core::args["root"].s() + "/plutosdr_source_config.json");
|
||||
config.load(defConf);
|
||||
config.enableAutoSave();
|
||||
|
||||
// Reset the configuration if the old format is still used
|
||||
config.acquire();
|
||||
if (!config.conf.contains("device") || !config.conf.contains("devices")) {
|
||||
config.conf = defConf;
|
||||
config.release(true);
|
||||
}
|
||||
else {
|
||||
config.release();
|
||||
}
|
||||
}
|
||||
|
||||
MOD_EXPORT ModuleManager::Instance* _CREATE_INSTANCE_(std::string name) {
|
||||
|
Loading…
Reference in New Issue
Block a user