From 5fd29b1bc2b4e05a34917a03ce82e3e0ac905ce2 Mon Sep 17 00:00:00 2001 From: Alexandru Lie Date: Tue, 3 Sep 2024 12:40:58 +0300 Subject: [PATCH] plugins: Added register map API Signed-off-by: Alexandru Lie --- plugins/regmap/include/regmap/regmapplugin.h | 4 + plugins/regmap/src/deviceregistermap.cpp | 2 + plugins/regmap/src/deviceregistermap.hpp | 2 + .../regmap/src/registermapsettingsmenu.hpp | 2 +- plugins/regmap/src/registermaptool.hpp | 1 + plugins/regmap/src/regmap_api.cpp | 100 ++++++++++++++++++ plugins/regmap/src/regmap_api.h | 40 +++++++ plugins/regmap/src/regmapplugin.cpp | 10 ++ tests/CMakeLists.txt | 8 ++ 9 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 plugins/regmap/src/regmap_api.cpp create mode 100644 plugins/regmap/src/regmap_api.h diff --git a/plugins/regmap/include/regmap/regmapplugin.h b/plugins/regmap/include/regmap/regmapplugin.h index cd3329c56c..8d8b4800ce 100644 --- a/plugins/regmap/include/regmap/regmapplugin.h +++ b/plugins/regmap/include/regmap/regmapplugin.h @@ -24,9 +24,11 @@ namespace regmap { class RegisterMapTool; class JsonFormatedElement; +class RegMap_API; class SCOPY_REGMAP_EXPORT RegmapPlugin : public QObject, public PluginBase { + friend class RegMap_API; Q_OBJECT SCOPY_PLUGIN; @@ -59,6 +61,8 @@ public Q_SLOTS: struct iio_device *getIioDevice(iio_context *ctx, const char *dev_name); bool isBufferCapable(iio_device *dev); RegisterMapTool *registerMapTool; + void InitApi(); + RegMap_API *api; }; } // namespace regmap } // namespace scopy diff --git a/plugins/regmap/src/deviceregistermap.cpp b/plugins/regmap/src/deviceregistermap.cpp index 97d80b8d03..96a33149eb 100644 --- a/plugins/regmap/src/deviceregistermap.cpp +++ b/plugins/regmap/src/deviceregistermap.cpp @@ -220,6 +220,8 @@ bool DeviceRegisterMap::hasTemplate() return false; } +bool DeviceRegisterMap::getAutoread() { return autoread; } + void DeviceRegisterMap::initSettings() { QObject::connect(this, &DeviceRegisterMap::requestRead, registerMapValues, &RegisterMapValues::requestRead); diff --git a/plugins/regmap/src/deviceregistermap.hpp b/plugins/regmap/src/deviceregistermap.hpp index 9b004fab3b..c44ddf6c5c 100644 --- a/plugins/regmap/src/deviceregistermap.hpp +++ b/plugins/regmap/src/deviceregistermap.hpp @@ -24,6 +24,7 @@ class RegisterMapTable; class SCOPY_REGMAP_EXPORT DeviceRegisterMap : public QWidget { + friend class RegMap_API; Q_OBJECT public: explicit DeviceRegisterMap(RegisterMapTemplate *registerMapTemplate = nullptr, @@ -34,6 +35,7 @@ class SCOPY_REGMAP_EXPORT DeviceRegisterMap : public QWidget void toggleAutoread(bool toggled); void applyFilters(QString filter); bool hasTemplate(); + bool getAutoread(); private: ToolTemplate *tool; diff --git a/plugins/regmap/src/registermapsettingsmenu.hpp b/plugins/regmap/src/registermapsettingsmenu.hpp index 0454062dc1..9e241400da 100644 --- a/plugins/regmap/src/registermapsettingsmenu.hpp +++ b/plugins/regmap/src/registermapsettingsmenu.hpp @@ -15,7 +15,7 @@ namespace regmap { class RegisterMapSettingsMenu : public QWidget { friend class RegmapStyleHelper; - + friend class RegMap_API; Q_OBJECT public: diff --git a/plugins/regmap/src/registermaptool.hpp b/plugins/regmap/src/registermaptool.hpp index 0d119ea9a8..da53182c44 100644 --- a/plugins/regmap/src/registermaptool.hpp +++ b/plugins/regmap/src/registermaptool.hpp @@ -23,6 +23,7 @@ class SearchBarWidget; class SCOPY_REGMAP_EXPORT RegisterMapTool : public QWidget { + friend class RegMap_API; Q_OBJECT public: explicit RegisterMapTool(QWidget *parent = nullptr); diff --git a/plugins/regmap/src/regmap_api.cpp b/plugins/regmap/src/regmap_api.cpp new file mode 100644 index 0000000000..6f27adb1b6 --- /dev/null +++ b/plugins/regmap/src/regmap_api.cpp @@ -0,0 +1,100 @@ +#include "regmap_api.h" + +using namespace scopy::regmap; + +Q_LOGGING_CATEGORY(CAT_REGMAP_API, "RegMap_API") + +RegMap_API::RegMap_API(RegmapPlugin *regMapPlugin) + : ApiObject() + , m_regMapPlugin(regMapPlugin) +{} + +RegMap_API::~RegMap_API() {} + +void RegMap_API::write(QString addr, QString val) +{ + DeviceRegisterMap *devRegMap = getActiveDevRegMap(); + Q_EMIT devRegMap->registerMapValues->requestWrite(addr.toUInt(nullptr, 16), val.toUInt(nullptr, 16)); +} + +QStringList RegMap_API::getAvailableDevicesName() +{ + QStringList devicesName; + auto devices = m_regMapPlugin->registerMapTool->deviceList; + for(auto dev = devices->begin(); dev != devices->end(); dev++) { + devicesName.append(dev.key()); + } + return devicesName; +} + +bool RegMap_API::setDevice(QString device) +{ + m_regMapPlugin->registerMapTool->updateActiveRegisterMap(device); + QString currentRegMap = m_regMapPlugin->registerMapTool->activeRegisterMap; + if(currentRegMap == device) { + return true; + } + qWarning(CAT_REGMAP_API) << "Device " << device << " was not set"; + return false; +} + +QStringList RegMap_API::search(QString searchParam) +{ + QList resultIndexes; + DeviceRegisterMap *devRegMap = getActiveDevRegMap(); + resultIndexes = Search::searchForRegisters(devRegMap->registerMapTemplate->getRegisterList(), searchParam); + + QStringList resultList; + for(int i = 0; i < resultIndexes.size(); i++) { + RegisterModel *model = devRegMap->registerMapTemplate->getRegisterTemplate(resultIndexes[i]); + resultList.append(model->getName()); + } + return resultList; +} + +QStringList RegMap_API::readInterval(QString startAddr, QString stopAddr) +{ + DeviceRegisterMap *devRegMap = getActiveDevRegMap(); + uint32_t start = startAddr.toUInt(nullptr, 16); + uint32_t stop = stopAddr.toUInt(nullptr, 16); + QStringList readValues; + for(int i = start; i < stop; i++) { + Q_EMIT devRegMap->registerMapValues->requestRead(i); + } + QMap *readFinished = devRegMap->registerMapValues->getRegisterReadValues(); + + for(auto it = readFinished->begin(); it != readFinished->end(); it++) { + readValues.append(QString::number(it.value(), 16)); + } + return readValues; +} + +bool RegMap_API::enableAutoread(bool enable) +{ + DeviceRegisterMap *devRegMap = getActiveDevRegMap(); + devRegMap->toggleAutoread(enable); + if(devRegMap->getAutoread() == enable) { + return true; + } + qWarning(CAT_REGMAP_API) << "Autoread was not changed to " << enable; + return false; +} + +bool RegMap_API::isAutoreadEnabled() +{ + DeviceRegisterMap *devRegMap = getActiveDevRegMap(); + return devRegMap->getAutoread(); +} + +void RegMap_API::registerDump(QString filePath) +{ + DeviceRegisterMap *devRegMap = getActiveDevRegMap(); + Q_EMIT devRegMap->registerMapValues->registerDump(filePath); +} + +void RegMap_API::writeFromFile(QString filePath) { DeviceRegisterMap *devRegMap = getActiveDevRegMap(); } + +DeviceRegisterMap *RegMap_API::getActiveDevRegMap() +{ + return m_regMapPlugin->registerMapTool->deviceList->value(m_regMapPlugin->registerMapTool->activeRegisterMap); +} diff --git a/plugins/regmap/src/regmap_api.h b/plugins/regmap/src/regmap_api.h new file mode 100644 index 0000000000..19b06716f3 --- /dev/null +++ b/plugins/regmap/src/regmap_api.h @@ -0,0 +1,40 @@ +#ifndef REGMAP_API_H +#define REGMAP_API_H + +#include "scopy-regmap_export.h" + +#include +#include +#include +#include +#include +#include "search.hpp" + +namespace scopy::regmap { + +class SCOPY_REGMAP_EXPORT RegMap_API : public ApiObject +{ + + Q_OBJECT + +public: + explicit RegMap_API(RegmapPlugin *regMapPlugin); + ~RegMap_API(); + + Q_INVOKABLE void write(QString addr, QString val); + Q_INVOKABLE QStringList getAvailableDevicesName(); + Q_INVOKABLE bool setDevice(QString device); + Q_INVOKABLE QStringList search(QString searchParam); + Q_INVOKABLE QStringList readInterval(QString startAddr, QString stopAddr); + Q_INVOKABLE bool enableAutoread(bool enable); + Q_INVOKABLE bool isAutoreadEnabled(); + Q_INVOKABLE void registerDump(QString filePath); + Q_INVOKABLE void writeFromFile(QString filePath); + Q_INVOKABLE QString readRegister(QSttring addr); + +private: + RegmapPlugin *m_regMapPlugin; + DeviceRegisterMap *getActiveDevRegMap(); +}; +} // namespace scopy::regmap +#endif // REGMAP_API_H diff --git a/plugins/regmap/src/regmapplugin.cpp b/plugins/regmap/src/regmapplugin.cpp index 6f8e8c529c..8a3ef99202 100644 --- a/plugins/regmap/src/regmapplugin.cpp +++ b/plugins/regmap/src/regmapplugin.cpp @@ -27,12 +27,14 @@ #include #include #include "logging_categories.h" +#include #include "iioutil/connectionprovider.h" #include "jsonformatedelement.hpp" #include "scopy-regmap_config.h" #include "utils.hpp" #include "utils.hpp" +#include #if defined __APPLE__ #include #endif @@ -185,6 +187,7 @@ bool RegmapPlugin::onConnect() m_toolList[0]->setEnabled(true); m_toolList[0]->setTool(m_registerMapWidget); + InitApi(); return true; } @@ -275,4 +278,11 @@ bool RegmapPlugin::isBufferCapable(iio_device *dev) return false; } +void RegmapPlugin::InitApi() +{ + api = new RegMap_API(this); + ScopyJS *js = ScopyJS::GetInstance(); + api->setObjectName("regmap"); + js->registerApi(api); +} #include "moc_regmapplugin.cpp" diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a0d17d3732..ab9c088744 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -23,4 +23,12 @@ if(ENABLE_AUTOMATED_TESTS) ) endif() + # REGMAP + if(ENABLE_PLUGIN_REGMAP) + add_test(NAME "RegMapJSTests" + COMMAND bash ${CMAKE_SOURCE_DIR}/js/test.sh "${CMAKE_SOURCE_DIR}/resources/emuXml/" + "${CMAKE_SOURCE_DIR}/plugins/regmap/js/regMapFunctions.js" + ) + endif() + endif()