event#

Initializing the event Class#

An event is created by using the following constructor:

The event(callback) constructor uses two parameters:

Parameter

Description

callback

A previously defined function that will be called as a thread when the event is broadcast.

// Define the function print()
void print(){
  brain.screen.print("Event was broadcast");
}
// Construct an Event "Event" with the
// event class.
event Event = event(print);

This Event object will be used in all subsequent examples throughout this API documentation when referring to event class methods.

Class Methods#

set()#

The set(callback) command sets a new callback and arguments for the Event.

Parameters

Description

callback

The new callback function to be called when the event is triggered.

Returns: None.

broadcast()#

The broadcast() command broadcasts the event and causes all registered callback functions to run.

Returns: None.

void runOnBroadcast1() {
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Broadcast1 Running");
}

void runOnBroadcast2() {
  Brain.Screen.setCursor(2, 1);
  Brain.Screen.print("Broadcast2 Running");
}

int main() {

  // Register callback functions to event.
  Event(runOnBroadcast1);
  Event(runOnBroadcast2);
  
  // Brief wait to ensure event is registered
  wait(15, msec);

  // Broadcast the event.
  Event.broadcast();
}

broadcastAndWait()#

The broadcastAndWait() command broadcasts the event, causes all registered callback functions to run, and waits until all callback functions have been completed.

Returns: None.

void runOnBroadcast1() {
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Broadcast1 Running");
}

void runOnBroadcast2() {
  Brain.Screen.setCursor(2, 1);
  Brain.Screen.print("Broadcast2 Running");
}

int main() {
  // Register callback functions to event.
  Event(runOnBroadcast1);
  Event(runOnBroadcast2);
  
  // Brief wait to ensure event is registered
  wait(15, msec);

  // Broadcast the event and wait for completion.
  Event.broadcastAndWait();

  Brain.Screen.setCursor(3, 1);
  Brain.Screen.print("Listeners finished running");
}