From 3db0fb6d224c5f431ee3917aebdd0e829d740d84 Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Fri, 5 Aug 2022 09:48:40 +0200 Subject: [PATCH] Allow result codes on a Finished Command state Helpful for the printer to show, that a command has been cancelled (or anything else we can think of in the future). MMU-146 --- src/application.cpp | 2 +- src/logic/command_base.h | 7 ++++++ src/logic/load_filament.cpp | 3 +++ src/logic/load_filament.h | 5 ++++ src/logic/result_codes.h | 13 +++++++++++ src/modules/protocol.cpp | 4 +--- src/version.h | 4 ++-- tests/unit/modules/protocol/test_protocol.cpp | 23 +++++++------------ 8 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 src/logic/result_codes.h diff --git a/src/application.cpp b/src/application.cpp index 28576d0..b93a1fe 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -89,7 +89,7 @@ mp::ResponseCommandStatus Application::RunningCommandStatus() const { case ErrorCode::RUNNING: return mp::ResponseCommandStatus(mp::ResponseMsgParamCodes::Processing, (uint16_t)currentCommand->State()); case ErrorCode::OK: - return mp::ResponseCommandStatus(mp::ResponseMsgParamCodes::Finished, 0); + return mp::ResponseCommandStatus(mp::ResponseMsgParamCodes::Finished, (uint16_t)currentCommand->Result()); default: return mp::ResponseCommandStatus(mp::ResponseMsgParamCodes::Error, (uint16_t)currentCommand->Error()); } diff --git a/src/logic/command_base.h b/src/logic/command_base.h index 05e5954..1571d5f 100644 --- a/src/logic/command_base.h +++ b/src/logic/command_base.h @@ -3,6 +3,7 @@ #include #include "error_codes.h" #include "progress_codes.h" +#include "result_codes.h" namespace modules { namespace motion { @@ -73,6 +74,12 @@ public: /// Please see ErrorCode for more details virtual ErrorCode Error() const { return error; } + /// @returns result of a command - only valid after the command finished its work. + /// Default returned value is OK for all commands. + /// So far there is only one example usage: LoadFilament can be terminated with a button -> Result will be Cancelled. + /// The printer then can display "Loading cancelled" + virtual ResultCode Result() const { return ResultCode::OK; } + /// Switches the state machine into an error state of code ec. /// It shall be used to halt the firmware while retaining the capability of reporting the error state to the printer /// - a kind of similar to runtime assertions. diff --git a/src/logic/load_filament.cpp b/src/logic/load_filament.cpp index 310ed9b..831feea 100644 --- a/src/logic/load_filament.cpp +++ b/src/logic/load_filament.cpp @@ -16,6 +16,8 @@ namespace logic { LoadFilament loadFilament; bool LoadFilament::Reset(uint8_t param) { + result = ResultCode::OK; + if (!CheckToolIndex(param)) { return false; } @@ -72,6 +74,7 @@ bool LoadFilament::StepInner() { break; case FeedToFinda::Stopped: // as requested in MMU-116 - stopping an unsuccessful feed should retract as well but not check the filament + result = ResultCode::Cancelled; verifyLoadedFilament = 0; // [[fallthrough]] case FeedToFinda::OK: diff --git a/src/logic/load_filament.h b/src/logic/load_filament.h index 76c1793..3cca0cd 100644 --- a/src/logic/load_filament.h +++ b/src/logic/load_filament.h @@ -25,6 +25,8 @@ public: /// @returns true if the state machine finished its job, false otherwise bool StepInner() override; + virtual ResultCode Result() const override { return result; } + private: void GoToRetractingFromFinda(); void Reset2(bool feedPhaseLimited); @@ -40,6 +42,9 @@ private: /// That ensures the filament can be loaded into the selector later when needed. /// verifyLoadedFilament holds the number of re-checks to be performed (we expect >1 re-checks will be requested one day ;) ) uint8_t verifyLoadedFilament; + + /// Result of the LoadFilament command + ResultCode result; }; /// The one and only instance of LoadFilament state machine in the FW diff --git a/src/logic/result_codes.h b/src/logic/result_codes.h new file mode 100644 index 0000000..c92ff68 --- /dev/null +++ b/src/logic/result_codes.h @@ -0,0 +1,13 @@ +/// @file result_codes.h +#pragma once +#include + +/// A complete set of result codes which may be a result of a high-level command/operation. +/// This header file shall be included in the printer's firmware as well as a reference, +/// therefore the error codes have been extracted to one place. +/// +/// Please note that currently only LoadFilament can return something else than "OK" +enum class ResultCode : uint_fast16_t { + OK = 0, + Cancelled = 1 +}; diff --git a/src/modules/protocol.cpp b/src/modules/protocol.cpp index b54f193..bd8e3d1 100644 --- a/src/modules/protocol.cpp +++ b/src/modules/protocol.cpp @@ -257,9 +257,7 @@ uint8_t Protocol::EncodeResponseQueryOperation(const RequestMsg &msg, ResponseCo txbuff[2] = ' '; txbuff[3] = (uint8_t)rcs.code; uint8_t *dst = txbuff + 4; - if (rcs.code != ResponseMsgParamCodes::Finished) { - dst += Value2Hex(rcs.value, dst); - } + dst += Value2Hex(rcs.value, dst); *dst = '\n'; return dst - txbuff + 1; } diff --git a/src/version.h b/src/version.h index 429d7e1..513e85d 100644 --- a/src/version.h +++ b/src/version.h @@ -7,8 +7,8 @@ extern "C" { #endif //__cplusplus #define project_version_major 2 -#define project_version_minor 0 -#define project_version_revision 19 +#define project_version_minor 1 +#define project_version_revision 1 #define project_version_build 634 #define FW_BUILD_NUMBER 0 diff --git a/tests/unit/modules/protocol/test_protocol.cpp b/tests/unit/modules/protocol/test_protocol.cpp index 2e60aca..8e079ee 100644 --- a/tests/unit/modules/protocol/test_protocol.cpp +++ b/tests/unit/modules/protocol/test_protocol.cpp @@ -310,18 +310,13 @@ TEST_CASE("protocol::EncodeResponseQueryOperation", "[protocol]") { CHECK(txbuff[2] == ' '); CHECK(txbuff[3] == (uint8_t)responseStatus); - if (responseStatus == mp::ResponseMsgParamCodes::Finished) { - CHECK(txbuff[4] == '\n'); - CHECK(msglen == 5); - } else { - char chk[6]; - int chars = snprintf(chk, 6, "%x\n", encodedParamValue); - REQUIRE(chars < 6); - std::string chks(chk, chk + chars); - std::string txs((const char *)(&txbuff[4]), (const char *)(&txbuff[msglen])); - REQUIRE(chk == txs); - CHECK(txbuff[msglen - 1] == '\n'); - } + char chk[6]; + int chars = snprintf(chk, 6, "%x\n", encodedParamValue); + REQUIRE(chars < 6); + std::string chks(chk, chk + chars); + std::string txs((const char *)(&txbuff[4]), (const char *)(&txbuff[msglen])); + REQUIRE(chk == txs); + CHECK(txbuff[msglen - 1] == '\n'); } TEST_CASE("protocol::DecodeRequest", "[protocol]") { @@ -427,9 +422,7 @@ TEST_CASE("protocol::DecodeResponseQueryOperation", "[protocol][.]") { CHECK((uint8_t)rsp.request.code == rxbuff[0]); CHECK(rsp.request.value == rxbuff[1] - '0'); CHECK((uint8_t)rsp.paramCode == rxbuff[3]); - if ((uint8_t)rsp.paramCode != (uint8_t)mp::ResponseMsgParamCodes::Finished) { - CHECK((uint8_t)rsp.paramValue == rxbuff[4] - '0'); - } + CHECK((uint8_t)rsp.paramValue == rxbuff[4] - '0'); } TEST_CASE("protocol::DecodeRequestErrors", "[protocol]") {