diff --git a/plugins/m2k/include/m2k/m2kcontroller.h b/plugins/m2k/include/m2k/m2kcontroller.h index b1a5b65ec5..213f665ae2 100644 --- a/plugins/m2k/include/m2k/m2kcontroller.h +++ b/plugins/m2k/include/m2k/m2kcontroller.h @@ -23,7 +23,7 @@ class M2kController : public QObject void startTemperatureTask(); void stopTemperatureTask(); - void connectM2k(iio_context *ctx); + void connectM2k(libm2k::context::M2k *m2k); void disconnectM2k(); public Q_SLOTS: @@ -44,7 +44,6 @@ public Q_SLOTS: private: M2kReadTemperatureTask *tempTask; M2kIdentifyTask *identifyTask; - iio_context *m_iioctx; QString uri; libm2k::context::M2k *m_m2k; diff --git a/plugins/m2k/include/m2k/m2kplugin.h b/plugins/m2k/include/m2k/m2kplugin.h index bcf47d154f..0d33cfbd8a 100644 --- a/plugins/m2k/include/m2k/m2kplugin.h +++ b/plugins/m2k/include/m2k/m2kplugin.h @@ -73,6 +73,8 @@ public Q_SLOTS: m2k_iio_manager *m2k_man; InfoPage *m_m2kInfoPage; + libm2k::context::M2k *m_m2k; + const int PING_PERIOD = 5000; const int infoPageTimerTimeout = 1000; const QStringList calibrationToolNames = {"Oscilloscope", "Spectrum Analyzer", "Network Analyzer", diff --git a/plugins/m2k/src/m2kcontroller.cpp b/plugins/m2k/src/m2kcontroller.cpp index 3b32138f1f..425ec3f15e 100644 --- a/plugins/m2k/src/m2kcontroller.cpp +++ b/plugins/m2k/src/m2kcontroller.cpp @@ -39,10 +39,9 @@ void M2kController::stopTemperatureTask() disconnect(tempTask, SIGNAL(newTemperature(double)), this, SIGNAL(newTemperature(double))); } -void M2kController::connectM2k(iio_context *ctx) +void M2kController::connectM2k(libm2k::context::M2k *m2k) { - m_iioctx = ctx; - m_m2k = m2kOpen(ctx, ""); + m_m2k = m2k; identify(); } @@ -55,11 +54,9 @@ void M2kController::disconnectM2k() if(identifyTask && identifyTask->isRunning()) { identifyTask->requestInterruption(); } - contextCloseAll(); } catch(std::exception &ex) { qDebug(CAT_M2KPLUGIN) << ex.what(); } - m_iioctx = nullptr; m_m2k = nullptr; } diff --git a/plugins/m2k/src/m2kplugin.cpp b/plugins/m2k/src/m2kplugin.cpp index 38d3f29595..f23d3b84de 100644 --- a/plugins/m2k/src/m2kplugin.cpp +++ b/plugins/m2k/src/m2kplugin.cpp @@ -363,6 +363,15 @@ void M2kPlugin::cleanup() m_btnCalibrate->setDisabled(true); clearPingTask(); + if(m_m2k) { + try { + contextClose(m_m2k); + } catch(...) { + qWarning(CAT_M2KPLUGIN) << "M2K plugin deinit on disconnect errored!"; + } + m_m2k = nullptr; + } + ConnectionProvider *c = ConnectionProvider::GetInstance(); c->close(m_param); } @@ -389,18 +398,19 @@ bool M2kPlugin::onConnect() } struct iio_context *ctx = conn->context(); try { + m_m2k = m2kOpen(ctx, m_param.toUtf8()); m2k_man = new m2k_iio_manager(); m_btnCalibrate->setDisabled(false); - m_m2kController->connectM2k(ctx); + m_m2kController->connectM2k(m_m2k); m_pingTask = new IIOPingTask(ctx, this); m_cyclicalTask = new CyclicalTask(m_pingTask); Filter *f = new Filter(ctx); QJSEngine *js = ScopyJS::GetInstance()->engine(); - auto calib = new Calibration(ctx); - auto diom = new DIOManager(ctx, f); + auto calib = new Calibration(m_m2k); + auto diom = new DIOManager(m_m2k, f); auto dmmTme = ToolMenuEntry::findToolMenuEntryById(m_toolList, "m2kdmm"); auto mancalTme = ToolMenuEntry::findToolMenuEntryById(m_toolList, "m2kcal"); auto dioTme = ToolMenuEntry::findToolMenuEntryById(m_toolList, "m2kdio"); @@ -413,23 +423,23 @@ bool M2kPlugin::onConnect() auto pgTme = ToolMenuEntry::findToolMenuEntryById(m_toolList, "m2kpattern"); m_adcBtnGrp = new QButtonGroup(this); - tools.insert("m2kdmm", new DMM(ctx, f, dmmTme, m2k_man)); + tools.insert("m2kdmm", new DMM(m_m2k, f, dmmTme, m2k_man)); dmmTme->setTool(tools["m2kdmm"]); - tools.insert("m2kcal", new ManualCalibration(ctx, f, mancalTme, nullptr, calib)); + tools.insert("m2kcal", new ManualCalibration(m_m2k, f, mancalTme, nullptr, calib)); mancalTme->setTool(tools["m2kcal"]); - tools.insert("m2kdio", new DigitalIO(ctx, f, dioTme, diom, js, nullptr)); + tools.insert("m2kdio", new DigitalIO(f, dioTme, diom, js, nullptr)); dioTme->setTool(tools["m2kdio"]); - tools.insert("m2kpower", new PowerController(ctx, pwrTme, js, nullptr)); + tools.insert("m2kpower", new PowerController(m_m2k, pwrTme, js, nullptr)); pwrTme->setTool(tools["m2kpower"]); - tools.insert("m2ksiggen", new SignalGenerator(ctx, f, siggenTme, js, nullptr)); + tools.insert("m2ksiggen", new SignalGenerator(m_m2k, f, siggenTme, js, nullptr)); siggenTme->setTool(tools["m2ksiggen"]); - tools.insert("m2kspec", new SpectrumAnalyzer(ctx, f, specTme, m2k_man, js, nullptr)); + tools.insert("m2kspec", new SpectrumAnalyzer(m_m2k, f, specTme, m2k_man, js, nullptr)); specTme->setTool(tools["m2kspec"]); - tools.insert("m2kosc", new Oscilloscope(ctx, f, oscTme, m2k_man, js, nullptr)); + tools.insert("m2kosc", new Oscilloscope(m_m2k, f, oscTme, m2k_man, js, nullptr)); oscTme->setTool(tools["m2kosc"]); - tools.insert("m2knet", new NetworkAnalyzer(ctx, f, netTme, m2k_man, js, nullptr)); + tools.insert("m2knet", new NetworkAnalyzer(m_m2k, f, netTme, m2k_man, js, nullptr)); netTme->setTool(tools["m2knet"]); - tools.insert("m2klogic", new logic::LogicAnalyzer(ctx, f, laTme, js, nullptr)); + tools.insert("m2klogic", new logic::LogicAnalyzer(m_m2k, f, laTme, js, nullptr)); laTme->setTool(tools["m2klogic"]); logic::LogicAnalyzer *logic_analyzer = dynamic_cast(tools["m2klogic"]); @@ -439,7 +449,7 @@ bool M2kPlugin::onConnect() oscilloscope->setLogicAnalyzer(logic_analyzer); network_analyzer->setOscilloscope(oscilloscope); - tools.insert("m2kpattern", new logic::PatternGenerator(ctx, f, pgTme, js, diom, nullptr)); + tools.insert("m2kpattern", new logic::PatternGenerator(m_m2k, f, pgTme, js, diom, nullptr)); pgTme->setTool(tools["m2kpattern"]); connect(dynamic_cast(siggenTme->tool())->getRunButton(), &QPushButton::toggled, this, [=](bool en) { diff --git a/plugins/m2k/src/old/calibration.cpp b/plugins/m2k/src/old/calibration.cpp index 0b1a0ca1fc..d35655ea49 100644 --- a/plugins/m2k/src/old/calibration.cpp +++ b/plugins/m2k/src/old/calibration.cpp @@ -32,15 +32,16 @@ #include #include +#include Q_LOGGING_CATEGORY(CAT_M2K_CALIBRATION, "M2KCalibration"); using namespace scopy; -Calibration::Calibration(struct iio_context *ctx) +Calibration::Calibration(libm2k::context::M2k *m2k) : m_api(new Calibration_API(this)) , m_cancel(false) - , m_ctx(ctx) + , m_m2k(m2k) , m_initialized(false) { m_api->setObjectName("calib"); @@ -53,11 +54,6 @@ bool Calibration::initialize() { m_initialized = false; - if(!m_ctx) { - return false; - } - - m_m2k = libm2k::context::m2kOpen(m_ctx, ""); if(!m_m2k) { return false; } @@ -130,22 +126,9 @@ double Calibration::getIioDevTemp(const QString &devName) const { double temp = -273.15; - struct iio_device *dev = iio_context_find_device(m_ctx, devName.toLatin1().data()); - - if(dev) { - struct iio_channel *chn = iio_device_find_channel(dev, "temp0", false); - if(chn) { - double offset; - double raw; - double scale; - - iio_channel_attr_read_double(chn, "offset", &offset); - iio_channel_attr_read_double(chn, "raw", &raw); - iio_channel_attr_read_double(chn, "scale", &scale); - - temp = (raw + offset) * scale / 1000; - } + libm2k::analog::DMM *dmm = m_m2k->getDMM(devName.toStdString()); + if(dmm) { + temp = dmm->readChannel("temp0").value; } - return temp; } diff --git a/plugins/m2k/src/old/calibration.hpp b/plugins/m2k/src/old/calibration.hpp index 7fe59d93d4..3b7270466f 100644 --- a/plugins/m2k/src/old/calibration.hpp +++ b/plugins/m2k/src/old/calibration.hpp @@ -32,7 +32,6 @@ extern "C" { - struct iio_context; struct iio_device; struct iio_channel; struct iio_buffer; @@ -64,7 +63,7 @@ class Calibration HIGH }; - Calibration(struct iio_context *ctx); + Calibration(libm2k::context::M2k *m2k); ~Calibration(); bool initialize(); @@ -88,7 +87,6 @@ class Calibration ApiObject *m_api; volatile bool m_cancel; - struct iio_context *m_ctx; libm2k::context::M2k *m_m2k; bool m_initialized; }; diff --git a/plugins/m2k/src/old/digitalchannel_manager.cpp b/plugins/m2k/src/old/digitalchannel_manager.cpp index 4a63860402..a2c688f9b5 100644 --- a/plugins/m2k/src/old/digitalchannel_manager.cpp +++ b/plugins/m2k/src/old/digitalchannel_manager.cpp @@ -51,8 +51,8 @@ namespace scopy { void DIOManager::init() {} -DIOManager::DIOManager(struct iio_context *ctx, Filter *filt) - : m_m2k_context(m2kOpen(ctx, "")) +DIOManager::DIOManager(libm2k::context::M2k *m2k, Filter *filt) + : m_m2k_context(m2k) , digital(m_m2k_context->getDigital()) { outputEnabled = false; diff --git a/plugins/m2k/src/old/digitalchannel_manager.hpp b/plugins/m2k/src/old/digitalchannel_manager.hpp index 7180de337d..43a05951b8 100644 --- a/plugins/m2k/src/old/digitalchannel_manager.hpp +++ b/plugins/m2k/src/old/digitalchannel_manager.hpp @@ -57,7 +57,7 @@ class DIOManager : public QObject public: void init(); - DIOManager(struct iio_context *ctx, Filter *filt); + DIOManager(libm2k::context::M2k *m2k, Filter *filt); ~DIOManager(); bool getOutputEnabled(); void enableOutput(bool output); diff --git a/plugins/m2k/src/old/digitalio.cpp b/plugins/m2k/src/old/digitalio.cpp index e55faaad14..74cdd2a997 100644 --- a/plugins/m2k/src/old/digitalio.cpp +++ b/plugins/m2k/src/old/digitalio.cpp @@ -157,9 +157,9 @@ void DigitalIO::setOutput() } } -DigitalIO::DigitalIO(struct iio_context *ctx, Filter *filt, ToolMenuEntry *toolMenuItem, DIOManager *diom, - QJSEngine *engine, QWidget *parent, bool offline_mode) - : M2kTool(ctx, toolMenuItem, new DigitalIO_API(this), "Digital IO", parent) +DigitalIO::DigitalIO(Filter *filt, ToolMenuEntry *toolMenuItem, DIOManager *diom, QJSEngine *engine, QWidget *parent, + bool offline_mode) + : M2kTool(toolMenuItem, new DigitalIO_API(this), "Digital IO", parent) , ui(new Ui::DigitalIO) , offline_mode(offline_mode) , diom(diom) diff --git a/plugins/m2k/src/old/digitalio.hpp b/plugins/m2k/src/old/digitalio.hpp index 590b564505..b2cdfba9de 100644 --- a/plugins/m2k/src/old/digitalio.hpp +++ b/plugins/m2k/src/old/digitalio.hpp @@ -104,8 +104,8 @@ private Q_SLOTS: void readPreferences() override; public: - explicit DigitalIO(struct iio_context *ctx, Filter *filt, ToolMenuEntry *toolMenuItem, DIOManager *diom, - QJSEngine *engine, QWidget *parent, bool offline_mode = 0); + explicit DigitalIO(Filter *filt, ToolMenuEntry *toolMenuItem, DIOManager *diom, QJSEngine *engine, + QWidget *parent, bool offline_mode = 0); ~DigitalIO(); void setDirection(int ch, int direction); void setOutput(int ch, int out); diff --git a/plugins/m2k/src/old/dmm.cpp b/plugins/m2k/src/old/dmm.cpp index 62f5a5ff5a..6024487119 100644 --- a/plugins/m2k/src/old/dmm.cpp +++ b/plugins/m2k/src/old/dmm.cpp @@ -64,12 +64,12 @@ using namespace scopy::m2k; using namespace libm2k; using namespace libm2k::context; -DMM::DMM(struct iio_context *ctx, Filter *filt, ToolMenuEntry *tme, m2k_iio_manager *m2k_man, QWidget *parent) - : M2kTool(ctx, tme, new DMM_API(this), "Voltmeter", parent) +DMM::DMM(libm2k::context::M2k *m2k, Filter *filt, ToolMenuEntry *tme, m2k_iio_manager *m2k_man, QWidget *parent) + : M2kTool(tme, new DMM_API(this), "Voltmeter", parent) , ui(new Ui::DMM) , signal(std::make_shared()) - , manager(m2k_man->get_instance(ctx, filt->device_name(TOOL_DMM))) - , m_m2k_context(m2kOpen(ctx, "")) + , manager(m2k_man->get_instance(m2k, filt->device_name(TOOL_DMM))) + , m_m2k_context(m2k) , m_m2k_analogin(m_m2k_context->getAnalogIn()) , m_adc_nb_channels(m_m2k_analogin->getNbChannels()) , interrupt_data_logging(false) diff --git a/plugins/m2k/src/old/dmm.hpp b/plugins/m2k/src/old/dmm.hpp index 45226fdfc6..30685f3f25 100644 --- a/plugins/m2k/src/old/dmm.hpp +++ b/plugins/m2k/src/old/dmm.hpp @@ -59,7 +59,7 @@ class DMM : public M2kTool Q_OBJECT public: - explicit DMM(struct iio_context *ctx, Filter *filt, ToolMenuEntry *toolMenuItem, m2k_iio_manager *m2k_man, + explicit DMM(libm2k::context::M2k *m2k, Filter *filt, ToolMenuEntry *toolMenuItem, m2k_iio_manager *m2k_man, QWidget *parent = nullptr); QPushButton *getRunButton(); ~DMM(); diff --git a/plugins/m2k/src/old/iio_manager.cpp b/plugins/m2k/src/old/iio_manager.cpp index 41956c6ad4..a0acba2b28 100644 --- a/plugins/m2k/src/old/iio_manager.cpp +++ b/plugins/m2k/src/old/iio_manager.cpp @@ -38,24 +38,21 @@ static constexpr int KERNEL_BUFFERS_DEFAULT = 1; Q_LOGGING_CATEGORY(CAT_M2K_IIO_MANAGER, "M2kIIOManager"); -iio_manager::iio_manager(unsigned int block_id, struct iio_context *ctx, QString _dev, unsigned long _buffer_size) +iio_manager::iio_manager(unsigned int block_id, libm2k::context::M2k *ctx, QString _dev, unsigned long _buffer_size) : QObject(nullptr) , top_block("IIO Manager " + std::to_string(block_id)) , id(block_id) , _started(false) , buffer_size(_buffer_size) , m_mixed_source(nullptr) + , m_context(ctx) { - m_context = libm2k::context::m2kOpen(ctx, ""); m_analogin = m_context->getAnalogIn(); - if(!ctx) - throw std::runtime_error("IIO context not created"); - - struct iio_device *dev = iio_context_find_device(ctx, _dev.toStdString().c_str()); - if(!dev) + if(m_analogin->getName() != _dev.toStdString()) { throw std::runtime_error("Device not found"); + } - nb_channels = iio_device_get_channels_count(dev); + nb_channels = m_analogin->getNbChannels(); Preferences *p = Preferences::GetInstance(); double targetFps = p->get("general_plot_target_fps").toString().toDouble(); // get target fps from preferences @@ -112,7 +109,7 @@ std::shared_ptr m2k_iio_manager::has_instance(QString _dev) return nullptr; } -std::shared_ptr m2k_iio_manager::get_instance(struct iio_context *ctx, QString _dev, +std::shared_ptr m2k_iio_manager::get_instance(libm2k::context::M2k *ctx, QString _dev, unsigned long buffer_size) { auto instance = has_instance(_dev); diff --git a/plugins/m2k/src/old/iio_manager.hpp b/plugins/m2k/src/old/iio_manager.hpp index 639309613a..d2262edaa7 100644 --- a/plugins/m2k/src/old/iio_manager.hpp +++ b/plugins/m2k/src/old/iio_manager.hpp @@ -53,7 +53,7 @@ class iio_manager : public QObject, public gr::top_block typedef gr::blocks::copy::sptr port_id; const unsigned id; - iio_manager(unsigned int id, struct iio_context *ctx, QString dev, unsigned long buffer_size); + iio_manager(unsigned int id, libm2k::context::M2k *ctx, QString dev, unsigned long buffer_size); ~iio_manager(); /* Connect a block to one of the channels of the IIO source. @@ -180,7 +180,7 @@ class m2k_iio_manager /* Get a shared pointer to the instance of iio_manager that * manages the requested device */ - std::shared_ptr get_instance(struct iio_context *ctx, QString dev, + std::shared_ptr get_instance(libm2k::context::M2k *ctx, QString dev, unsigned long buffer_size = IIO_BUFFER_SIZE); private: diff --git a/plugins/m2k/src/old/logicanalyzer/logic_analyzer.cpp b/plugins/m2k/src/old/logicanalyzer/logic_analyzer.cpp index ff4b238b9f..41cfd7ce60 100644 --- a/plugins/m2k/src/old/logicanalyzer/logic_analyzer.cpp +++ b/plugins/m2k/src/old/logicanalyzer/logic_analyzer.cpp @@ -72,9 +72,9 @@ static gint sort_pds(gconstpointer a, gconstpointer b) return strcmp(sda->id, sdb->id); } -LogicAnalyzer::LogicAnalyzer(struct iio_context *ctx, Filter *filt, ToolMenuEntry *tme, QJSEngine *engine, +LogicAnalyzer::LogicAnalyzer(libm2k::context::M2k *m2k, Filter *filt, ToolMenuEntry *tme, QJSEngine *engine, QWidget *parent, bool offline_mode_) - : M2kTool(nullptr, tme, new LogicAnalyzer_API(this), "Logic Analyzer", parent) + : M2kTool(tme, new LogicAnalyzer_API(this), "Logic Analyzer", parent) , ui(new Ui::LogicAnalyzer) , m_plot(this, false, 16, 10, new TimePrefixFormatter, new MetricPrefixFormatter) , m_bufferPreviewer(new DigitalBufferPreviewer(40, this)) @@ -97,7 +97,7 @@ LogicAnalyzer::LogicAnalyzer(struct iio_context *ctx, Filter *filt, ToolMenuEntr , m_sampleRate(1000000) , m_bufferSize(1000) , m_lastCapturedSample(0) - , m_m2k_context(m2kOpen(ctx, "")) + , m_m2k_context(m2k) , m_m2kDigital(m_m2k_context->getDigital()) , m_nbChannels(DIGITAL_NR_CHANNELS) , m_horizOffset(0.0) diff --git a/plugins/m2k/src/old/logicanalyzer/logic_analyzer.h b/plugins/m2k/src/old/logicanalyzer/logic_analyzer.h index 194f65c94e..8ca66e6f3f 100644 --- a/plugins/m2k/src/old/logicanalyzer/logic_analyzer.h +++ b/plugins/m2k/src/old/logicanalyzer/logic_analyzer.h @@ -77,7 +77,7 @@ class LogicAnalyzer : public M2kTool friend class LogicAnalyzer_API; public: - explicit LogicAnalyzer(struct iio_context *ctx, Filter *filt, ToolMenuEntry *toolMenuItem, QJSEngine *engine, + explicit LogicAnalyzer(libm2k::context::M2k *m2k, Filter *filt, ToolMenuEntry *toolMenuItem, QJSEngine *engine, QWidget *parent, bool offline_mode_ = 0); ~LogicAnalyzer(); void setNativeDialogs(bool nativeDialogs) override; diff --git a/plugins/m2k/src/old/m2ktool.cpp b/plugins/m2k/src/old/m2ktool.cpp index 3077c9a56d..7096b9860c 100644 --- a/plugins/m2k/src/old/m2ktool.cpp +++ b/plugins/m2k/src/old/m2ktool.cpp @@ -25,9 +25,8 @@ using namespace scopy; using namespace scopy::m2k; -M2kTool::M2kTool(struct iio_context *ctx, ToolMenuEntry *tme, ApiObject *api, const QString &name, QWidget *parent) +M2kTool::M2kTool(ToolMenuEntry *tme, ApiObject *api, const QString &name, QWidget *parent) : QWidget(parent) - , ctx(ctx) , api(api) , name(name) , saveOnExit(true) diff --git a/plugins/m2k/src/old/m2ktool.hpp b/plugins/m2k/src/old/m2ktool.hpp index 3fd2358c5d..7939d2cfff 100644 --- a/plugins/m2k/src/old/m2ktool.hpp +++ b/plugins/m2k/src/old/m2ktool.hpp @@ -52,8 +52,7 @@ class M2kTool : public QWidget, public ResourceUser Q_OBJECT public: - explicit M2kTool(struct iio_context *ctx, ToolMenuEntry *tme, ApiObject *api, const QString &name, - QWidget *parent); + explicit M2kTool(ToolMenuEntry *tme, ApiObject *api, const QString &name, QWidget *parent); ~M2kTool(); const QString &getName(); @@ -76,7 +75,6 @@ public Q_SLOTS: virtual void readPreferences(); protected: - struct iio_context *ctx; ApiObject *api; QString name; bool saveOnExit; diff --git a/plugins/m2k/src/old/manualcalibration.cpp b/plugins/m2k/src/old/manualcalibration.cpp index f15e5ac815..2d2e4ae7ab 100644 --- a/plugins/m2k/src/old/manualcalibration.cpp +++ b/plugins/m2k/src/old/manualcalibration.cpp @@ -74,15 +74,14 @@ Measure the Voltage on the "V-" and enter the value in the field below. The value should be around -4.5V)"); -ManualCalibration::ManualCalibration(struct iio_context *ctx, Filter *filt, ToolMenuEntry *tme, QWidget *parent, +ManualCalibration::ManualCalibration(libm2k::context::M2k *m2k, Filter *filt, ToolMenuEntry *tme, QWidget *parent, Calibration *cal) - : M2kTool(ctx, tme, new ManualCalibration_API(this), "Calibration", parent) + : M2kTool(tme, new ManualCalibration_API(this), "Calibration", parent) , ui(new Ui::ManualCalibration) , filter(filt) - , ctx(ctx) , calib(cal) , calibrationFilePath("") - , m_m2k_context(m2kOpen(ctx, "")) + , m_m2k_context(m2k) , m_m2k_powersupply(m_m2k_context->getPowerSupply()) { ui->setupUi(this); @@ -495,8 +494,8 @@ void ManualCalibration::displayStartUpCalibrationValues(void) void ManualCalibration::initParameters(void) { QStringList tableHeader; - const char *name; - const char *value; + QString name; + QString value; QTableWidgetItem *item; tableHeader << "Name" @@ -505,12 +504,17 @@ void ManualCalibration::initParameters(void) paramTable->setColumnCount(2); paramTable->setHorizontalHeaderLabels(tableHeader); - for(int i = 4; i < 12; i++) { - if(!iio_context_get_attr(ctx, i, &name, &value)) { - item = new QTableWidgetItem(QString(name + 4)); + unsigned int i = 0; + auto ctxAttrs = m_m2k_context->getAvailableContextAttributes(); + for(auto &&attr : qAsConst(ctxAttrs)) { + name = QString::fromStdString(attr); + if(name.startsWith("cal,")) { + value = QString::fromStdString(m_m2k_context->getContextAttributeValue(attr)); + item = new QTableWidgetItem(name.mid(4)); item->setFlags(Qt::ItemIsSelectable); - paramTable->setItem(i - 4, 0, item); - paramTable->setItem(i - 4, 1, new QTableWidgetItem(QString(value))); + paramTable->setItem(i, 0, item); + paramTable->setItem(i, 1, new QTableWidgetItem(value)); + i++; } } diff --git a/plugins/m2k/src/old/manualcalibration.h b/plugins/m2k/src/old/manualcalibration.h index 0bde940523..134e1f14b0 100644 --- a/plugins/m2k/src/old/manualcalibration.h +++ b/plugins/m2k/src/old/manualcalibration.h @@ -102,7 +102,7 @@ class ManualCalibration : public M2kTool Q_OBJECT public: - explicit ManualCalibration(struct iio_context *ctx, Filter *filt, ToolMenuEntry *tme, QWidget *parent = 0, + explicit ManualCalibration(libm2k::context::M2k *m2k, Filter *filt, ToolMenuEntry *tme, QWidget *parent = 0, Calibration *cal = 0); ~ManualCalibration(); @@ -155,7 +155,6 @@ private Q_SLOTS: QJSEngine *eng; Calibration *calib; - iio_context *ctx; struct stCalibStory stCalibrationStory; struct stCalibParam stParameters; diff --git a/plugins/m2k/src/old/network_analyzer.cpp b/plugins/m2k/src/old/network_analyzer.cpp index 07c47ebc25..3569d9c876 100644 --- a/plugins/m2k/src/old/network_analyzer.cpp +++ b/plugins/m2k/src/old/network_analyzer.cpp @@ -171,9 +171,9 @@ void NetworkAnalyzer::_configureAdcFlowgraph(size_t buffer_size) ui->btnHelp->setUrl("https://wiki.analog.com/university/tools/m2k/scopy/networkanalyzer"); } -NetworkAnalyzer::NetworkAnalyzer(struct iio_context *ctx, Filter *filt, ToolMenuEntry *tme, m2k_iio_manager *m2k_man, +NetworkAnalyzer::NetworkAnalyzer(libm2k::context::M2k *m2k, Filter *filt, ToolMenuEntry *tme, m2k_iio_manager *m2k_man, QJSEngine *engine, QWidget *parent) - : M2kTool(ctx, tme, new NetworkAnalyzer_API(this), "Network Analyzer", parent) + : M2kTool(tme, new NetworkAnalyzer_API(this), "Network Analyzer", parent) , ui(new Ui::NetworkAnalyzer) , m_m2k_context(nullptr) , m_m2k_analogin(nullptr) @@ -198,18 +198,15 @@ NetworkAnalyzer::NetworkAnalyzer(struct iio_context *ctx, Filter *filt, ToolMenu , m_nb_averaging(1) , m_nb_periods(2) { - if(ctx) { - iio = m2k_man->get_instance(ctx, filt->device_name(TOOL_NETWORK_ANALYZER, 2)); - - m_m2k_context = m2kOpen(ctx, ""); - if(m_m2k_context) { - m_m2k_analogin = m_m2k_context->getAnalogIn(); - m_m2k_analogout = m_m2k_context->getAnalogOut(); - m_adc_nb_channels = m_m2k_analogin->getNbChannels(); - m_dac_nb_channels = m_m2k_analogout->getNbChannels(); - m_m2k_analogout->setKernelBuffersCount(0, 1); - m_m2k_analogout->setKernelBuffersCount(1, 1); - } + if(m2k) { + m_m2k_context = m2k; + iio = m2k_man->get_instance(m_m2k_context, filt->device_name(TOOL_NETWORK_ANALYZER, 2)); + m_m2k_analogin = m_m2k_context->getAnalogIn(); + m_m2k_analogout = m_m2k_context->getAnalogOut(); + m_adc_nb_channels = m_m2k_analogin->getNbChannels(); + m_dac_nb_channels = m_m2k_analogout->getNbChannels(); + m_m2k_analogout->setKernelBuffersCount(0, 1); + m_m2k_analogout->setKernelBuffersCount(1, 1); } ui->setupUi(this); diff --git a/plugins/m2k/src/old/network_analyzer.hpp b/plugins/m2k/src/old/network_analyzer.hpp index dd2423a9d3..1db167a1ee 100644 --- a/plugins/m2k/src/old/network_analyzer.hpp +++ b/plugins/m2k/src/old/network_analyzer.hpp @@ -56,11 +56,6 @@ #include #include -extern "C" -{ - struct iio_context; -} - namespace Ui { class NetworkAnalyzer; } @@ -80,7 +75,7 @@ class NetworkAnalyzer : public M2kTool Q_OBJECT public: - explicit NetworkAnalyzer(struct iio_context *ctx, Filter *filt, ToolMenuEntry *toolMenuItem, + explicit NetworkAnalyzer(libm2k::context::M2k *m2k, Filter *filt, ToolMenuEntry *toolMenuItem, m2k_iio_manager *m2k_man, QJSEngine *engine, QWidget *parent = nullptr); ~NetworkAnalyzer(); QPushButton *getRunButton(); diff --git a/plugins/m2k/src/old/oscilloscope.cpp b/plugins/m2k/src/old/oscilloscope.cpp index d41785fa1d..01238316e6 100644 --- a/plugins/m2k/src/old/oscilloscope.cpp +++ b/plugins/m2k/src/old/oscilloscope.cpp @@ -105,10 +105,10 @@ using namespace std::placeholders; constexpr int MAX_BUFFER_SIZE_STREAM = 1024 * 1024; constexpr int MAX_KERNEL_BUFFERS = 64; -Oscilloscope::Oscilloscope(struct iio_context *ctx, Filter *filt, ToolMenuEntry *tme, m2k_iio_manager *m2k_man, +Oscilloscope::Oscilloscope(libm2k::context::M2k *m2k, Filter *filt, ToolMenuEntry *tme, m2k_iio_manager *m2k_man, QJSEngine *engine, QWidget *parent) - : M2kTool(ctx, tme, new Oscilloscope_API(this), "Oscilloscope", parent) - , m_m2k_context(m2kOpen(ctx, "")) + : M2kTool(tme, new Oscilloscope_API(this), "Oscilloscope", parent) + , m_m2k_context(m2k) , m_m2k_analogin(m_m2k_context->getAnalogIn()) , m_m2k_digital(m_m2k_context->getDigital()) , nb_channels(m_m2k_analogin->getNbChannels()) @@ -223,7 +223,7 @@ Oscilloscope::Oscilloscope(struct iio_context *ctx, Filter *filt, ToolMenuEntry for(uint i = 0; i < nb_channels; i++) fft_plot.setYaxisMouseGesturesEnabled(i, false); - iio = m2k_man->get_instance(ctx, filt->device_name(TOOL_OSCILLOSCOPE)); + iio = m2k_man->get_instance(m2k, filt->device_name(TOOL_OSCILLOSCOPE)); gr::hier_block2_sptr hier = iio->to_hier_block2(); qDebug(CAT_M2K_OSCILLOSCOPE) << "Manager created:\n" << gr::dot_graph(hier).c_str(); diff --git a/plugins/m2k/src/old/oscilloscope.hpp b/plugins/m2k/src/old/oscilloscope.hpp index 15715ddda2..50865f3890 100644 --- a/plugins/m2k/src/old/oscilloscope.hpp +++ b/plugins/m2k/src/old/oscilloscope.hpp @@ -124,7 +124,7 @@ class Oscilloscope : public M2kTool Q_OBJECT public: - explicit Oscilloscope(struct iio_context *ctx, Filter *filt, ToolMenuEntry *tme, m2k_iio_manager *m2k_man, + explicit Oscilloscope(libm2k::context::M2k *m2k, Filter *filt, ToolMenuEntry *tme, m2k_iio_manager *m2k_man, QJSEngine *engine, QWidget *parent = 0); ~Oscilloscope(); diff --git a/plugins/m2k/src/old/patterngenerator/pattern_generator.cpp b/plugins/m2k/src/old/patterngenerator/pattern_generator.cpp index cb41224dbe..1d0ced9bd1 100644 --- a/plugins/m2k/src/old/patterngenerator/pattern_generator.cpp +++ b/plugins/m2k/src/old/patterngenerator/pattern_generator.cpp @@ -73,16 +73,16 @@ int lcm(int a, int b) } // namespace detail -PatternGenerator::PatternGenerator(struct iio_context *ctx, Filter *filt, ToolMenuEntry *tme, QJSEngine *engine, +PatternGenerator::PatternGenerator(libm2k::context::M2k *m2k, Filter *filt, ToolMenuEntry *tme, QJSEngine *engine, DIOManager *diom, QWidget *parent) - : M2kTool(nullptr, tme, new PatternGenerator_API(this), "Pattern Generator", parent) + : M2kTool(tme, new PatternGenerator_API(this), "Pattern Generator", parent) , m_ui(new Ui::PatternGenerator) , m_plot(this, false, 16, 10, new TimePrefixFormatter, new MetricPrefixFormatter) , m_plotScrollBar(new QScrollBar(Qt::Vertical, this)) , m_selectedChannel(-1) , m_nbChannels(DIGITAL_NR_CHANNELS) , m_currentGroupMenu(nullptr) - , m_m2k_context(m2kOpen(ctx, "")) + , m_m2k_context(m2k) , m_m2kDigital(m_m2k_context->getDigital()) , m_bufferSize(1) , m_sampleRate(1) diff --git a/plugins/m2k/src/old/patterngenerator/pattern_generator.h b/plugins/m2k/src/old/patterngenerator/pattern_generator.h index 6155501fd0..057f3b7078 100644 --- a/plugins/m2k/src/old/patterngenerator/pattern_generator.h +++ b/plugins/m2k/src/old/patterngenerator/pattern_generator.h @@ -67,7 +67,7 @@ class PatternGenerator : public M2kTool friend class PatternGenerator_API; public: - explicit PatternGenerator(struct iio_context *ctx, Filter *filt, ToolMenuEntry *tme, QJSEngine *engine, + explicit PatternGenerator(libm2k::context::M2k *m2k, Filter *filt, ToolMenuEntry *tme, QJSEngine *engine, DIOManager *diom, QWidget *parent); ~PatternGenerator(); void setNativeDialogs(bool nativeDialogs) override; diff --git a/plugins/m2k/src/old/power_controller.cpp b/plugins/m2k/src/old/power_controller.cpp index 97187311d5..5f46e3b730 100644 --- a/plugins/m2k/src/old/power_controller.cpp +++ b/plugins/m2k/src/old/power_controller.cpp @@ -46,11 +46,11 @@ using namespace libm2k::analog; #include Q_LOGGING_CATEGORY(CAT_M2K_POWERCONTROL, "M2KPowerController"); -PowerController::PowerController(struct iio_context *ctx, ToolMenuEntry *tme, QJSEngine *engine, QWidget *parent) - : M2kTool(ctx, tme, new PowerController_API(this), "Power Supply", parent) +PowerController::PowerController(libm2k::context::M2k *m2k, ToolMenuEntry *tme, QJSEngine *engine, QWidget *parent) + : M2kTool(tme, new PowerController_API(this), "Power Supply", parent) , ui(new Ui::PowerController) , in_sync(false) - , m_m2k_context(m2kOpen(ctx, "")) + , m_m2k_context(m2k) , m_m2k_powersupply(m_m2k_context->getPowerSupply()) { ui->setupUi(this); diff --git a/plugins/m2k/src/old/power_controller.hpp b/plugins/m2k/src/old/power_controller.hpp index 642d186923..b16d67a109 100644 --- a/plugins/m2k/src/old/power_controller.hpp +++ b/plugins/m2k/src/old/power_controller.hpp @@ -62,7 +62,7 @@ class PowerController : public M2kTool public: const int AVERAGE_COUNT = 5; - explicit PowerController(struct iio_context *ctx, ToolMenuEntry *tme, QJSEngine *engine, QWidget *parent = 0); + explicit PowerController(libm2k::context::M2k *m2k, ToolMenuEntry *tme, QJSEngine *engine, QWidget *parent = 0); ~PowerController(); public Q_SLOTS: diff --git a/plugins/m2k/src/old/signal_generator.cpp b/plugins/m2k/src/old/signal_generator.cpp index 2826ef8d9a..dac337f596 100644 --- a/plugins/m2k/src/old/signal_generator.cpp +++ b/plugins/m2k/src/old/signal_generator.cpp @@ -132,12 +132,12 @@ bool SignalGenerator::chunkCompare(chunk_header_t &ptr, const char *id2) return true; } -SignalGenerator::SignalGenerator(struct iio_context *_ctx, Filter *filt, ToolMenuEntry *tme, QJSEngine *engine, +SignalGenerator::SignalGenerator(libm2k::context::M2k *m2k, Filter *filt, ToolMenuEntry *tme, QJSEngine *engine, QWidget *parent) - : M2kTool(_ctx, tme, new SignalGenerator_API(this), "Signal Generator", parent) + : M2kTool(tme, new SignalGenerator_API(this), "Signal Generator", parent) , ui(new Ui::SignalGenerator) , time_block_data(new struct time_block_data) - , m_m2k_context(m2kOpen(ctx, "")) + , m_m2k_context(m2k) , m_m2k_analogout(m_m2k_context->getAnalogOut()) , nr_of_periods(2) , currentChannel(0) diff --git a/plugins/m2k/src/old/signal_generator.hpp b/plugins/m2k/src/old/signal_generator.hpp index 73585c47ba..404a5ecc89 100644 --- a/plugins/m2k/src/old/signal_generator.hpp +++ b/plugins/m2k/src/old/signal_generator.hpp @@ -46,11 +46,6 @@ #include #include -extern "C" -{ - struct iio_context; -} - namespace Ui { class SignalGenerator; } @@ -143,7 +138,7 @@ class SignalGenerator : public M2kTool Q_OBJECT public: - explicit SignalGenerator(struct iio_context *ctx, Filter *filt, ToolMenuEntry *tme, QJSEngine *engine, + explicit SignalGenerator(libm2k::context::M2k *m2k, Filter *filt, ToolMenuEntry *tme, QJSEngine *engine, QWidget *parent); ~SignalGenerator(); diff --git a/plugins/m2k/src/old/spectrum_analyzer.cpp b/plugins/m2k/src/old/spectrum_analyzer.cpp index 8010f95ee2..5b090134d0 100644 --- a/plugins/m2k/src/old/spectrum_analyzer.cpp +++ b/plugins/m2k/src/old/spectrum_analyzer.cpp @@ -127,20 +127,18 @@ void SpectrumAnalyzer::initInstrumentStrings() }; } -SpectrumAnalyzer::SpectrumAnalyzer(struct iio_context *ctx, Filter *filt, ToolMenuEntry *tme, m2k_iio_manager *m2k_man, - QJSEngine *engine, QWidget *parent) - : M2kTool(ctx, tme, new SpectrumAnalyzer_API(this), "Spectrum Analyzer", parent) +SpectrumAnalyzer::SpectrumAnalyzer(libm2k::context::M2k *m2k, Filter *filt, ToolMenuEntry *tme, + m2k_iio_manager *m2k_man, QJSEngine *engine, QWidget *parent) + : M2kTool(tme, new SpectrumAnalyzer_API(this), "Spectrum Analyzer", parent) , ui(new Ui::SpectrumAnalyzer) - , m_m2k_context(nullptr) + , m_m2k_context(m2k) , m_m2k_analogin(nullptr) - , m_generic_context(nullptr) - , m_generic_analogin(nullptr) , marker_selector(new DbClickButtons(this)) , fft_plot(nullptr) , waterfall_plot(nullptr) , settings_group(new QButtonGroup(this)) , channels_group(new QButtonGroup(this)) - , adc_name(ctx ? filt->device_name(TOOL_SPECTRUM_ANALYZER) : "") + , adc_name(filt->device_name(TOOL_SPECTRUM_ANALYZER)) , crt_channel_id(0) , crt_peak(0) , max_peak_count(10) @@ -162,24 +160,17 @@ SpectrumAnalyzer::SpectrumAnalyzer(struct iio_context *ctx, Filter *filt, ToolMe // Get the list of names of the available channels QList channel_names; - if(ctx) { - auto libm2k_ctx = contextOpen(ctx, ""); - if(libm2k_ctx->toM2k()) { - m_m2k_context = libm2k_ctx->toM2k(); - m_m2k_analogin = m_m2k_context->getAnalogIn(); - m_adc_nb_channels = m_m2k_analogin->getNbChannels(); - m_max_sample_rate = m_m2k_analogin->getMaximumSamplerate(); - } else { - m_generic_context = libm2k_ctx->toGeneric(); - m_generic_analogin = m_generic_context->getAnalogIn(0); - m_adc_nb_channels = m_generic_analogin->getNbChannels(); - } - iio = m2k_man->get_instance(ctx, adc_name); + if(m_m2k_context) { + m_m2k_analogin = m_m2k_context->getAnalogIn(); + m_adc_nb_channels = m_m2k_analogin->getNbChannels(); + m_max_sample_rate = m_m2k_analogin->getMaximumSamplerate(); + iio = m2k_man->get_instance(m_m2k_context, adc_name); + } - for(unsigned int i = 0; i < m_adc_nb_channels; i++) { - channel_names.push_back(QString("Channel %1").arg(i + 1)); - } + for(unsigned int i = 0; i < m_adc_nb_channels; i++) { + channel_names.push_back(QString("Channel %1").arg(i + 1)); } + sample_rate = m_max_sample_rate; ui->setupUi(this); @@ -508,7 +499,7 @@ SpectrumAnalyzer::SpectrumAnalyzer(struct iio_context *ctx, Filter *filt, ToolMe connect(this, &SpectrumAnalyzer::selectedChannelChanged, this, [=](int id) { setWaterfallWindow(channels.at(id)->getWindow(), id); }); - if(ctx) { + if(m2k) { build_gnuradio_block_chain(); } else { build_gnuradio_block_chain_no_ctx(); @@ -2578,10 +2569,6 @@ void SpectrumAnalyzer::writeAllSettingsToHardware() trigger->setAnalogMode(i, libm2k::ALWAYS); } } - } else { - for(unsigned int i = 0; i < m_adc_nb_channels; i++) { - m_generic_analogin->setSampleRate(i, m_generic_analogin->getMaximumSamplerate(i)); - } } } catch(libm2k::m2k_exception &e) { HANDLE_EXCEPTION(e); @@ -2703,15 +2690,6 @@ void SpectrumAnalyzer::setSampleRate(double sr) HANDLE_EXCEPTION(e); qDebug(CAT_M2K_SPECTRUM_ANALYZER) << "Can't write oversampling ratio: " << e.what(); } - } else { - try { - for(unsigned int i = 0; i < m_adc_nb_channels; i++) { - m_generic_analogin->setSampleRate(i, sr); - } - } catch(libm2k::m2k_exception &e) { - HANDLE_EXCEPTION(e); - qDebug(CAT_M2K_SPECTRUM_ANALYZER) << "Can't write sampling frequency: " << e.what(); - } } fft_plot->presetSampleRate(new_sr); diff --git a/plugins/m2k/src/old/spectrum_analyzer.hpp b/plugins/m2k/src/old/spectrum_analyzer.hpp index fc5d47fdd7..c8c14c7dd8 100644 --- a/plugins/m2k/src/old/spectrum_analyzer.hpp +++ b/plugins/m2k/src/old/spectrum_analyzer.hpp @@ -56,7 +56,6 @@ extern "C" { struct iio_buffer; struct iio_channel; - struct iio_context; struct iio_device; } @@ -121,7 +120,7 @@ class SpectrumAnalyzer : public M2kTool typedef std::shared_ptr channel_sptr; - explicit SpectrumAnalyzer(struct iio_context *iio, Filter *filt, ToolMenuEntry *tme, m2k_iio_manager *m2k_man, + explicit SpectrumAnalyzer(libm2k::context::M2k *m2k, Filter *filt, ToolMenuEntry *tme, m2k_iio_manager *m2k_man, QJSEngine *engine, QWidget *parent); ~SpectrumAnalyzer(); QPushButton *getRunButton(); @@ -252,8 +251,6 @@ private Q_SLOTS: private: libm2k::context::M2k *m_m2k_context; libm2k::analog::M2kAnalogIn *m_m2k_analogin; - libm2k::context::Generic *m_generic_context; - libm2k::analog::GenericAnalogIn *m_generic_analogin; Ui::SpectrumAnalyzer *ui; scopy::SmallOnOffSwitch *btnSyncPlotCursors; QHBoxLayout *horizontalLockLayout;