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:
2:5e2336b52d0a
Added unittests

Who changed what in which revision?

UserRevisionLine numberNew contents of line
martin13 4:22b4462fcb26 1
martin13 4:22b4462fcb26 2
martin13 0:f4fdca2c4c67 3 #ifndef __UTT_FOOBAR_H__
martin13 0:f4fdca2c4c67 4 #define __UTT_FOOBAR_H__
martin13 0:f4fdca2c4c67 5
martin13 4:22b4462fcb26 6 #include "mbed.h"
martin13 0:f4fdca2c4c67 7 #include "StateMachine.h"
martin13 4:22b4462fcb26 8
martin13 0:f4fdca2c4c67 9 class Foo : public State{
martin13 4:22b4462fcb26 10
martin13 0:f4fdca2c4c67 11 public:
martin13 4:22b4462fcb26 12
martin13 0:f4fdca2c4c67 13 static const char* OUTCOME_BAR;
martin13 4:22b4462fcb26 14
martin13 0:f4fdca2c4c67 15 Foo(const char* uuid, UserData *ud):
martin13 0:f4fdca2c4c67 16 State(uuid){
martin13 0:f4fdca2c4c67 17 /* Capture UserData, see in utt_foobar_with_ud.h example. */
martin13 0:f4fdca2c4c67 18 }
martin13 0:f4fdca2c4c67 19
martin13 0:f4fdca2c4c67 20 virtual void onEntry(){
martin13 0:f4fdca2c4c67 21 /* Do something before onExecute() */
martin13 0:f4fdca2c4c67 22 }
martin13 0:f4fdca2c4c67 23
martin13 0:f4fdca2c4c67 24 virtual const char* onExecute(){
martin13 0:f4fdca2c4c67 25 /* Do something */
martin13 0:f4fdca2c4c67 26 return OUTCOME_BAR;
martin13 0:f4fdca2c4c67 27 }
martin13 0:f4fdca2c4c67 28
martin13 0:f4fdca2c4c67 29 virtual void onExit(){
martin13 0:f4fdca2c4c67 30 /* Do something before exit state */
martin13 0:f4fdca2c4c67 31 }
martin13 0:f4fdca2c4c67 32 };
martin13 4:22b4462fcb26 33
martin13 0:f4fdca2c4c67 34 const char* Foo::OUTCOME_BAR = "OUTCOME_BAR";
martin13 4:22b4462fcb26 35
martin13 0:f4fdca2c4c67 36 class Bar : public State{
martin13 4:22b4462fcb26 37
martin13 0:f4fdca2c4c67 38 public:
martin13 4:22b4462fcb26 39
martin13 0:f4fdca2c4c67 40 static const char* OUTCOME_FOO;
martin13 4:22b4462fcb26 41
martin13 0:f4fdca2c4c67 42 Bar(const char* uuid, UserData *ud):
martin13 0:f4fdca2c4c67 43 State(uuid){
martin13 0:f4fdca2c4c67 44 /* Capture UserData, see in utt_foobar_with_ud.h example. */
martin13 0:f4fdca2c4c67 45 }
martin13 0:f4fdca2c4c67 46
martin13 0:f4fdca2c4c67 47 virtual void onEntry(){
martin13 0:f4fdca2c4c67 48 /* Do something before onExecute() */
martin13 0:f4fdca2c4c67 49 }
martin13 0:f4fdca2c4c67 50
martin13 0:f4fdca2c4c67 51 virtual const char* onExecute(){
martin13 0:f4fdca2c4c67 52 /* Do something */
martin13 0:f4fdca2c4c67 53 return OUTCOME_FOO;
martin13 0:f4fdca2c4c67 54 }
martin13 0:f4fdca2c4c67 55
martin13 0:f4fdca2c4c67 56 virtual void onExit(){
martin13 0:f4fdca2c4c67 57 /* Do something before exit state */
martin13 0:f4fdca2c4c67 58 }
martin13 0:f4fdca2c4c67 59 };
martin13 4:22b4462fcb26 60
martin13 0:f4fdca2c4c67 61 const char* Bar::OUTCOME_FOO = "OUTCOME_FOO";
martin13 4:22b4462fcb26 62
martin13 0:f4fdca2c4c67 63 class FooBar : public StateMachine{
martin13 4:22b4462fcb26 64
martin13 0:f4fdca2c4c67 65 public:
martin13 4:22b4462fcb26 66
martin13 0:f4fdca2c4c67 67 FooBar(const char* uuid):
martin13 0:f4fdca2c4c67 68 StateMachine(uuid),
martin13 0:f4fdca2c4c67 69 foo(NULL), bar(NULL)
martin13 0:f4fdca2c4c67 70 {
martin13 0:f4fdca2c4c67 71 // States instance with StateMachine context
martin13 0:f4fdca2c4c67 72 foo = this->Instance<Foo>("FOO");
martin13 0:f4fdca2c4c67 73 bar = this->Instance<Bar>("BAR");
martin13 0:f4fdca2c4c67 74
martin13 0:f4fdca2c4c67 75 // Connect Foo to Bar
martin13 0:f4fdca2c4c67 76 this->connect(STATE(foo), Foo::OUTCOME_BAR, STATE(bar));
martin13 0:f4fdca2c4c67 77
martin13 0:f4fdca2c4c67 78 // Connect Bar to Foo
martin13 0:f4fdca2c4c67 79 this->connect(STATE(bar), Bar::OUTCOME_FOO, STATE(foo));
martin13 0:f4fdca2c4c67 80
martin13 0:f4fdca2c4c67 81 // Set the first state calling when the state machine starts
martin13 0:f4fdca2c4c67 82 this->setInitialState(STATE(foo));
martin13 0:f4fdca2c4c67 83 }
martin13 4:22b4462fcb26 84
martin13 0:f4fdca2c4c67 85 private:
martin13 0:f4fdca2c4c67 86 Foo *foo;
martin13 0:f4fdca2c4c67 87 Bar *bar;
martin13 0:f4fdca2c4c67 88 };
martin13 4:22b4462fcb26 89
martin13 0:f4fdca2c4c67 90 // Preempt state mahine button
martin13 0:f4fdca2c4c67 91 InterruptIn button(PC_13);
martin13 4:22b4462fcb26 92
martin13 0:f4fdca2c4c67 93 void unit_test(){
martin13 0:f4fdca2c4c67 94
martin13 0:f4fdca2c4c67 95 FooBar foobar("FOOBAR_SM");
martin13 0:f4fdca2c4c67 96
martin13 0:f4fdca2c4c67 97 button.rise(&foobar, &FooBar::preempt);
martin13 0:f4fdca2c4c67 98
martin13 0:f4fdca2c4c67 99 foobar.printGraph();
martin13 0:f4fdca2c4c67 100
martin13 0:f4fdca2c4c67 101 Logger::info("STATE MACHINE FINISHED WITH OUTCOME \"%s\"",foobar.execute());
martin13 0:f4fdca2c4c67 102 }
martin13 4:22b4462fcb26 103
martin13 4:22b4462fcb26 104
martin13 0:f4fdca2c4c67 105 #endif /* #ifndef __UTT_FOOBAR_H__*/