From b4d4807971463ad452e307366ded4f3b64674103 Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Tue, 31 Aug 2021 07:40:33 +0200 Subject: [PATCH] Add globa Panic() function The intent is to halt the firmware while retaining the reporting capabilities and blinking LEDs --- src/logic/command_base.cpp | 10 ++++++++++ src/logic/command_base.h | 8 ++++++++ src/main.cpp | 6 ++++++ src/panic.h | 10 ++++++++++ 4 files changed, 34 insertions(+) create mode 100644 src/panic.h diff --git a/src/logic/command_base.cpp b/src/logic/command_base.cpp index d6e2f0d..cd8f460 100644 --- a/src/logic/command_base.cpp +++ b/src/logic/command_base.cpp @@ -2,6 +2,7 @@ #include "../modules/idler.h" #include "../modules/selector.h" #include "../modules/motion.h" +#include "../modules/leds.h" namespace logic { @@ -76,6 +77,15 @@ bool CommandBase::Step() { return StepInner(); } +void CommandBase::Panic(ErrorCode ec) { + state = ProgressCode::ERRInternal; + error = ec; + for (uint8_t i = 0; i < config::toolCount; ++i) { + ml::leds.SetMode(i, ml::green, ml::blink0); + ml::leds.SetMode(i, ml::red, ml::blink0); + } +} + bool CommandBase::CheckToolIndex(uint8_t index) { if (index >= config::toolCount) { error = ErrorCode::INVALID_TOOL; diff --git a/src/logic/command_base.h b/src/logic/command_base.h index 143705f..f57a9b9 100644 --- a/src/logic/command_base.h +++ b/src/logic/command_base.h @@ -61,6 +61,14 @@ public: /// Please see ErrorCode for more details virtual ErrorCode Error() const { return error; } + /// 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. + /// Called from main.cpp's global funtion Panic() . + /// The derived state machines have no (implemented) way of getting out of this state (intentionally). + /// The only way out is to reset the board. + void Panic(ErrorCode ec); + protected: /// @returns true if the slot/tool index is within specified range (0 - config::toolCount) /// If not, it returns false and sets the error to ErrorCode::INVALID_TOOL diff --git a/src/main.cpp b/src/main.cpp index 067492f..e397488 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,6 +31,8 @@ #include "version.h" +#include "panic.h" + /// Global instance of the protocol codec static mp::Protocol protocol; @@ -331,6 +333,10 @@ bool CheckMsgs() { return false; } +void Panic(ErrorCode ec) { + currentCommand->Panic(ec); +} + /// Main loop of the firmware /// Proposed architecture /// checkMsgs(); diff --git a/src/panic.h b/src/panic.h new file mode 100644 index 0000000..06c8566 --- /dev/null +++ b/src/panic.h @@ -0,0 +1,10 @@ +#pragma once +#include +#include "logic/error_codes.h" + +/// Switches the currently active logic 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. +/// Implementation is in main.cpp, where we know the currently active logic state machine. +/// The only way out is to reset the board. +extern void Panic(ErrorCode ec);