|
|
## Post events
|
|
|
Two types of events can be posted: "standard" events are posted at the moment of the function call, while delayed events are posted after a specified amount of time.
|
|
|
To post a "standard" event the following code is used
|
|
|
### Standard events
|
|
|
```
|
|
|
// 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({EV_TIMEOUT_SHADOW_MODE}, TOPIC_ADA, delay);
|
|
|
}
|
|
|
```
|
|
|
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({EV_TIMEOUT_SHADOW_MODE}, TOPIC_ADA, delay);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ...
|
|
|
|
|
|
|
|
|
// Function handling events for the current state
|
|
|
void MyClass::stateMyState(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;
|
|
|
}
|
|
|
// ...
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
```
|
|
|
### Events with parameters
|
|
|
If the event has some parameter associated (a typical example is a remote command from the ground station) the event has to be manually instanciated
|
|
|
```
|
|
|
// Include the event broker
|
|
|
#include <events/EventBroker.h>
|
|
|
|
|
|
// Include the events and topics definitions
|
|
|
#include <DeathStack/Events.h>
|
|
|
#include <DeathStack/Topics.h>
|
|
|
|
|
|
// Include the event classes
|
|
|
#include <DeathStack/EventClasses.h>
|
|
|
|
|
|
|
|
|
// The function from which you post the event
|
|
|
void someFunction()
|
|
|
{
|
|
|
int alt = 1000;
|
|
|
|
|
|
// Create the event
|
|
|
DeploymentAltitudeEvent ev;
|
|
|
|
|
|
// Set the event type
|
|
|
ev.sig = EV_TC_SET_DPL_ALTITUDE;
|
|
|
|
|
|
// Set the parameter to the desired value
|
|
|
ev.dplAltitude = alt;
|
|
|
|
|
|
// Post the event
|
|
|
sEventBroker->post(ev, TOPIC_TC);
|
|
|
}
|
|
|
```
|
|
|
``` |