SD Card#

Introduction#

The SD Card API for VEX IQ (2nd gen) enables you to store and access files on a removable SD card inserted into the Brain’s SD card slot. Whether you’re saving sensor data or loading custom images, these methods provide full control over file operations. The removable SD card allows you to easily transfer data between your Brain and computer.

The IQ (2nd gen) Brain requires an SD card no larger than 32GB, formatted as FAT32. SD cards larger than 32GB use exFAT formatting by default, which is not compatible with the VEX IQ (2nd gen) Brain. Ensure your SD card is properly formatted to FAT32 before use.

All files must be placed in the root folder of the SD card to be accessible.

For the examples below, the constructed Brain includes access to the SD Card methods and will be used in all subsequent examples throughout this API documentation when referring to those methods.

Below is a list of all methods:

Actions – Perform file operations and display images from the SD card.

  • loadfile – Loads a file from the SD card into a bytearray.

  • savefile – Saves a bytearray into the SD card.

  • appendfile – Appends a bytearray to an existing file.

  • draw_image_from_file – Displays a Bitmap Image from the SD card.

Getters – Return SD card and file information.

  • is_inserted – Returns a Boolean indicating whether an SD card is inserted into the Brain.

  • filesize – Returns the size of a file in bytes.

  • size – Returns the size of a file in bytes.

  • exists – Returns a Boolean indicating whether a file is found on the SD card.

Actions#

loadfile#

loadfile loads a file from the SD card into a bytearray.

Usage:
brain.sdcard.loadfile(filename, buffer)

Parameters

Description

filename

The name of the file to load as a string. The file’s name must include the file extension.

buffer

Optional. An existing bytearray to load the file’s content into. If the existing bytearray is larger, its contents will be overwritten. If the existing bytearray is smaller, the method may not load all data. By default, a new bytearray will be returned if no buffer is provided.

# Display a .txt file's contents on the brain
# Change the name of the file
text_file = brain.sdcard.loadfile("Text.txt")
brain.screen.print(text_file.decode("utf-8"))

savefile#

savefile saves a bytearray into the SD card.

Usage
brain.sdcard.savefile(filename, buffer)

Parameters

Description

filename

The name of the file to save as a string. The file’s name must include the file extension. If the file already exists, it will be overwritten. If the file doesn’t exist, a new file will be created.

buffer

Optional. A bytearray to write into the file.

# Overwrite an existing txt file and display the new text
brain.sdcard.savefile("Text.txt", bytearray("Hello "))
text_file = brain.sdcard.loadfile("Text.txt")
brain.screen.print(text_file.decode("utf-8"))

appendfile#

appendfile appends a bytearray to an existing file, adding new data to the end of the file without altering the current contents.

Usage:
brain.sdcard.appendfile(filename, buffer)

Parameters

Description

filename

The name of the file to append as a string. The file’s name must include the file extension. If the file already exists, it will be overwritten. If the file doesn’t exist, a new file will be created.

buffer

A bytearray containing the data to append.

# Append "World" to an existing txt file then display it
brain.sdcard.appendfile('Text.txt', bytearray("World "))
text_file = brain.sdcard.loadfile("Text.txt")
brain.screen.print(text_file.decode("utf-8"))

draw_image_from_file#

draw_image_from_file displays a Bitmap Image (BMP) stored on the SD card.

.bmp files are limited to 5120 pixels and 6KB in size.

Note: The IQ (2nd gen) Brain only supports displaying .bmp files. 8-bit RLE encoding is recommended to minimize file size.

Usage
brain.screen.draw_image_from_file(filename, x, y)

Parameters

Description

filename

The filename 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.

# Draw a bmp file on the Brain's screen at coordinate 0, 0
brain.screen.draw_image_from_file('test_image.bmp', 0, 0)

Getters#

is_inserted#

is_inserted returns a Boolean indicating whether an SD card is currently inserted into the Brain.

  • True - The Brain has an SD card inserted.

  • False - The Brain does not have an SD card inserted.

Usage:
brain.sdcard.is_inserted()

Parameters

Description

This method has no parameters.

# Display a message if the SDcard is inserted
if brain.sdcard.is_inserted():
    brain.screen.print("Inserted!")

filesize#

filesize returns the size of a file in bytes as a float.

Usage:
brain.sdcard.filesize(filename)

Parameters

Description

filename

The name of the file as a string. The file’s name must include the file extension.

# Display the amount of bytes in a txt file
brain.screen.print(brain.sdcard.filesize("Text.txt"))

size#

size returns the size of a file in bytes as a float.

Usage:
brain.sdcard.size(filename)

Parameters

Description

filename

The name of the file as a string. The file’s name must include the file extension.

# Display the amount of bytes in a txt file
brain.screen.print(brain.sdcard.size("Text.txt"))

exists#

exists returns a Boolean indicating whether a file is found on the SD card.

  • True - The file is on the SD card.

  • False - The file is not on the SD card.

Usage:
brain.sdcard.exists(filename)

Parameters

Description

filename

The name of the file as a string. The file’s name must include the file extension.

# Determine if a requested file exists
if brain.sdcard.exists("Text.txt"):
    brain.screen.print("File exists")
else:
    brain.screen.print("Create file")