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 lights and driving—at the same time.

In order to create and then use an Event, follow these steps in order:

  1. Create the event with Event.

  2. Register a function to trigger when the Event is broadcast using event.

  3. 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()

Parameter

Description

This constructor has no parameters.

# Blink the screen while moving forward
# Create move_event as an Event object
move_event = Event()

def blink_screen():
    while True:
        brain.screen.clear_screen(Color.RED)
        wait(0.5, SECONDS)
        brain.screen.clear_screen()
        wait(0.5, SECONDS)

move_event(blink_screen)
move_event.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:
event(callback, args)

Parameter

Description

event

The name of the previously created event object.

callback

A function that is previously defined to execute when the event is broadcasted.

args

Optional. A tuple containing arguments to pass to the callback function. See Using Functions with Parameters for more information.

# Blink the screen while turning
move_event = Event()

def blink_screen():
    while True:
        brain.screen.clear_screen(Color.RED)
        wait(0.5, SECONDS)
        brain.screen.clear_screen()
        wait(0.5, SECONDS)

def turning():
    drivetrain.turn(RIGHT)

# Register multiple functions to the Event object
move_event(blink_screen)
move_event(turning)

move_event.broadcast()

# Move in a specified direction
move_event = Event()

def move_distance(movement_angle):
    drivetrain.turn_for(RIGHT, movement_angle, DEGREES)
    drivetrain.drive_for(FORWARD, 100, MM)

# Change the parameter to modify how the robot moves
move_event(move_distance, (180,))
move_event.broadcast()

Broadcast an Event#

broadcast#

broadcast triggers an event, starting all registered functions in separate threads. This method does not pause execution of any subsequent functions.

Usage:
event.broadcast()

Parameter

Description

event

The name of the previously created event object.

# Blink the screen while moving forward
# Create move_event as an Event object
move_event = Event()

def blink_screen():
    while True:
        brain.screen.clear_screen(Color.RED)
        wait(0.5, SECONDS)
        brain.screen.clear_screen()
        wait(0.5, SECONDS)

move_event(blink_screen)
move_event.broadcast()
drivetrain.drive(FORWARD)

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()

Parameter

Description

event

The name of the previously created event object.

# Move after the screen is pressed
move_event = Event()

def move_and_turn():
    drivetrain.drive_for(FORWARD, 150, MM)
    drivetrain.turn_for(RIGHT, 90)

move_event(move_and_turn)

while True:
    if brain.buttonCheck.pressing():
        move_event.broadcast_and_wait()
        break
brain.screen.print("Movement done.")