Eventos#
Introducción#
Los eventos permiten ejecutar funciones en paralelo. En lugar de llamarlas secuencialmente, puedes registrar varias funciones en un evento y activarlas todas a la vez. Cada función registrada se ejecuta en su propio hilo, lo que permite que tu robot realice varias tareas simultáneamente, como mostrar valores en la pantalla mientras se desplaza.
Para crear y luego usar un evento, siga estos pasos en orden:
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.
Crear un evento#
Event creates an event object that manages function execution in separate threads.
Uso:
Event()
Parámetros |
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)
Registrar funciones en un evento#
Cuando se registra una función en un evento, esta se ejecutará en un hilo independiente cuando se transmita dicho evento.
Usage:
event(callback, args)
Parámetros |
Descripción |
|---|---|
|
El nombre del objeto de evento creado previamente. |
|
Una función definida previamente que se ejecutará cuando se transmita el evento. |
|
Opcional. Una tupla de argumentos para pasar a la función de devolución de llamada. |
# 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)
Transmitir un evento#
broadcast#
broadcast triggers an event, starting all registered functions in separate threads. This method does not pause execution of any subsequent code.
Uso:
event.broadcast()
Parámetros |
Descripción |
|---|---|
|
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ámetros |
Descripción |
|---|---|
|
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)