From e2ee6f6ff1dfb312c4f28aca3189e65ffe5d1b8b Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Fri, 17 Feb 2023 10:22:29 +0100 Subject: [PATCH] Add X42 as EEPROM reset command + handling + visualization MMU-193 --- src/application.cpp | 54 +++++++++++++++++++++++++++++++++++++++++---- src/application.h | 1 + 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/application.cpp b/src/application.cpp index c462016..4578e73 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -2,12 +2,13 @@ #include "application.h" #include "registers.h" -#include "modules/leds.h" -#include "modules/globals.h" -#include "modules/user_input.h" #include "modules/finda.h" #include "modules/fsensor.h" +#include "modules/globals.h" +#include "modules/leds.h" +#include "modules/permanent_storage.h" #include "modules/serial.h" +#include "modules/user_input.h" #include "logic/command_base.h" #include "logic/cut_filament.h" @@ -179,6 +180,51 @@ void Application::ReportWriteRegister(const mp::RequestMsg &rq) { modules::serial::WriteToUSART(rsp, len); } +/// Performs a blocking (!) LED "snake" from one side to the other and back. +/// Intended only for visualization of EEPROM reset, not to be used in while the MMU is running normally +namespace EEPROMResetVis { + +static void Delay(){ + uint32_t start = mt::timebase.Millis(); + while( ! mt::timebase.Elapsed(start, 100) ){ + ml::leds.Step(); + } +} + +static void __attribute__((noinline)) Green(uint8_t i){ + ml::leds.SetPairButOffOthers(i, ml::on, ml::off); + Delay(); +} + +static void __attribute__((noinline)) Red(uint8_t i){ + ml::leds.SetPairButOffOthers(i, ml::on, ml::off); + Delay(); +} + +static void Run(){ + for(uint8_t i = 0; i < ml::leds.LedPairsCount(); ++i){ + Green(i); + Red(i); + } + for(uint8_t i = ml::leds.LedPairsCount(); i != 0; --i){ + Red(i); + Green(i); + } +} + +} // namespace EEPROMResetVis + +void Application::ProcessReset(uint8_t resetType){ + switch(resetType){ + case 42: // perform an EEPROM reset if the resetType == The Answer to the Ultimate Question of Life, the Universe, and Everything :) + mps::EraseAll(); + EEPROMResetVis::Run(); + [[fallthrough]]; + default: // anything else "just" restarts the board + hal::cpu::Reset(); + } +} + void Application::ProcessRequestMsg(const mp::RequestMsg &rq) { switch (rq.code) { case mp::RequestMsgCodes::Button: @@ -196,7 +242,7 @@ void Application::ProcessRequestMsg(const mp::RequestMsg &rq) { break; case mp::RequestMsgCodes::Reset: // immediately reset the board - there is no response in this case - hal::cpu::Reset(); + ProcessReset(rq.value); break; case mp::RequestMsgCodes::Version: case mp::RequestMsgCodes::Read: diff --git a/src/application.h b/src/application.h index a8cf10e..c0372eb 100644 --- a/src/application.h +++ b/src/application.h @@ -46,6 +46,7 @@ private: void ReportReadRegister(const mp::RequestMsg &rq); void ReportWriteRegister(const mp::RequestMsg &rq); void ProcessRequestMsg(const mp::RequestMsg &rq); + void ProcessReset(uint8_t resetType); uint16_t lastCommandProcessedMs;