... | @@ -37,9 +37,11 @@ Here we define three possible events: `EV_1`, `EV_2` and `EV_3`. |
... | @@ -37,9 +37,11 @@ Here we define three possible events: `EV_1`, `EV_2` and `EV_3`. |
|
|
|
|
|
#include "events/EventHandler.h"
|
|
#include "events/EventHandler.h"
|
|
|
|
|
|
|
|
namespace Boardcore {
|
|
|
|
|
|
enum ExampleEvents : uint8_t
|
|
enum ExampleEvents : uint8_t
|
|
{
|
|
{
|
|
EV_1 = EV_FIRST_SIGNAL,
|
|
EV_1 = EV_FIRST_CUSTOM,
|
|
EV_2,
|
|
EV_2,
|
|
EV_3
|
|
EV_3
|
|
};
|
|
};
|
... | @@ -58,7 +60,7 @@ public: |
... | @@ -58,7 +60,7 @@ public: |
|
protected:
|
|
protected:
|
|
void handleEvent(const Event& ev) override
|
|
void handleEvent(const Event& ev) override
|
|
{
|
|
{
|
|
switch(ev.sig) // do something according to the event's id
|
|
switch(ev) // do something according to the event's id
|
|
{
|
|
{
|
|
case EV_1:
|
|
case EV_1:
|
|
TRACE("Received EV_1 \n");
|
|
TRACE("Received EV_1 \n");
|
... | @@ -70,22 +72,26 @@ protected: |
... | @@ -70,22 +72,26 @@ protected: |
|
TRACE("Invalid event \n");
|
|
TRACE("Invalid event \n");
|
|
}
|
|
}
|
|
|
|
|
|
last_event = ev.sig;
|
|
last_event = ev;
|
|
}
|
|
}
|
|
|
|
|
|
private:
|
|
private:
|
|
uint8_t last_event;
|
|
uint8_t last_event;
|
|
};
|
|
};
|
|
|
|
|
|
|
|
} // namespace Boardcore
|
|
|
|
|
|
```
|
|
```
|
|
|
|
|
|
##### test-myeventhandler.cpp
|
|
##### test-myeventhandler.cpp
|
|
In order to test that our class works we can create a MyEventHandler object and start it.
|
|
In order to test that our class works we can create a MyEventHandler object and start it.
|
|
We can then post some events and see what the output is.
|
|
We can then post some events and see what the output is.
|
|
```cpp
|
|
```cpp
|
|
#include <Common.h>
|
|
#include <miosix.h>
|
|
#include "MyEventHandler.h"
|
|
#include "MyEventHandler.h"
|
|
|
|
|
|
using namespace miosix;
|
|
using namespace miosix;
|
|
|
|
using namespace Boardcore;
|
|
|
|
|
|
int main()
|
|
int main()
|
|
{
|
|
{
|
... | @@ -111,14 +117,15 @@ int main() |
... | @@ -111,14 +117,15 @@ int main() |
|
|
|
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
```
|
|
```
|
|
##### Output
|
|
##### Output
|
|
Remember to add the `test-myeventhandler.cpp` to `sbs.conf` and to specify all the needed includes.
|
|
Remember to add the `test-myeventhandler.cpp` to `sbs.conf` and to specify all the needed includes.
|
|
After you run the example you should see the following output:
|
|
After you run the example you should see the following output:
|
|
```
|
|
```
|
|
0.13> Received EV_1
|
|
0.03> Received EV_1
|
|
0.23> Received EV_2
|
|
0.13> Received EV_2
|
|
|
|
0.23> Invalid event
|
|
0.33> Invalid event
|
|
0.33> Invalid event
|
|
0.43> Invalid event
|
|
|
|
```
|
|
```
|
|
The last "Invalid event" is due to the `EV_EMPTY` posted by the `stop()` method of the EventHandler object. |
|
The last "Invalid event" is due to the `EV_EMPTY` posted by the `stop()` method of the EventHandler object. |
|
\ No newline at end of file |
|
|