Brain & Display

The Brain class provides access to the VEX V5 brain's built-in components, including the screen, battery, and SD card. This is the central hub for interacting with the robot's onboard hardware.


Brain

vex.Brain

Brain class

The Brain class creates a number of instances of internal classes that allow access\ to the screen, battery, 3wire ports and sd card on the V5 brain.

Arguments:
None
Returns:
An instance of the Brain class
Examples:
brain = Brain()

battery = Brain.Battery()

An instance of the Battery class

screen = Brain.Lcd()

An instance of the Lcd class

sdcard = Brain.Sdcard()

An instance of the Sdcard class

three_wire_port = Triport(Ports.PORT22)

An instance of the Triport (3wire) class

timer = Timer()

An instance of the Timer class

Battery

Battery class - access the brain battery
Arguments:
None
Returns:
Instance of Battery class
Examples:

capacity()

read remaining capacity of the battery
Arguments:
None
Returns:
capacity as percentage
Examples:

current(units=CurrentUnits.AMP)

read the current of the battery
Arguments:
units (optional) : AMP, default is mA but jot available as an enum.
Returns:
current in supplied units
Examples:

temperature(units=PercentUnits.PERCENT)

read the temperature of the battery
Arguments:
units (optional) : PERCENT, CELSIUS or FAHRENHEIT, default is CELSIUS
Returns:
temperature in supplied units
Examples:

voltage(units=VoltageUnits.MV)

read the voltage of the battery
Arguments:
units (optional) : VOLTS or MV, default is MV
Returns:
voltage in supplied units
Examples:

Lcd

Brain.Lcd class

A class used to access to screen on the V5 for drawing and receiving touch events.

Arguments:
None
Returns:
An instance of the Brain.Lcd class

clear_row(number=None, color=Color.BLACK)

Clear screen row to a single color

The color can be passed in similar ways to the Color class.\

Arguments:
row (optional) : The row to clear, default is current cursor row
color (optional) : The color the screen will be set to, default is BLACK
Returns:
None
Examples:
# clear row to black\
brain.screen.clear_row()

# clear row 2 to red\
brain.screen.clear_row(2, Color.RED)

clear_screen(color=Color.BLACK)

Clear the whole screen to a single color

The color can be passed in similar ways to the Color class.\

Arguments:
color (optional) : The color the screen will be set to, default is BLACK
Returns:
None
Examples:
# clear screen to black\
brain.screen.clear_screen()

# clear screen to blue using predefined color\
brain.screen.clear_screen(Color.BLUE)

column()

Return the current column where text will be printed

draw_circle(x, y, radius, color=None)

Draw a circle on the screen using the current pen and fill colors.
Arguments:
x : The x position of the circle center referenced to the screen origin.
y : The y position of the circle center referenced to the screen origin.
radius : The height of the circle.
color (optional) : An optional fill color, the current fill color will be used if not supplied
Returns:
None
Examples:
# draw a green circle on the screen that is filled using blue\
brain.screen.set_pen_color(Color.GREEN)\
brain.screen.set_fill_color(Color.BLUE)\
brain.screen.draw_circle(50, 50, 10)

# draw a green circle on the screen that is filled using red\
brain.screen.set_pen_color(Color.GREEN)\
brain.screen.draw_circle(100, 50, 10, Color.RED)

draw_image_from_file(filename, x, y)

Display the named image from the SD Card
Arguments:
filename : The file name of the image.
x : The X coordinate for the top left corner of the image on the screen
y : The Y coordinate for the top left corner of the image on the screen
Returns:
True if successfully drawn, False on error
Examples:
# draw the vex.bmp image on the screen at coordinate 0, 0\
# an image named vex.bmp must be on the SD Card in the root folder\
brain.screen.draw_image_from_file('vex.bmp', 0, 0)

draw_line(x1, y1, x2, y2)

Draw a line on the screen using the current pen color.
Arguments:
x1 : The x position of the beginning of the line referenced to the screen origin.
y1 : The y position of the beginning of the line referenced to the screen origin.
x2 : The x position of the end of the line referenced to the screen origin.
y2 : The y position of the end of the line referenced to the screen origin.
Returns:
None
Examples:
# draw a red line on the screen\
brain.screen.set_pen_color(Color.RED)\
brain.screen.draw_line(10, 10, 20, 20)

draw_pixel(x, y)

Draw a pixel on the screen using the current pen color.
Arguments:
x : The x position to draw the pixel referenced to the screen origin.
y : The y position to draw the pixel referenced to the screen origin.
Returns:
None
Examples:
# draw a red pixel on the screen\
brain.screen.set_pen_color(Color.RED)\
brain.screen.draw_pixel(10, 10)

draw_rectangle(x, y, width, height, color=None)

Draw a rectangle on the screen using the current pen and fill colors.
Arguments:
x : The x position of the rectangle top/left corner referenced to the screen origin.
y : The y position of the rectangle top/left corner referenced to the screen origin.
width : The width of the rectangle.
height : The height of the rectangle.
color (optional) : An optional fill color, the current fill color will be used if not supplied
Returns:
None
Examples:
# draw a green rectangle on the screen that is filled using blue\
brain.screen.set_pen_color(Color.GREEN)\
brain.screen.set_fill_color(Color.BLUE)\
brain.screen.draw_rectangle(10, 10, 20, 20)

# draw a green rectangle on the screen that is filled using red\
brain.screen.set_pen_color(Color.GREEN)\
brain.screen.draw_rectangle(50, 50, 20, 20, Color.RED)

get_string_height(*args)

get height of a string
Arguments:
arguments are in the same format as can be passed to the print function.
Returns:
height of string as integer.

get_string_width(*args)

get width of a string
Arguments:
arguments are in the same format as can be passed to the print function.
Returns:
width of string as integer.

next_row()

Move the cursor to the beginning of the next row
Arguments:
None
Returns:
None

pressed(callback, arg=())

Register a function to be called when the screen is pressed
Arguments:
callback : A function that will be called when the screen is pressed
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("screen pressed")

brain.screen.pressed(foo)

pressing()

Returns whether the screen is currently being pressed (touched)
Arguments:
None
Returns:
True or False

print(*args, **kwargs)

print text on the screen using current curser position.
Arguments:
Optional keyword arguments:
sep : string inserted between values, default a space.
precision : the number of decimal places to display when printing simple numbers, default is 2
Returns:
None
Examples:
# print the number 1 on the screen at current cursor position\
brain.screen.print(1)

# print the numbers 1, 2, 3 and 4 on the screen at current cursor position separated by a '-'\
brain.screen.print(1, 2, 3, 4, sep='-')

# print motor1 velocity on the screen using a format string\
brain.screen.print("motor  1 : % 7.2f" %(motor1.velocity()))

print_at(*args, **kwargs)

print text on the screen at x and coordinates.
Arguments:
Required keyword arguments
x : The x position of the text referenced to the screen origin.
y : The y position of the text referenced to the screen origin.

Optional keyword arguments:
sep : string inserted between values, default a space.
precision : the number of decimal places to display when printing simple numbers, default is 2
opaque : text does not clear background pixels if set to False. default is True.
Returns:
None
Examples:
# print the number 1 on the screen at position x=100, y=40\
brain.screen.print_at(1, x=100, y=40)

# print the numbers 1, 2, 3 and 4 on the screen at position x=100, y=40\
brain.screen.print_at(1, 2, 3, 4, x=100, y=40)

# print motor1 velocity on the screen using a format string at position x=100, y=40\
brain.screen.print_at("motor  1 : % 7.2f" %(motor1.velocity()), x=100, y=40)

released(callback, arg=())

Register a function to be called when the screen is released (touch removed)
Arguments:
callback : A function that will be called when the screen is released
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("screen released")

brain.screen.released(foo)

render()

Switch drawing to double buffered and render too screen.

Once called, further drawing will not appear on the screen until the next time\ render is called. This function will block until the screen can be updated.

Arguments:
None
Returns:
True if buffer was successfully rendered to screen.
Examples:

row()

Return the current row where text will be printed

set_clip_region(x, y, width, height)

Sets the clip region for drawing to the supplied rectangle.

All drawing is clipped to the given rectangle.\ This is set on a per thread basis.

Arguments:
x : The x position of the rectangle top/left corner referenced to the screen origin.
y : The y position of the rectangle top/left corner referenced to the screen origin.
width : The width of the rectangle.
height : The height of the rectangle.
Returns:
None
Examples:

set_cursor(row, col)

Set the cursor position used for printing text on the screen

row and column spacing will take into account the selected font.\ The base cell size if 10x20 pixels for the MONO20 font.\ text may not accurately print if using a proportional font.\ The top, left corner of the screen is position 1,1

Arguments:
row : The cursor row
col : The cursor column
Returns:
None

set_fill_color(color)

Set the fill color used for drawing rectangles and circles

The color can be passed in similar ways to the Color class.\ The color is specific to the running thread.

Arguments:
color : The fill color
Returns:
None
Examples:
# set pen color red using a hex value\
brain.screen.set_fill_color(0xFF0000)

# set pen color blue using predefined color\
brain.screen.set_fill_color(Color.BLUE)

# set pen color green using web string\
brain.screen.set_fill_color("#00FF00")

set_font(fontname)

Set the font type used for printing text on the screen
Arguments:
fontname : The font name
Returns:
None
Examples:
brain.screen.font_type(FontType.MONO40)

set_origin(x, y)

Set the origin used for drawing graphics on the screen

drawing functions consider the top left corner of the screen as the origin.\ This function can move the origin to an alternate position such as the center of the screen.

Arguments:
x : The origins x position relative to top left corner
y : The origins y position relative to top left corner
Returns:
None

set_pen_color(color)

Set the pen color used for drawing lines, rectangles and circles

The color can be passed in similar ways to the Color class.\ The color is specific to the running thread.

Arguments:
color : The pen color
Returns:
None
Examples:
# set pen color red using a hex value\
brain.screen.set_pen_color(0xFF0000)

# set pen color blue using predefined color\
brain.screen.set_pen_color(Color.BLUE)

# set pen color green using web string\
brain.screen.set_pen_color("#00FF00")

set_pen_width(width)

Set the pen width used for drawing lines, rectangles and circles
Arguments:
width : The pen width
Returns:
None

x_position()

The X coordinate of the last screen event, press or release
Arguments:
None
Returns:
The X coordinate as an int
Examples:
def foo():
    print("screen pressed at ", brain.screen.x_position())

brain.screen.pressed(foo)

y_position()

The Y coordinate of the last screen event, press or release
Arguments:
None
Returns:
The Y coordinate as an int
Examples:
def foo():
    print("screen pressed at ", brain.screen.y_position())

brain.screen.pressed(foo)

Sdcard

Sdcard class - access the brain sdcard
Arguments:
None
Returns:
Instance of Sdcard class
Examples:

appendfile(filename, *args)

append a bytearray into a named file

Append is used to add more data to an existing file.

Arguments:
filename : The name of the file to write
buffer : A bytearray to write into the file
Returns:
The number of bytes written
Examples:
# append bytearray into file\
brain.sdcard.appendfile('MyTextFile.txt', bytearray("World "))

exists(*args)

check to see if named file exists on the sd card
Arguments:
filename : The name of the file to check
Returns:
True if file exists
Examples:

filesize(filename)

returns the size in bytes of the named file
Arguments:
filename : The name of the file to check
Returns:
size of file in bytes
Examples:

is_inserted()

returns status of SD Card
Arguments:
None
Returns:
True if an sdcard is inserted into the brain
Examples:

loadfile(filename, *args)

load the named file
Arguments:
filename : The name of the file to read
buffer (optional) : A bytearray to read the file into
Returns:
A bytearray with file data
Examples:
# read file into new bytearray\
b = brain.sdcard.loadfile('MyTextFile.txt')

savefile(filename, *args)

Save a bytearray into a named file

If the optional bytearray is None, then an empty file is created.

Arguments:
filename : The name of the file to write
buffer (optional) : A bytearray to write into the file
Returns:
The number of bytes written
Examples:
# write bytearray into file\
brain.sdcard.savefile('MyTextFile.txt', bytearray("Hello "))

size(filename)

returns the size in bytes of the named file
Arguments:
filename : The name of the file to check
Returns:
size of file in bytes
Examples:

LCD Display

The Brain.Lcd class allows you to draw graphics, print text, and handle touch events on the V5 brain's screen.

vex.Brain.Lcd

Brain.Lcd class

A class used to access to screen on the V5 for drawing and receiving touch events.

Arguments:
None
Returns:
An instance of the Brain.Lcd class

clear_row(number=None, color=Color.BLACK)

Clear screen row to a single color

The color can be passed in similar ways to the Color class.\

Arguments:
row (optional) : The row to clear, default is current cursor row
color (optional) : The color the screen will be set to, default is BLACK
Returns:
None
Examples:
# clear row to black\
brain.screen.clear_row()

# clear row 2 to red\
brain.screen.clear_row(2, Color.RED)

clear_screen(color=Color.BLACK)

Clear the whole screen to a single color

The color can be passed in similar ways to the Color class.\

Arguments:
color (optional) : The color the screen will be set to, default is BLACK
Returns:
None
Examples:
# clear screen to black\
brain.screen.clear_screen()

# clear screen to blue using predefined color\
brain.screen.clear_screen(Color.BLUE)

column()

Return the current column where text will be printed

draw_circle(x, y, radius, color=None)

Draw a circle on the screen using the current pen and fill colors.
Arguments:
x : The x position of the circle center referenced to the screen origin.
y : The y position of the circle center referenced to the screen origin.
radius : The height of the circle.
color (optional) : An optional fill color, the current fill color will be used if not supplied
Returns:
None
Examples:
# draw a green circle on the screen that is filled using blue\
brain.screen.set_pen_color(Color.GREEN)\
brain.screen.set_fill_color(Color.BLUE)\
brain.screen.draw_circle(50, 50, 10)

# draw a green circle on the screen that is filled using red\
brain.screen.set_pen_color(Color.GREEN)\
brain.screen.draw_circle(100, 50, 10, Color.RED)

draw_image_from_file(filename, x, y)

Display the named image from the SD Card
Arguments:
filename : The file name of the image.
x : The X coordinate for the top left corner of the image on the screen
y : The Y coordinate for the top left corner of the image on the screen
Returns:
True if successfully drawn, False on error
Examples:
# draw the vex.bmp image on the screen at coordinate 0, 0\
# an image named vex.bmp must be on the SD Card in the root folder\
brain.screen.draw_image_from_file('vex.bmp', 0, 0)

draw_line(x1, y1, x2, y2)

Draw a line on the screen using the current pen color.
Arguments:
x1 : The x position of the beginning of the line referenced to the screen origin.
y1 : The y position of the beginning of the line referenced to the screen origin.
x2 : The x position of the end of the line referenced to the screen origin.
y2 : The y position of the end of the line referenced to the screen origin.
Returns:
None
Examples:
# draw a red line on the screen\
brain.screen.set_pen_color(Color.RED)\
brain.screen.draw_line(10, 10, 20, 20)

draw_pixel(x, y)

Draw a pixel on the screen using the current pen color.
Arguments:
x : The x position to draw the pixel referenced to the screen origin.
y : The y position to draw the pixel referenced to the screen origin.
Returns:
None
Examples:
# draw a red pixel on the screen\
brain.screen.set_pen_color(Color.RED)\
brain.screen.draw_pixel(10, 10)

draw_rectangle(x, y, width, height, color=None)

Draw a rectangle on the screen using the current pen and fill colors.
Arguments:
x : The x position of the rectangle top/left corner referenced to the screen origin.
y : The y position of the rectangle top/left corner referenced to the screen origin.
width : The width of the rectangle.
height : The height of the rectangle.
color (optional) : An optional fill color, the current fill color will be used if not supplied
Returns:
None
Examples:
# draw a green rectangle on the screen that is filled using blue\
brain.screen.set_pen_color(Color.GREEN)\
brain.screen.set_fill_color(Color.BLUE)\
brain.screen.draw_rectangle(10, 10, 20, 20)

# draw a green rectangle on the screen that is filled using red\
brain.screen.set_pen_color(Color.GREEN)\
brain.screen.draw_rectangle(50, 50, 20, 20, Color.RED)

get_string_height(*args)

get height of a string
Arguments:
arguments are in the same format as can be passed to the print function.
Returns:
height of string as integer.

get_string_width(*args)

get width of a string
Arguments:
arguments are in the same format as can be passed to the print function.
Returns:
width of string as integer.

next_row()

Move the cursor to the beginning of the next row
Arguments:
None
Returns:
None

pressed(callback, arg=())

Register a function to be called when the screen is pressed
Arguments:
callback : A function that will be called when the screen is pressed
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("screen pressed")

brain.screen.pressed(foo)

pressing()

Returns whether the screen is currently being pressed (touched)
Arguments:
None
Returns:
True or False

print(*args, **kwargs)

print text on the screen using current curser position.
Arguments:
Optional keyword arguments:
sep : string inserted between values, default a space.
precision : the number of decimal places to display when printing simple numbers, default is 2
Returns:
None
Examples:
# print the number 1 on the screen at current cursor position\
brain.screen.print(1)

# print the numbers 1, 2, 3 and 4 on the screen at current cursor position separated by a '-'\
brain.screen.print(1, 2, 3, 4, sep='-')

# print motor1 velocity on the screen using a format string\
brain.screen.print("motor  1 : % 7.2f" %(motor1.velocity()))

print_at(*args, **kwargs)

print text on the screen at x and coordinates.
Arguments:
Required keyword arguments
x : The x position of the text referenced to the screen origin.
y : The y position of the text referenced to the screen origin.

Optional keyword arguments:
sep : string inserted between values, default a space.
precision : the number of decimal places to display when printing simple numbers, default is 2
opaque : text does not clear background pixels if set to False. default is True.
Returns:
None
Examples:
# print the number 1 on the screen at position x=100, y=40\
brain.screen.print_at(1, x=100, y=40)

# print the numbers 1, 2, 3 and 4 on the screen at position x=100, y=40\
brain.screen.print_at(1, 2, 3, 4, x=100, y=40)

# print motor1 velocity on the screen using a format string at position x=100, y=40\
brain.screen.print_at("motor  1 : % 7.2f" %(motor1.velocity()), x=100, y=40)

released(callback, arg=())

Register a function to be called when the screen is released (touch removed)
Arguments:
callback : A function that will be called when the screen is released
arg (optional) : A tuple that is used to pass arguments to the callback function.
Returns:
An instance of the Event class
Examples:
def foo():
    print("screen released")

brain.screen.released(foo)

render()

Switch drawing to double buffered and render too screen.

Once called, further drawing will not appear on the screen until the next time\ render is called. This function will block until the screen can be updated.

Arguments:
None
Returns:
True if buffer was successfully rendered to screen.
Examples:

row()

Return the current row where text will be printed

set_clip_region(x, y, width, height)

Sets the clip region for drawing to the supplied rectangle.

All drawing is clipped to the given rectangle.\ This is set on a per thread basis.

Arguments:
x : The x position of the rectangle top/left corner referenced to the screen origin.
y : The y position of the rectangle top/left corner referenced to the screen origin.
width : The width of the rectangle.
height : The height of the rectangle.
Returns:
None
Examples:

set_cursor(row, col)

Set the cursor position used for printing text on the screen

row and column spacing will take into account the selected font.\ The base cell size if 10x20 pixels for the MONO20 font.\ text may not accurately print if using a proportional font.\ The top, left corner of the screen is position 1,1

Arguments:
row : The cursor row
col : The cursor column
Returns:
None

set_fill_color(color)

Set the fill color used for drawing rectangles and circles

The color can be passed in similar ways to the Color class.\ The color is specific to the running thread.

Arguments:
color : The fill color
Returns:
None
Examples:
# set pen color red using a hex value\
brain.screen.set_fill_color(0xFF0000)

# set pen color blue using predefined color\
brain.screen.set_fill_color(Color.BLUE)

# set pen color green using web string\
brain.screen.set_fill_color("#00FF00")

set_font(fontname)

Set the font type used for printing text on the screen
Arguments:
fontname : The font name
Returns:
None
Examples:
brain.screen.font_type(FontType.MONO40)

set_origin(x, y)

Set the origin used for drawing graphics on the screen

drawing functions consider the top left corner of the screen as the origin.\ This function can move the origin to an alternate position such as the center of the screen.

Arguments:
x : The origins x position relative to top left corner
y : The origins y position relative to top left corner
Returns:
None

set_pen_color(color)

Set the pen color used for drawing lines, rectangles and circles

The color can be passed in similar ways to the Color class.\ The color is specific to the running thread.

Arguments:
color : The pen color
Returns:
None
Examples:
# set pen color red using a hex value\
brain.screen.set_pen_color(0xFF0000)

# set pen color blue using predefined color\
brain.screen.set_pen_color(Color.BLUE)

# set pen color green using web string\
brain.screen.set_pen_color("#00FF00")

set_pen_width(width)

Set the pen width used for drawing lines, rectangles and circles
Arguments:
width : The pen width
Returns:
None

x_position()

The X coordinate of the last screen event, press or release
Arguments:
None
Returns:
The X coordinate as an int
Examples:
def foo():
    print("screen pressed at ", brain.screen.x_position())

brain.screen.pressed(foo)

y_position()

The Y coordinate of the last screen event, press or release
Arguments:
None
Returns:
The Y coordinate as an int
Examples:
def foo():
    print("screen pressed at ", brain.screen.y_position())

brain.screen.pressed(foo)

Battery

The Brain.Battery class provides information about the V5 brain's battery, such as capacity, voltage, current, and temperature.

vex.Brain.Battery

Battery class - access the brain battery
Arguments:
None
Returns:
Instance of Battery class
Examples:

capacity()

read remaining capacity of the battery
Arguments:
None
Returns:
capacity as percentage
Examples:

current(units=CurrentUnits.AMP)

read the current of the battery
Arguments:
units (optional) : AMP, default is mA but jot available as an enum.
Returns:
current in supplied units
Examples:

temperature(units=PercentUnits.PERCENT)

read the temperature of the battery
Arguments:
units (optional) : PERCENT, CELSIUS or FAHRENHEIT, default is CELSIUS
Returns:
temperature in supplied units
Examples:

voltage(units=VoltageUnits.MV)

read the voltage of the battery
Arguments:
units (optional) : VOLTS or MV, default is MV
Returns:
voltage in supplied units
Examples:

SD Card

The Brain.Sdcard class allows you to interact with the SD card inserted into the V5 brain, including file operations.

vex.Brain.Sdcard

Sdcard class - access the brain sdcard
Arguments:
None
Returns:
Instance of Sdcard class
Examples:

appendfile(filename, *args)

append a bytearray into a named file

Append is used to add more data to an existing file.

Arguments:
filename : The name of the file to write
buffer : A bytearray to write into the file
Returns:
The number of bytes written
Examples:
# append bytearray into file\
brain.sdcard.appendfile('MyTextFile.txt', bytearray("World "))

exists(*args)

check to see if named file exists on the sd card
Arguments:
filename : The name of the file to check
Returns:
True if file exists
Examples:

filesize(filename)

returns the size in bytes of the named file
Arguments:
filename : The name of the file to check
Returns:
size of file in bytes
Examples:

is_inserted()

returns status of SD Card
Arguments:
None
Returns:
True if an sdcard is inserted into the brain
Examples:

loadfile(filename, *args)

load the named file
Arguments:
filename : The name of the file to read
buffer (optional) : A bytearray to read the file into
Returns:
A bytearray with file data
Examples:
# read file into new bytearray\
b = brain.sdcard.loadfile('MyTextFile.txt')

savefile(filename, *args)

Save a bytearray into a named file

If the optional bytearray is None, then an empty file is created.

Arguments:
filename : The name of the file to write
buffer (optional) : A bytearray to write into the file
Returns:
The number of bytes written
Examples:
# write bytearray into file\
brain.sdcard.savefile('MyTextFile.txt', bytearray("Hello "))

size(filename)

returns the size in bytes of the named file
Arguments:
filename : The name of the file to check
Returns:
size of file in bytes
Examples: