From 584ea846b9c9dd0e14a768f942bca4415d329f94 Mon Sep 17 00:00:00 2001 From: Mandar1jn Date: Fri, 9 Sep 2022 17:51:42 +0200 Subject: [PATCH] Added multi-threding for unverified commands --- PortalWorker.cpp | 26 ++++++++++++++++++++++++ PortalWorker.h | 39 ++++++++++++++++++++++++++++++++++++ RWCommand.cpp | 14 ------------- RWCommand.h | 1 - SkylandersEditor.pro | 4 +++- portal/Portal.cpp | 47 ++++++++++++++++++++++++++------------------ portal/Portal.h | 5 +++++ 7 files changed, 101 insertions(+), 35 deletions(-) create mode 100644 PortalWorker.cpp create mode 100644 PortalWorker.h diff --git a/PortalWorker.cpp b/PortalWorker.cpp new file mode 100644 index 0000000..a5b4d3f --- /dev/null +++ b/PortalWorker.cpp @@ -0,0 +1,26 @@ +#include "PortalWorker.h" + +#include "portal/Portal.h" + +void PortalWorker::Run() +{ + Portal* portal = Portal::GetPortal(); + portal->portalMutex.lock(); + + if(!portal->connected) + goto end; + + if(!unverifiedCommands.empty()) + { + RWCommand* command = unverifiedCommands.front(); + + portal->Write(command); + + delete command; + + unverifiedCommands.erase(unverifiedCommands.begin()); + } + + end: + portal->portalMutex.unlock(); +} diff --git a/PortalWorker.h b/PortalWorker.h new file mode 100644 index 0000000..7618b61 --- /dev/null +++ b/PortalWorker.h @@ -0,0 +1,39 @@ +#ifndef PORTALWORKER_H +#define PORTALWORKER_H + +#include +#include + +#include + +#include + +#include "RWCommand.h" + +class PortalWorker : public QThread +{ + Q_OBJECT +public: + void run() override + { + unverifiedCommands = std::vector(); + + while(!isInterruptionRequested()) + { + Run(); + } + } + + void Run(); + + void AddUnverifiedCommand(RWCommand* command) + { + unverifiedCommands.push_back(command); + } + +private: + + std::vector unverifiedCommands; +}; + +#endif // PORTALWORKER_H diff --git a/RWCommand.cpp b/RWCommand.cpp index 807a8a3..f7bb08c 100644 --- a/RWCommand.cpp +++ b/RWCommand.cpp @@ -18,20 +18,6 @@ RWCommand::~RWCommand() { } -bool RWCommand::SendUnverified() -{ - Portal* portal = Portal::GetPortal(); - - if(!portal->connected) - { - return false; - } - - portal->Write(this); - - return true; -} - bool RWCommand::SendVerified(char expectedChar, unsigned int attempts) { Portal* portal = Portal::GetPortal(); diff --git a/RWCommand.h b/RWCommand.h index 2384e7e..ccbaf66 100644 --- a/RWCommand.h +++ b/RWCommand.h @@ -11,7 +11,6 @@ class RWCommand RWCommand(); ~RWCommand(); - bool SendUnverified(); bool SendVerified(char expectedChar, unsigned int attempts); }; diff --git a/SkylandersEditor.pro b/SkylandersEditor.pro index 72d4895..b4831f0 100644 --- a/SkylandersEditor.pro +++ b/SkylandersEditor.pro @@ -10,6 +10,7 @@ CONFIG += c++17 SOURCES += \ PortalWidget.cpp \ + PortalWorker.cpp \ RWCommand.cpp \ main.cpp \ MainWindow.cpp \ @@ -20,8 +21,9 @@ HEADERS += \ MainWindow.h \ MenuBar.h \ PortalWidget.h \ + PortalWorker.h \ RWCommand.h \ - portal/Portal.h + portal/Portal.h \ include("./projects/hidapi.pro") diff --git a/portal/Portal.cpp b/portal/Portal.cpp index 82f210a..630f0c0 100644 --- a/portal/Portal.cpp +++ b/portal/Portal.cpp @@ -13,6 +13,8 @@ Portal::Portal(QObject *parent) connected = false; features = NONE; + worker = new PortalWorker; + Connect(); } @@ -25,6 +27,9 @@ Portal::~Portal() } hid_close(handle); + + delete worker; + worker = NULL; } Portal* Portal::GetPortal() @@ -68,6 +73,8 @@ void Portal::Connect() connected = true; + worker->start(); + Ready(); Activate(); @@ -111,26 +118,26 @@ void Portal::Deactivate() { if (!connected) return; - RWCommand deactivateCommand = RWCommand(); + RWCommand* deactivateCommand = new RWCommand(); - deactivateCommand.writeBuffer[1] = 'A'; - deactivateCommand.writeBuffer[2] = 0x00; + deactivateCommand->writeBuffer[1] = 'A'; + deactivateCommand->writeBuffer[2] = 0x00; - deactivateCommand.SendUnverified(); + worker->AddUnverifiedCommand(deactivateCommand); } void Portal::SetColor(int r, int g, int b) { if (!connected) return; - RWCommand colorCommand = RWCommand(); + RWCommand* colorCommand = new RWCommand(); - colorCommand.writeBuffer[1] = 'C'; - colorCommand.writeBuffer[2] = r; - colorCommand.writeBuffer[3] = g; - colorCommand.writeBuffer[4] = b; + colorCommand->writeBuffer[1] = 'C'; + colorCommand->writeBuffer[2] = r; + colorCommand->writeBuffer[3] = g; + colorCommand->writeBuffer[4] = b; - colorCommand.SendUnverified(); + worker->AddUnverifiedCommand(colorCommand); } /* @@ -150,17 +157,17 @@ void Portal::SetColorAlternative(int side, int r, int g, int b, int u, int durat { if (!connected) return; - RWCommand colorCommand = RWCommand(); + RWCommand* colorCommand = new RWCommand(); - colorCommand.writeBuffer[1] = 'J'; - colorCommand.writeBuffer[2] = side; - colorCommand.writeBuffer[3] = r; - colorCommand.writeBuffer[4] = g; - colorCommand.writeBuffer[5] = b; - colorCommand.writeBuffer[6] = u; - colorCommand.writeBuffer[7] = duration; + colorCommand->writeBuffer[1] = 'J'; + colorCommand->writeBuffer[2] = side; + colorCommand->writeBuffer[3] = r; + colorCommand->writeBuffer[4] = g; + colorCommand->writeBuffer[5] = b; + colorCommand->writeBuffer[6] = u; + colorCommand->writeBuffer[7] = duration; - colorCommand.SendUnverified(); + worker->AddUnverifiedCommand(colorCommand); } @@ -231,6 +238,8 @@ PortalStatus* Portal::GetStatus() void Portal::Disconnect(bool allowWrite) { + worker->requestInterruption(); + features = NONE; if(allowWrite) diff --git a/portal/Portal.h b/portal/Portal.h index 5dd009d..2432aa6 100644 --- a/portal/Portal.h +++ b/portal/Portal.h @@ -4,8 +4,10 @@ #include #include #include +#include #include "RWCommand.h" +#include "PortalWorker.h" enum PortalFeatures : int { SIDED_COLOR = (1 << 1), @@ -44,6 +46,8 @@ class Portal : public QObject hid_device* handle; + QMutex portalMutex; + signals: void StateChanged(); @@ -51,6 +55,7 @@ class Portal : public QObject void SetFeatures(); static QPointer self; + PortalWorker* worker; };