Post Events
Two types of events can be posted: "normal" events are posted at the moment of the function call, while delayed events are posted after a specified amount of time.
Normal Events
To post a "normal" event the following code is used:
// Include the event broker
#include <events/EventBroker.h>
// Include the events and topics definitions
#include <DeathStack/Events.h>
#include <DeathStack/Topics.h>
// The function from which you post the event
void someFunction()
{
// Post the event EV_ADA_READY on topic TOPIC_ADA
// NOTE THE CURLY BRACKETS AROUND THE EVENT NAME!
sEventBroker->post({EV_ADA_READY}, TOPIC_ADA);
}
Delayed Events
The delayed events are posted in a very similar way:
// Include the event broker
#include <events/EventBroker.h>
// Include the events and topics definitions
#include <DeathStack/Events.h>
#include <DeathStack/Topics.h>
// The function from which you post the event
void someFunction()
{
unsigned int delay = 1000; // Delay is defined in milliseconds
// Post the event EV_TIMEOUT_SHADOW_MODE on topic TOPIC_ADA delayed by 1 second
// NOTE THE CURLY BRACKETS AROUND THE EVENT NAME!
event_id = sEventBroker->postDelayed<delay>({EV_TIMEOUT_SHADOW_MODE}, TOPIC_ADA);
}
It is good practice to save the event id returned by the function in order to remove it later.
// Define a private property in your class to store the event id
class MyClass : public FSM<MyClass>
{
private:
uint16_t event_id = 0;
// ...
void someFunction()
{
unsigned int delay = 1000; // Delay is defined in milliseconds
// Store the event id
event_id = sEventBroker->postDelayed<delay>({EV_TIMEOUT_SHADOW_MODE}, TOPIC_ADA);
}
// ...
// Function handling events for the current state
void MyClass::state_MyState(const Event& ev)
{
switch (ev.sig)
{
// ...
case EV_EXIT:
{
// If it's not needed, remove the delayed event
sEventBroker->removeDelayed(shadow_delayed_event_id);
break;
}
// ...
}
}
};
Troubleshooting
Check all the following points before beating your head against the wall :)
- Do not post events with payload: the current EventBroker implementation does not support them.
- Call the
.start()
method on all the EventHandler, FSM or HSM subclasses used and onsEventBroker
:
#include <events/EventBroker.h>
ADA ada;
int main()
{
sEventBroker->start();
ada.start();
}