diff --git a/src/logic/no_command.cpp b/src/logic/no_command.cpp index 6832381..8e77119 100644 --- a/src/logic/no_command.cpp +++ b/src/logic/no_command.cpp @@ -1,8 +1,42 @@ /// @file no_command.cpp #include "no_command.h" +#include "../modules/buttons.h" +#include "../modules/finda.h" +#include "../modules/user_input.h" namespace logic { NoCommand noCommand; +bool NoCommand::StepInner() { + switch (state) { + case ProgressCode::OK: + return true; + case ProgressCode::ERRWaitingForUser: { + // waiting for user buttons and/or a command from the printer + mui::Event ev = mui::userInput.ConsumeEvent(); + switch (ev) { + case mui::Event::Middle: + switch (error) { + case ErrorCode::FINDA_VS_EEPROM_DISREPANCY: + // Retry + if (!mf::finda.CheckFINDAvsEEPROM()) { + error = ErrorCode::FINDA_VS_EEPROM_DISREPANCY; + state = ProgressCode::ERRWaitingForUser; + } else { + error = ErrorCode::OK; + state = ProgressCode::OK; + } + break; + } + break; // mui::Event::Middle + } + break; // ProgressCode::ERRWaitingForUser + } + default: + // Do nothing + break; + } + return false; +} } // namespace logic diff --git a/src/logic/no_command.h b/src/logic/no_command.h index c65ef17..466313b 100644 --- a/src/logic/no_command.h +++ b/src/logic/no_command.h @@ -15,12 +15,15 @@ public: bool Reset(uint8_t /*param*/) override { return true; } /// @returns true if the state machine finished its job, false otherwise - bool StepInner() override { return true; } + bool StepInner() override; /// Used to report initialization errors (which can be reported if the UART started up). /// Intentionally only available in the "noCommand" operation /// which is only active when the MMU starts and before it gets any other command from the printer. - inline void SetInitError(ErrorCode ec) { error = ec; } + inline void SetInitError(ErrorCode ec) { + error = ec; + state = ProgressCode::ERRWaitingForUser; + } }; /// The one and only instance of NoCommand state machine in the FW diff --git a/src/main.cpp b/src/main.cpp index 80a0a2c..5e08350 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -90,20 +90,8 @@ static void setup2() { // which is abused to let the LEDs shine for ~100ms mf::finda.BlockingInit(); - 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); + if (!mf::finda.CheckFINDAvsEEPROM()) { logic::noCommand.SetInitError(ErrorCode::FINDA_VS_EEPROM_DISREPANCY); - } 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); } /// Turn off all leds diff --git a/src/modules/finda.cpp b/src/modules/finda.cpp index 77e9886..74f99af 100644 --- a/src/modules/finda.cpp +++ b/src/modules/finda.cpp @@ -1,5 +1,6 @@ /// @file finda.cpp #include "finda.h" +#include "globals.h" #include "timebase.h" #include "../hal/gpio.h" #include "../pins.h" @@ -21,5 +22,26 @@ 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); + ret = false; + } + return ret; +} + } // namespace finda } // namespace modules diff --git a/src/modules/finda.h b/src/modules/finda.h index f536a76..167e2cb 100644 --- a/src/modules/finda.h +++ b/src/modules/finda.h @@ -22,6 +22,9 @@ 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; };