Events#
Introduction#
Events allow you to run functions in parallel by using event objects. Instead of calling functions sequentially, events let you register functions and then trigger them all at once. Each registered function runs in its own thread, so your robot can do multiple things—like blinking LEDs and driving—at the same time.
Below is a list of available methods and constructors:
event – Creates a new event object.
Register Functions to an Event – Registers a function to the event object.
broadcast – Triggers all registered functions in an event object to run in parallel.
broadcastAndWait – Triggers all registered functions in an event object and waits for them to finish before continuing.
Create an Event Object#
The event
constructor is used to create an event object that manages function execution in separate threads.
Usage:
event(callback);
Parameter |
Description |
---|---|
|
Optional. A function that is previously defined to execute when the event is broadcasted. |
// Blink the screen while moving forward
// Create moveEvent as an event object
event moveEvent = event();
void blinkScreen() {
while (true) {
Brain.Screen.clearScreen(red);
wait(0.5, seconds);
Brain.Screen.clearScreen();
wait(0.5, seconds);
}
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
moveEvent(blinkScreen);
moveEvent.broadcast();
Drivetrain.drive(forward);
}
// Blink the screen while moving forward
// Create moveEvent as an event object
void blinkScreen() {
while (true) {
Brain.Screen.clearScreen(red);
wait(0.5, seconds);
Brain.Screen.clearScreen();
wait(0.5, seconds);
}
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
event moveEvent = event(blinkScreen);
moveEvent.broadcast();
Drivetrain.drive(forward);
}
Register Functions to an Event#
When you register a function to an event, it will execute in a separate thread when the event is broadcasted.
Usage:
eventName(callback);
Parameter |
Description |
---|---|
|
The name of the previously created event object. |
|
A function that is previously defined to execute when the event is broadcasted. |
// Blink the screen while turning
event moveEvent = event();
void blinkScreen() {
while (true) {
Brain.Screen.clearScreen(red);
wait(0.5, seconds);
Brain.Screen.clearScreen();
wait(0.5, seconds);
}
}
void turning() {
Drivetrain.turn(right);
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Register multiple functions to the event object
moveEvent(blinkScreen);
moveEvent(turning);
moveEvent.broadcast();
}
broadcast#
broadcast()
triggers an event, starting all registered functions in separate threads. This method does not pause execution of any subsequent functions.
Usage:
myEvent.broadcast();
Parameter |
Description |
---|---|
|
The name of the previously created event object. |
// Blink the screen while moving forward
// Create moveEvent as an event object
event moveEvent = event();
void blinkScreen() {
while (true) {
Brain.Screen.clearScreen(red);
wait(0.5, seconds);
Brain.Screen.clearScreen();
wait(0.5, seconds);
}
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
moveEvent(blinkScreen);
moveEvent.broadcast();
Drivetrain.drive(forward);
}
broadcastAndWait#
broadcastAndWait()
starts an event but waits for all registered functions to finish before continuing with subsequent functions.
Usage:
myEvent.broadcastAndWait();
Parameter |
Description |
---|---|
|
The name of the previously created event object. |
// Move after the left button is pressed
event moveEvent = event();
void moveAndTurn() {
Drivetrain.driveFor(forward, 150, mm);
Drivetrain.turnFor(right, 90, degrees);
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
moveEvent(moveAndTurn);
while (true) {
if (Brain.buttonLeft.pressing()) {
moveEvent.broadcastAndWait();
break;
}
wait(20, msec);
}
Brain.Screen.print("Movement done.");
}