Refactor CheckFINDAvsEEPROM and setup2

pull/253/head
D.R.racer 2022-12-20 16:33:10 +01:00
parent 6dd5057b67
commit 074f89c751
5 changed files with 41 additions and 32 deletions

View File

@ -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

View File

@ -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

View File

@ -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();

View File

@ -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

View File

@ -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;
};