diff --git a/logic/include/model/ButtonHelper.h b/logic/include/model/ButtonHelper.h index 9e3db87..2a31b2a 100644 --- a/logic/include/model/ButtonHelper.h +++ b/logic/include/model/ButtonHelper.h @@ -36,4 +36,30 @@ public: \ signals: \ void SMALL_NAME ## LedChanged(int value) +/*************************************************************************************************/ +#define ROTARY_BUTTON(CAPITAL_NAME, SMALL_NAME, FUNC_CODE, LED_FUNC_CODE) \ +private: \ + RotaryButton _ ## SMALL_NAME{FUNC_CODE, LED_FUNC_CODE}; \ +public: \ + void rotate ## CAPITAL_NAME(int value) \ + { \ + auto arr = _ ## SMALL_NAME.rotate(value); \ + _dataSender->send(arr); \ + } \ +signals: \ + void SMALL_NAME ## LedChanged(int value) + +/*************************************************************************************************/ +#define ROTARY_BUTTON_NO_LED(CAPITAL_NAME, SMALL_NAME, FUNC_CODE) \ +private: \ + RotaryButton _ ## SMALL_NAME{FUNC_CODE}; \ +public: \ + void rotate ## CAPITAL_NAME(int value) \ + { \ + auto arr = _ ## SMALL_NAME.rotate(value); \ + _dataSender->send(arr); \ + } \ +signals: \ + void SMALL_NAME ## LedChanged(int value) + #endif //BUTTONHELPER_H diff --git a/logic/include/model/Console.h b/logic/include/model/Console.h index f7eb13d..5030534 100644 --- a/logic/include/model/Console.h +++ b/logic/include/model/Console.h @@ -6,23 +6,112 @@ #include "ButtonHelper.h" #include "FunctionCodes.h" #include "PushButton.h" -#include "RotayButton.h" +#include "RotaryButton.h" #include "DataSender.h" +#include "Logger.h" class Console : public QObject { - Q_OBJECT + Q_OBJECT + + // PushButton with LED + PUSH_BUTTON(Dual, dual, DUAL_FC, static_cast(DUAL_LED_FC)); + PUSH_BUTTON(Quad, quad, QUAD_FC, static_cast(QUAD_LED_FC)); + PUSH_BUTTON(Single, single, SINGLE_FC, static_cast(SINGLE_LED_FC)); + PUSH_BUTTON(P1, p1, P1_FC, static_cast(P1_LED_FC)); + PUSH_BUTTON(P2, p2, P2_FC, static_cast(P2_LED_FC)); + PUSH_BUTTON(P3, p3, P3_FC, static_cast(P3_LED_FC)); + PUSH_BUTTON(P4, p4, P4_FC, static_cast(P4_LED_FC)); + PUSH_BUTTON(P5, p5, P5_FC, static_cast(P5_LED_FC)); + PUSH_BUTTON(P6, p6, P6_FC, static_cast(P6_LED_FC)); + PUSH_BUTTON(Exit, exit, EXIT_FC, static_cast(EXIT_LED_FC)); + PUSH_BUTTON(Freeze, freeze, FREEZE_FC, static_cast(FREEZE_LED_FC)); + PUSH_BUTTON(Pointer, pointer, POINTER_FC, static_cast(POINTER_LED_FC)); + PUSH_BUTTON(Auto, auto, AUTOSET_FC, static_cast(AUTOSET_LED_FC)); + PUSH_BUTTON(Abc, abc, ABC_FC, static_cast(ABC_LED_FC)); + PUSH_BUTTON(FourD, fourD, FOUR_D_FC, static_cast(FOUR_D_LED_FC)); + PUSH_BUTTON(Clear, clear, CLEAR_FC, static_cast(CLEAR_LED_FC)); + PUSH_BUTTON(ThreeD, threeD, THREE_D_FC, static_cast(THREE_D_LED_FC)); + PUSH_BUTTON(Measure, measure, MEASURE_FC, static_cast(MEASURE_LED_FC)); + PUSH_BUTTON(BodyMark, bodyMark, BODY_MARK_FC, static_cast(BODY_MARK_LED_FC)); + PUSH_BUTTON(Patient, patient, PATIENT_FC, static_cast(PATIENT_LED_FC)); + PUSH_BUTTON(Utils, utils, UTILS_FC, static_cast(UTILS_LED_FC)); + PUSH_BUTTON(Dvd, dvd, DVD_FC, static_cast(DVD_LED_FC)); + PUSH_BUTTON(Report, report, REPORT_FC, static_cast(REPORT_LED_FC)); + PUSH_BUTTON(Probe, probe, PROBE_FC, static_cast(PROBE_LED_FC)); + PUSH_BUTTON(Archive, archive, ARCHIVE_FC, static_cast(ARCHIVE_LED_FC)); + PUSH_BUTTON(End, end, END_FC, static_cast(END_LED_FC)); + PUSH_BUTTON(Xtd, xtd, XTD_FC, static_cast(XTD_LED_FC)); + PUSH_BUTTON(Bf, bf, BF_FC, static_cast(BF_LED_FC)); + PUSH_BUTTON(DepthCenter, depthCenter, DEPTH_CENTER_FC, static_cast(ZOOM_LED_FC)); + PUSH_BUTTON(FocusCenter, focusCenter, FOCUS_CENTER_FC, static_cast(FOCUS_ZONE_LED_FC)); + PUSH_BUTTON(ModePwCenter, modePwCenter, MODE_PW_CENTER_FC, static_cast(MODE_PW_LED_FC)); + PUSH_BUTTON(ModeMCenter, modeMCenter, MODE_M_CENTER_FC, static_cast(MODE_M_LED_FC)); + PUSH_BUTTON(ModePdCenter, modePdCenter, MODE_PD_CENTER_FC, static_cast(MODE_PD_LED_FC)); + PUSH_BUTTON(ModeCCenter, modeCCenter, MODE_C_CENTER_FC, static_cast(MODE_C_LED_FC)); + PUSH_BUTTON(ModeBCenter, modeBCenter, MODE_2D_CENTER_FC, static_cast(MODE_2D_LED_FC)); + PUSH_BUTTON(DepthBottom, depthBottom, DEPTH_BOTTOM_FC, static_cast(DEPTH_LED_FC)); + PUSH_BUTTON(FocusBottom, focusBottom, FOCUS_BOTTOM_FC, static_cast(FOCUS_DEPTH_LED_FC)); + + // PushButton with No LED + PUSH_BUTTON_NO_LED(Js1Top, js1Top, JOYSTICK1_TOP_FC); + PUSH_BUTTON_NO_LED(Js2Top, js2Top, JOYSTICK2_TOP_FC); + PUSH_BUTTON_NO_LED(Js3Top, js3Top, JOYSTICK3_TOP_FC); + PUSH_BUTTON_NO_LED( Js4Top, Js4Top, JOYSTICK4_TOP_FC); + PUSH_BUTTON_NO_LED(Js5Top, js5Top, JOYSTICK5_TOP_FC); + PUSH_BUTTON_NO_LED(DepthTop, depthTop, DEPTHTOP_FC); + PUSH_BUTTON_NO_LED(FocusTop, focusTop, FOCUSTOP_FC); + PUSH_BUTTON_NO_LED(Js1Center, js1Center, JOYSTICK1_CENTER_FC); + PUSH_BUTTON_NO_LED(Js2Center, js2Center, JOYSTICK2_CENTER_FC); + PUSH_BUTTON_NO_LED(Js3Center, js3Center, JOYSTICK3_CENTER_FC); + PUSH_BUTTON_NO_LED(Js4Center, js4Center, JOYSTICK4_CENTER_FC); + PUSH_BUTTON_NO_LED(Js5Center, js5Center, JOYSTICK5_CENTER_FC); + PUSH_BUTTON_NO_LED(Js1Bottom, js1Bottom, JOYSTICK1_BOTTOM_FC); + PUSH_BUTTON_NO_LED(Js2Bottom, js2Bottom, JOYSTICK2_BOTTOM_FC); + PUSH_BUTTON_NO_LED(Js3Bottom, js3Bottom, JOYSTICK3_BOTTOM_FC); + PUSH_BUTTON_NO_LED(Js4Bottom, js4Bottom, JOYSTICK4_BOTTOM_FC); + PUSH_BUTTON_NO_LED(Js5Bottom, js5Bottom, JOYSTICK5_BOTTOM_FC); + PUSH_BUTTON_NO_LED(TrackballTop, trackballTop, TRACKBALL_TOP_FC); + PUSH_BUTTON_NO_LED(TrackballTopRight, trackballTopRight, TRACKBALL_TOP_RIGHT_FC); + PUSH_BUTTON_NO_LED(TrackballRight, trackballRight, TRACKBALL_RIGHT_FC); + PUSH_BUTTON_NO_LED(TrackballBottomRight, trackballBottomRight, TRACKBALL_BOTTOM_RIGHT_FC); + PUSH_BUTTON_NO_LED(TrackballBottom, trackballBottom, TRACKBALL_BOTTOM_FC); + PUSH_BUTTON_NO_LED(TrackballBottomLeft, trackballBottomLeft, TRACKBALL_TOP_BOTTOM_FC); + PUSH_BUTTON_NO_LED(TrackballLeft, trackballLeft, TRACKBALL_LEFT_FC); + PUSH_BUTTON_NO_LED(TrackballTopLeft, trackballTopLeft, TRACKBALL_TOP_LEFT_FC); + + // ROTAY_BUTTON with LED + ROTARY_BUTTON(Focus, focus, FOCUS_FC, static_cast(ANGLE_LED_FC)); + ROTARY_BUTTON(ModeM, modeM, MODE_M_FC, static_cast(QUADRAT_LED_FC)); + ROTARY_BUTTON(ModePd, modePd, MODE_PD_FC, static_cast(Z_LED_FC)); + ROTARY_BUTTON(ModeC, modeC, MODE_C_FC, static_cast(Y_LED_FC)); + ROTARY_BUTTON(ModeB, modeB, MODE_2D_FC, static_cast(X_LED_FC)); + + // ROTAY_BUTTON with No LED + ROTARY_BUTTON_NO_LED(Js1, js1, JOYSTICK1_FC); + ROTARY_BUTTON_NO_LED(Js2, js2, JOYSTICK2_FC); + ROTARY_BUTTON_NO_LED(Js3, js3, JOYSTICK3_FC); + ROTARY_BUTTON_NO_LED(Js4, js4, JOYSTICK4_FC); + ROTARY_BUTTON_NO_LED(Js5, js5, JOYSTICK5_FC); + ROTARY_BUTTON_NO_LED(Depth, depth, DEPTH_FC); + ROTARY_BUTTON_NO_LED(ModePw, modePw, MODE_PW_FC); - PUSH_BUTTON(Dual, dual, DUAL_FC, static_cast(DUAL_LED_FC)); - PUSH_BUTTON(Quad, quad, QUAD_FC, static_cast(QUAD_LED_FC)); private: - DataSender* _dataSender; + DataSender* _dataSender; + Logger* _logger; + QString _message; + QByteArray _newData; public: - Console(); - void injectDataSender(DataSender* sender); + Console(); + void injectDataSender(DataSender* sender); + void injectLogger(Logger* logger); + void newData(QByteArray data); + bool hasValidDataFormat(); + bool isEchoPacket(); + }; #endif //CONSOLE_H diff --git a/logic/include/model/Logger.h b/logic/include/model/Logger.h new file mode 100644 index 0000000..afa8255 --- /dev/null +++ b/logic/include/model/Logger.h @@ -0,0 +1,12 @@ +#ifndef LOGGER_H +#define LOGGER_H +#include "QString" + +class Logger +{ +public: + virtual void log(QString message) = 0; + virtual ~Logger(){} +}; + +#endif // LOGGER_H diff --git a/logic/include/model/PushButton.h b/logic/include/model/PushButton.h index 81e9e61..f0d661d 100644 --- a/logic/include/model/PushButton.h +++ b/logic/include/model/PushButton.h @@ -14,6 +14,7 @@ private: public: PushButton(char functionCode); PushButton(char functionCode, char ledFunctionCode); + QByteArray press(); QByteArray release(); Led* getLed(); diff --git a/logic/include/model/RotaryButton.h b/logic/include/model/RotaryButton.h new file mode 100644 index 0000000..24ff286 --- /dev/null +++ b/logic/include/model/RotaryButton.h @@ -0,0 +1,20 @@ +#ifndef RotaryButton_H +#define RotaryButton_H + +#include "Led.h" + +class RotaryButton +{ +private: + char _functionCode; + Led _led; + +public: + RotaryButton(char functionCode); + RotaryButton(char functionCode, char ledFunctionCode); + + Led* getLed(); + QByteArray rotate(int value); +}; + +#endif //RotaryButton_H diff --git a/logic/include/model/RotayButton.h b/logic/include/model/RotayButton.h deleted file mode 100644 index 540ae20..0000000 --- a/logic/include/model/RotayButton.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef ROTAYBUTTON_H -#define ROTAYBUTTON_H - -#include "Led.h" - -class RotayButton -{ -private: - char _functionCode; - Led _led; - -public: - RotayButton(char functionCode); - RotayButton(char functionCode, char ledFunctionCode); - Led* getLed(); - QByteArray rotate(int value); -}; - -#endif //ROTAYBUTTON_H diff --git a/logic/logic.pro b/logic/logic.pro index daa0325..1f4e77b 100644 --- a/logic/logic.pro +++ b/logic/logic.pro @@ -10,7 +10,8 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += $$files(src/*.cpp, true) -HEADERS += $$files(include/*.h, true) +HEADERS += $$files(include/*.h, true) \ + include/model/Logger.h INCLUDEPATH += $$PWD/include/ INCLUDEPATH += $$PWD/../network/ diff --git a/logic/src/model/Console.cpp b/logic/src/model/Console.cpp index 5c4756b..bc32f54 100644 --- a/logic/src/model/Console.cpp +++ b/logic/src/model/Console.cpp @@ -1,6 +1,7 @@ #include "model/Console.h" #include "model/DataSender.h" +#include "QByteArray" Console::Console() { @@ -9,5 +10,80 @@ Console::Console() /*************************************************************************************************/ void Console::injectDataSender(DataSender* sender) { - _dataSender = sender; + _dataSender = sender; +} + +/*************************************************************************************************/ +void Console::newData(QByteArray data) +{ + _newData = data; + if(hasValidDataFormat()) + { + if(isEchoPacket()) + { + //send echo response + _dataSender->send(_newData); + } + else + { + // call all buttons newData + } + } + else + { + _logger->log(_message); + } + +} + +/*************************************************************************************************/ +bool Console::hasValidDataFormat() +{ + if(_newData.isNull() | _newData.isEmpty() | _newData.size() !=8) + { + _message = "Data is not Correct"; + return false; + } + if(_newData[0] != static_cast(0X00)) + { + _message = "Data direction is not From App"; + return false; + } + if(!(_newData[1] == static_cast(0X01) || _newData[1] == static_cast(0X02))) + { + _message = "data length is not correct"; + return false; + } + /* if(!(data[2] == static_cast(0X04) || data[2] == static_cast(0X05) || data[2] == static_cast(0X06))) + { + _logger->log("Data type is not : pushButton:4, Rotary:5, Special: 6"); + return false; + }*/ + return true; + +} + +/*************************************************************************************************/ +bool Console::isEchoPacket() +{ + QByteArray echoPacket; + + echoPacket.resize(8); + echoPacket[0] = 0x01; + echoPacket[1] = 0x02; + echoPacket[2] = 0x06; + echoPacket[3] = static_cast(0xA4); + echoPacket[4] = 0x00; + echoPacket[5] = 0x00; + echoPacket[6] = 0x00; + echoPacket[7] = 0x00; + + for(int i=0 ; i<_newData.length() ; i++) + { + if (_newData[i] != echoPacket[i]) + { + return false; + } + } + return true; } diff --git a/logic/src/model/Led.cpp b/logic/src/model/Led.cpp index 06d55b3..64481ff 100644 --- a/logic/src/model/Led.cpp +++ b/logic/src/model/Led.cpp @@ -9,5 +9,10 @@ Led::Led() Led::Led(char functionCode) { _functionCode = functionCode; - _hasLed = true; + _hasLed = true; +} + +void Led::newData(QByteArray data) +{ + } diff --git a/logic/src/model/PushButton.cpp b/logic/src/model/PushButton.cpp index 3523bf7..a906921 100644 --- a/logic/src/model/PushButton.cpp +++ b/logic/src/model/PushButton.cpp @@ -3,19 +3,19 @@ /*************************************************************************************************/ QByteArray PushButton::generateCode(bool isPressed) { - QByteArray arr; - - arr.resize(8); - arr[0] = 0x00; - arr[1] = 0x01; - arr[2] = 0x04; - arr[3] = _functionCode; - arr[4] = isPressed ? 0x01 : 0x00; - arr[5] = 0x00; - arr[6] = 0x00; - arr[7] = 0x00; - - return arr; + QByteArray arr; + + arr.resize(8); + arr[0] = 0x00; + arr[1] = 0x01; + arr[2] = 0x04; + arr[3] = _functionCode; + arr[4] = isPressed ? 0x01 : 0x00; + arr[5] = 0x00; + arr[6] = 0x00; + arr[7] = 0x00; + + return arr; } /*************************************************************************************************/ @@ -38,7 +38,6 @@ QByteArray PushButton::press() } /*************************************************************************************************/ - QByteArray PushButton::release() { return generateCode(false); diff --git a/logic/src/model/RotayButton.cpp b/logic/src/model/RotaryButton.cpp similarity index 65% rename from logic/src/model/RotayButton.cpp rename to logic/src/model/RotaryButton.cpp index 5bab4fc..ebdfcbd 100644 --- a/logic/src/model/RotayButton.cpp +++ b/logic/src/model/RotaryButton.cpp @@ -1,25 +1,25 @@ -#include "model/RotayButton.h" +#include "model/RotaryButton.h" -RotayButton::RotayButton(char functionCode) +RotaryButton::RotaryButton(char functionCode) { _functionCode = functionCode; } /*************************************************************************************************/ -RotayButton::RotayButton(char functionCode, char ledFunctionCode) : +RotaryButton::RotaryButton(char functionCode, char ledFunctionCode) : _led(ledFunctionCode) { _functionCode = functionCode; } /*************************************************************************************************/ -Led* RotayButton::getLed() +Led* RotaryButton::getLed() { return &_led; } /*************************************************************************************************/ -QByteArray RotayButton::rotate(int value) +QByteArray RotaryButton::rotate(int value) { QByteArray arr; @@ -28,8 +28,8 @@ QByteArray RotayButton::rotate(int value) arr[1] = 0X02; arr[2] = 0X05; arr[3] = _functionCode; - arr[4] = 0x01; - arr[5] = 0x00; + arr[4] = static_cast(value >> 8); + arr[5] = static_cast(value); arr[6] = 0x00; arr[7] = 0x00; diff --git a/test/tst_console.cpp b/test/tst_console.cpp index 4867cb3..ffd29f4 100644 --- a/test/tst_console.cpp +++ b/test/tst_console.cpp @@ -18,6 +18,9 @@ private slots: void releaseDual_test_case(); void pressQuad_test_case(); void releaseQuad_test_case(); + void rotateJs1NoLed_test_case(); + void rotatFocusWithLed_test_case(); + }; /*************************************************************************************************/ @@ -113,7 +116,50 @@ void ConsoleTest::releaseQuad_test_case() arr[6] = 0x00; arr[7] = 0x00; - QCOMPARE(t->consoleData, arr); + QCOMPARE(t->consoleData, arr); +} + +void ConsoleTest::rotateJs1NoLed_test_case() +{ + Console c; + auto t = new TestDataSender; + c.injectDataSender(t); + c.rotateJs1(0); + + QByteArray arr; + + arr.resize(8); + arr[0] = 0x00; + arr[1] = 0x02; + arr[2] = 0x05; + arr[3] = 0x56; + arr[4] = static_cast(0 >> 8); + arr[5] = static_cast(0); + arr[6] = 0x00; + arr[7] = 0x00; + + QCOMPARE(t->consoleData, arr); +} + +void ConsoleTest::rotatFocusWithLed_test_case() +{ + Console c; + auto t = new TestDataSender; + c.injectDataSender(t); + c.rotateFocus(4000); + QByteArray arr; + + arr.resize(8); + arr[0] = 0x00; + arr[1] = 0x02; + arr[2] = 0x05; + arr[3] = 0x51; + arr[4] = static_cast(4000 >> 8); + arr[5] = static_cast(4000); + arr[6] = 0x00; + arr[7] = 0x00; + + QCOMPARE(t->consoleData, arr); } QTEST_APPLESS_MAIN(ConsoleTest)