... | @@ -16,15 +16,18 @@ This example shows the usage of the EventCounter using a single topic. |
... | @@ -16,15 +16,18 @@ This example shows the usage of the EventCounter using a single topic. |
|
|
|
|
|
First of all define the possible events and topics:
|
|
First of all define the possible events and topics:
|
|
```cpp
|
|
```cpp
|
|
#include <Common.h>
|
|
#include <miosix.h>
|
|
#include "events/EventBroker.h"
|
|
#include "events/EventBroker.h"
|
|
#include "events/utils/EventCounter.h"
|
|
#include "events/utils/EventCounter.h"
|
|
|
|
|
|
|
|
#define sEventBroker Singleton<EventBroker>::getInstance()
|
|
|
|
|
|
using namespace miosix;
|
|
using namespace miosix;
|
|
|
|
using namespace Boardcore;
|
|
|
|
|
|
enum ExampleEvents : uint8_t
|
|
enum ExampleEvents : uint8_t
|
|
{
|
|
{
|
|
EV_1 = EV_FIRST_SIGNAL,
|
|
EV_1 = EV_FIRST_CUSTOM,
|
|
EV_2
|
|
EV_2
|
|
};
|
|
};
|
|
|
|
|
... | @@ -38,14 +41,14 @@ If we post some events, we can check how many events have been posted, both in t |
... | @@ -38,14 +41,14 @@ If we post some events, we can check how many events have been posted, both in t |
|
```cpp
|
|
```cpp
|
|
int main()
|
|
int main()
|
|
{
|
|
{
|
|
EventCounter counter{*sEventBroker};
|
|
EventCounter counter{sEventBroker};
|
|
counter.subscribe(TOPIC_1);
|
|
counter.subscribe(TOPIC_1);
|
|
Thread::sleep(100);
|
|
Thread::sleep(100);
|
|
|
|
|
|
// post two events
|
|
// post two events
|
|
sEventBroker->post(Event{EV_1}, TOPIC_1);
|
|
sEventBroker.post(Event{EV_1}, TOPIC_1);
|
|
Thread::sleep(100);
|
|
Thread::sleep(100);
|
|
sEventBroker->post(Event{EV_2}, TOPIC_1);
|
|
sEventBroker.post(Event{EV_2}, TOPIC_1);
|
|
Thread::sleep(100);
|
|
Thread::sleep(100);
|
|
|
|
|
|
// As a result we have 2 events in total
|
|
// As a result we have 2 events in total
|
... | @@ -59,7 +62,7 @@ int main() |
... | @@ -59,7 +62,7 @@ int main() |
|
TRACE("Last event : %d \n", counter.getLastEvent());
|
|
TRACE("Last event : %d \n", counter.getLastEvent());
|
|
|
|
|
|
counter.stop();
|
|
counter.stop();
|
|
sEventBroker->stop();
|
|
sEventBroker.stop();
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
... | @@ -67,8 +70,8 @@ int main() |
... | @@ -67,8 +70,8 @@ int main() |
|
|
|
|
|
#### Output
|
|
#### Output
|
|
```sh
|
|
```sh
|
|
0.42> Total : 2
|
|
0.34> Total : 2
|
|
0.43> EV_1 count : 1
|
|
0.34> EV_1 count : 1
|
|
0.44> EV_2 count : 1
|
|
0.34> EV_2 count : 1
|
|
0.45> Last event : 5
|
|
0.34> Last event : 5
|
|
``` |
|
``` |
|
|
|
\ No newline at end of file |