|
@@ -6,35 +6,33 @@
|
|
|
|
|
|
namespace MMU2 {
|
|
|
|
|
|
-static constexpr uint8_t supportedMmuFWVersionMajor = 2;
|
|
|
-static constexpr uint8_t supportedMmuFWVersionMinor = 1;
|
|
|
-static constexpr uint8_t supportedMmuFWVersionBuild = 1;
|
|
|
+static const uint8_t supportedMmuFWVersion[3] PROGMEM = { 2, 1, 1 };
|
|
|
|
|
|
-void ProtocolLogic::CheckAndReportAsyncEvents(){
|
|
|
+void ProtocolLogic::CheckAndReportAsyncEvents() {
|
|
|
// even when waiting for a query period, we need to report a change in filament sensor's state
|
|
|
// - it is vital for a precise synchronization of moves of the printer and the MMU
|
|
|
uint8_t fs = (uint8_t)WhereIsFilament();
|
|
|
- if( fs != lastFSensor ){
|
|
|
+ if (fs != lastFSensor) {
|
|
|
SendAndUpdateFilamentSensor();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void ProtocolLogic::SendQuery(){
|
|
|
+void ProtocolLogic::SendQuery() {
|
|
|
SendMsg(RequestMsg(RequestMsgCodes::Query, 0));
|
|
|
scopeState = ScopeState::QuerySent;
|
|
|
}
|
|
|
|
|
|
-void ProtocolLogic::SendFINDAQuery(){
|
|
|
- SendMsg(RequestMsg(RequestMsgCodes::Finda, 0 ) );
|
|
|
+void ProtocolLogic::SendFINDAQuery() {
|
|
|
+ SendMsg(RequestMsg(RequestMsgCodes::Finda, 0));
|
|
|
scopeState = ScopeState::FINDAReqSent;
|
|
|
}
|
|
|
|
|
|
-void ProtocolLogic::SendAndUpdateFilamentSensor(){
|
|
|
- SendMsg(RequestMsg(RequestMsgCodes::FilamentSensor, lastFSensor = (uint8_t)WhereIsFilament() ) );
|
|
|
+void ProtocolLogic::SendAndUpdateFilamentSensor() {
|
|
|
+ SendMsg(RequestMsg(RequestMsgCodes::FilamentSensor, lastFSensor = (uint8_t)WhereIsFilament()));
|
|
|
scopeState = ScopeState::FilamentSensorStateSent;
|
|
|
}
|
|
|
|
|
|
-void ProtocolLogic::SendButton(uint8_t btn){
|
|
|
+void ProtocolLogic::SendButton(uint8_t btn) {
|
|
|
SendMsg(RequestMsg(RequestMsgCodes::Button, btn));
|
|
|
scopeState = ScopeState::ButtonSent;
|
|
|
}
|
|
@@ -73,14 +71,14 @@ struct OldMMUFWDetector {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
-StepStatus ProtocolLogic::ExpectingMessage(uint32_t timeout) {
|
|
|
+StepStatus ProtocolLogic::ExpectingMessage() {
|
|
|
int bytesConsumed = 0;
|
|
|
int c = -1;
|
|
|
|
|
|
OldMMUFWDetector oldMMUh4x0r; // old MMU FW hacker ;)
|
|
|
|
|
|
// try to consume as many rx bytes as possible (until a message has been completed)
|
|
|
- while((c = uart->read()) >= 0){
|
|
|
+ while ((c = uart->read()) >= 0) {
|
|
|
++bytesConsumed;
|
|
|
RecordReceivedByte(c);
|
|
|
switch (protocol.DecodeResponse(c)) {
|
|
@@ -109,10 +107,10 @@ StepStatus ProtocolLogic::ExpectingMessage(uint32_t timeout) {
|
|
|
return ProtocolError;
|
|
|
}
|
|
|
}
|
|
|
- if( bytesConsumed != 0 ){
|
|
|
+ if (bytesConsumed != 0) {
|
|
|
RecordUARTActivity(); // something has happened on the UART, update the timeout record
|
|
|
- return Processing; // consumed some bytes, but message still not ready
|
|
|
- } else if (Elapsed(timeout)) {
|
|
|
+ return Processing; // consumed some bytes, but message still not ready
|
|
|
+ } else if (Elapsed(linkLayerTimeout)) {
|
|
|
return CommunicationTimeout;
|
|
|
}
|
|
|
return Processing;
|
|
@@ -144,123 +142,147 @@ void ProtocolLogic::IdleRestart() {
|
|
|
scopeState = ScopeState::Ready;
|
|
|
}
|
|
|
|
|
|
-StepStatus ProtocolLogic::StartSeqStep(){
|
|
|
- if (auto expmsg = ExpectingMessage(linkLayerTimeout); expmsg != MessageReady)
|
|
|
- return expmsg;
|
|
|
-
|
|
|
- // solve initial handshake
|
|
|
- switch (scopeState) {
|
|
|
- case ScopeState::S0Sent: // received response to S0 - major
|
|
|
- if( rsp.request.code != RequestMsgCodes::Version || rsp.request.value != 0 ){
|
|
|
- // got a response to something else - protocol corruption probably, repeat the query
|
|
|
- SendVersion(0);
|
|
|
- } else {
|
|
|
- mmuFwVersionMajor = rsp.paramValue;
|
|
|
- if (mmuFwVersionMajor != supportedMmuFWVersionMajor) {
|
|
|
- if( --retries == 0){
|
|
|
- // if (--retries == 0) has a specific meaning - since we are losing bytes on the UART for no obvious reason
|
|
|
- // it can happen, that the reported version number is not complete - i.e. "1" instead of "19"
|
|
|
- // Therefore we drop the MMU only if we run out of retries for this very reason.
|
|
|
- // There is a limited amount of retries per the whole start seq.
|
|
|
- // We also must be able to actually detect an unsupported MMU FW version, so the amount of retries shall be kept small.
|
|
|
- return VersionMismatch;
|
|
|
- } else {
|
|
|
- SendVersion(0);
|
|
|
- }
|
|
|
+StepStatus ProtocolLogic::ProcessVersionResponse(uint8_t stage) {
|
|
|
+ if (rsp.request.code != RequestMsgCodes::Version || rsp.request.value != stage) {
|
|
|
+ // got a response to something else - protocol corruption probably, repeat the query OR restart the comm by issuing S0?
|
|
|
+ SendVersion(stage);
|
|
|
+ } else {
|
|
|
+ mmuFwVersion[stage] = rsp.paramValue;
|
|
|
+ if (mmuFwVersion[stage] != pgm_read_byte(supportedMmuFWVersion[stage])) {
|
|
|
+ if (--retries == 0) {
|
|
|
+ return VersionMismatch;
|
|
|
} else {
|
|
|
- dataTO.Reset(); // got meaningful response from the MMU, stop data layer timeout tracking
|
|
|
- SendVersion(1);
|
|
|
+ SendVersion(stage);
|
|
|
}
|
|
|
- }
|
|
|
- break;
|
|
|
- case ScopeState::S1Sent: // received response to S1 - minor
|
|
|
- if( rsp.request.code != RequestMsgCodes::Version || rsp.request.value != 1 ){
|
|
|
- // got a response to something else - protocol corruption probably, repeat the query OR restart the comm by issuing S0?
|
|
|
- SendVersion(1);
|
|
|
} else {
|
|
|
- mmuFwVersionMinor = rsp.paramValue;
|
|
|
- if (mmuFwVersionMinor != supportedMmuFWVersionMinor){
|
|
|
- if( --retries == 0) {
|
|
|
- return VersionMismatch;
|
|
|
- } else {
|
|
|
- SendVersion(1);
|
|
|
- }
|
|
|
- } else {
|
|
|
- SendVersion(2);
|
|
|
- }
|
|
|
+ dataTO.Reset(); // got a meaningful response from the MMU, stop data layer timeout tracking
|
|
|
+ SendVersion(stage + 1);
|
|
|
}
|
|
|
- break;
|
|
|
- case ScopeState::S2Sent: // received response to S2 - revision
|
|
|
- if( rsp.request.code != RequestMsgCodes::Version || rsp.request.value != 2 ){
|
|
|
+ }
|
|
|
+ return Processing;
|
|
|
+}
|
|
|
+
|
|
|
+StepStatus ProtocolLogic::ScopeStep() {
|
|
|
+ if ((uint_fast8_t)scopeState & (uint8_t)ScopeState::NotExpectsResponse) {
|
|
|
+ // we are waiting for something
|
|
|
+ switch (currentScope) {
|
|
|
+ case Scope::DelayedRestart:
|
|
|
+ return DelayedRestartWait();
|
|
|
+ case Scope::Idle:
|
|
|
+ return IdleWait();
|
|
|
+ case Scope::Command:
|
|
|
+ return CommandWait();
|
|
|
+ case Scope::Stopped:
|
|
|
+ return StoppedStep();
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // we are expecting a message
|
|
|
+ if (auto expmsg = ExpectingMessage(); expmsg != MessageReady) // this whole statement takes 12B
|
|
|
+ return expmsg;
|
|
|
+
|
|
|
+ // process message
|
|
|
+ switch (currentScope) {
|
|
|
+ case Scope::StartSeq:
|
|
|
+ return StartSeqStep(); // ~270B
|
|
|
+ case Scope::Idle:
|
|
|
+ return IdleStep(); // ~300B
|
|
|
+ case Scope::Command:
|
|
|
+ return CommandStep(); // ~430B
|
|
|
+ case Scope::Stopped:
|
|
|
+ return StoppedStep();
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Finished;
|
|
|
+}
|
|
|
+
|
|
|
+StepStatus ProtocolLogic::StartSeqStep() {
|
|
|
+ // solve initial handshake
|
|
|
+ switch (scopeState) {
|
|
|
+ case ScopeState::S0Sent: // received response to S0 - major
|
|
|
+ case ScopeState::S1Sent: // received response to S1 - minor
|
|
|
+ case ScopeState::S2Sent: // received response to S2 - minor
|
|
|
+ return ProcessVersionResponse((uint8_t)scopeState - (uint8_t)ScopeState::S0Sent);
|
|
|
+ case ScopeState::S3Sent: // received response to S3 - revision
|
|
|
+ if (rsp.request.code != RequestMsgCodes::Version || rsp.request.value != 3) {
|
|
|
// got a response to something else - protocol corruption probably, repeat the query OR restart the comm by issuing S0?
|
|
|
- SendVersion(2);
|
|
|
+ SendVersion(3);
|
|
|
} else {
|
|
|
- mmuFwVersionBuild = rsp.paramValue;
|
|
|
- if (mmuFwVersionBuild < supportedMmuFWVersionBuild){
|
|
|
- if( --retries == 0 ) {
|
|
|
- return VersionMismatch;
|
|
|
- } else {
|
|
|
- SendVersion(2);
|
|
|
- }
|
|
|
- } else {
|
|
|
- // Start General Interrogation after line up.
|
|
|
- // For now we just send the state of the filament sensor, but we may request
|
|
|
- // data point states from the MMU as well. TBD in the future, especially with another protocol
|
|
|
- SendAndUpdateFilamentSensor();
|
|
|
- }
|
|
|
+ mmuFwVersionBuild = rsp.paramValue; // just register the build number
|
|
|
+ // Start General Interrogation after line up.
|
|
|
+ // For now we just send the state of the filament sensor, but we may request
|
|
|
+ // data point states from the MMU as well. TBD in the future, especially with another protocol
|
|
|
+ SendAndUpdateFilamentSensor();
|
|
|
}
|
|
|
- break;
|
|
|
+ return Processing;
|
|
|
case ScopeState::FilamentSensorStateSent:
|
|
|
- scopeState = ScopeState::Ready;
|
|
|
SwitchFromStartToIdle();
|
|
|
return Processing; // Returning Finished is not a good idea in case of a fast error recovery
|
|
|
// - it tells the printer, that the command which experienced a protocol error and recovered successfully actually terminated.
|
|
|
// In such a case we must return "Processing" in order to keep the MMU state machine running and prevent the printer from executing next G-codes.
|
|
|
break;
|
|
|
- case ScopeState::RecoveringProtocolError:
|
|
|
- // timer elapsed, clear the input buffer
|
|
|
- while (uart->read() >= 0)
|
|
|
- ;
|
|
|
- SendVersion(0);
|
|
|
- break;
|
|
|
default:
|
|
|
return VersionMismatch;
|
|
|
}
|
|
|
return Finished;
|
|
|
}
|
|
|
|
|
|
-StepStatus ProtocolLogic::DelayedRestartStep() {
|
|
|
- switch (scopeState) {
|
|
|
- case ScopeState::RecoveringProtocolError:
|
|
|
- if (Elapsed(heartBeatPeriod)) { // this basically means, that we are waiting until there is some traffic on
|
|
|
- while (uart->read() != -1)
|
|
|
- ; // clear the input buffer
|
|
|
- // switch to StartSeq
|
|
|
- Start();
|
|
|
- }
|
|
|
+StepStatus ProtocolLogic::DelayedRestartWait() {
|
|
|
+ if (Elapsed(heartBeatPeriod)) { // this basically means, that we are waiting until there is some traffic on
|
|
|
+ while (uart->read() != -1)
|
|
|
+ ; // clear the input buffer
|
|
|
+ // switch to StartSeq
|
|
|
+ Start();
|
|
|
+ }
|
|
|
+ return Processing;
|
|
|
+}
|
|
|
+
|
|
|
+StepStatus ProtocolLogic::CommandWait() {
|
|
|
+ if (Elapsed(heartBeatPeriod)) {
|
|
|
+ SendQuery();
|
|
|
+ } else {
|
|
|
+ // even when waiting for a query period, we need to report a change in filament sensor's state
|
|
|
+ // - it is vital for a precise synchronization of moves of the printer and the MMU
|
|
|
+ CheckAndReportAsyncEvents();
|
|
|
+ }
|
|
|
+ return Processing;
|
|
|
+}
|
|
|
+
|
|
|
+StepStatus ProtocolLogic::ProcessCommandQueryResponse() {
|
|
|
+ switch (rsp.paramCode) {
|
|
|
+ case ResponseMsgParamCodes::Processing:
|
|
|
+ progressCode = static_cast<ProgressCode>(rsp.paramValue);
|
|
|
+ errorCode = ErrorCode::OK;
|
|
|
+ SendAndUpdateFilamentSensor(); // keep on reporting the state of fsensor regularly
|
|
|
return Processing;
|
|
|
- break;
|
|
|
+ case ResponseMsgParamCodes::Error:
|
|
|
+ // in case of an error the progress code remains as it has been before
|
|
|
+ errorCode = static_cast<ErrorCode>(rsp.paramValue);
|
|
|
+ // keep on reporting the state of fsensor regularly even in command error state
|
|
|
+ // - the MMU checks FINDA and fsensor even while recovering from errors
|
|
|
+ SendAndUpdateFilamentSensor();
|
|
|
+ return CommandError;
|
|
|
+ case ResponseMsgParamCodes::Button:
|
|
|
+ // The user pushed a button on the MMU. Save it, do what we need to do
|
|
|
+ // to prepare, then pass it back to the MMU so it can work its magic.
|
|
|
+ buttonCode = static_cast<Buttons>(rsp.paramValue);
|
|
|
+ SendAndUpdateFilamentSensor();
|
|
|
+ return ButtonPushed;
|
|
|
+ case ResponseMsgParamCodes::Finished:
|
|
|
+ progressCode = ProgressCode::OK;
|
|
|
+ scopeState = ScopeState::Ready;
|
|
|
+ return Finished;
|
|
|
default:
|
|
|
- break;
|
|
|
+ return ProtocolError;
|
|
|
}
|
|
|
- return Finished;
|
|
|
}
|
|
|
|
|
|
StepStatus ProtocolLogic::CommandStep() {
|
|
|
switch (scopeState) {
|
|
|
- case ScopeState::Wait:
|
|
|
- if (Elapsed(heartBeatPeriod)) {
|
|
|
- SendQuery();
|
|
|
- } else {
|
|
|
- // even when waiting for a query period, we need to report a change in filament sensor's state
|
|
|
- // - it is vital for a precise synchronization of moves of the printer and the MMU
|
|
|
- CheckAndReportAsyncEvents();
|
|
|
- }
|
|
|
- break;
|
|
|
case ScopeState::CommandSent: {
|
|
|
- if (auto expmsg = ExpectingMessage(linkLayerTimeout); expmsg != MessageReady)
|
|
|
- return expmsg;
|
|
|
-
|
|
|
switch (rsp.paramCode) { // the response should be either accepted or rejected
|
|
|
case ResponseMsgParamCodes::Accepted:
|
|
|
progressCode = ProgressCode::OK;
|
|
@@ -275,59 +297,21 @@ StepStatus ProtocolLogic::CommandStep() {
|
|
|
default:
|
|
|
return ProtocolError;
|
|
|
}
|
|
|
- } break;
|
|
|
+ } break;
|
|
|
case ScopeState::QuerySent:
|
|
|
- if (auto expmsg = ExpectingMessage(linkLayerTimeout); expmsg != MessageReady)
|
|
|
- return expmsg;
|
|
|
- [[fallthrough]];
|
|
|
- case ScopeState::ContinueFromIdle:
|
|
|
- switch (rsp.paramCode) {
|
|
|
- case ResponseMsgParamCodes::Processing:
|
|
|
- progressCode = static_cast<ProgressCode>(rsp.paramValue);
|
|
|
- errorCode = ErrorCode::OK;
|
|
|
- SendAndUpdateFilamentSensor(); // keep on reporting the state of fsensor regularly
|
|
|
- break;
|
|
|
- case ResponseMsgParamCodes::Error:
|
|
|
- // in case of an error the progress code remains as it has been before
|
|
|
- errorCode = static_cast<ErrorCode>(rsp.paramValue);
|
|
|
- // keep on reporting the state of fsensor regularly even in command error state
|
|
|
- // - the MMU checks FINDA and fsensor even while recovering from errors
|
|
|
- SendAndUpdateFilamentSensor();
|
|
|
- return CommandError;
|
|
|
- case ResponseMsgParamCodes::Button:
|
|
|
- // The user pushed a button on the MMU. Save it, do what we need to do
|
|
|
- // to prepare, then pass it back to the MMU so it can work its magic.
|
|
|
- buttonCode = static_cast<Buttons>(rsp.paramValue);
|
|
|
- SendAndUpdateFilamentSensor();
|
|
|
- return ButtonPushed;
|
|
|
- case ResponseMsgParamCodes::Finished:
|
|
|
- progressCode = ProgressCode::OK;
|
|
|
- scopeState = ScopeState::Ready;
|
|
|
- return Finished;
|
|
|
- default:
|
|
|
- return ProtocolError;
|
|
|
- }
|
|
|
- break;
|
|
|
+ return ProcessCommandQueryResponse();
|
|
|
case ScopeState::FilamentSensorStateSent:
|
|
|
- if (auto expmsg = ExpectingMessage(linkLayerTimeout); expmsg != MessageReady)
|
|
|
- return expmsg;
|
|
|
SendFINDAQuery();
|
|
|
scopeState = ScopeState::FINDAReqSent;
|
|
|
return Processing;
|
|
|
case ScopeState::FINDAReqSent:
|
|
|
- if (auto expmsg = ExpectingMessage(linkLayerTimeout); expmsg != MessageReady)
|
|
|
- return expmsg;
|
|
|
- SendReadRegister(3, ScopeState::StatisticsSent);
|
|
|
+ SendReadRegister(4, ScopeState::StatisticsSent);
|
|
|
scopeState = ScopeState::StatisticsSent;
|
|
|
return Processing;
|
|
|
case ScopeState::StatisticsSent:
|
|
|
- if (auto expmsg = ExpectingMessage(linkLayerTimeout); expmsg != MessageReady)
|
|
|
- return expmsg;
|
|
|
scopeState = ScopeState::Wait;
|
|
|
return Processing;
|
|
|
case ScopeState::ButtonSent:
|
|
|
- if (auto expmsg = ExpectingMessage(linkLayerTimeout); expmsg != MessageReady)
|
|
|
- return expmsg;
|
|
|
if (rsp.paramCode == ResponseMsgParamCodes::Accepted) {
|
|
|
// Button was accepted, decrement the retry.
|
|
|
mmu2.DecrementRetryAttempts();
|
|
@@ -340,83 +324,82 @@ StepStatus ProtocolLogic::CommandStep() {
|
|
|
return Processing;
|
|
|
}
|
|
|
|
|
|
-StepStatus ProtocolLogic::IdleStep() {
|
|
|
- if(scopeState == ScopeState::Ready){ // check timeout
|
|
|
+StepStatus ProtocolLogic::IdleWait() {
|
|
|
+ if (scopeState == ScopeState::Ready) { // check timeout
|
|
|
if (Elapsed(heartBeatPeriod)) {
|
|
|
SendQuery();
|
|
|
return Processing;
|
|
|
}
|
|
|
- } else {
|
|
|
- if (auto expmsg = ExpectingMessage(linkLayerTimeout); expmsg != MessageReady)
|
|
|
- return expmsg;
|
|
|
-
|
|
|
- switch (scopeState) {
|
|
|
- case ScopeState::QuerySent: // check UART
|
|
|
- // If we are accidentally in Idle and we receive something like "T0 P1" - that means the communication dropped out while a command was in progress.
|
|
|
- // That causes no issues here, we just need to switch to Command processing and continue there from now on.
|
|
|
- // The usual response in this case should be some command and "F" - finished - that confirms we are in an Idle state even on the MMU side.
|
|
|
- switch( rsp.request.code ){
|
|
|
- case RequestMsgCodes::Cut:
|
|
|
- case RequestMsgCodes::Eject:
|
|
|
- case RequestMsgCodes::Load:
|
|
|
- case RequestMsgCodes::Mode:
|
|
|
- case RequestMsgCodes::Tool:
|
|
|
- case RequestMsgCodes::Unload:
|
|
|
- if( rsp.paramCode != ResponseMsgParamCodes::Finished ){
|
|
|
- SwitchFromIdleToCommand();
|
|
|
- return Processing;
|
|
|
- }
|
|
|
- break;
|
|
|
- case RequestMsgCodes::Reset:
|
|
|
- // this one is kind of special
|
|
|
- // we do not transfer to any "running" command (i.e. we stay in Idle),
|
|
|
- // but in case there is an error reported we must make sure it gets propagated
|
|
|
- switch( rsp.paramCode ){
|
|
|
- case ResponseMsgParamCodes::Button:
|
|
|
- // The user pushed a button on the MMU. Save it, do what we need to do
|
|
|
- // to prepare, then pass it back to the MMU so it can work its magic.
|
|
|
- buttonCode = static_cast<Buttons>(rsp.paramValue);
|
|
|
- SendFINDAQuery();
|
|
|
- return ButtonPushed;
|
|
|
- case ResponseMsgParamCodes::Processing:
|
|
|
- // @@TODO we may actually use this branch to report progress of manual operation on the MMU
|
|
|
- // The MMU sends e.g. X0 P27 after its restart when the user presses an MMU button to move the Selector
|
|
|
- // For now let's behave just like "finished"
|
|
|
- case ResponseMsgParamCodes::Finished:
|
|
|
- errorCode = ErrorCode::OK;
|
|
|
- break;
|
|
|
- default:
|
|
|
- errorCode = static_cast<ErrorCode>(rsp.paramValue);
|
|
|
- SendFINDAQuery(); // continue Idle state without restarting the communication
|
|
|
- return CommandError;
|
|
|
- }
|
|
|
- break;
|
|
|
- default:
|
|
|
- return ProtocolError;
|
|
|
+ }
|
|
|
+ return Finished;
|
|
|
+}
|
|
|
+
|
|
|
+StepStatus ProtocolLogic::IdleStep() {
|
|
|
+ switch (scopeState) {
|
|
|
+ case ScopeState::QuerySent: // check UART
|
|
|
+ // If we are accidentally in Idle and we receive something like "T0 P1" - that means the communication dropped out while a command was in progress.
|
|
|
+ // That causes no issues here, we just need to switch to Command processing and continue there from now on.
|
|
|
+ // The usual response in this case should be some command and "F" - finished - that confirms we are in an Idle state even on the MMU side.
|
|
|
+ switch (rsp.request.code) {
|
|
|
+ case RequestMsgCodes::Cut:
|
|
|
+ case RequestMsgCodes::Eject:
|
|
|
+ case RequestMsgCodes::Load:
|
|
|
+ case RequestMsgCodes::Mode:
|
|
|
+ case RequestMsgCodes::Tool:
|
|
|
+ case RequestMsgCodes::Unload:
|
|
|
+ if (rsp.paramCode != ResponseMsgParamCodes::Finished) {
|
|
|
+ return SwitchFromIdleToCommand();
|
|
|
}
|
|
|
- SendFINDAQuery();
|
|
|
- return Processing;
|
|
|
break;
|
|
|
- case ScopeState::FINDAReqSent:
|
|
|
- SendReadRegister(3, ScopeState::StatisticsSent);
|
|
|
- scopeState = ScopeState::StatisticsSent;
|
|
|
- return Processing;
|
|
|
- case ScopeState::StatisticsSent:
|
|
|
- failStatistics = rsp.paramValue;
|
|
|
- scopeState = ScopeState::Ready;
|
|
|
- return Processing;
|
|
|
- case ScopeState::ButtonSent:
|
|
|
- if (rsp.paramCode == ResponseMsgParamCodes::Accepted) {
|
|
|
- // Button was accepted, decrement the retry.
|
|
|
- mmu2.DecrementRetryAttempts();
|
|
|
+ case RequestMsgCodes::Reset:
|
|
|
+ // this one is kind of special
|
|
|
+ // we do not transfer to any "running" command (i.e. we stay in Idle),
|
|
|
+ // but in case there is an error reported we must make sure it gets propagated
|
|
|
+ switch (rsp.paramCode) {
|
|
|
+ case ResponseMsgParamCodes::Button:
|
|
|
+ // The user pushed a button on the MMU. Save it, do what we need to do
|
|
|
+ // to prepare, then pass it back to the MMU so it can work its magic.
|
|
|
+ buttonCode = static_cast<Buttons>(rsp.paramValue);
|
|
|
+ SendFINDAQuery();
|
|
|
+ return ButtonPushed;
|
|
|
+ case ResponseMsgParamCodes::Processing:
|
|
|
+ // @@TODO we may actually use this branch to report progress of manual operation on the MMU
|
|
|
+ // The MMU sends e.g. X0 P27 after its restart when the user presses an MMU button to move the Selector
|
|
|
+ // For now let's behave just like "finished"
|
|
|
+ case ResponseMsgParamCodes::Finished:
|
|
|
+ errorCode = ErrorCode::OK;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ errorCode = static_cast<ErrorCode>(rsp.paramValue);
|
|
|
+ SendFINDAQuery(); // continue Idle state without restarting the communication
|
|
|
+ return CommandError;
|
|
|
}
|
|
|
- SendFINDAQuery();
|
|
|
break;
|
|
|
default:
|
|
|
return ProtocolError;
|
|
|
}
|
|
|
+ SendFINDAQuery();
|
|
|
+ return Processing;
|
|
|
+ break;
|
|
|
+ case ScopeState::FINDAReqSent:
|
|
|
+ SendReadRegister(4, ScopeState::StatisticsSent);
|
|
|
+ scopeState = ScopeState::StatisticsSent;
|
|
|
+ return Processing;
|
|
|
+ case ScopeState::StatisticsSent:
|
|
|
+ failStatistics = rsp.paramValue;
|
|
|
+ scopeState = ScopeState::Ready;
|
|
|
+ return Finished;
|
|
|
+ case ScopeState::ButtonSent:
|
|
|
+ if (rsp.paramCode == ResponseMsgParamCodes::Accepted) {
|
|
|
+ // Button was accepted, decrement the retry.
|
|
|
+ mmu2.DecrementRetryAttempts();
|
|
|
+ }
|
|
|
+ SendFINDAQuery();
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ return ProtocolError;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// The "return Finished" in this state machine requires a bit of explanation:
|
|
|
// The Idle state either did nothing (still waiting for the heartbeat timeout)
|
|
|
// or just successfully received the answer to Q0, whatever that was.
|
|
@@ -443,9 +426,7 @@ ProtocolLogic::ProtocolLogic(MMU2Serial *uart)
|
|
|
, lastFSensor((uint8_t)WhereIsFilament())
|
|
|
, findaPressed(false)
|
|
|
, failStatistics(0)
|
|
|
- , mmuFwVersionMajor(0)
|
|
|
- , mmuFwVersionMinor(0)
|
|
|
- , mmuFwVersionBuild(0)
|
|
|
+ , mmuFwVersion { 0, 0, 0 }
|
|
|
{}
|
|
|
|
|
|
void ProtocolLogic::Start() {
|
|
@@ -480,7 +461,7 @@ void ProtocolLogic::EjectFilament(uint8_t slot) {
|
|
|
PlanGenericRequest(RequestMsg(RequestMsgCodes::Eject, slot));
|
|
|
}
|
|
|
|
|
|
-void ProtocolLogic::CutFilament(uint8_t slot){
|
|
|
+void ProtocolLogic::CutFilament(uint8_t slot) {
|
|
|
PlanGenericRequest(RequestMsg(RequestMsgCodes::Cut, slot));
|
|
|
}
|
|
|
|
|
@@ -488,28 +469,28 @@ void ProtocolLogic::ResetMMU() {
|
|
|
PlanGenericRequest(RequestMsg(RequestMsgCodes::Reset, 0));
|
|
|
}
|
|
|
|
|
|
-void ProtocolLogic::Button(uint8_t index){
|
|
|
+void ProtocolLogic::Button(uint8_t index) {
|
|
|
PlanGenericRequest(RequestMsg(RequestMsgCodes::Button, index));
|
|
|
}
|
|
|
|
|
|
-void ProtocolLogic::Home(uint8_t mode){
|
|
|
+void ProtocolLogic::Home(uint8_t mode) {
|
|
|
PlanGenericRequest(RequestMsg(RequestMsgCodes::Home, mode));
|
|
|
}
|
|
|
|
|
|
void ProtocolLogic::PlanGenericRequest(RequestMsg rq) {
|
|
|
plannedRq = rq;
|
|
|
- if( ! ExpectsResponse() ){
|
|
|
+ if (!ExpectsResponse()) {
|
|
|
ActivatePlannedRequest();
|
|
|
} // otherwise wait for an empty window to activate the request
|
|
|
}
|
|
|
|
|
|
-bool ProtocolLogic::ActivatePlannedRequest(){
|
|
|
- if( plannedRq.code == RequestMsgCodes::Button ){
|
|
|
+bool ProtocolLogic::ActivatePlannedRequest() {
|
|
|
+ if (plannedRq.code == RequestMsgCodes::Button) {
|
|
|
// only issue the button to the MMU and do not restart the state machines
|
|
|
SendButton(plannedRq.value);
|
|
|
plannedRq = RequestMsg(RequestMsgCodes::unknown, 0);
|
|
|
return true;
|
|
|
- } else if( plannedRq.code != RequestMsgCodes::unknown ){
|
|
|
+ } else if (plannedRq.code != RequestMsgCodes::unknown) {
|
|
|
currentScope = Scope::Command;
|
|
|
SetRequestMsg(plannedRq);
|
|
|
plannedRq = RequestMsg(RequestMsgCodes::unknown, 0);
|
|
@@ -519,12 +500,12 @@ bool ProtocolLogic::ActivatePlannedRequest(){
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
-void ProtocolLogic::SwitchFromIdleToCommand(){
|
|
|
+StepStatus ProtocolLogic::SwitchFromIdleToCommand() {
|
|
|
currentScope = Scope::Command;
|
|
|
SetRequestMsg(rsp.request);
|
|
|
// we are recovering from a communication drop out, the command is already running
|
|
|
// and we have just received a response to a Q0 message about a command progress
|
|
|
- CommandContinueFromIdle();
|
|
|
+ return ProcessCommandQueryResponse();
|
|
|
}
|
|
|
|
|
|
void ProtocolLogic::SwitchToIdle() {
|
|
@@ -533,7 +514,7 @@ void ProtocolLogic::SwitchToIdle() {
|
|
|
IdleRestart();
|
|
|
}
|
|
|
|
|
|
-void ProtocolLogic::SwitchFromStartToIdle(){
|
|
|
+void ProtocolLogic::SwitchFromStartToIdle() {
|
|
|
state = State::Running;
|
|
|
currentScope = Scope::Idle;
|
|
|
IdleRestart();
|
|
@@ -541,24 +522,6 @@ void ProtocolLogic::SwitchFromStartToIdle(){
|
|
|
scopeState = ScopeState::QuerySent;
|
|
|
}
|
|
|
|
|
|
-StepStatus ProtocolLogic::ScopeStep(){
|
|
|
- switch(currentScope){
|
|
|
- case Scope::StartSeq:
|
|
|
- return StartSeqStep();
|
|
|
- case Scope::DelayedRestart:
|
|
|
- return DelayedRestartStep();
|
|
|
- case Scope::Idle:
|
|
|
- return IdleStep();
|
|
|
- case Scope::Command:
|
|
|
- return CommandStep();
|
|
|
- case Scope::Stopped:
|
|
|
- return StoppedStep();
|
|
|
- default:
|
|
|
- break;
|
|
|
- }
|
|
|
- return Finished;
|
|
|
-}
|
|
|
-
|
|
|
bool ProtocolLogic::Elapsed(uint32_t timeout) const {
|
|
|
return _millis() >= (lastUARTActivityMs + timeout);
|
|
|
}
|
|
@@ -567,12 +530,12 @@ void ProtocolLogic::RecordUARTActivity() {
|
|
|
lastUARTActivityMs = _millis();
|
|
|
}
|
|
|
|
|
|
-void ProtocolLogic::RecordReceivedByte(uint8_t c){
|
|
|
+void ProtocolLogic::RecordReceivedByte(uint8_t c) {
|
|
|
lastReceivedBytes[lrb] = c;
|
|
|
- lrb = (lrb+1) % lastReceivedBytes.size();
|
|
|
+ lrb = (lrb + 1) % lastReceivedBytes.size();
|
|
|
}
|
|
|
|
|
|
-constexpr char NibbleToChar(uint8_t c){
|
|
|
+constexpr char NibbleToChar(uint8_t c) {
|
|
|
switch (c) {
|
|
|
case 0:
|
|
|
case 1:
|
|
@@ -597,42 +560,46 @@ constexpr char NibbleToChar(uint8_t c){
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void ProtocolLogic::FormatLastReceivedBytes(char *dst){
|
|
|
- for(uint8_t i = 0; i < lastReceivedBytes.size(); ++i){
|
|
|
- uint8_t b = lastReceivedBytes[ (lrb-i-1) % lastReceivedBytes.size() ];
|
|
|
- dst[i*3] = NibbleToChar(b >> 4);
|
|
|
- dst[i*3+1] = NibbleToChar(b & 0xf);
|
|
|
- dst[i*3+2] = ' ';
|
|
|
+void ProtocolLogic::FormatLastReceivedBytes(char *dst) {
|
|
|
+ for (uint8_t i = 0; i < lastReceivedBytes.size(); ++i) {
|
|
|
+ uint8_t b = lastReceivedBytes[(lrb - i - 1) % lastReceivedBytes.size()];
|
|
|
+ dst[i * 3] = NibbleToChar(b >> 4);
|
|
|
+ dst[i * 3 + 1] = NibbleToChar(b & 0xf);
|
|
|
+ dst[i * 3 + 2] = ' ';
|
|
|
}
|
|
|
- dst[ (lastReceivedBytes.size() - 1) * 3 + 2] = 0; // terminate properly
|
|
|
+ dst[(lastReceivedBytes.size() - 1) * 3 + 2] = 0; // terminate properly
|
|
|
}
|
|
|
|
|
|
-void ProtocolLogic::FormatLastResponseMsgAndClearLRB(char *dst){
|
|
|
+void ProtocolLogic::FormatLastResponseMsgAndClearLRB(char *dst) {
|
|
|
*dst++ = '<';
|
|
|
- for(uint8_t i = 0; i < lrb; ++i){
|
|
|
- uint8_t b = lastReceivedBytes[ i ];
|
|
|
- if( b < 32 )b = '.';
|
|
|
- if( b > 127 )b = '.';
|
|
|
+ for (uint8_t i = 0; i < lrb; ++i) {
|
|
|
+ uint8_t b = lastReceivedBytes[i];
|
|
|
+ if (b < 32)
|
|
|
+ b = '.';
|
|
|
+ if (b > 127)
|
|
|
+ b = '.';
|
|
|
*dst++ = b;
|
|
|
}
|
|
|
*dst = 0; // terminate properly
|
|
|
- lrb = 0; // reset the input buffer index in case of a clean message
|
|
|
+ lrb = 0; // reset the input buffer index in case of a clean message
|
|
|
}
|
|
|
|
|
|
-void ProtocolLogic::LogRequestMsg(const uint8_t *txbuff, uint8_t size){
|
|
|
+void ProtocolLogic::LogRequestMsg(const uint8_t *txbuff, uint8_t size) {
|
|
|
constexpr uint_fast8_t rqs = modules::protocol::Protocol::MaxRequestSize() + 2;
|
|
|
char tmp[rqs] = ">";
|
|
|
static char lastMsg[rqs] = "";
|
|
|
- for(uint8_t i = 0; i < size; ++i){
|
|
|
+ for (uint8_t i = 0; i < size; ++i) {
|
|
|
uint8_t b = txbuff[i];
|
|
|
- if( b < 32 )b = '.';
|
|
|
- if( b > 127 )b = '.';
|
|
|
- tmp[i+1] = b;
|
|
|
+ if (b < 32)
|
|
|
+ b = '.';
|
|
|
+ if (b > 127)
|
|
|
+ b = '.';
|
|
|
+ tmp[i + 1] = b;
|
|
|
}
|
|
|
- tmp[size+1] = '\n';
|
|
|
- tmp[size+2] = 0;
|
|
|
- if( !strncmp_P(tmp, PSTR(">S0*99.\n"), rqs) && !strncmp(lastMsg, tmp, rqs) ){
|
|
|
- // @@TODO we skip the repeated request msgs for now
|
|
|
+ tmp[size + 1] = '\n';
|
|
|
+ tmp[size + 2] = 0;
|
|
|
+ if (!strncmp_P(tmp, PSTR(">S0*99.\n"), rqs) && !strncmp(lastMsg, tmp, rqs)) {
|
|
|
+ // @@TODO we skip the repeated request msgs for now
|
|
|
// to avoid spoiling the whole log just with ">S0" messages
|
|
|
// especially when the MMU is not connected.
|
|
|
// We'll lose the ability to see if the printer is actually
|
|
@@ -644,16 +611,16 @@ void ProtocolLogic::LogRequestMsg(const uint8_t *txbuff, uint8_t size){
|
|
|
memcpy(lastMsg, tmp, rqs);
|
|
|
}
|
|
|
|
|
|
-void ProtocolLogic::LogError(const char *reason_P){
|
|
|
+void ProtocolLogic::LogError(const char *reason_P) {
|
|
|
char lrb[lastReceivedBytes.size() * 3];
|
|
|
FormatLastReceivedBytes(lrb);
|
|
|
-
|
|
|
+
|
|
|
MMU2_ERROR_MSGRPGM(reason_P);
|
|
|
SERIAL_ECHOPGM(", last bytes: ");
|
|
|
SERIAL_ECHOLN(lrb);
|
|
|
}
|
|
|
|
|
|
-void ProtocolLogic::LogResponse(){
|
|
|
+void ProtocolLogic::LogResponse() {
|
|
|
char lrb[lastReceivedBytes.size()];
|
|
|
FormatLastResponseMsgAndClearLRB(lrb);
|
|
|
MMU2_ECHO_MSG(lrb);
|
|
@@ -661,7 +628,7 @@ void ProtocolLogic::LogResponse(){
|
|
|
}
|
|
|
|
|
|
StepStatus ProtocolLogic::SuppressShortDropOuts(const char *msg_P, StepStatus ss) {
|
|
|
- if( dataTO.Record(ss) ){
|
|
|
+ if (dataTO.Record(ss)) {
|
|
|
LogError(msg_P);
|
|
|
return dataTO.InitialCause();
|
|
|
} else {
|
|
@@ -685,7 +652,7 @@ StepStatus ProtocolLogic::HandleProtocolError() {
|
|
|
}
|
|
|
|
|
|
StepStatus ProtocolLogic::Step() {
|
|
|
- if( ! ExpectsResponse() ){ // if not waiting for a response, activate a planned request immediately
|
|
|
+ if (!ExpectsResponse()) { // if not waiting for a response, activate a planned request immediately
|
|
|
ActivatePlannedRequest();
|
|
|
}
|
|
|
auto currentStatus = ScopeStep();
|
|
@@ -697,12 +664,12 @@ StepStatus ProtocolLogic::Step() {
|
|
|
// We are ok, switching to Idle if there is no potential next request planned.
|
|
|
// But the trouble is we must report a finished command if the previous command has just been finished
|
|
|
// i.e. only try to find some planned command if we just finished the Idle cycle
|
|
|
- bool previousCommandFinished = currentScope == Scope::Command; // @@TODO this is a nasty hack :(
|
|
|
- if( ! ActivatePlannedRequest() ){ // if nothing is planned, switch to Idle
|
|
|
+ bool previousCommandFinished = currentScope == Scope::Command; // @@TODO this is a nasty hack :(
|
|
|
+ if (!ActivatePlannedRequest()) { // if nothing is planned, switch to Idle
|
|
|
SwitchToIdle();
|
|
|
} else {
|
|
|
// if the previous cycle was Idle and now we have planned a new command -> avoid returning Finished
|
|
|
- if( ! previousCommandFinished && currentScope == Scope::Command){
|
|
|
+ if (!previousCommandFinished && currentScope == Scope::Command) {
|
|
|
currentStatus = Processing;
|
|
|
}
|
|
|
}
|
|
@@ -736,13 +703,13 @@ StepStatus ProtocolLogic::Step() {
|
|
|
}
|
|
|
|
|
|
uint8_t ProtocolLogic::CommandInProgress() const {
|
|
|
- if( currentScope != Scope::Command )
|
|
|
+ if (currentScope != Scope::Command)
|
|
|
return 0;
|
|
|
return (uint8_t)ReqMsg().code;
|
|
|
}
|
|
|
|
|
|
-bool DropOutFilter::Record(StepStatus ss){
|
|
|
- if( occurrences == maxOccurrences ){
|
|
|
+bool DropOutFilter::Record(StepStatus ss) {
|
|
|
+ if (occurrences == maxOccurrences) {
|
|
|
cause = ss;
|
|
|
}
|
|
|
--occurrences;
|