... | ... | @@ -71,6 +71,10 @@ As always when dealing with events and topics, after we include the required dep |
|
|
#include "events/FSM.h"
|
|
|
#include "events/EventBroker.h"
|
|
|
|
|
|
#define sEventBroker Singleton<EventBroker>::getInstance()
|
|
|
|
|
|
|
|
|
namespace Boardcore {
|
|
|
enum ExampleEvents : uint8_t
|
|
|
{
|
|
|
EV_1 = EV_FIRST_CUSTOM,
|
... | ... | @@ -81,11 +85,7 @@ enum ExampleTopics : uint8_t |
|
|
{
|
|
|
TOPIC_1
|
|
|
};
|
|
|
```
|
|
|
|
|
|
Then we can define the MyFSM class and its states, which are identified by the two methods `state_S1` and `state_S2`.
|
|
|
|
|
|
```cpp
|
|
|
class MyFSM : public FSM<MyFSM>
|
|
|
{
|
|
|
public:
|
... | ... | @@ -94,18 +94,18 @@ public: |
|
|
last_event(0)
|
|
|
{
|
|
|
// make this object to subscribe to TOPIC_1
|
|
|
sEventBroker->subscribe(this, TOPIC_1);
|
|
|
sEventBroker.subscribe(this, TOPIC_1);
|
|
|
}
|
|
|
|
|
|
~MyFSM()
|
|
|
{
|
|
|
// unsubscribe from all the topics this object was subscribed to
|
|
|
sEventBroker->unsubscribe(this);
|
|
|
sEventBroker.unsubscribe(this);
|
|
|
}
|
|
|
|
|
|
void state_S1(const Event& ev)
|
|
|
{
|
|
|
switch (ev.sig)
|
|
|
switch (ev)
|
|
|
{
|
|
|
case EV_ENTRY:
|
|
|
case EV_EXIT:
|
... | ... | @@ -131,12 +131,12 @@ public: |
|
|
}
|
|
|
}
|
|
|
|
|
|
last_event = ev.sig;
|
|
|
last_event = ev;
|
|
|
}
|
|
|
|
|
|
void state_S2(const Event& ev)
|
|
|
{
|
|
|
switch (ev.sig)
|
|
|
switch (ev)
|
|
|
{
|
|
|
case EV_ENTRY:
|
|
|
case EV_EXIT:
|
... | ... | @@ -162,52 +162,58 @@ public: |
|
|
}
|
|
|
}
|
|
|
|
|
|
last_event = ev.sig;
|
|
|
last_event = ev;
|
|
|
}
|
|
|
|
|
|
private:
|
|
|
uint8_t last_event;
|
|
|
};
|
|
|
|
|
|
} //namespace Boardcore
|
|
|
```
|
|
|
|
|
|
#### test-fsm.cpp
|
|
|
```cpp
|
|
|
#include <Common.h>
|
|
|
#include "MyFSM.h"
|
|
|
#include <miosix.h>
|
|
|
#include "events/MyFSM.h"
|
|
|
|
|
|
#define broker Singleton<EventBroker>::getInstance()
|
|
|
|
|
|
|
|
|
using namespace miosix;
|
|
|
using namespace Boardcore;
|
|
|
|
|
|
int main()
|
|
|
{
|
|
|
EventBroker* broker = EventBroker::getInstance(); // singleton object
|
|
|
broker->start();
|
|
|
//EventBroker* broker = EventBroker::getInstance(); // singleton object
|
|
|
broker.start();
|
|
|
|
|
|
MyFSM fsm;
|
|
|
|
|
|
if (fsm.start())
|
|
|
{
|
|
|
// trigger self-loop on state S1
|
|
|
broker->post(Event{EV_1}, TOPIC_1);
|
|
|
broker.post(Event{EV_1}, TOPIC_1);
|
|
|
Thread::sleep(100);
|
|
|
|
|
|
// trigger transition to state S2
|
|
|
broker->post(Event{EV_2}, TOPIC_1);
|
|
|
broker.post(Event{EV_2}, TOPIC_1);
|
|
|
Thread::sleep(100);
|
|
|
|
|
|
// trigger self-loop on state S2
|
|
|
broker->post(Event{EV_2}, TOPIC_1);
|
|
|
broker.post(Event{EV_2}, TOPIC_1);
|
|
|
Thread::sleep(100);
|
|
|
|
|
|
// trigger transition to state S1 with a delayed event
|
|
|
const unsigned int DELAY_MS = 1000;
|
|
|
broker->postDelayed<DELAY_MS>(Event{EV_1}, TOPIC_1);
|
|
|
broker.postDelayed<DELAY_MS>(Event{EV_1}, TOPIC_1);
|
|
|
|
|
|
// wait for the delayed event to be posted
|
|
|
// before stopping the active objects
|
|
|
Thread::sleep(2000);
|
|
|
|
|
|
fsm.stop();
|
|
|
broker->stop();
|
|
|
broker.stop();
|
|
|
}
|
|
|
else {
|
|
|
TRACE("Failed to start MyFSM\n");
|
... | ... | @@ -220,10 +226,10 @@ int main() |
|
|
#### Output
|
|
|
|
|
|
```
|
|
|
0.12> S1: EV_1
|
|
|
0.22> S1: EV_2 ---> S2
|
|
|
0.32> S2: EV_2
|
|
|
1.42> S2: EV_1 ---> S1
|
|
|
0.04> S1: EV_1
|
|
|
0.14> S1: EV_2 ---> S2
|
|
|
0.24> S2: EV_2
|
|
|
1.34> S2: EV_1 ---> S1
|
|
|
```
|
|
|
|
|
|
## Example 2 - Timeouts
|
... | ... | |