Skip to main content

EventBus

The EventBus handles event subscriptions and timers. Every exported sketch has a global eventBus instance.

C++
extern EventBus eventBus;

Call eventBus.tick() in every loop() iteration to drive timers and fire queued events.

#Events

Define event IDs as an enum at the top of your sketch (the code generator does this automatically):

C++
enum Events {
  BUTTON_PRESSED  = 0,
  BUTTON_RELEASED = 1,
  LED_CHANGED     = 2,
};
MethodDescription
void on(uint8_t event, Handler handler, void* ctx)Subscribe to an event
void emit(uint8_t event)Fire an event immediately
C++
eventBus.on(BUTTON_PRESSED, [](void*) {
  led.toggle();
}, nullptr);

#Timers

Timers are managed by EventBus and fire via tick(). All timer IDs are integers; use clearTimer to cancel.

MethodDescription
int setTimeout(Handler h, void* ctx, uint32_t delayMs)Fire once after delayMs milliseconds
int setInterval(Handler h, void* ctx, uint32_t intervalMs)Fire repeatedly every intervalMs milliseconds
void clearTimer(int id)Cancel a timer
C++
// Blink LED every 500ms
int blinkTimer = eventBus.setInterval([](void*) {
  led.toggle();
}, nullptr, 500);

// Cancel after 5 seconds
eventBus.setTimeout([](void* ctx) {
  eventBus.clearTimer(*(int*)ctx);
}, &blinkTimer, 5000);

#Tick

C++
void loop() {
  protocol.processCommands();
  eventBus.tick();      // must be called every loop
  blokdots::tickAll();
}

#Limits

The maximum number of subscriptions and timers are controlled by compile-time defines:

C++
#define EVENTBUS_MAX_SUBS   48   // total event subscriptions
#define EVENTBUS_MAX_TIMERS 16   // concurrent timers

These are set automatically by the code generator based on the number of components in your project.