Events#
The Connect Application Framework provides a simple event scheduler, which can be considered an extension of the callback mechanism. You can use this to set up delayed (or immediate) events without directly using a timer—similar to how you might accomplish this behavior using an RTOS task. However, Connect does not provide mutexes, semaphores, or queues without a proper RTOS (see Note below). The events scheduler cannot be disabled, as the Connect stack also uses this feature to schedule stack events (like periodic beacon transmission).
Unless you need tight scheduling, Silicon Labs highly recommends that you use events instead of timers in Connect.
Note: Events are still available when the RTOS kernel component is enabled. RTOS tasks usually use a lot of memory, so you can only create a handful of RTOS tasks. Alternatively, you can have 6 Connect events, and you only need to define a pointer variable to an
EmberEventControl
structure for each event.
Creating an Event#
Events are created at run-time using emberAfAllocateEvent()
, which is included with the Application Framework Common component.
This will add the event to the emAppEvents
array (defined in autogen/app_framework_event_table.c), which is used by the event scheduler. Each event requires a control (which is used to schedule it) and a callback (which is the function that will be executed when the scheduled event runs). Note: The events that populate emAppEvents
in source originate from the stack and installed components, and are regenerated upon component install/uninstall operations – overwriting any user edits.
Be sure to define an EmberEventControl type pointer variable in your application for the control and implement the callback for any events you plan to use. Typically, this should be in app_process.c, and will resemble the following:
EmberEventControl *myControl;
void myHandler(void){
}
See https://docs.silabs.com/connect-stack/3.0/group-app-framework-common#gaf720508a9ada3dece4864ea2b320c9d7 for more information.
Scheduling Events#
Once the callback of the scheduled event handler is running, it will be re-called repeatedly until either 1) it is set inactive, or 2) it is delayed. However, the callback function itself will run until it returns – it will not be stopped by anemberEventControlSetDelayMS
command (like an RTOS).
Therefore, a primary responsibility for an event callback is to carefully manage the scheduling of the next repetition of the source event. The following APIs are most commonly used to schedule events:
emberEventControlSetActive(EmberEventControl control)
– Schedules an event as soon as possible.emberEventControlSetDelayMS(EmberEventControl control, uint32_t delay)
– Schedules an event to occur delay ms in the future.emberEventControlSetInactive(EmberEventControl control)
– Turns off an event until it is scheduled again (either withSetActive
orSetDelayMS
).
A typical event handler will look something like this:
void myHandler(void){
emberEventControlSetInactive(myControl); //make sure to set the event inactive, if we need to reschedule, we'll do it later
EmberStatus status = someTaskToPerform();
if ( status != EMBER_SUCCESS ){ //task was unsuccessful, try again 1 second later
emberEventControlSetDelayMS(myControl, 1000);
}
}
For more details and the full available API, see the related API documentation.