This library is designed to create and run state graphs. It supports hierarchical states and parallel states execution.

Committer:
martin13
Date:
Tue Feb 02 20:47:45 2021 +0000
Revision:
4:22b4462fcb26
Parent:
0:f4fdca2c4c67
Added unittests

Who changed what in which revision?

UserRevisionLine numberNew contents of line
martin13 0:f4fdca2c4c67 1 #ifndef __UTT_PARALLEL_STATES_H__
martin13 0:f4fdca2c4c67 2 #define __UTT_PARALLEL_STATES_H__
martin13 0:f4fdca2c4c67 3
martin13 0:f4fdca2c4c67 4 #include "ParallelStateMachine.h"
martin13 0:f4fdca2c4c67 5
martin13 0:f4fdca2c4c67 6 class S1 : public State{
martin13 0:f4fdca2c4c67 7
martin13 0:f4fdca2c4c67 8 public:
martin13 0:f4fdca2c4c67 9 static OUTCOME CONTINUE;
martin13 0:f4fdca2c4c67 10
martin13 0:f4fdca2c4c67 11 S1(const char* uuid, UserData* ud):
martin13 0:f4fdca2c4c67 12 State(uuid),
martin13 0:f4fdca2c4c67 13 m_count(0)
martin13 0:f4fdca2c4c67 14 {
martin13 0:f4fdca2c4c67 15 }
martin13 0:f4fdca2c4c67 16
martin13 0:f4fdca2c4c67 17 virtual void onEntry(){}
martin13 0:f4fdca2c4c67 18
martin13 0:f4fdca2c4c67 19 virtual const char* onExecute(){
martin13 0:f4fdca2c4c67 20 m_count++;
martin13 0:f4fdca2c4c67 21 if (m_count > 20){
martin13 0:f4fdca2c4c67 22 m_count = 0;
martin13 0:f4fdca2c4c67 23 return SUCCEDED;
martin13 0:f4fdca2c4c67 24 }
martin13 0:f4fdca2c4c67 25
martin13 0:f4fdca2c4c67 26 return CONTINUE;
martin13 0:f4fdca2c4c67 27 }
martin13 0:f4fdca2c4c67 28
martin13 0:f4fdca2c4c67 29 virtual void onExit(){}
martin13 0:f4fdca2c4c67 30
martin13 0:f4fdca2c4c67 31 private:
martin13 0:f4fdca2c4c67 32 int m_count;
martin13 0:f4fdca2c4c67 33 };
martin13 0:f4fdca2c4c67 34
martin13 0:f4fdca2c4c67 35 OUTCOME S1::CONTINUE = "CONTINUE";
martin13 0:f4fdca2c4c67 36
martin13 0:f4fdca2c4c67 37 class S2 : public State{
martin13 0:f4fdca2c4c67 38
martin13 0:f4fdca2c4c67 39 public:
martin13 0:f4fdca2c4c67 40
martin13 0:f4fdca2c4c67 41 S2(const char* uuid, UserData* ud):
martin13 0:f4fdca2c4c67 42 State(uuid)
martin13 0:f4fdca2c4c67 43 {
martin13 0:f4fdca2c4c67 44 }
martin13 0:f4fdca2c4c67 45
martin13 0:f4fdca2c4c67 46 virtual void onEntry(){}
martin13 0:f4fdca2c4c67 47
martin13 0:f4fdca2c4c67 48 virtual const char* onExecute(){
martin13 0:f4fdca2c4c67 49 return SUCCEDED;
martin13 0:f4fdca2c4c67 50 }
martin13 0:f4fdca2c4c67 51
martin13 0:f4fdca2c4c67 52 virtual void onExit(){}
martin13 0:f4fdca2c4c67 53 };
martin13 0:f4fdca2c4c67 54
martin13 0:f4fdca2c4c67 55 class FooBar : public StateMachine{
martin13 0:f4fdca2c4c67 56
martin13 0:f4fdca2c4c67 57 public:
martin13 0:f4fdca2c4c67 58
martin13 0:f4fdca2c4c67 59 FooBar(const char* uuid, UserData* ud):
martin13 0:f4fdca2c4c67 60 StateMachine(uuid, ud)
martin13 0:f4fdca2c4c67 61 {
martin13 0:f4fdca2c4c67 62
martin13 0:f4fdca2c4c67 63 S1 *s1 = this->Instance<S1>("S1");
martin13 0:f4fdca2c4c67 64 S2 *s2 = this->Instance<S2>("S2");
martin13 0:f4fdca2c4c67 65
martin13 0:f4fdca2c4c67 66 this->connect(STATE(s1), S1::CONTINUE, STATE(s2));
martin13 0:f4fdca2c4c67 67 this->connect(STATE(s1), S1::SUCCEDED, "DONE");
martin13 0:f4fdca2c4c67 68
martin13 0:f4fdca2c4c67 69 this->connect(STATE(s2), S2::SUCCEDED, STATE(s1));
martin13 0:f4fdca2c4c67 70
martin13 0:f4fdca2c4c67 71 this->setInitialState(STATE(s1));
martin13 0:f4fdca2c4c67 72 }
martin13 0:f4fdca2c4c67 73 };
martin13 0:f4fdca2c4c67 74
martin13 0:f4fdca2c4c67 75 InterruptIn button(PC_13);
martin13 0:f4fdca2c4c67 76 DigitalOut led(LED1);
martin13 0:f4fdca2c4c67 77
martin13 0:f4fdca2c4c67 78 const char* resolveConcurrenceOutcomes(const char **outcomes, int number){
martin13 0:f4fdca2c4c67 79
martin13 0:f4fdca2c4c67 80
martin13 0:f4fdca2c4c67 81 if(strcmp(outcomes[0],"DONE")==0 && strcmp(outcomes[1], "DONE")==0){
martin13 0:f4fdca2c4c67 82 return "DONE";
martin13 0:f4fdca2c4c67 83 }
martin13 0:f4fdca2c4c67 84
martin13 0:f4fdca2c4c67 85 return "FAIL";
martin13 0:f4fdca2c4c67 86 }
martin13 0:f4fdca2c4c67 87
martin13 0:f4fdca2c4c67 88 void unit_test(){
martin13 0:f4fdca2c4c67 89
martin13 0:f4fdca2c4c67 90 ParallelStateMachine root("ROOT");
martin13 0:f4fdca2c4c67 91
martin13 0:f4fdca2c4c67 92 root.attachOutcomesResolver(&resolveConcurrenceOutcomes);
martin13 0:f4fdca2c4c67 93 button.rise(&root, &StateMachine::preempt);
martin13 0:f4fdca2c4c67 94
martin13 0:f4fdca2c4c67 95 FooBar *foobar1 = root.Instance<FooBar>("FOO_BAR_1");
martin13 0:f4fdca2c4c67 96 FooBar *foobar2 = root.Instance<FooBar>("FOO_BAR_2");
martin13 0:f4fdca2c4c67 97
martin13 0:f4fdca2c4c67 98 root.printGraph();
martin13 0:f4fdca2c4c67 99
martin13 0:f4fdca2c4c67 100 printf("PARALLEL SM RETURN %s\n",root.execute());
martin13 0:f4fdca2c4c67 101
martin13 0:f4fdca2c4c67 102 }
martin13 0:f4fdca2c4c67 103
martin13 0:f4fdca2c4c67 104
martin13 0:f4fdca2c4c67 105 #endif /* #ifndef __UTT_PARALLEL_STATES_H__*/