-
Notifications
You must be signed in to change notification settings - Fork 5
3. Event system
You don't have need for writing main function with handleEvent(...) calls chain. Instead of this it's recomended to define EventSystem:
#include "simple-system.h"
EventSystem
{
registerEvent(EventA);
registerEvent(EventB);
}
void main()
{
runEventSystem();
}You can make some events more important than others. So, when the system is handling EventA, it's proccessing all messages that lay in channel during one EventSystemStep, not just one per step as in case of EventB:
EventSystem
{
HighPriority(registerEvent(EventA));
registerEvent(EventB);
}If you need to extended priorities, you can use a prioritized event system (the definition is strict, you can't omit any block or change order of blocks, but you can leave block empty, eg. LowPriority {}):
#include "prioritized-system.h"
EventSystem
{
HighestPriority
{
registerEvent(HighEvent);
}
MediumPriority
{
registerEvent(MediumEvent);
}
LowPriority
{
registerEvent(LowEvent);
}
}In addition for previous situation, we will be handling all events with high priority betwwen checking any other channel. Caution. You can't have both of event systems. So you should to be careful to not include both systems:
#include "prioritized-system.h"
#include "simple-system.h" // ignoredYou can specify lower priorities via skipTicks(number). It can be used in SystemTick as well as in event registration:
SystemTick
{
skipTicks(3) bindListener(check_is_data_arrived);
}
EventSystem
{
skipTicks(10) registerEvent(CheckIsButtonPressed);
skipTicks(100)
{
registerEvent(BurnFlash);
}
}If you want to do something in special system cases you can defclare and register system events:
SystemStart
{
bindListener(init);
}
SystemTick
{
bindListener(checkIsDataArrived);
}
SystemStop
{
bindListener(onStop);
}
EventSystem
{
registerEvent(SystemStop);
registerEvent(SystemStart);
registerEvent(SystemTick);
}Also you can omit registratrion of events by #define SYSTEM_LIFECYCLE. But in this case you must define all events!
#define SYSTEM_LIFECYCLE
SystemStart {}
SystemTick {}
SystemStop {}
EventSystem { /* All events will be registered automaticaly */ }If you want to stop system, you can publish the stop event: publishEvent(SystemStop). After that, the loop in runEventSystem will be broken.