Browse Source

process of recive newData and some test added

mode-button
nasi 3 years ago
parent
commit
1af6a8cee0
  1. 100
      logic/include/model/ButtonHelper.h
  2. 22
      logic/include/model/Console.h
  3. 2
      logic/include/model/DataSender.h
  4. 25
      logic/include/model/Led.h
  5. 8
      logic/include/model/PushButton.h
  6. 6
      logic/include/model/RotaryButton.h
  7. 143
      logic/src/model/Console.cpp
  8. 14
      logic/src/model/Led.cpp
  9. 16
      logic/src/model/PushButton.cpp
  10. 14
      logic/src/model/RotaryButton.cpp
  11. 2
      network/UdpDataSender.cpp
  12. 2
      network/UdpDataSender.h
  13. 2
      test/TestDataSender.h
  14. 216
      test/tst_console.cpp

100
logic/include/model/ButtonHelper.h

@ -2,64 +2,80 @@
#define BUTTONHELPER_H
#define PUSH_BUTTON(CAPITAL_NAME, SMALL_NAME, FUNC_CODE, LED_FUNC_CODE) \
private: \
PushButton _ ## SMALL_NAME{FUNC_CODE, LED_FUNC_CODE}; \
public: \
void press ## CAPITAL_NAME() \
{ \
auto arr = _ ## SMALL_NAME.press(); \
_dataSender->send(arr); \
} \
void release ## CAPITAL_NAME() \
{ \
auto arr = _ ## SMALL_NAME.release(); \
_dataSender->send(arr); \
} \
signals: \
void SMALL_NAME ## LedChanged(int value)
private: \
PushButton _ ## SMALL_NAME{FUNC_CODE, LED_FUNC_CODE}; \
void init ## CAPITAL_NAME() \
{ \
connect(this, &Console::dataReady, _ ## SMALL_NAME.getLed(), &Led::newData);\
connect(_ ## SMALL_NAME.getLed(), SIGNAL(ledChanged(char)), this, SIGNAL(SMALL_NAME ## LedChanged(char)));\
} \
public: \
void press ## CAPITAL_NAME() \
{ \
auto arr = _ ## SMALL_NAME.press(); \
_dataSender->send(arr); \
} \
void release ## CAPITAL_NAME() \
{ \
auto arr = _ ## SMALL_NAME.release(); \
_dataSender->send(arr); \
} \
Q_SIGNAL void SMALL_NAME ## LedChanged(char value)
/*************************************************************************************************/
#define PUSH_BUTTON_NO_LED(CAPITAL_NAME, SMALL_NAME, FUNC_CODE) \
private: \
PushButton _ ## SMALL_NAME{FUNC_CODE}; \
public: \
void press ## CAPITAL_NAME() \
{ \
auto arr = _ ## SMALL_NAME.press(); \
_dataSender->send(arr); \
} \
void release ## CAPITAL_NAME() \
{ \
auto arr = _ ## SMALL_NAME.release(); \
_dataSender->send(arr); \
} \
signals: \
void SMALL_NAME ## LedChanged(int value)
private: \
PushButton _ ## SMALL_NAME{FUNC_CODE}; \
void init ## CAPITAL_NAME() \
{ \
connect(this, &Console::dataReady, _ ## SMALL_NAME.getLed(), &Led::newData);\
connect(_ ## SMALL_NAME.getLed(), SIGNAL(ledChanged(char)), this, SIGNAL(SMALL_NAME ## LedChanged(char)));\
} \
public: \
void press ## CAPITAL_NAME() \
{ \
auto arr = _ ## SMALL_NAME.press(); \
_dataSender->send(arr); \
} \
void release ## CAPITAL_NAME() \
{ \
auto arr = _ ## SMALL_NAME.release(); \
_dataSender->send(arr); \
} \
Q_SIGNAL void SMALL_NAME ## LedChanged(char value)
/*************************************************************************************************/
#define ROTARY_BUTTON(CAPITAL_NAME, SMALL_NAME, FUNC_CODE, LED_FUNC_CODE) \
private: \
private: \
RotaryButton _ ## SMALL_NAME{FUNC_CODE, LED_FUNC_CODE}; \
public: \
void init ## CAPITAL_NAME() \
{ \
connect(this, &Console::dataReady, _ ## SMALL_NAME.getLed(), &Led::newData);\
connect(_ ## SMALL_NAME.getLed(), SIGNAL(ledChanged(char)), this, SIGNAL(SMALL_NAME ## LedChanged(char)));\
} \
public: \
void rotate ## CAPITAL_NAME(int value) \
{ \
auto arr = _ ## SMALL_NAME.rotate(value); \
_dataSender->send(arr); \
auto arr = _ ## SMALL_NAME.rotate(value); \
_dataSender->send(arr); \
} \
signals: \
void SMALL_NAME ## LedChanged(int value)
Q_SIGNAL void SMALL_NAME ## LedChanged(char value)
/*************************************************************************************************/
#define ROTARY_BUTTON_NO_LED(CAPITAL_NAME, SMALL_NAME, FUNC_CODE) \
private: \
private: \
RotaryButton _ ## SMALL_NAME{FUNC_CODE}; \
public: \
void init ## CAPITAL_NAME() \
{ \
connect(this, &Console::dataReady, _ ## SMALL_NAME.getLed(), &Led::newData);\
connect(_ ## SMALL_NAME.getLed(), SIGNAL(ledChanged(char)), this, SIGNAL(SMALL_NAME ## LedChanged(char)));\
} \
public: \
void rotate ## CAPITAL_NAME(int value) \
{ \
auto arr = _ ## SMALL_NAME.rotate(value); \
_dataSender->send(arr); \
auto arr = _ ## SMALL_NAME.rotate(value); \
_dataSender->send(arr); \
} \
signals: \
void SMALL_NAME ## LedChanged(int value)
Q_SIGNAL void SMALL_NAME ## LedChanged(char value)
#endif //BUTTONHELPER_H

22
logic/include/model/Console.h

@ -1,6 +1,15 @@
#ifndef CONSOLE_H
#define CONSOLE_H
#define DataLength 8
#define PacketAppDirection 0x01
#define EchoDataLength 0x02
#define EchoType 0x06
#define EchoFunctionCode 0xA4
#define ZeroValue 0x00
#include <QObject>
#include "ButtonHelper.h"
@ -28,7 +37,7 @@ class Console : public QObject
PUSH_BUTTON(Exit, exit, EXIT_FC, static_cast<char>(EXIT_LED_FC));
PUSH_BUTTON(Freeze, freeze, FREEZE_FC, static_cast<char>(FREEZE_LED_FC));
PUSH_BUTTON(Pointer, pointer, POINTER_FC, static_cast<char>(POINTER_LED_FC));
PUSH_BUTTON(Auto, auto, AUTOSET_FC, static_cast<char>(AUTOSET_LED_FC));
PUSH_BUTTON(AutoSet, autoSet, AUTOSET_FC, static_cast<char>(AUTOSET_LED_FC));
PUSH_BUTTON(Abc, abc, ABC_FC, static_cast<char>(ABC_LED_FC));
PUSH_BUTTON(FourD, fourD, FOUR_D_FC, static_cast<char>(FOUR_D_LED_FC));
PUSH_BUTTON(Clear, clear, CLEAR_FC, static_cast<char>(CLEAR_LED_FC));
@ -101,16 +110,19 @@ class Console : public QObject
private:
DataSender* _dataSender;
Logger* _logger;
QString _message;
QByteArray _newData;
public:
Console();
void injectDataSender(DataSender* sender);
void injectLogger(Logger* logger);
void newData(QByteArray data);
bool hasValidDataFormat();
bool isEchoPacket();
void hasValidDataFormat(const QByteArray& data);
bool isEchoPacket(const QByteArray& data);
void initializeButtons();
signals:
void dataReady(QByteArray data);
};

2
logic/include/model/DataSender.h

@ -6,7 +6,7 @@
class DataSender
{
public:
virtual int send(const QByteArray& data) = 0;
virtual bool send(const QByteArray& data) = 0;
virtual ~DataSender(){}
};

25
logic/include/model/Led.h

@ -1,23 +1,34 @@
#ifndef LED_H
#define LED_H
#define LedOFF 0x00
#define LedColorWhite 0x01
#define LedColorGreen 0x02
#define FunctionCode 3
#define LedColor 4
#include <QObject>
#include "Logger.h"
class Led : public QObject
{
Q_OBJECT
Q_OBJECT
private:
char _functionCode;
bool _hasLed;
char _functionCode;
bool _hasLed;
Logger* _logger;
public:
Led();
Led(char functionCode);
void newData(QByteArray data);
Led();
Led(char functionCode);
signals:
void ledChanged(char value);
void ledChanged(char value);
public slots:
void newData(QByteArray data);
};
#endif //LED_H

8
logic/include/model/PushButton.h

@ -1,6 +1,14 @@
#ifndef PUSHBUTTON_H
#define PUSHBUTTON_H
#define DataLength 8
#define ConsoleDirection 0x00
#define PushButtonDataLength 0x01
#define PushButtonType 0x04
#define PushButtonPush 0x01
#define PushButtonRelease 0x00
#define TimeTag 0x00
#include "Led.h"
class PushButton

6
logic/include/model/RotaryButton.h

@ -1,6 +1,12 @@
#ifndef RotaryButton_H
#define RotaryButton_H
#define DataLength 8
#define ConsoleDirection 0x00
#define RotaryDataLength 0X02
#define RotaryType 0x05
#define TimeTag 0x00
#include "Led.h"
class RotaryButton

143
logic/src/model/Console.cpp

@ -5,6 +5,7 @@
Console::Console()
{
initializeButtons();
}
/*************************************************************************************************/
@ -16,74 +17,140 @@ void Console::injectDataSender(DataSender* sender)
/*************************************************************************************************/
void Console::newData(QByteArray data)
{
_newData = data;
if(hasValidDataFormat())
try
{
if(isEchoPacket())
hasValidDataFormat(data);
if(isEchoPacket(data))
{
//send echo response
_dataSender->send(_newData);
_dataSender->send(data);
}
else
{
// call all buttons newData
emit dataReady(data);
}
}
else
catch (const std::exception& e)
{
_logger->log(_message);
_logger->log(e.what());
}
}
/*************************************************************************************************/
bool Console::hasValidDataFormat()
void Console::hasValidDataFormat(const QByteArray& data)
{
if(_newData.isNull() | _newData.isEmpty() | _newData.size() !=8)
{
_message = "Data is not Correct";
return false;
}
if(_newData[0] != static_cast<char>(0X00))
if(data.isNull() || data.size() != DataLength)
{
_message = "Data direction is not From App";
return false;
throw std::runtime_error("Data is not Correct");
}
if(!(_newData[1] == static_cast<char>(0X01) || _newData[1] == static_cast<char>(0X02)))
if(data[0] != static_cast<char>(PacketAppDirection))
{
_message = "data length is not correct";
return false;
throw std::runtime_error("Data direction is not From App");
}
/* if(!(data[2] == static_cast<char>(0X04) || data[2] == static_cast<char>(0X05) || data[2] == static_cast<char>(0X06)))
{
_logger->log("Data type is not : pushButton:4, Rotary:5, Special: 6");
return false;
}*/
return true;
}
/*************************************************************************************************/
bool Console::isEchoPacket()
bool Console::isEchoPacket(const QByteArray& data)
{
QByteArray echoPacket;
echoPacket.resize(8);
echoPacket[0] = 0x01;
echoPacket[1] = 0x02;
echoPacket[2] = 0x06;
echoPacket[3] = static_cast<char>(0xA4);
echoPacket[4] = 0x00;
echoPacket[5] = 0x00;
echoPacket[6] = 0x00;
echoPacket[7] = 0x00;
echoPacket.resize(DataLength);
echoPacket[0] = PacketAppDirection;
echoPacket[1] = EchoDataLength;
echoPacket[2] = EchoType;
echoPacket[3] = static_cast<char>(EchoFunctionCode);
echoPacket[4] = ZeroValue;
echoPacket[5] = ZeroValue;
echoPacket[6] = ZeroValue;
echoPacket[7] = ZeroValue;
for(int i=0 ; i<_newData.length() ; i++)
for(int i=0 ; i<data.length() ; i++)
{
if (_newData[i] != echoPacket[i])
if (data[i] != echoPacket[i])
{
return false;
}
}
return true;
}
/*************************************************************************************************/
void Console::initializeButtons()
{
initDual();
initQuad();
initSingle();
initP1();
initP2();
initP3();
initP4();
initP5();
initP6();
initExit();
initFreeze();
initPointer();
initAutoSet();
initAbc();
initFourD();
initClear();
initThreeD();
initMeasure();
initBodyMark();
initPatient();
initUtils();
initDvd();
initReport();
initProbe();
initArchive();
initEnd();
initXtd();
initBf();
initJs1Top();
initJs2Top();
initJs3Top();
initJs4Top();
initJs5Top();
initDepthTop();
initFocusTop();
initJs1Center();
initJs2Center();
initJs3Center();
initJs4Center();
initJs5Center();
initDepthCenter();
initFocusCenter();
initModePwCenter();
initModeMCenter();
initModePdCenter();
initModeCCenter();
initModeBCenter();
initJs1Bottom();
initJs2Bottom();
initJs3Bottom();
initJs4Bottom();
initJs5Bottom();
initDepthBottom();
initFocusBottom();
initTrackballTop();
initTrackballTopRight();
initTrackballRight();
initTrackballBottomRight();
initTrackballBottom();
initTrackballBottomLeft();
initTrackballLeft();
initTrackballTopLeft();
initJs1();
initJs2();
initJs3();
initJs4();
initJs5();
initDepth();
initFocus();
initModePw();
initModeM();
initModePd();
initModeC();
initModeB();
}

14
logic/src/model/Led.cpp

@ -14,5 +14,19 @@ Led::Led(char functionCode)
void Led::newData(QByteArray data)
{
try {
if(!(data[LedColor] == static_cast<char>(LedOFF) || data[LedColor] == static_cast<char>(LedColorWhite) || data[LedColor] == static_cast<char>(LedColorGreen)))
{
throw std::runtime_error("Led state color is not correct");
}
if(data[FunctionCode] == _functionCode)
{
emit ledChanged(data[LedColor]);
}
} catch (const std::exception& e)
{
_logger->log(e.what());
}
}

16
logic/src/model/PushButton.cpp

@ -5,15 +5,15 @@ QByteArray PushButton::generateCode(bool isPressed)
{
QByteArray arr;
arr.resize(8);
arr[0] = 0x00;
arr[1] = 0x01;
arr[2] = 0x04;
arr.resize(DataLength);
arr[0] = ConsoleDirection;
arr[1] = PushButtonDataLength;
arr[2] = PushButtonType;
arr[3] = _functionCode;
arr[4] = isPressed ? 0x01 : 0x00;
arr[5] = 0x00;
arr[6] = 0x00;
arr[7] = 0x00;
arr[4] = isPressed ? PushButtonPush : PushButtonRelease;
arr[5] = TimeTag;
arr[6] = TimeTag;
arr[7] = TimeTag;
return arr;
}

14
logic/src/model/RotaryButton.cpp

@ -23,15 +23,15 @@ QByteArray RotaryButton::rotate(int value)
{
QByteArray arr;
arr.resize(8);
arr[0] = 0x00;
arr[1] = 0X02;
arr[2] = 0X05;
arr.resize(DataLength);
arr[0] = ConsoleDirection;
arr[1] = RotaryDataLength;
arr[2] = RotaryType;
arr[3] = _functionCode;
arr[4] = static_cast<char>(value >> 8);
arr[4] = static_cast<char>(value >> DataLength);
arr[5] = static_cast<char>(value);
arr[6] = 0x00;
arr[7] = 0x00;
arr[6] = TimeTag;
arr[7] = TimeTag;
return arr;
}

2
network/UdpDataSender.cpp

@ -1,7 +1,7 @@
#include "UdpDataSender.h"
#include <QDebug>
int UdpDataSender::send(const QByteArray &data)
bool UdpDataSender::send(const QByteArray &data)
{
qDebug() << "Just a little test";
}

2
network/UdpDataSender.h

@ -6,7 +6,7 @@
class UdpDataSender : public DataSender
{
public:
int send(const QByteArray &data) override;
bool send(const QByteArray &data) override;
~UdpDataSender() override;
};

2
test/TestDataSender.h

@ -8,7 +8,7 @@ class TestDataSender : public DataSender
public:
QByteArray consoleData;
int send(const QByteArray &data) override
bool send(const QByteArray &data) override
{
consoleData = data;
return data.length();

216
test/tst_console.cpp

@ -3,24 +3,31 @@
#include "TestDataSender.h"
#include "model/Console.h"
#include <QSignalSpy>
class ConsoleTest : public QObject
{
Q_OBJECT
Q_OBJECT
public:
ConsoleTest();
~ConsoleTest();
ConsoleTest();
~ConsoleTest();
//uncrustify off
//uncrustify off
private slots:
//uncrustify on
void pressDual_test_case();
void releaseDual_test_case();
void pressQuad_test_case();
void releaseQuad_test_case();
//uncrustify on
void pressDual_test_case();
void releaseDual_test_case();
void sendDatatoDual_test_case();
void pressQuad_test_case();
void releaseQuad_test_case();
void rotateJs1NoLed_test_case();
void rotatFocusWithLed_test_case();
void echoSignalTest();
};
/*************************************************************************************************/
@ -36,89 +43,120 @@ ConsoleTest::~ConsoleTest()
/*************************************************************************************************/
void ConsoleTest::pressDual_test_case()
{
Console c;
auto t = new TestDataSender;
c.injectDataSender(t);
c.pressDual();
QByteArray arr;
arr.resize(8);
arr[0] = 0x00;
arr[1] = 0x01;
arr[2] = 0x04;
arr[3] = 0x1D;
arr[4] = 0x01;
arr[5] = 0x00;
arr[6] = 0x00;
arr[7] = 0x00;
QCOMPARE(t->consoleData, arr);
Console c;
auto t = new TestDataSender;
c.injectDataSender(t);
c.pressDual();
QByteArray arr;
arr.resize(8);
arr[0] = 0x00;
arr[1] = 0x01;
arr[2] = 0x04;
arr[3] = 0x1D;
arr[4] = 0x01;
arr[5] = 0x00;
arr[6] = 0x00;
arr[7] = 0x00;
QCOMPARE(t->consoleData, arr);
}
/*************************************************************************************************/
void ConsoleTest::releaseDual_test_case()
{
Console c;
auto t = new TestDataSender;
c.injectDataSender(t);
c.releaseDual();
QByteArray arr;
arr.resize(8);
arr[0] = 0x00;
arr[1] = 0x01;
arr[2] = 0x04;
arr[3] = 0x1D;
arr[4] = 0x00;
arr[5] = 0x00;
arr[6] = 0x00;
arr[7] = 0x00;
QCOMPARE(t->consoleData, arr);
Console c;
auto t = new TestDataSender;
c.injectDataSender(t);
c.releaseDual();
QByteArray arr;
arr.resize(8);
arr[0] = 0x00;
arr[1] = 0x01;
arr[2] = 0x04;
arr[3] = 0x1D;
arr[4] = 0x00;
arr[5] = 0x00;
arr[6] = 0x00;
arr[7] = 0x00;
QCOMPARE(t->consoleData, arr);
}
/*************************************************************************************************/
void ConsoleTest::sendDatatoDual_test_case()
{
Console c;
auto t = new TestDataSender;
c.injectDataSender(t);
QSignalSpy spy(&c, SIGNAL(dualLedChanged(char)));
QByteArray arr;
char ledValue = 0x01;
arr.resize(8);
arr[0] = 0x01;
arr[1] = 0x01;
arr[2] = 0x03;
arr[3] = static_cast<char>(0x82);
arr[4] = ledValue;
arr[5] = 0x00;
arr[6] = 0x00;
arr[7] = 0x00;
c.newData(arr);
auto result = spy.takeFirst()[0].value<QByteArray>();
QCOMPARE(result[0], ledValue);
}
/*************************************************************************************************/
void ConsoleTest::pressQuad_test_case()
{
Console c;
auto t = new TestDataSender;
c.injectDataSender(t);
c.pressQuad();
QByteArray arr;
arr.resize(8);
arr[0] = 0x00;
arr[1] = 0x01;
arr[2] = 0x04;
arr[3] = 0x1C;
arr[4] = 0x01;
arr[5] = 0x00;
arr[6] = 0x00;
arr[7] = 0x00;
QCOMPARE(t->consoleData, arr);
Console c;
auto t = new TestDataSender;
c.injectDataSender(t);
c.pressQuad();
QByteArray arr;
arr.resize(8);
arr[0] = 0x00;
arr[1] = 0x01;
arr[2] = 0x04;
arr[3] = 0x1C;
arr[4] = 0x01;
arr[5] = 0x00;
arr[6] = 0x00;
arr[7] = 0x00;
QCOMPARE(t->consoleData, arr);
}
/*************************************************************************************************/
void ConsoleTest::releaseQuad_test_case()
{
Console c;
auto t = new TestDataSender;
c.injectDataSender(t);
c.releaseQuad();
QByteArray arr;
arr.resize(8);
arr[0] = 0x00;
arr[1] = 0x01;
arr[2] = 0x04;
arr[3] = 0x1C;
arr[4] = 0x00;
arr[5] = 0x00;
arr[6] = 0x00;
arr[7] = 0x00;
Console c;
auto t = new TestDataSender;
c.injectDataSender(t);
c.releaseQuad();
QByteArray arr;
arr.resize(8);
arr[0] = 0x00;
arr[1] = 0x01;
arr[2] = 0x04;
arr[3] = 0x1C;
arr[4] = 0x00;
arr[5] = 0x00;
arr[6] = 0x00;
arr[7] = 0x00;
QCOMPARE(t->consoleData, arr);
}
/*************************************************************************************************/
void ConsoleTest::rotateJs1NoLed_test_case()
{
Console c;
@ -141,12 +179,13 @@ void ConsoleTest::rotateJs1NoLed_test_case()
QCOMPARE(t->consoleData, arr);
}
/*************************************************************************************************/
void ConsoleTest::rotatFocusWithLed_test_case()
{
Console c;
auto t = new TestDataSender;
c.injectDataSender(t);
c.rotateFocus(4000);
c.rotateFocus(40000);
QByteArray arr;
arr.resize(8);
@ -154,14 +193,39 @@ void ConsoleTest::rotatFocusWithLed_test_case()
arr[1] = 0x02;
arr[2] = 0x05;
arr[3] = 0x51;
arr[4] = static_cast<char>(4000 >> 8);
arr[5] = static_cast<char>(4000);
arr[4] = static_cast<char>(40000 >> 8);
arr[5] = static_cast<char>(40000);
arr[6] = 0x00;
arr[7] = 0x00;
QCOMPARE(t->consoleData, arr);
}
/*************************************************************************************************/
void ConsoleTest::echoSignalTest()
{
Console c;
auto t = new TestDataSender;
c.injectDataSender(t);
QByteArray arr;
arr.resize(8);
arr[0] = 0x01;
arr[1] = 0x02;
arr[2] = 0x06;
arr[3] = static_cast<char>(0xA4);
arr[4] = 0x00;
arr[5] = 0x00;
arr[6] = 0x00;
arr[7] = 0x00;
c.newData(arr);
QCOMPARE(t->consoleData, arr);
}
/*************************************************************************************************/
QTEST_APPLESS_MAIN(ConsoleTest)
#include "tst_console.moc"

Loading…
Cancel
Save