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 LED y conducir.
A continuación se muestra una lista de métodos y constructores disponibles:
Event – Creates a new event object.
event – Registers a function to the event object, optionally with arguments.
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.
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. |
# 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#
Cuando se registra una función en un evento, se ejecutará en un hilo separado cuando se transmita el evento.
Uso:
event(callback, args)
Parámetro |
Descripción |
---|---|
|
El nombre del objeto de evento creado previamente. |
|
Una función que se define previamente para ejecutarse cuando se transmite el evento. |
|
Optional. A tuple containing arguments to pass to the callback function. See Using Functions with Parameters for more information. |
# 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#
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 |
---|---|
|
El nombre del objeto de evento creado previamente. |
# 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
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 |
---|---|
|
El nombre del objeto de evento creado previamente. |
# 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.")