Eventos#
Introducción#
Events allow you to run functions in parallel. Instead of calling functions sequentially, you can register multiple functions to an event and trigger them all at once. Each registered function runs in its own thread, enabling your robot to perform multiple tasks simultaneously—like displaying values on the screen while driving.
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.
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)
Registrar funciones en un evento#
When you register a function to an event, it will execute in a separate thread when the event is broadcast.
Usage:
event(callback, args)
Parámetro |
Descripción |
|---|---|
|
El nombre del objeto de evento creado previamente. |
|
A previously defined function to execute when the event is broadcast. |
|
Optional. A tuple of arguments to pass to the callback function. |
# 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()
Transmitir un evento#
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 |
|---|---|
|
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 |
|---|---|
|
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.")