Skip to content

Commit e738b68

Browse files
committed
feat: move state transition to process functions
1 parent 3ad8233 commit e738b68

1 file changed

Lines changed: 61 additions & 36 deletions

File tree

TissueProcessor/TissueProcessor.ino

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -531,13 +531,16 @@ bool unknownDirectionPredicate(id_t id);
531531
void unknownDirectionActionChanged(EventArgs e);
532532

533533
bool middlePredicate(id_t id);
534+
void middleProcess(id_t id);
534535
void middleActionChanged(EventArgs e);
535536

536537
bool upRecoveryPredicate(id_t id);
537538
void upRecoveryActionChanged(EventArgs e);
539+
void UpRecoveryProcess(id_t id);
538540

539541
bool idlePredicate(id_t id);
540542
void idleActionChanged(EventArgs e);
543+
void idleProcess(id_t id);
541544

542545
bool checkingPredicate(id_t id);
543546
void checkingActionChanged(EventArgs e);
@@ -562,8 +565,9 @@ void raisingActionChanged(EventArgs e);
562565
bool checkingPredicate(id_t id);
563566
void checkingActionChanged(EventArgs e);
564567

565-
bool transitiningPredicate(id_t id);
566-
void transitiningActionChanged(EventArgs e);
568+
bool transitioningPredicate(id_t id);
569+
void transitioningProcess(id_t id);
570+
void transitioningActionChanged(EventArgs e);
567571

568572
void errorActionChanged(EventArgs e);
569573

@@ -584,14 +588,14 @@ Transition transitions[] = {
584588

585589
// S_MIDDLE_RECOVERY: if sample moved to new tank -> S_STARTING_NEW_TANK
586590
// otherwise start lowing in same tank
587-
{middlePredicate, S_MIDDLE_RECOVERY, S_UP, nullptr, middleActionChanged},
591+
{middlePredicate, S_MIDDLE_RECOVERY, S_UP, middleProcess, middleActionChanged},
588592

589593
// S_UP_RECOVERY: if sample moved to new tank -> S_STARTING_NEW_TANK
590594
// otherwise start lowing in same tank
591-
{upRecoveryPredicate, S_UP_RECOVERY, S_STARTING_NEW_TANK, nullptr, upRecoveryActionChanged},
595+
{upRecoveryPredicate, S_UP_RECOVERY, S_STARTING_NEW_TANK, UpRecoveryProcess, upRecoveryActionChanged},
592596

593597
// S_IDLE: wait start button. When pressed -> CHECK_START
594-
{idlePredicate, S_IDLE, S_PRE_DOWN, nullptr, idleActionChanged},
598+
{idlePredicate, S_IDLE, S_PRE_DOWN, idleProcess, idleActionChanged},
595599

596600
// S_PRE_DOWN: Just wait for MOTOR_SWITCH_DELAY_MS before moving to next
597601
// state
@@ -624,7 +628,7 @@ Transition transitions[] = {
624628

625629
// S_TRANSITIONING: small delay between tanks, then either go to next tank's
626630
// logic or to START if finished
627-
{transitiningPredicate, S_TRANSITIONING, S_STARTING_NEW_TANK, nullptr, transitiningActionChanged},
631+
{transitioningPredicate, S_TRANSITIONING, S_STARTING_NEW_TANK, transitioningProcess, transitioningActionChanged},
628632

629633
// S_STARTING_NEW_TANK: read tank and prepare moving down the sample
630634
{startingPredicate, S_STARTING_NEW_TANK, S_LOWERING, nullptr, startingActionChanged},
@@ -681,10 +685,14 @@ bool upRecoveryPredicate(id_t id)
681685
if (topLimit.isActive() && tankChanged)
682686
return true;
683687

688+
return false;
689+
}
690+
691+
void UpRecoveryProcess(id_t id)
692+
{
684693
if (!bottomLimit.isActive() && !topLimit.isActive())
685694
fsm.begin(S_LOWERING);
686-
687-
return false;
695+
return;
688696
}
689697

690698
void upRecoveryActionChanged(EventArgs e)
@@ -707,15 +715,20 @@ void upRecoveryActionChanged(EventArgs e)
707715

708716
bool middlePredicate(id_t id)
709717
{
710-
if (bottomLimit.isActive())
711-
fsm.begin(S_PRE_DOWN);
712-
713718
if (topLimit.isActive())
714719
return true;
715720

716721
return false;
717722
}
718723

724+
void middleProcess(id_t id)
725+
{
726+
if (bottomLimit.isActive())
727+
fsm.begin(S_PRE_DOWN);
728+
729+
return;
730+
}
731+
719732
void middleActionChanged(EventArgs e)
720733
{
721734
switch (e.action)
@@ -743,18 +756,21 @@ bool idlePredicate(id_t id)
743756
startButton.reset();
744757
return true;
745758
}
759+
760+
return false;
761+
}
762+
void idleProcess(id_t id)
763+
{
746764
if (skipButton.isActive())
747765
{
748766
vibOff();
749767
skipButton.reset();
750768
DBGLN("Raising to top");
751769
lcdShowStatus(F("Skip tank"), F("Raising..."));
752770
fsm.begin(S_RAISING);
771+
return;
753772
}
754-
755-
return false;
756773
}
757-
758774
void idleActionChanged(EventArgs e)
759775
{
760776
switch (e.action)
@@ -840,12 +856,6 @@ void loweringActionChanged(EventArgs e)
840856

841857
bool downPredicate(id_t id)
842858
{
843-
if (!bottomLimit.isActive())
844-
{
845-
lcdShowStatus(F("ERROR"), F("TOP or BOTTOM S"));
846-
fsm.begin(S_ERROR);
847-
}
848-
849859
if (skipButton.isActive() || raiseButton.isActive())
850860
{
851861
vibOff();
@@ -856,14 +866,6 @@ bool downPredicate(id_t id)
856866
lcdShowStatus(F("Button Pressed"), F("Raising..."));
857867
return false;
858868
}
859-
if (startButton.isActive())
860-
{
861-
vibOff();
862-
moveOff();
863-
startButton.reset();
864-
lcdShowStatus(F("Button pressed"), F("Go to idle"));
865-
fsm.begin(S_IDLE);
866-
}
867869
if (waitingWaxMelt)
868870
{
869871
uint8_t s = getNextRequiredSensor(lastStableTank);
@@ -880,13 +882,28 @@ bool downPredicate(id_t id)
880882
}
881883
void downProcess(id_t id)
882884
{
885+
if (!bottomLimit.isActive())
886+
{
887+
lcdShowStatus(F("ERROR"), F("TOP or BOTTOM S"));
888+
fsm.begin(S_ERROR);
889+
return;
890+
}
891+
883892
if (finished)
884893
{
885894
vibOff();
886895
fsm.begin(S_IDLE);
887896
return;
888897
}
889-
898+
if (startButton.isActive())
899+
{
900+
vibOff();
901+
moveOff();
902+
startButton.reset();
903+
lcdShowStatus(F("Button pressed"), F("Go to idle"));
904+
fsm.begin(S_IDLE);
905+
return;
906+
}
890907
vibOn();
891908

892909
if (waitingWaxMelt)
@@ -1041,19 +1058,25 @@ void raisingActionChanged(EventArgs e)
10411058
}
10421059
}
10431060

1044-
bool transitiningPredicate(id_t id)
1061+
bool transitioningPredicate(id_t id)
1062+
{
1063+
syncTankID();
1064+
1065+
return tankChanged;
1066+
}
1067+
1068+
void transitioningProcess(id_t id)
10451069
{
10461070
if (!topLimit.isActive())
10471071
{
10481072
lcdShowStatus(F("Critical Error"), F("Top sensor"));
10491073
fsm.begin(S_ERROR); // Top sensor is not active -> Error
1074+
return;
10501075
}
1051-
1052-
syncTankID();
1053-
1054-
return tankChanged;
1076+
return;
10551077
}
1056-
void transitiningActionChanged(EventArgs e)
1078+
1079+
void transitioningActionChanged(EventArgs e)
10571080
{
10581081
switch (e.action)
10591082
{
@@ -1176,12 +1199,14 @@ void verifyWaxConditions()
11761199
{
11771200
lcdShowStatus(F("Critical Error"), F("H1 or S1"));
11781201
fsm.begin(S_ERROR);
1202+
return;
11791203
}
11801204
if ((s & 2) && !wax2Ready.isActive())
11811205
{
11821206
lcdShowStatus(F("Critical Error"), F("H1 H2 or S1 S2"));
11831207
DBGLN("Critical error container 12 without melted wax");
11841208
fsm.begin(S_ERROR);
1209+
return;
11851210
}
11861211
}
11871212
void fsmTask() { fsm.execute(); }
@@ -1242,8 +1267,8 @@ void loop()
12421267
wdt_reset();
12431268
sensorTask();
12441269
buttonsTask();
1245-
safetyTask();
12461270
verifyWaxConditions();
1271+
safetyTask();
12471272
fsmTask();
12481273

12491274
#ifdef DEBUG

0 commit comments

Comments
 (0)