diff --git a/src/modules/protocol.cpp b/src/modules/protocol.cpp index 056a28f..b54f193 100644 --- a/src/modules/protocol.cpp +++ b/src/modules/protocol.cpp @@ -125,18 +125,7 @@ uint8_t Protocol::EncodeRequest(const RequestMsg &msg, uint8_t *txbuff) { } uint8_t Protocol::EncodeWriteRequest(const RequestMsg &msg, uint16_t value2, uint8_t *txbuff) { - uint8_t i = 1; - txbuff[0] = (uint8_t)msg.code; - uint8_t v = msg.value >> 4; - if (v != 0) { // skip the first '0' if any - txbuff[i] = Nibble2Char(v); - ++i; - } - v = msg.value & 0xf; - txbuff[i] = Nibble2Char(v); - ++i; - txbuff[i] = ' '; - ++i; + uint8_t i = BeginEncodeRequest(msg, txbuff); // dump the value i += Value2Hex(value2, txbuff + i); @@ -231,18 +220,7 @@ DecodeStatus Protocol::DecodeResponse(uint8_t c) { } uint8_t Protocol::EncodeResponseCmdAR(const RequestMsg &msg, ResponseMsgParamCodes ar, uint8_t *txbuff) { - uint8_t i = 1; - txbuff[0] = (uint8_t)msg.code; - uint8_t v = msg.value >> 4U; - if (v != 0) { // skip the first '0' if any - txbuff[i] = Nibble2Char(v); - ++i; - } - v = msg.value & 0xfU; - txbuff[i] = Nibble2Char(v); - ++i; - txbuff[i] = ' '; - ++i; + uint8_t i = BeginEncodeRequest(msg, txbuff); txbuff[i] = (uint8_t)ar; ++i; txbuff[i] = '\n'; @@ -287,19 +265,7 @@ uint8_t Protocol::EncodeResponseQueryOperation(const RequestMsg &msg, ResponseCo } uint8_t Protocol::EncodeResponseRead(const RequestMsg &msg, bool accepted, uint16_t value2, uint8_t *txbuff) { - uint8_t i = 1; - txbuff[0] = (uint8_t)msg.code; - uint8_t v = msg.value >> 4U; - if (v != 0) { // skip the first '0' if any - txbuff[i] = Nibble2Char(v); - ++i; - } - v = msg.value & 0xfU; - txbuff[i] = Nibble2Char(v); - ++i; - txbuff[i] = ' '; - ++i; - + uint8_t i = BeginEncodeRequest(msg, txbuff); if (accepted) { txbuff[i] = (uint8_t)ResponseMsgParamCodes::Accepted; ++i; @@ -335,5 +301,20 @@ uint8_t Protocol::Value2Hex(uint16_t value, uint8_t *dst) { return charsOut; } +uint8_t Protocol::BeginEncodeRequest(const RequestMsg &msg, uint8_t *txbuff) { + uint8_t i = 1; + txbuff[0] = (uint8_t)msg.code; + uint8_t v = msg.value >> 4U; + if (v != 0) { // skip the first '0' if any + txbuff[i] = Nibble2Char(v); + ++i; + } + v = msg.value & 0xfU; + txbuff[i] = Nibble2Char(v); + ++i; + txbuff[i] = ' '; + return i + 1; +} + } // namespace protocol } // namespace modules diff --git a/src/modules/protocol.h b/src/modules/protocol.h index 9f95619..fa9a9ec 100644 --- a/src/modules/protocol.h +++ b/src/modules/protocol.h @@ -261,6 +261,8 @@ private: /// @returns number of characters written static uint8_t Value2Hex(uint16_t value, uint8_t *dst); + + static uint8_t BeginEncodeRequest(const RequestMsg &msg, uint8_t *txbuff); }; } // namespace protocol diff --git a/tests/unit/logic/cut_filament/CMakeLists.txt b/tests/unit/logic/cut_filament/CMakeLists.txt index d736517..deb0255 100644 --- a/tests/unit/logic/cut_filament/CMakeLists.txt +++ b/tests/unit/logic/cut_filament/CMakeLists.txt @@ -2,6 +2,8 @@ add_executable( cut_filament_tests ${CMAKE_SOURCE_DIR}/src/application.cpp + ${CMAKE_SOURCE_DIR}/src/registers.cpp + ${CMAKE_SOURCE_DIR}/src/version.c ${CMAKE_SOURCE_DIR}/src/logic/command_base.cpp ${CMAKE_SOURCE_DIR}/src/logic/cut_filament.cpp ${CMAKE_SOURCE_DIR}/src/logic/eject_filament.cpp diff --git a/tests/unit/logic/eject_filament/CMakeLists.txt b/tests/unit/logic/eject_filament/CMakeLists.txt index 701239d..96e4a2d 100644 --- a/tests/unit/logic/eject_filament/CMakeLists.txt +++ b/tests/unit/logic/eject_filament/CMakeLists.txt @@ -2,6 +2,8 @@ add_executable( eject_filament_tests ${CMAKE_SOURCE_DIR}/src/application.cpp + ${CMAKE_SOURCE_DIR}/src/registers.cpp + ${CMAKE_SOURCE_DIR}/src/version.c ${CMAKE_SOURCE_DIR}/src/logic/command_base.cpp ${CMAKE_SOURCE_DIR}/src/logic/cut_filament.cpp ${CMAKE_SOURCE_DIR}/src/logic/eject_filament.cpp diff --git a/tests/unit/logic/failing_tmc/CMakeLists.txt b/tests/unit/logic/failing_tmc/CMakeLists.txt index 6e2026f..90c1d82 100644 --- a/tests/unit/logic/failing_tmc/CMakeLists.txt +++ b/tests/unit/logic/failing_tmc/CMakeLists.txt @@ -2,6 +2,8 @@ add_executable( failing_tmc_tests ${CMAKE_SOURCE_DIR}/src/application.cpp + ${CMAKE_SOURCE_DIR}/src/registers.cpp + ${CMAKE_SOURCE_DIR}/src/version.c ${CMAKE_SOURCE_DIR}/src/logic/command_base.cpp ${CMAKE_SOURCE_DIR}/src/logic/cut_filament.cpp ${CMAKE_SOURCE_DIR}/src/logic/eject_filament.cpp diff --git a/tests/unit/logic/feed_to_bondtech/CMakeLists.txt b/tests/unit/logic/feed_to_bondtech/CMakeLists.txt index 2d6e8f0..1252518 100644 --- a/tests/unit/logic/feed_to_bondtech/CMakeLists.txt +++ b/tests/unit/logic/feed_to_bondtech/CMakeLists.txt @@ -2,6 +2,8 @@ add_executable( feed_to_bondtech_tests ${CMAKE_SOURCE_DIR}/src/application.cpp + ${CMAKE_SOURCE_DIR}/src/registers.cpp + ${CMAKE_SOURCE_DIR}/src/version.c ${CMAKE_SOURCE_DIR}/src/logic/command_base.cpp ${CMAKE_SOURCE_DIR}/src/logic/cut_filament.cpp ${CMAKE_SOURCE_DIR}/src/logic/eject_filament.cpp diff --git a/tests/unit/logic/feed_to_finda/CMakeLists.txt b/tests/unit/logic/feed_to_finda/CMakeLists.txt index 53741ab..13066b8 100644 --- a/tests/unit/logic/feed_to_finda/CMakeLists.txt +++ b/tests/unit/logic/feed_to_finda/CMakeLists.txt @@ -2,6 +2,8 @@ add_executable( feed_to_finda_tests ${CMAKE_SOURCE_DIR}/src/application.cpp + ${CMAKE_SOURCE_DIR}/src/registers.cpp + ${CMAKE_SOURCE_DIR}/src/version.c ${CMAKE_SOURCE_DIR}/src/logic/command_base.cpp ${CMAKE_SOURCE_DIR}/src/logic/cut_filament.cpp ${CMAKE_SOURCE_DIR}/src/logic/eject_filament.cpp diff --git a/tests/unit/logic/homing/CMakeLists.txt b/tests/unit/logic/homing/CMakeLists.txt index 2856390..2b99786 100644 --- a/tests/unit/logic/homing/CMakeLists.txt +++ b/tests/unit/logic/homing/CMakeLists.txt @@ -2,6 +2,8 @@ add_executable( homing_tests ${CMAKE_SOURCE_DIR}/src/application.cpp + ${CMAKE_SOURCE_DIR}/src/registers.cpp + ${CMAKE_SOURCE_DIR}/src/version.c ${CMAKE_SOURCE_DIR}/src/logic/command_base.cpp ${CMAKE_SOURCE_DIR}/src/logic/cut_filament.cpp ${CMAKE_SOURCE_DIR}/src/logic/eject_filament.cpp diff --git a/tests/unit/logic/load_filament/CMakeLists.txt b/tests/unit/logic/load_filament/CMakeLists.txt index ae1f669..7b54465 100644 --- a/tests/unit/logic/load_filament/CMakeLists.txt +++ b/tests/unit/logic/load_filament/CMakeLists.txt @@ -2,6 +2,8 @@ add_executable( load_filament_tests ${CMAKE_SOURCE_DIR}/src/application.cpp + ${CMAKE_SOURCE_DIR}/src/registers.cpp + ${CMAKE_SOURCE_DIR}/src/version.c ${CMAKE_SOURCE_DIR}/src/logic/command_base.cpp ${CMAKE_SOURCE_DIR}/src/logic/cut_filament.cpp ${CMAKE_SOURCE_DIR}/src/logic/eject_filament.cpp diff --git a/tests/unit/logic/tool_change/CMakeLists.txt b/tests/unit/logic/tool_change/CMakeLists.txt index 86fbe1d..c15640f 100644 --- a/tests/unit/logic/tool_change/CMakeLists.txt +++ b/tests/unit/logic/tool_change/CMakeLists.txt @@ -2,6 +2,8 @@ add_executable( tool_change_tests ${CMAKE_SOURCE_DIR}/src/application.cpp + ${CMAKE_SOURCE_DIR}/src/registers.cpp + ${CMAKE_SOURCE_DIR}/src/version.c ${CMAKE_SOURCE_DIR}/src/logic/command_base.cpp ${CMAKE_SOURCE_DIR}/src/logic/cut_filament.cpp ${CMAKE_SOURCE_DIR}/src/logic/eject_filament.cpp diff --git a/tests/unit/logic/unload_filament/CMakeLists.txt b/tests/unit/logic/unload_filament/CMakeLists.txt index b06a670..1ef983c 100644 --- a/tests/unit/logic/unload_filament/CMakeLists.txt +++ b/tests/unit/logic/unload_filament/CMakeLists.txt @@ -2,6 +2,8 @@ add_executable( unload_filament_tests ${CMAKE_SOURCE_DIR}/src/application.cpp + ${CMAKE_SOURCE_DIR}/src/registers.cpp + ${CMAKE_SOURCE_DIR}/src/version.c ${CMAKE_SOURCE_DIR}/src/logic/command_base.cpp ${CMAKE_SOURCE_DIR}/src/logic/cut_filament.cpp ${CMAKE_SOURCE_DIR}/src/logic/eject_filament.cpp diff --git a/tests/unit/logic/unload_to_finda/CMakeLists.txt b/tests/unit/logic/unload_to_finda/CMakeLists.txt index 84ba51f..b2a6828 100644 --- a/tests/unit/logic/unload_to_finda/CMakeLists.txt +++ b/tests/unit/logic/unload_to_finda/CMakeLists.txt @@ -2,6 +2,8 @@ add_executable( unload_to_finda_tests ${CMAKE_SOURCE_DIR}/src/application.cpp + ${CMAKE_SOURCE_DIR}/src/registers.cpp + ${CMAKE_SOURCE_DIR}/src/version.c ${CMAKE_SOURCE_DIR}/src/logic/command_base.cpp ${CMAKE_SOURCE_DIR}/src/logic/cut_filament.cpp ${CMAKE_SOURCE_DIR}/src/logic/eject_filament.cpp