Events#
Introduction#
Events allow you to run functions in parallel by using event objects. Instead of calling functions or threads one after another, 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 TouchLEDs and driving—at the same time.
Below is a list of available methods and constructors:
Event– Creates a new event object.broadcast– Triggers all registered functions in an event object to run in parallel.broadcast_and_wait– Triggers all registered functions in an event object and waits for them to finish before continuing.event– Registers a function to the event object, optionally with arguments.
Create an Event Object#
The Event constructor is used to create an event object that manages function execution in separate threads.
Usage:
Event()
Parameter |
description |
|---|---|
This constructor has no parameters. |
def dash_lines():
# Move the pen up and down
while True:
pen.move(DOWN)
wait(0.5, SECONDS)
pen.move(UP)
wait(0.5, SECONDS)
def main():
# Set the event
move_event = Event()
move_event(dash_lines)
# Draw the dash line while driving forward
move_event.broadcast()
drivetrain.drive_for(FORWARD, 1000, MM)
# VR threads — Do not delete
vr_thread(main)
Broadcast#
broadcast triggers an event, starting all registered functions in separate threads. This method does not pause execution of any subsequent functions.
Usage:
my_event.broadcast()
parameter |
description |
|---|---|
|
The name of the previously created event object. |
def dash_lines():
# Move the pen up and down
while True:
pen.move(DOWN)
wait(0.5, SECONDS)
pen.move(UP)
wait(0.5, SECONDS)
def main():
# Set the event
move_event = Event()
move_event(dash_lines)
# Draw the dash line while driving forward
move_event.broadcast()
drivetrain.drive_for(FORWARD, 1000, MM)
# VR threads — Do not delete
vr_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:
my_event.broadcast_and_wait()
parameter |
description |
|---|---|
|
The name of the previously created event object. |
def move_and_turn():
# Drive forward and turn
drivetrain.drive_for(FORWARD, 150, MM)
drivetrain.turn_for(RIGHT, 90, DEGREES)
def main():
# Wait for the event to finish after 3 seconds
move_event = Event()
move_event(move_and_turn)
while True:
if brain.timer_time(SECONDS) > 3:
move_event.broadcast_and_wait()
break
wait(5, MSEC)
brain.print("Movement done.")
# VR threads — Do not delete
vr_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 broadcasted.
Usage:
event(callback, args)
Parameter |
Description |
|---|---|
|
The name of the previously created event object. |
|
A function that is previously defined to execute when the event is broadcasted. |
def dash_lines():
# Move the pen up and down
while True:
pen.move(DOWN)
wait(0.5, SECONDS)
pen.move(UP)
wait(0.5, SECONDS)
def main():
# Set and call the event and start driving
move_event = Event()
move_event(dash_lines)
move_event.broadcast()
drivetrain.drive(FORWARD)
# VR threads — Do not delete
vr_thread(main)