diff --git a/Plx/include/LowLevel/HonaLowLevelAPI.h b/Plx/include/LowLevel/HonaLowLevelAPI.h index 77e9111..0ff54e2 100644 --- a/Plx/include/LowLevel/HonaLowLevelAPI.h +++ b/Plx/include/LowLevel/HonaLowLevelAPI.h @@ -5,48 +5,58 @@ #include "LowLevel/Setting/Setting.h" #include "HonaPacket.h" +#include "QWaitCondition" +#include "QMutex" +#include "include/LowLevel/Exception/HonaAlreadyStartedException.h" class HonaLowLevelAPI : public QObject { - Q_OBJECT + Q_OBJECT private: - bool _hsruIsStarted; - bool _hiruIsStarted; - HonaSetting _honaSetting; + bool _hsruIsStarted; + bool _hiruIsStarted; + HonaSetting _honaSetting; + QWaitCondition HsruReadDone, HsruUpdateAck, HsruStopEvent; + QMutex _mutex; + int _threadSforValue = 0; + bool _isReaderThreadFree = false; + bool _isUpdateThreadFree = false; + public: - explicit HonaLowLevelAPI(QObject* parent = nullptr); - bool isHsruStarted(); - bool isHiruStarted(); - void deviceReset(); - void setConfig(HonaSetting* honaSetting); - HonaSetting getConfig(); - QString getSwVersion(); - QString getDeviceId(); - void hsruStarte(); - void hiruStart(); - void init(); - - bool getHsruIsStarted() const; - void setHsruIsStarted(bool hsruIsStarted); - - bool getHiruIsStarted() const; - void setHiruIsStarted(bool hiruIsStarted); + explicit HonaLowLevelAPI(QObject* parent = nullptr); + bool isHsruStarted(); + bool isHiruStarted(); + void deviceReset(); + void setConfig(HonaSetting* honaSetting); + HonaSetting getConfig(); + QString getSwVersion(); + QString getDeviceId(); + void hsruStart(); + void hiruStart(); + void init(); + + bool getHsruIsStarted() const; + void setHsruIsStarted(bool hsruIsStarted); + + bool getHiruIsStarted() const; + void setHiruIsStarted(bool hiruIsStarted); private: - void writeSettingToRegisters(HonaSetting& honaSetting); - void setConfig(); - void hsruMainThread(); - void hiruReadThread(); - void hsruUpdateThread(); - int hiruGetDOA(); + void writeSettingToRegisters(HonaSetting& honaSetting); + void setConfig(); + void hsruMainThread(); + void hsruReadThread(); + void hsruUpdateThread(); + int hiruGetDOA(); + signals: - void signalToTop(QList honaPacketList, int i, int j); - //uncrustify off + void signalToTop(QList honaPacketList, int i, int j); + //uncrustify off public slots: - //uncrustify on + //uncrustify on }; #endif //HONALOWLEVELAPI_H diff --git a/Plx/include/LowLevel/Utils/Utils.h b/Plx/include/LowLevel/Utils/Utils.h index 7bbb95d..d67f89e 100644 --- a/Plx/include/LowLevel/Utils/Utils.h +++ b/Plx/include/LowLevel/Utils/Utils.h @@ -3,14 +3,13 @@ /*************************************************************************************************/ -typedef enum _APIRESULT + enum ApiResult { success, alreadyStarted, busy, error - -}apiResult; +}; /*************************************************************************************************/ typedef enum _DRXDEVICEIFCHANNEL { diff --git a/Plx/src/LowLevel/HonaLowLevelAPI.cpp b/Plx/src/LowLevel/HonaLowLevelAPI.cpp index 74cd9ba..c875df7 100644 --- a/Plx/src/LowLevel/HonaLowLevelAPI.cpp +++ b/Plx/src/LowLevel/HonaLowLevelAPI.cpp @@ -1,29 +1,31 @@ #include "../../include/LowLevel/HonaLowLevelAPI.h" +#include #include "qdebug.h" #include "QTime" +#include "QtConcurrent/QtConcurrent" bool HonaLowLevelAPI::getHsruIsStarted() const { - return _hsruIsStarted; + return _hsruIsStarted; } /*************************************************************************************************/ void HonaLowLevelAPI::setHsruIsStarted(bool hsruIsStarted) { - _hsruIsStarted = hsruIsStarted; + _hsruIsStarted = hsruIsStarted; } /*************************************************************************************************/ bool HonaLowLevelAPI::getHiruIsStarted() const { - return _hiruIsStarted; + return _hiruIsStarted; } /*************************************************************************************************/ void HonaLowLevelAPI::setHiruIsStarted(bool hiruIsStarted) { - _hiruIsStarted = hiruIsStarted; + _hiruIsStarted = hiruIsStarted; } /*************************************************************************************************/ @@ -34,50 +36,55 @@ HonaLowLevelAPI::HonaLowLevelAPI(QObject* parent) : QObject(parent) /*************************************************************************************************/ bool HonaLowLevelAPI::isHsruStarted() { - return getHsruIsStarted(); + return getHsruIsStarted(); } /*************************************************************************************************/ bool HonaLowLevelAPI::isHiruStarted() { - return getHiruIsStarted(); + return getHiruIsStarted(); } /*************************************************************************************************/ void HonaLowLevelAPI::deviceReset() { - /* - QTime t; - t.elapsed() + ApiResult status; + if(ApiResult::success) + { - PLX_STATUS status = base.DeviceReset(); - qDebug() << "Board Reseted, status: " + status.ToString()); + } + /* + QTime t; + t.elapsed() - if (status == PLX_STATUS.ApiSuccess) - { - toaStartBoard = (ulong) (DateTime.Now.Ticks); - return ApiResult.Success; - } - else - return ApiResult.Error; */ + PLX_STATUS status = base.DeviceReset(); + qDebug() << "Board Reseted, status: " + status.ToString()); + + if (status == PLX_STATUS.ApiSuccess) + { + toaStartBoard = (ulong) (DateTime.Now.Ticks); + return ApiResult.Success; + } + else + return ApiResult.Error; */ } /*************************************************************************************************/ void HonaLowLevelAPI::setConfig(HonaSetting* honaSetting) { - //Initial State Checking - /* - HonaRegisterBuffer = new uint[8]; // Mode4 :: changed from 5 to 8 by H.H - DeviceReadRegisters(0x10000000, ref HonaRegisterBuffer); + //Initial State Checking + /* + HonaRegisterBuffer = new uint[8]; // Mode4 :: changed from 5 to 8 by H.H + DeviceReadRegisters(0x10000000, ref HonaRegisterBuffer); - _honaSetting = Settings; - HonaFillRegisters(Settings); - DeviceWriteRegisters(0x10000000, HonaRegisterBuffer); + _honaSetting = Settings; + HonaFillRegisters(Settings); + DeviceWriteRegisters(0x10000000, HonaRegisterBuffer); - HonaRegisterBuffer = new uint[8]; // Mode4 :: changed from 5 to 8 by H.H - DeviceReadRegisters(0x10000000, ref HonaRegisterBuffer); + HonaRegisterBuffer = new uint[8]; // Mode4 :: changed from 5 to 8 by H.H + DeviceReadRegisters(0x10000000, ref HonaRegisterBuffer); - return ApiResult.Success; */ + return ApiResult.Success; */ } /*************************************************************************************************/ @@ -97,13 +104,17 @@ QString HonaLowLevelAPI::getDeviceId() } /*************************************************************************************************/ -void HonaLowLevelAPI::hsruStarte() +void HonaLowLevelAPI::hsruStart() { } /*************************************************************************************************/ void HonaLowLevelAPI::hiruStart() { + if (isHiruStarted()) + //exceptiom + + QtConcurrent::run(this,&HonaLowLevelAPI::hsruMainThread); } /*************************************************************************************************/ @@ -114,6 +125,7 @@ void HonaLowLevelAPI::init() /*************************************************************************************************/ void HonaLowLevelAPI::writeSettingToRegisters(HonaSetting& honaSetting) { + } /*************************************************************************************************/ @@ -124,16 +136,48 @@ void HonaLowLevelAPI::setConfig() /*************************************************************************************************/ void HonaLowLevelAPI::hsruMainThread() { + QtConcurrent::run(this, &HonaLowLevelAPI::hsruReadThread); + QtConcurrent::run(this, &HonaLowLevelAPI::hsruUpdateThread); + + _mutex.lock(); + HsruStopEvent.wait(&_mutex); + _mutex.unlock(); + + _isReaderThreadFree = true; + _isUpdateThreadFree = true; } /*************************************************************************************************/ -void HonaLowLevelAPI::hiruReadThread() +void HonaLowLevelAPI::hsruReadThread() { + while (true) + { + if (_isReaderThreadFree) + break; + //dosth + _mutex.lock(); + HsruReadDone.wakeAll(); + HsruUpdateAck.wait(&_mutex); + _mutex.unlock(); + + } } /*************************************************************************************************/ void HonaLowLevelAPI::hsruUpdateThread() { + + while (true) + { + _mutex.lock(); + HsruReadDone.wait(&_mutex); + _mutex.unlock(); + if (_isUpdateThreadFree) + break; + //dosth + HsruUpdateAck.wakeAll(); + + } } /*************************************************************************************************/