Skip to content

Commit

Permalink
iioutil: created IIOUnitsManager singleton
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Suciu <adrian.suciu@analog.com>
  • Loading branch information
adisuciu committed Jun 27, 2024
1 parent 22eb7a3 commit 2f19008
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 62 deletions.
5 changes: 2 additions & 3 deletions core/src/scopymainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@
#include "iioutil/connectionprovider.h"
#include "pluginbase/messagebroker.h"
#include "scopy-core_config.h"
#include "popupwidget.h"
#include "pluginbase/statusbarmanager.h"
#include "scopytitlemanager.h"
#include <common/scopyconfig.h>
#include <translationsrepository.h>
#include <libsigrokdecode/libsigrokdecode.h>
#include <functional>
#include <utility>
#include <stylehelper.h>
#include <scopymainwindow_api.h>
#include <QVersionNumber>
#include <iioutil/iiounits.h>

using namespace scopy;
using namespace scopy::gui;
Expand All @@ -58,6 +56,7 @@ ScopyMainWindow::ScopyMainWindow(QWidget *parent)
ScopyTitleManager::setGitHash(QString(SCOPY_VERSION_GIT));

StyleHelper::GetInstance()->initColorMap();
IIOUnitsManager::GetInstance();
setAttribute(Qt::WA_QuitOnClose, true);
initPythonWIN32();
initStatusBar();
Expand Down
48 changes: 48 additions & 0 deletions iioutil/include/iioutil/iiounits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef IIOUNITS_H
#define IIOUNITS_H

#include <QString>
#include <QMap>
#include <QObject>

#include "scopy-iioutil_export.h"
#include <iio.h>

namespace scopy {

typedef struct IIOUnit_
{
QString name;
QString symbol;
double scale = 1;
} IIOUnit;

class SCOPY_IIOUTIL_EXPORT IIOUnitsManager : public QObject
{
Q_OBJECT
protected:
IIOUnitsManager(QObject *parent = nullptr);
~IIOUnitsManager();

public:
// singleton
IIOUnitsManager(IIOUnitsManager &other) = delete;
void operator=(const IIOUnitsManager &) = delete;
static IIOUnitsManager *GetInstance();

static QMap<iio_chan_type, IIOUnit> iioChannelTypes();
static QMap<hwmon_chan_type, IIOUnit> hwmonChannelTypes();

private:
QMap<iio_chan_type, IIOUnit> _iioChannelTypes();
QMap<hwmon_chan_type, IIOUnit> _hwmonChannelTypes();

private:
static IIOUnitsManager *pinstance_;
QMap<iio_chan_type, IIOUnit> m_iioChannelTypes;
QMap<hwmon_chan_type, IIOUnit> m_hwmonChannelTypes;
};

}; // namespace scopy

#endif // IIOUNITS_H
56 changes: 56 additions & 0 deletions iioutil/src/iiounits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iiounits.h>
#include <QApplication>

using namespace scopy;

IIOUnitsManager *IIOUnitsManager::pinstance_{nullptr};

IIOUnitsManager::IIOUnitsManager(QObject *parent)
: QObject(parent)
{

m_iioChannelTypes = QMap<iio_chan_type, IIOUnit>({
{IIO_VOLTAGE, {"Voltage", "V", 0.001}},
{IIO_TEMP, {"Degree Celsius", "°C", 0.001}},
{IIO_CURRENT, {"Ampere", "A", 0.001}},
{IIO_PRESSURE, {"Pascal", "Pa", 1000}},
{IIO_ACCEL, {"Metre per second squared", "m/s2", 1}},
{IIO_ANGL_VEL, {"Radian per second", "rad/s", 1}},
{IIO_MAGN, {"Gauss", "Gs", 1}}
/// and others
});

m_hwmonChannelTypes = {
{HWMON_VOLTAGE, {"Voltage", "V", 0.001}}, {HWMON_TEMP, {"Degree Celsius", "°C", 0.001}},
{HWMON_CURRENT, {"Ampere", "A", 0.001}}, {HWMON_POWER, {"Watt", "W", 0.000001}},
{HWMON_ENERGY, {"Joule", "J", 0.000001}}, {HWMON_FAN, {"Revolution/Min", "RPM", 1}},
{HWMON_HUMIDITY, {"percent", "%", 0.001}}
/// and others
};
}

IIOUnitsManager *IIOUnitsManager::GetInstance()
{
if(pinstance_ == nullptr) {
pinstance_ = new IIOUnitsManager(QApplication::instance()); // singleton has the app as parent
}
return pinstance_;
}

QMap<iio_chan_type, IIOUnit> IIOUnitsManager::iioChannelTypes()
{
return IIOUnitsManager::GetInstance()->_iioChannelTypes();
}

QMap<hwmon_chan_type, IIOUnit> IIOUnitsManager::hwmonChannelTypes()
{
return IIOUnitsManager::GetInstance()->_hwmonChannelTypes();
}

IIOUnitsManager::~IIOUnitsManager() {}

QMap<iio_chan_type, IIOUnit> IIOUnitsManager::_iioChannelTypes() { return m_iioChannelTypes; }

QMap<hwmon_chan_type, IIOUnit> IIOUnitsManager::_hwmonChannelTypes() { return m_hwmonChannelTypes; }

#include "moc_iiounits.cpp"
2 changes: 1 addition & 1 deletion plugins/datalogger/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ target_include_directories(${PROJECT_NAME} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
target_include_directories(${PROJECT_NAME} PRIVATE ${INCLUDE_DIRECTORIES})

target_include_directories(${PROJECT_NAME} PUBLIC scopy-pluginbase scopy-gui)
target_include_directories(${PROJECT_NAME} PRIVATE ${IIO_INCLUDE_DIRS} scopy-gui scopy-iio-widgets)
target_include_directories(${PROJECT_NAME} PRIVATE ${IIO_INCLUDE_DIRS} scopy-gui scopy-iio-widgets scopy-iioutil)

target_link_libraries(
${PROJECT_NAME}
Expand Down
16 changes: 5 additions & 11 deletions plugins/datalogger/include/datalogger/dmm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@
#include "datamonitor/dmmdatamonitormodel.hpp"
#include "iio.h"
#include "scopy-datalogger_export.h"
#include "iioutil/iiounits.h"

namespace scopy {
namespace datamonitor {

struct DMMInfo
{
QString key;
QString key_symbol;
double umScale = 1;
};

class SCOPY_DATALOGGER_EXPORT DMM : public QObject
{
Q_OBJECT
Expand All @@ -30,12 +24,12 @@ class SCOPY_DATALOGGER_EXPORT DMM : public QObject
bool iioChannelHasAttribute(iio_channel *chn, std::string const &attr);
void generateDictionaries();

QMap<int, DMMInfo> iioDevices() const;
QMap<int, DMMInfo> hwmonDevices() const;
QMap<iio_chan_type, IIOUnit> iioDevices() const;
QMap<hwmon_chan_type, IIOUnit> hwmonDevices() const;

private:
QMap<int, DMMInfo> m_iioDevices;
QMap<int, DMMInfo> m_hwmonDevices;
QMap<iio_chan_type, IIOUnit> m_iioDevices;
QMap<hwmon_chan_type, IIOUnit> m_hwmonDevices;
};
} // namespace datamonitor
} // namespace scopy
Expand Down
55 changes: 8 additions & 47 deletions plugins/datalogger/src/dmm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ QList<DmmDataMonitorModel *> DMM::getDmmMonitors(iio_context *ctx)

int type = iio_channel_get_type(chn);
if(type != iio_chan_type::IIO_CHAN_TYPE_UNKNOWN) {
DMMInfo dmmInfo = m_iioDevices.value(type);
IIOUnit dmmInfo = m_iioDevices.value(static_cast<iio_chan_type>(type));

if(isHwmon(dev, chn)) {
dmmInfo = m_hwmonDevices.value(type);
dmmInfo = m_hwmonDevices.value(static_cast<hwmon_chan_type>(type));
}

channelModel->setUnitOfMeasure(
new UnitOfMeasurement(dmmInfo.key, dmmInfo.key_symbol));
new UnitOfMeasurement(dmmInfo.name, dmmInfo.symbol));

DMMReadStrategy *dmmReadStrategy = new DMMReadStrategy(dev, chn);
dmmReadStrategy->setUmScale(dmmInfo.umScale);
dmmReadStrategy->setUmScale(dmmInfo.scale);
channelModel->setReadStrategy(dmmReadStrategy);

if(iioChannelHasAttribute(chn, "offset")) {
Expand Down Expand Up @@ -103,51 +103,12 @@ bool DMM::iioChannelHasAttribute(iio_channel *chn, const std::string &attr)

void DMM::generateDictionaries()
{
DMMInfo hwmon_voltage = {"Voltage", "V", 0.001};
m_hwmonDevices.insert(hwmon_chan_type::HWMON_VOLTAGE, hwmon_voltage);

DMMInfo hwmon_temp = {"Degree Celsius", "°C", 0.001};
m_hwmonDevices.insert(hwmon_chan_type::HWMON_TEMP, hwmon_temp);

DMMInfo hwmon_current = {"Ampere", "A", 0.001};
m_hwmonDevices.insert(hwmon_chan_type::HWMON_CURRENT, hwmon_current);

DMMInfo hwmon_power = {"milliWatt", "mW", 0.001};
m_hwmonDevices.insert(hwmon_chan_type::HWMON_POWER, hwmon_power);

DMMInfo hwmon_energy = {"milliJoule", "mJ", 0.001};
m_hwmonDevices.insert(hwmon_chan_type::HWMON_ENERGY, hwmon_energy);

DMMInfo hwmon_fan = {"Revolution/Min", "RPM"};
m_hwmonDevices.insert(hwmon_chan_type::HWMON_FAN, hwmon_fan);

DMMInfo hwmon_humidity = {"milli-percent", "pcm"};
m_hwmonDevices.insert(hwmon_chan_type::HWMON_HUMIDITY, hwmon_humidity);

DMMInfo iio_voltage = {"Voltage", "V", 0.001};
m_iioDevices.insert(iio_chan_type::IIO_VOLTAGE, iio_voltage);

DMMInfo iio_temp = {"Degree Celsius", "°C", 0.001};
m_iioDevices.insert(iio_chan_type::IIO_TEMP, iio_temp);

DMMInfo iio_current = {"Ampere", "A", 0.001};
m_iioDevices.insert(iio_chan_type::IIO_CURRENT, iio_current);

DMMInfo iio_pressure = {"Pascal", "Pa", 1000};
m_iioDevices.insert(iio_chan_type::IIO_PRESSURE, iio_pressure);

DMMInfo iio_accel = {"Metre per second squared", "m/s²"};
m_iioDevices.insert(iio_chan_type::IIO_ACCEL, iio_accel);

DMMInfo iio_angl_vel = {"Radian per second", "rad/s"};
m_iioDevices.insert(iio_chan_type::IIO_ANGL_VEL, iio_angl_vel);

DMMInfo iio_magn = {"Gauss", "Gs"};
m_iioDevices.insert(iio_chan_type::IIO_MAGN, iio_magn);
m_hwmonDevices = IIOUnitsManager::hwmonChannelTypes();
m_iioDevices = IIOUnitsManager::iioChannelTypes();
}

QMap<int, DMMInfo> DMM::iioDevices() const { return m_iioDevices; }
QMap<iio_chan_type, IIOUnit> DMM::iioDevices() const { return m_iioDevices; }

QMap<int, DMMInfo> DMM::hwmonDevices() const { return m_hwmonDevices; }
QMap<hwmon_chan_type, IIOUnit> DMM::hwmonDevices() const { return m_hwmonDevices; }

#include "moc_dmm.cpp"

0 comments on commit 2f19008

Please sign in to comment.