Eventos#

Introducción#

Los eventos permiten ejecutar funciones en paralelo mediante objetos de evento. En lugar de llamar a funciones o subprocesos uno tras otro, los eventos permiten registrar funciones y activarlas todas a la vez. Cada función registrada se ejecuta en su propio subproceso, lo que permite que el robot realice varias acciones simultáneamente, como hacer parpadear los TouchLED y conducir.

A continuación se muestra una lista de métodos y constructores disponibles:

  • 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#

El constructor Event se utiliza para crear un objeto de evento que administra la ejecución de funciones en subprocesos separados.

Uso:
Event()

Parámetro

Descripción

Este constructor no tiene parámetros.

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

Broadcast#

La transmisión broadcast activa un evento que inicia todas las funciones registradas en subprocesos separados. Este método no detiene la ejecución de ninguna función posterior.

Uso:
event.broadcast()

Parámetro

Descripción

event

El nombre del objeto de evento creado previamente.

# 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 inicia un evento pero espera a que finalicen todas las funciones registradas antes de continuar con las funciones subsiguientes.

Uso:
event.broadcast_and_wait()

Parámetro

Descripción

event

El nombre del objeto de evento creado previamente.

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

Register Functions to an Event#

Cuando se registra una función en un evento, se ejecutará en un hilo separado cuando se transmita el evento.

Usage:
event(callback, args)

Parámetro

Descripción

event

El nombre del objeto de evento creado previamente.

callback

Una función que se define previamente para ejecutarse cuando se transmite el evento.

args

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

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