Eventos#
Introdução#
Os eventos permitem executar funções em paralelo. Em vez de chamar funções sequencialmente, pode registar várias funções num evento e acioná-las simultaneamente. Cada função registada é executada no seu próprio thread, permitindo que o seu robô realize várias tarefas ao mesmo tempo — como exibir valores no ecrã enquanto se move.
Para criar e utilizar um evento, siga os seguintes passos pela ordem indicada:
Crie o evento com
Event.Register a function to trigger when the
Eventis broadcast by calling theeventobject with the function as an argument.Broadcast an
Event:broadcast— Aciona todas as funções registadas num objeto de evento para serem executadas em simultâneo.broadcast_and_wait— Aciona todas as funções registadas num objeto de evento e espera que terminem antes de continuar.
Criar um evento#
Event cria um objeto de evento que gere a execução de funções em threads separados.
Usage:
Event()
Parâmetro |
Descrição |
|---|---|
Este construtor não possui parâmetros. |
# Blink the screen while moving forward
# Create move_event as an Event object
move_event = Event()
def blink_screen():
while True:
brain.screen.clear_screen(Color.RED)
wait(0.5, SECONDS)
brain.screen.clear_screen()
wait(0.5, SECONDS)
move_event(blink_screen)
move_event.broadcast()
drivetrain.drive(FORWARD)
Registar funções num evento#
Ao registar uma função para um evento, esta será executada num thread separado quando o evento for transmitido.
Usage:
event(callback, args)
Parâmetro |
Descrição |
|---|---|
|
O nome do objeto de evento criado anteriormente. |
|
Uma função previamente definida para ser executada quando o evento é transmitido. |
|
Opcional. Uma tupla de argumentos para passar para a função de callback. |
# Blink the screen while turning
move_event = Event()
def blink_screen():
while True:
brain.screen.clear_screen(Color.RED)
wait(0.5, SECONDS)
brain.screen.clear_screen()
wait(0.5, SECONDS)
def turning():
drivetrain.turn(RIGHT)
# Register multiple functions to the Event object
move_event(blink_screen)
move_event(turning)
move_event.broadcast()
# Move in a specified direction
move_event = Event()
def move_distance(movement_angle):
drivetrain.turn_for(RIGHT, movement_angle, DEGREES)
drivetrain.drive_for(FORWARD, 100, MM)
# Change the parameter to modify how the robot moves
move_event(move_distance, (180,))
move_event.broadcast()
Transmita um evento#
broadcast#
O comando broadcast dispara um evento, iniciando todas as funções registadas em threads separados. Este método não pausa a execução de nenhuma função subsequente.
Usage:
event.broadcast()
Parâmetro |
Descrição |
|---|---|
|
O nome do objeto de evento criado anteriormente. |
# Blink the screen while moving forward
# Create move_event as an Event object
move_event = Event()
def blink_screen():
while True:
brain.screen.clear_screen(Color.RED)
wait(0.5, SECONDS)
brain.screen.clear_screen()
wait(0.5, SECONDS)
move_event(blink_screen)
move_event.broadcast()
drivetrain.drive(FORWARD)
broadcast_and_wait#
broadcast_and_wait inicia um evento, mas espera que todas as funções registadas terminem antes de continuar com as funções subsequentes.
Usage:
event.broadcast_and_wait()
Parâmetro |
Descrição |
|---|---|
|
O nome do objeto de evento criado anteriormente. |
# Move after the screen is pressed
move_event = Event()
def move_and_turn():
drivetrain.drive_for(FORWARD, 150, MM)
drivetrain.turn_for(RIGHT, 90)
move_event(move_and_turn)
while True:
if brain.buttonCheck.pressing():
move_event.broadcast_and_wait()
break
brain.screen.print("Movement done.")