@@ -8,6 +8,17 @@ namespace MMU2 {
8
8
9
9
static const uint8_t supportedMmuFWVersion[3 ] PROGMEM = { 2 , 1 , 3 };
10
10
11
+ const uint8_t ProtocolLogic::regs8Addrs[ProtocolLogic::regs8Count] PROGMEM = {
12
+ 8 , // FINDA state
13
+ 0x1b , // Selector slot
14
+ 0x1c , // Idler slot
15
+ };
16
+
17
+ const uint8_t ProtocolLogic::regs16Addrs[ProtocolLogic::regs16Count] PROGMEM = {
18
+ 4 , // MMU errors - aka statistics
19
+ 0x1a , // Pulley position [mm]
20
+ };
21
+
11
22
void ProtocolLogic::CheckAndReportAsyncEvents () {
12
23
// even when waiting for a query period, we need to report a change in filament sensor's state
13
24
// - it is vital for a precise synchronization of moves of the printer and the MMU
@@ -22,9 +33,36 @@ void ProtocolLogic::SendQuery() {
22
33
scopeState = ScopeState::QuerySent;
23
34
}
24
35
25
- void ProtocolLogic::SendFINDAQuery () {
26
- SendMsg (RequestMsg (RequestMsgCodes::Finda, 0 ));
27
- scopeState = ScopeState::FINDAReqSent;
36
+ void ProtocolLogic::StartReading8bitRegisters () {
37
+ regIndex = 0 ;
38
+ SendReadRegister (pgm_read_byte (regs8Addrs + regIndex), ScopeState::Reading8bitRegisters);
39
+ }
40
+
41
+ void ProtocolLogic::ProcessRead8bitRegister (){
42
+ regs8[regIndex] = rsp.paramValue ;
43
+ ++regIndex;
44
+ if (regIndex >= regs8Count){
45
+ // proceed with reading 16bit registers
46
+ StartReading16bitRegisters ();
47
+ } else {
48
+ SendReadRegister (pgm_read_byte (regs8Addrs + regIndex), ScopeState::Reading8bitRegisters);
49
+ }
50
+ }
51
+
52
+ void ProtocolLogic::StartReading16bitRegisters () {
53
+ regIndex = 0 ;
54
+ SendReadRegister (pgm_read_byte (regs16Addrs + regIndex), ScopeState::Reading16bitRegisters);
55
+ }
56
+
57
+ ProtocolLogic::ScopeState __attribute__ ((noinline)) ProtocolLogic::ProcessRead16bitRegister(ProtocolLogic::ScopeState stateAtEnd){
58
+ regs16[regIndex] = rsp.paramValue ;
59
+ ++regIndex;
60
+ if (regIndex >= regs16Count){
61
+ return stateAtEnd;
62
+ } else {
63
+ SendReadRegister (pgm_read_byte (regs16Addrs + regIndex), ScopeState::Reading16bitRegisters);
64
+ }
65
+ return ScopeState::Reading16bitRegisters;
28
66
}
29
67
30
68
void ProtocolLogic::SendAndUpdateFilamentSensor () {
@@ -313,14 +351,13 @@ StepStatus ProtocolLogic::CommandStep() {
313
351
case ScopeState::QuerySent:
314
352
return ProcessCommandQueryResponse ();
315
353
case ScopeState::FilamentSensorStateSent:
316
- SendFINDAQuery ();
354
+ StartReading8bitRegisters ();
317
355
return Processing;
318
- case ScopeState::FINDAReqSent:
319
- findaPressed = rsp.paramValue ;
320
- SendReadRegister (4 , ScopeState::StatisticsSent);
356
+ case ScopeState::Reading8bitRegisters:
357
+ ProcessRead8bitRegister ();
321
358
return Processing;
322
- case ScopeState::StatisticsSent :
323
- scopeState = ScopeState::Wait;
359
+ case ScopeState::Reading16bitRegisters :
360
+ scopeState = ProcessRead16bitRegister ( ScopeState::Wait) ;
324
361
return Processing;
325
362
case ScopeState::ButtonSent:
326
363
if (rsp.paramCode == ResponseMsgParamCodes::Accepted) {
@@ -371,7 +408,7 @@ StepStatus ProtocolLogic::IdleStep() {
371
408
// The user pushed a button on the MMU. Save it, do what we need to do
372
409
// to prepare, then pass it back to the MMU so it can work its magic.
373
410
buttonCode = static_cast <Buttons>(rsp.paramValue );
374
- SendFINDAQuery ();
411
+ StartReading8bitRegisters ();
375
412
return ButtonPushed;
376
413
case ResponseMsgParamCodes::Processing:
377
414
// @@TODO we may actually use this branch to report progress of manual operation on the MMU
@@ -382,29 +419,27 @@ StepStatus ProtocolLogic::IdleStep() {
382
419
break ;
383
420
default :
384
421
errorCode = static_cast <ErrorCode>(rsp.paramValue );
385
- SendFINDAQuery (); // continue Idle state without restarting the communication
422
+ StartReading8bitRegisters (); // continue Idle state without restarting the communication
386
423
return CommandError;
387
424
}
388
425
break ;
389
426
default :
390
427
return ProtocolError;
391
428
}
392
- SendFINDAQuery ();
429
+ StartReading8bitRegisters ();
393
430
return Processing;
394
- case ScopeState::FINDAReqSent:
395
- findaPressed = rsp.paramValue ;
396
- SendReadRegister (4 , ScopeState::StatisticsSent);
431
+ case ScopeState::Reading8bitRegisters:
432
+ ProcessRead8bitRegister ();
397
433
return Processing;
398
- case ScopeState::StatisticsSent:
399
- failStatistics = rsp.paramValue ;
400
- scopeState = ScopeState::Ready;
401
- return Finished;
434
+ case ScopeState::Reading16bitRegisters:
435
+ scopeState = ProcessRead16bitRegister (ScopeState::Ready);
436
+ return scopeState == ScopeState::Ready ? Finished : Processing;
402
437
case ScopeState::ButtonSent:
403
438
if (rsp.paramCode == ResponseMsgParamCodes::Accepted) {
404
439
// Button was accepted, decrement the retry.
405
440
mmu2.DecrementRetryAttempts ();
406
441
}
407
- SendFINDAQuery ();
442
+ StartReading8bitRegisters ();
408
443
return Processing;
409
444
case ScopeState::ReadRegisterSent:
410
445
if (rsp.paramCode == ResponseMsgParamCodes::Accepted) {
@@ -444,8 +479,9 @@ ProtocolLogic::ProtocolLogic(MMU2Serial *uart)
444
479
, progressCode(ProgressCode::OK)
445
480
, buttonCode(NoButton)
446
481
, lastFSensor((uint8_t )WhereIsFilament())
447
- , findaPressed(false )
448
- , failStatistics(0 )
482
+ , regs8 { 0 , 0 , 0 }
483
+ , regs16 { 0 , 0 }
484
+ , regIndex(0 )
449
485
, mmuFwVersion { 0 , 0 , 0 }
450
486
{}
451
487
0 commit comments