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:

  1. Create the event with Event.

  2. Register a function to trigger when the Event is broadcast by calling the event object with the function as an argument.

  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 LEDs while moving forward
# Create move_event as an Event object
move_event = Event()

def blink_leds():
    while True:
        robot.led.on(ALL_LEDS, RED)
        wait(0.5, SECONDS)
        robot.led.on(ALL_LEDS, BLACK)
        wait(0.5, SECONDS)

move_event(blink_leds)
move_event.broadcast()
robot.move_at(0)

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)

Parameter

Description

event

The name of the previously created event object.

callback

A previously defined function to execute when the event is broadcast.

args

Optional. A tuple of arguments to pass to the callback function.

# Blink LEDs while moving forward and turning
move_event = Event()

def blink_leds():
    while True:
        robot.led.on(ALL_LEDS, RED)
        wait(0.5, SECONDS)
        robot.led.on(ALL_LEDS, BLACK)
        wait(0.5, SECONDS)

def turning():
    robot.turn(RIGHT)

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

move_event.broadcast()

# Move in a specified direction
move_event = Event()

def move_distance(movement_angle):
    robot.move_for(50, movement_angle)

# 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 LEDs while moving forward and turning
move_event = Event()

def blink_leds():
    while True:
        robot.led.on(ALL_LEDS, RED)
        wait(0.5, SECONDS)
        robot.led.on(ALL_LEDS, BLACK)
        wait(0.5, SECONDS)


move_event(blink_leds)

# Trigger the move_event Event
move_event.broadcast()
robot.move_at(0)

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():
    robot.move_for(50, 0)
    robot.turn_for(RIGHT, 90)

move_event(move_and_turn)

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