Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

m2kplugin: pass M2k* objects in the constructor rather than iio_context #1769

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions plugins/m2k/include/m2k/m2kcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -44,7 +44,6 @@ public Q_SLOTS:
private:
M2kReadTemperatureTask *tempTask;
M2kIdentifyTask *identifyTask;
iio_context *m_iioctx;
QString uri;
libm2k::context::M2k *m_m2k;

Expand Down
2 changes: 2 additions & 0 deletions plugins/m2k/include/m2k/m2kplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 2 additions & 5 deletions plugins/m2k/src/m2kcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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;
}

Expand Down
36 changes: 23 additions & 13 deletions plugins/m2k/src/m2kplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,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);
}
Expand All @@ -386,18 +395,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");
Expand All @@ -410,23 +420,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<logic::LogicAnalyzer *>(tools["m2klogic"]);
Expand All @@ -436,7 +446,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<SignalGenerator *>(siggenTme->tool())->getRunButton(), &QPushButton::toggled, this,
[=](bool en) {
Expand Down
29 changes: 6 additions & 23 deletions plugins/m2k/src/old/calibration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@

#include <errno.h>
#include <libm2k/m2kexceptions.hpp>
#include <libm2k/analog/dmm.hpp>

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");
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
4 changes: 1 addition & 3 deletions plugins/m2k/src/old/calibration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

extern "C"
{
struct iio_context;
struct iio_device;
struct iio_channel;
struct iio_buffer;
Expand Down Expand Up @@ -64,7 +63,7 @@ class Calibration
HIGH
};

Calibration(struct iio_context *ctx);
Calibration(libm2k::context::M2k *m2k);
~Calibration();

bool initialize();
Expand All @@ -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;
};
Expand Down
4 changes: 2 additions & 2 deletions plugins/m2k/src/old/digitalchannel_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion plugins/m2k/src/old/digitalchannel_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions plugins/m2k/src/old/digitalio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions plugins/m2k/src/old/digitalio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions plugins/m2k/src/old/dmm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<signal_sample>())
, 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)
Expand Down
2 changes: 1 addition & 1 deletion plugins/m2k/src/old/dmm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
15 changes: 6 additions & 9 deletions plugins/m2k/src/old/iio_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -112,7 +109,7 @@ std::shared_ptr<iio_manager> m2k_iio_manager::has_instance(QString _dev)
return nullptr;
}

std::shared_ptr<iio_manager> m2k_iio_manager::get_instance(struct iio_context *ctx, QString _dev,
std::shared_ptr<iio_manager> m2k_iio_manager::get_instance(libm2k::context::M2k *ctx, QString _dev,
unsigned long buffer_size)
{
auto instance = has_instance(_dev);
Expand Down
4 changes: 2 additions & 2 deletions plugins/m2k/src/old/iio_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<iio_manager> get_instance(struct iio_context *ctx, QString dev,
std::shared_ptr<iio_manager> get_instance(libm2k::context::M2k *ctx, QString dev,
unsigned long buffer_size = IIO_BUFFER_SIZE);

private:
Expand Down
6 changes: 3 additions & 3 deletions plugins/m2k/src/old/logicanalyzer/logic_analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion plugins/m2k/src/old/logicanalyzer/logic_analyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions plugins/m2k/src/old/m2ktool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions plugins/m2k/src/old/m2ktool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -76,7 +75,6 @@ public Q_SLOTS:
virtual void readPreferences();

protected:
struct iio_context *ctx;
ApiObject *api;
QString name;
bool saveOnExit;
Expand Down
Loading
Loading