Event loop

sequenceDiagram Main->>EventLoop: Init loop thread EventLoop->>EventLoop: Listen end Main->>EventLoop: RegisterCallback(event1) Main->>Driver1: Init loop thread Driver1->>Driver1: i2c end Main->>EventLoop: RegisterCallback(event2) Main->>Driver2: Init loop thread Driver2->>Driver2: i2c end Driver1->>EventLoop: Post(event1) EventLoop-->>Main: callback(type1) Driver2->>EventLoop: Post(event2) EventLoop-->>Main: callback(type2)

Event Loop.

The event loop implements the asynchronous core logic of the framework. It enables different drivers and tasks to run asynchronously and notify the main application once there is an event to react to.

Typedefs

typedef void (*sc_event_cb)(void)

Callback for an event

Functions

bool sc_event_init(void)

Initialize the event loop.

Must be called before initializing other subsystems.

Return Value
  • true: if successful.
  • false: if failed.

bool sc_event_post(uint8_t type)

Post an event.

The type must be registered first with sc_event_register_type (TODO: link)

Parameters
  • type: Event type.
Return Value
  • true: if successful.
  • false: if failed.

bool sc_event_register_type(const char *name, uint8_t *type)

Register a type.

Calling this function will return an index for the type. If one did not exist yet, it was registered. If it was already registered, the index of that event is returned.

Parameters
  • name: Name of the event. The name must be a well-known name between the caller and the callee.
  • type: Index the type is returned in this pointer if the retval is true.
Return Value
  • true: if successful.
  • false: if failed.

bool sc_event_register_cb(uint8_t type, sc_event_cb func)

Register a callback for an event type.

The callback will be called from the Event Loop’s thread. The callback must return as soon as possible to unblock handling of other events.

The type must be registered first with sc_event_register_type (TODO: link)

Parameters
  • type: Event type.
  • func: Callback function.
Return Value
  • true: if successful.
  • false: if failed.

bool sc_event_register_type_cb(const char *name, sc_event_cb func)

A convenience function to register and event and assign a callback.

Internally this calls first sc_event_register_type() and then sc_event_register_cb(). Event type is not returned.

Return Value
  • true: if successful.
  • false: if failed.