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#

The Event constructor is used to create an event object that manages function execution in separate threads.

Usage:
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.

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

Opcional. Una tupla que contiene los argumentos que se pasarán a la función de devolución de llamada. Consulte Uso de funciones con parámetros para obtener más información.

# 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 triggers an event, starting all registered functions in separate threads. This method does not pause execution of any subsequent functions.

Usage:
event.broadcast()

parámetro

descripción

event

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 starts an event but waits for all registered functions to finish before continuing with subsequent functions.

Usage:
event.broadcast_and_wait()

parámetro

descripción

event

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.")