Events#

bumper.pressed()#

The bumper.pressed(callback) command is used to run a specified function when the bumper is pressed.

This is a non-waiting command and allows any subsequent commands to execute without delay.

Parameters

Description

callback

The name of a previously defined function.

Returns: None.

# Define a new function called "bumper_was_pressed"
def bumper_was_pressed():
    brain.print("Bumper pressed!")
 
# Use the defined function in the callback parameter.
right_bumper.pressed(bumper_was_pressed)

bumper.released()#

The bumper.released(callback) command is used to run a specified function when the bumper is released.

This is a non-waiting command and allows any subsequent commands to execute without delay.

Parameters

Description

callback

The name of a previously defined function.

Returns: None.

# Define a new function called "bumper_was_released"
def bumper_was_released():
    brain.print("Bumper released!")
 
# Use the defined function in the callback parameter.
right_bumper.released(bumper_was_released)

brain.timer.event()#

The brain.timer.event(callback, time) command is used to run the specified callback function when the Brain’s timer is equal to the specified time value.

This is a non-waiting command and allows any subsequent commands to execute without delay.

The Brain’s timer begins at the beginning of each project or whenever the Brain’s timer is reset.

Parameters

Description

callback

Any previously defined function.

time

The amount of time to wait after the start of the project, in Milliseconds.

Returns: None.

# Define a new function called "five_second_event".
def five_second_event():
    brain.print("Five seconds have passed.")
  
# Create a timer to trigger the defined "five_second_event" after
# 5000 Milliseconds (5 seconds).
brain.timer_event(five_second_event, 5000)

my_event = Event()#

The my_event = Event() command is used to create a new event.

This is a non-waiting command and allows any subsequent commands to execute without delay.

Parameters

Description

my_event

A name for the new event. This can not contain any numbers.

Returns: None.

# Define a new function
def draw_line():
    pen.move(DOWN)
    drivetrain.drive_for(FORWARD, 400, MM)

def main():
    # Create a new draw event.
    draw_event = Event()

    # Register the defined function to the new event.
    draw_event(draw_line)

    # Wait 15 Milliseconds to give the event time to register.
    wait(15, MSEC)

    # Broadcast the draw event to trigger the defined function.
    draw_event.broadcast()

event()#

The event(callback) command is used to register a callback function to run when an event is triggered.

This is a non-waiting command and allows any subsequent commands to execute without delay.

Parameters

Description

event

The name of a previously created event.

callback

The name of a previously defined function.

Returns: None.

# Define a new function
def draw_line():
    pen.move(DOWN)
    drivetrain.drive_for(FORWARD, 400, MM)

def main():
    # Create a new draw event.
    drawEvent = Event()

    # Register the defined function to the new event.
    drawEvent(draw_line)

    # Wait 15 Milliseconds to give the event time to register.
    wait(15, MSEC)

    # Broadcast the draw event to trigger the defined function.
    drawEvent.broadcast()

event.broadcast()#

The event.broadcast() command is used to broadcast event messages to run listener functions.

This is a non-waiting command, and any subsequent commands will execute without waiting for any listener functions to finish.

Parameters

Description

event

The name of a previously created event.

Returns: None.

# Define a new function
def draw_line():
    pen.move(DOWN)
    drivetrain.drive_for(FORWARD, 400, MM)

def main():
    # Create a new draw event.
    drawEvent = Event()

    # Register the defined function to the new event.
    drawEvent(draw_line)

    # Wait 15 Milliseconds to give the event time to register.
    wait(15, MSEC)

    # Broadcast the draw event to trigger the defined function.
    drawEvent.broadcast()

event.broadcast_and_wait()#

The event.broadcast_and_wait() command is used to broadcast event message to run listener functions and wait for all listeners to finish before continuing.

This is a waiting command and will prevent any subsequent commands from executing until all listener functions have finished.

Parameters

Description

event

The name of a previously created event.

Returns: None.

# Define a new function
def draw_line():
    pen.move(DOWN)
    drivetrain.drive_for(FORWARD, 400, MM)

def main():
    # Create a new draw event.
    drawEvent = Event()

    # Register the defined function to the new event.
    drawEvent(draw_line)

    # Wait 15 Milliseconds to give the event time to register.
    wait(15, MSEC)

    # Broadcast the draw event to trigger the defined function and wait for
    # the listener functions to finish before executing any subsequent
    # functions.
    drawEvent.broadcast_and_wait()

eye_sensor.object_detected()#

The eye_sensor.object_detected(callback) command is used to run the callback function when an object is detected by the Eye Sensor.

This is a non-waiting command and allows any subsequent commands to execute without delay.

Parameters

Description

eye_sensor

Which Eye Sensor to use: front_eye or down_eye.

callback

A previously defined function.

Returns: None.

# Define a new function called "front_object_detected"
def front_object_detected():
    brain.print("Front eye detected an object!")

# Use the defined function in the callback parameter.
front_eye.object_detected(front_object_detected)

eye_sensor.object_lost()#

The eye_sensor.object_lost(callback) command can be used to run the callback function when an object is no longer being detected by the Eye Sensor.

This is a non-waiting command and allows any subsequent commands to execute without delay.

Parameters

Description

eye_sensor

Which Eye Sensor to use: front_eye or down_eye.

callback

A previously defined function.

Returns: None.

# Define a new function called "front_object_detected"
def down_object_loss():
    brain.print("Down eye lost an object!")

# Use the defined function in the callback parameter.
down_eye.object_lost(down_object_loss)