Events#
Introduction#
Events allow you to run functions in parallel. Instead of calling functions sequentially, you can register multiple functions to an event and trigger them all at once. Each registered function runs in its own thread, enabling your robot to perform multiple tasks simultaneously—like displaying values on the screen while driving.
In order to create and then use an Event, follow these steps in order:
Create the event with
Event.Register a function to trigger when the
Eventis broadcast by calling theeventobject with the function as an argument.Broadcast an
Event:broadcast– Triggers all registered functions in an event object to run concurrently.broadcast_and_wait– Triggers all registered functions in an event object and waits for them to finish before continuing.
Create an Event#
Event creates an event object that manages function execution in separate threads.
Usage:
Event()
Parameters |
Description |
|---|---|
This constructor has no parameters. |
# Build Used: Super Code Base 2.0
def move_square():
for index in range(4):
drivetrain.drive_for(FORWARD, 150)
drivetrain.turn_for(RIGHT, 90)
def main():
# Creating the move_event Event
move_event = Event()
move_event(move_square)
move_event.broadcast()
bumper.set_color(GREEN)
# Start threads — Do not delete
start_thread(main)
Register Functions to an Event#
When you register a function to an event, it will execute in a separate thread when the event is broadcast.
Usage:
event(callback, args)
Parameters |
Description |
|---|---|
|
The name of the previously created event object. |
|
A previously defined function to execute when the event is broadcast. |
|
Optional. A tuple of arguments to pass to the callback function. |
# Build Used: Super Code Base 2.0
def move_square():
for index in range(4):
drivetrain.drive_for(FORWARD, 150)
drivetrain.turn_for(RIGHT, 90)
def main():
move_event = Event()
# Register the move_square function to the move_event Event
move_event(move_square)
move_event.broadcast()
bumper.set_color(GREEN)
# Start threads — Do not delete
start_thread(main)
Broadcast an Event#
broadcast#
broadcast triggers an event, starting all registered functions in separate threads. This method does not pause execution of any subsequent code.
Usage:
event.broadcast()
Parameters |
Description |
|---|---|
|
The name of the previously created event object. |
# Build Used: Super Code Base 2.0
def move_square():
for index in range(4):
drivetrain.drive_for(FORWARD, 150)
drivetrain.turn_for(RIGHT, 90)
def main():
move_event = Event()
move_event(move_square)
# Broadcasting the move_event Event
move_event.broadcast()
bumper.set_color(GREEN)
# Start threads — Do not delete
start_thread(main)
broadcast_and_wait#
broadcast_and_wait starts an event but waits for all registered functions to finish before continuing with subsequent functions.
Usage:
event.broadcast_and_wait()
Parameters |
Description |
|---|---|
|
The name of the previously created event object. |
# Build Used: Super Code Base 2.0
def move_square():
for index in range(4):
drivetrain.drive_for(FORWARD, 150)
drivetrain.turn_for(RIGHT, 90)
def main():
# Set up and call the move_square event
move_event = Event()
move_event(move_square)
# Broadcast the move_event Event and wait
move_event.broadcast_and_wait()
bumper.set_color(GREEN)
# Start threads — Do not delete
start_thread(main)