From b6c3cba396afda8a0ee359b8a20a235299dc162b Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Tue, 20 Dec 2022 16:33:10 +0100 Subject: [PATCH] Refactor CheckFINDAvsEEPROM and setup2 --- src/logic/start_up.cpp | 32 +++++++++++++++++++++++++++++++- src/logic/start_up.h | 6 +++++- src/main.cpp | 11 +++++------ src/modules/finda.cpp | 21 --------------------- src/modules/finda.h | 3 --- 5 files changed, 41 insertions(+), 32 deletions(-) diff --git a/src/logic/start_up.cpp b/src/logic/start_up.cpp index b76ce08..2478c23 100644 --- a/src/logic/start_up.cpp +++ b/src/logic/start_up.cpp @@ -2,12 +2,21 @@ #include "start_up.h" #include "../modules/buttons.h" #include "../modules/finda.h" +#include "../modules/globals.h" #include "../modules/user_input.h" namespace logic { StartUp startUp; +bool StartUp::Reset(uint8_t) { + if (!CheckFINDAvsEEPROM()) { + SetInitError(ErrorCode::FINDA_VS_EEPROM_DISREPANCY); + } + + return true; +} + bool StartUp::StepInner() { switch (state) { case ProgressCode::OK: @@ -20,7 +29,7 @@ bool StartUp::StepInner() { switch (error) { case ErrorCode::FINDA_VS_EEPROM_DISREPANCY: // Retry - if (!mf::finda.CheckFINDAvsEEPROM()) { + if (!CheckFINDAvsEEPROM()) { error = ErrorCode::FINDA_VS_EEPROM_DISREPANCY; state = ProgressCode::ERRWaitingForUser; } else { @@ -43,4 +52,25 @@ bool StartUp::StepInner() { } return false; } + +bool StartUp::CheckFINDAvsEEPROM() { + bool ret = true; + if (mf::finda.Pressed() && mg::globals.FilamentLoaded() < mg::InFSensor) { + // This is a tricky situation - EEPROM doesn't have a record about loaded filament (blocking the Selector) + // To be on the safe side, we have to override the EEPROM information about filament position - at least InFSensor + // Moreover - we need to override the active slot position as well, because we cannot know where the Selector is. + // For this we speculatively set the active slot to 2 (in the middle ;) ) + // Ideally this should be signalled as an error state and displayed on the printer and recovered properly. + //mg::globals.SetFilamentLoaded(2, mg::InFSensor); + ret = false; + } else if (!mf::finda.Pressed() && mg::globals.FilamentLoaded() >= mg::InSelector) { + // Opposite situation - not so dangerous, but definitely confusing to users. + // FINDA is not pressed but we have a record in the EEPROM. + // It has been decided, that we shall clear such a record from EEPROM automagically + // and presume there is no filament at all (requires working FINDA) + mg::globals.SetFilamentLoaded(config::toolCount, mg::AtPulley); + } + return ret; +} + } // namespace logic diff --git a/src/logic/start_up.h b/src/logic/start_up.h index 76c6436..f031979 100644 --- a/src/logic/start_up.h +++ b/src/logic/start_up.h @@ -12,7 +12,7 @@ public: : CommandBase() {} /// Restart the automaton - bool Reset(uint8_t /*param*/) override { return true; } + bool Reset(uint8_t /*param*/) override; /// @returns true if the state machine finished its job, false otherwise bool StepInner() override; @@ -24,6 +24,10 @@ public: error = ec; state = ProgressCode::ERRWaitingForUser; } + +private: + /// @returns true if there is no discrepency, false otherwise + static bool CheckFINDAvsEEPROM(); }; /// The one and only instance of StartUp state machine in the FW diff --git a/src/main.cpp b/src/main.cpp index a8eeb54..1c592fd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -90,11 +90,7 @@ static void setup2() { // which is abused to let the LEDs shine for ~100ms mf::finda.BlockingInit(); - if (!mf::finda.CheckFINDAvsEEPROM()) { - logic::startUp.SetInitError(ErrorCode::FINDA_VS_EEPROM_DISREPANCY); - } - - /// Turn off all leds + // Turn off all leds for (uint8_t i = 0; i < config::toolCount; i++) { ml::leds.SetMode(i, ml::green, ml::off); ml::leds.SetMode(i, ml::red, ml::off); @@ -103,7 +99,7 @@ static void setup2() { // Prep hardware sanity: logic::hwSanity.Reset(0); - + // Process HW sanity checks exclusively while (!logic::hwSanity.StepInner()) { ml::leds.Step(); } @@ -112,6 +108,9 @@ static void setup2() { // forward the issue to the logic startup handler. logic::startUp.SetInitError(logic::hwSanity.Error()); } else { + // When HW is sane, activate sequence of start up checks and let it run asynchronnously + logic::startUp.Reset(0); + // Idler and Selector decide whether homing is possible/safe mi::idler.Init(); ms::selector.Init(); diff --git a/src/modules/finda.cpp b/src/modules/finda.cpp index e50e2cd..77e9886 100644 --- a/src/modules/finda.cpp +++ b/src/modules/finda.cpp @@ -1,6 +1,5 @@ /// @file finda.cpp #include "finda.h" -#include "globals.h" #include "timebase.h" #include "../hal/gpio.h" #include "../pins.h" @@ -22,25 +21,5 @@ void FINDA::BlockingInit() { } } -bool FINDA::CheckFINDAvsEEPROM() { - bool ret = true; - if (mf::finda.Pressed() && mg::globals.FilamentLoaded() < mg::InFSensor) { - // This is a tricky situation - EEPROM doesn't have a record about loaded filament (blocking the Selector) - // To be on the safe side, we have to override the EEPROM information about filament position - at least InFSensor - // Moreover - we need to override the active slot position as well, because we cannot know where the Selector is. - // For this we speculatively set the active slot to 2 (in the middle ;) ) - // Ideally this should be signalled as an error state and displayed on the printer and recovered properly. - //mg::globals.SetFilamentLoaded(2, mg::InFSensor); - ret = false; - } else if (!mf::finda.Pressed() && mg::globals.FilamentLoaded() >= mg::InSelector) { - // Opposite situation - not so dangerous, but definitely confusing to users. - // FINDA is not pressed but we have a record in the EEPROM. - // It has been decided, that we shall clear such a record from EEPROM automagically - // and presume there is no filament at all (requires working FINDA) - mg::globals.SetFilamentLoaded(config::toolCount, mg::AtPulley); - } - return ret; -} - } // namespace finda } // namespace modules diff --git a/src/modules/finda.h b/src/modules/finda.h index 167e2cb..f536a76 100644 --- a/src/modules/finda.h +++ b/src/modules/finda.h @@ -22,9 +22,6 @@ public: /// to set correct FINDA state at startup void BlockingInit(); - /// @returns true if there is no discrepency, false otherwise - bool CheckFINDAvsEEPROM(); - using debounce::Debouncer::Pressed; };