Eventos#
Introducción#
Los eventos le permiten ejecutar funciones en paralelo mediante el uso de objetos de evento. En lugar de llamar a funciones o hilos uno tras otro, los eventos le permiten registrar funciones y luego activarlas todas a la vez. Cada función registrada se ejecuta en su propio hilo, por lo que tu robot puede hacer varias cosas al mismo tiempo, como parpadear con los TouchLED y conducir.
A continuación se muestra una lista de métodos y constructores disponibles:
Event– Crea un nuevo objeto de evento.broadcast: activa todas las funciones registradas en un objeto de evento para que se ejecuten en paralelo.broadcast_and_wait: activa todas las funciones registradas en un objeto de evento y espera a que finalicen antes de continuar.event– Registra una función en el objeto event, opcionalmente con argumentos.
Create an Event Object#
Event crea un objeto de evento que administra la ejecución de funciones en subprocesos separados.
Usage:
Event()
Parámetro |
Descripción |
|---|---|
Este constructor no tiene parámetros. |
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 desencadena un evento, iniciando todas las funciones registradas en subprocesos separados. Este método no detiene la ejecución de ninguna función posterior.
Usage:
my_event.broadcast()
Parámetro |
Descripción |
|---|---|
|
El nombre del objeto de evento creado anteriormente. |
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 inicia un evento pero espera a que todas las funciones registradas terminen antes de continuar con las funciones posteriores.
Usage:
my_event.broadcast_and_wait()
Parámetro |
Descripción |
|---|---|
|
El nombre del objeto de evento creado anteriormente. |
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#
Cuando registra una función en un evento, se ejecutará en un subproceso separado cuando se transmita el evento.
Usage:
event(callback, args)
Parámetro |
Descripción |
|---|---|
|
El nombre del objeto de evento creado anteriormente. |
|
Una función previamente definida para ejecutar cuando se transmite el evento. |
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)