This library is designed to create and run state graphs. It supports hierarchical states and parallel states execution.
StateMachineLib/State.h@0:f4fdca2c4c67, 2017-10-03 (annotated)
- Committer:
- martin13
- Date:
- Tue Oct 03 08:29:22 2017 +0000
- Revision:
- 0:f4fdca2c4c67
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
martin13 | 0:f4fdca2c4c67 | 1 | //////////////////////////////////////////////////////////////////////////////// |
martin13 | 0:f4fdca2c4c67 | 2 | // Copyright Rottor SAS 2017 |
martin13 | 0:f4fdca2c4c67 | 3 | // All rigths reserved. |
martin13 | 0:f4fdca2c4c67 | 4 | // |
martin13 | 0:f4fdca2c4c67 | 5 | // File Name : State.h |
martin13 | 0:f4fdca2c4c67 | 6 | // Authors : Martin Matignon |
martin13 | 0:f4fdca2c4c67 | 7 | // |
martin13 | 0:f4fdca2c4c67 | 8 | // If you find any bug or if you have any question please contact |
martin13 | 0:f4fdca2c4c67 | 9 | // Martin Matignon <martin.matignon@rottor.fr> |
martin13 | 0:f4fdca2c4c67 | 10 | // Nicolas Forestier <nicolas.forestier@rottor.fr> |
martin13 | 0:f4fdca2c4c67 | 11 | // |
martin13 | 0:f4fdca2c4c67 | 12 | //////////////////////////////////////////////////////////////////////////////// |
martin13 | 0:f4fdca2c4c67 | 13 | |
martin13 | 0:f4fdca2c4c67 | 14 | #ifndef __SM_STATE_H__ |
martin13 | 0:f4fdca2c4c67 | 15 | #define __SM_STATE_H__ |
martin13 | 0:f4fdca2c4c67 | 16 | |
martin13 | 0:f4fdca2c4c67 | 17 | #include "mbed.h" |
martin13 | 0:f4fdca2c4c67 | 18 | #include "UserData.h" |
martin13 | 0:f4fdca2c4c67 | 19 | |
martin13 | 0:f4fdca2c4c67 | 20 | /** Global preempt state flag |
martin13 | 0:f4fdca2c4c67 | 21 | */ |
martin13 | 0:f4fdca2c4c67 | 22 | extern bool preempt_flag; |
martin13 | 0:f4fdca2c4c67 | 23 | |
martin13 | 0:f4fdca2c4c67 | 24 | /** A virtual class for executing a simple state into state machine |
martin13 | 0:f4fdca2c4c67 | 25 | */ |
martin13 | 0:f4fdca2c4c67 | 26 | class State { |
martin13 | 0:f4fdca2c4c67 | 27 | |
martin13 | 0:f4fdca2c4c67 | 28 | public: |
martin13 | 0:f4fdca2c4c67 | 29 | |
martin13 | 0:f4fdca2c4c67 | 30 | /** Standard outcomes |
martin13 | 0:f4fdca2c4c67 | 31 | */ |
martin13 | 0:f4fdca2c4c67 | 32 | static const char* SUCCEDED; |
martin13 | 0:f4fdca2c4c67 | 33 | static const char* ABORTED; |
martin13 | 0:f4fdca2c4c67 | 34 | static const char* PREEMPTED; |
martin13 | 0:f4fdca2c4c67 | 35 | |
martin13 | 0:f4fdca2c4c67 | 36 | /** State type definition |
martin13 | 0:f4fdca2c4c67 | 37 | */ |
martin13 | 0:f4fdca2c4c67 | 38 | enum StateType_t{ |
martin13 | 0:f4fdca2c4c67 | 39 | SIMPLE_STATE = 1, |
martin13 | 0:f4fdca2c4c67 | 40 | STATE_MACHINE |
martin13 | 0:f4fdca2c4c67 | 41 | }; |
martin13 | 0:f4fdca2c4c67 | 42 | |
martin13 | 0:f4fdca2c4c67 | 43 | /** Create a State |
martin13 | 0:f4fdca2c4c67 | 44 | * |
martin13 | 0:f4fdca2c4c67 | 45 | * @param uuid The state uuid |
martin13 | 0:f4fdca2c4c67 | 46 | * @param type The state type, default type is SIMPLE_STATE |
martin13 | 0:f4fdca2c4c67 | 47 | */ |
martin13 | 0:f4fdca2c4c67 | 48 | State(const char* uuid, StateType_t type = SIMPLE_STATE); |
martin13 | 0:f4fdca2c4c67 | 49 | |
martin13 | 0:f4fdca2c4c67 | 50 | /** Remap state uuid |
martin13 | 0:f4fdca2c4c67 | 51 | * |
martin13 | 0:f4fdca2c4c67 | 52 | * @param uuid The new state uuid |
martin13 | 0:f4fdca2c4c67 | 53 | */ |
martin13 | 0:f4fdca2c4c67 | 54 | void setUUID(const char* uuid); |
martin13 | 0:f4fdca2c4c67 | 55 | |
martin13 | 0:f4fdca2c4c67 | 56 | /** Provide state uuid |
martin13 | 0:f4fdca2c4c67 | 57 | * |
martin13 | 0:f4fdca2c4c67 | 58 | * @return uuid The state uuid |
martin13 | 0:f4fdca2c4c67 | 59 | */ |
martin13 | 0:f4fdca2c4c67 | 60 | const char* getUUID(); |
martin13 | 0:f4fdca2c4c67 | 61 | |
martin13 | 0:f4fdca2c4c67 | 62 | /** Remap state type |
martin13 | 0:f4fdca2c4c67 | 63 | * |
martin13 | 0:f4fdca2c4c67 | 64 | * @param type The new state type |
martin13 | 0:f4fdca2c4c67 | 65 | */ |
martin13 | 0:f4fdca2c4c67 | 66 | void setType(StateType_t type); |
martin13 | 0:f4fdca2c4c67 | 67 | |
martin13 | 0:f4fdca2c4c67 | 68 | /** Provide state type |
martin13 | 0:f4fdca2c4c67 | 69 | * |
martin13 | 0:f4fdca2c4c67 | 70 | * @return type The state type |
martin13 | 0:f4fdca2c4c67 | 71 | */ |
martin13 | 0:f4fdca2c4c67 | 72 | StateType_t getType(); |
martin13 | 0:f4fdca2c4c67 | 73 | |
martin13 | 0:f4fdca2c4c67 | 74 | /** Virtual method called by state machine before onExecute |
martin13 | 0:f4fdca2c4c67 | 75 | * (need to redefine into inherited object) |
martin13 | 0:f4fdca2c4c67 | 76 | */ |
martin13 | 0:f4fdca2c4c67 | 77 | virtual void onEntry() = 0; |
martin13 | 0:f4fdca2c4c67 | 78 | |
martin13 | 0:f4fdca2c4c67 | 79 | /** Virtual method called by state machine during execution |
martin13 | 0:f4fdca2c4c67 | 80 | * (need to redefine into inherited object) |
martin13 | 0:f4fdca2c4c67 | 81 | */ |
martin13 | 0:f4fdca2c4c67 | 82 | virtual const char* onExecute() = 0; |
martin13 | 0:f4fdca2c4c67 | 83 | |
martin13 | 0:f4fdca2c4c67 | 84 | /** Virtual method called by state machine after execution |
martin13 | 0:f4fdca2c4c67 | 85 | * (need to redefine into inherited object) |
martin13 | 0:f4fdca2c4c67 | 86 | */ |
martin13 | 0:f4fdca2c4c67 | 87 | virtual void onExit() = 0; |
martin13 | 0:f4fdca2c4c67 | 88 | |
martin13 | 0:f4fdca2c4c67 | 89 | /** This method call onExecute during thread execution |
martin13 | 0:f4fdca2c4c67 | 90 | * (method reserved for parallel state machine) |
martin13 | 0:f4fdca2c4c67 | 91 | */ |
martin13 | 0:f4fdca2c4c67 | 92 | void _onParallelExecute(); |
martin13 | 0:f4fdca2c4c67 | 93 | |
martin13 | 0:f4fdca2c4c67 | 94 | /** Provide state outcome |
martin13 | 0:f4fdca2c4c67 | 95 | * (method reserved for parallel state machine) |
martin13 | 0:f4fdca2c4c67 | 96 | * |
martin13 | 0:f4fdca2c4c67 | 97 | * @return outcome The parallel state outcome |
martin13 | 0:f4fdca2c4c67 | 98 | */ |
martin13 | 0:f4fdca2c4c67 | 99 | const char* getOutcome(); |
martin13 | 0:f4fdca2c4c67 | 100 | |
martin13 | 0:f4fdca2c4c67 | 101 | /** Method called if preempt requested by parent state machine |
martin13 | 0:f4fdca2c4c67 | 102 | */ |
martin13 | 0:f4fdca2c4c67 | 103 | void preempt(); |
martin13 | 0:f4fdca2c4c67 | 104 | |
martin13 | 0:f4fdca2c4c67 | 105 | /** Provide preempt state |
martin13 | 0:f4fdca2c4c67 | 106 | * (Use this method in while(!isPreempted()){// do }) |
martin13 | 0:f4fdca2c4c67 | 107 | * |
martin13 | 0:f4fdca2c4c67 | 108 | * @return bool The preempt state (true/false) |
martin13 | 0:f4fdca2c4c67 | 109 | */ |
martin13 | 0:f4fdca2c4c67 | 110 | bool isPreempted(); |
martin13 | 0:f4fdca2c4c67 | 111 | |
martin13 | 0:f4fdca2c4c67 | 112 | private: |
martin13 | 0:f4fdca2c4c67 | 113 | const char* m_uuid; /* State uuid */ |
martin13 | 0:f4fdca2c4c67 | 114 | StateType_t m_type; /* State type */ |
martin13 | 0:f4fdca2c4c67 | 115 | bool m_preempt; /* Preempt state */ |
martin13 | 0:f4fdca2c4c67 | 116 | const char* m_outcome; /* Parallel outcome state */ |
martin13 | 0:f4fdca2c4c67 | 117 | }; |
martin13 | 0:f4fdca2c4c67 | 118 | |
martin13 | 0:f4fdca2c4c67 | 119 | #endif /* #ifndef __SM_STATE_H__*/ |