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

  • 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.

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

Getters

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

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

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

Action#

loadfile#

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

Usage:
Brain.SDcard.loadfile(name, buffer, len);

Parameters

Description

name

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

buffer

A pointer to a pre-allocated memory buffer where the file’s contents will be loaded. This buffer must be large enough to hold the expected data. If the file is larger than the buffer, only the portion that fits will be loaded.

len

The maximum number of bytes to read into the buffer. If the file is larger than this size, only the first length bytes will be loaded. If smaller, the buffer will be partially filled.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Get the size of "Text.txt" on the SD card
  int fileSize = Brain.SDcard.size("Text.txt");

  // Make a buffer the same size as the file
  uint8_t buffer[fileSize];

  // Load the file into the buffer
  Brain.SDcard.loadfile("Text.txt", buffer, sizeof(buffer));

  // Display the file contents on the screen
  Brain.Screen.print("%s", buffer);
}

savefile#

savefile saves a bytearray into the SD card.

Usage
Brain.SDcard.savefile(name, buffer, len);

Parameters

Description

name

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

A pointer to the data you want to write.

len

The number of bytes to write from the buffer into the file. Only this many bytes will be saved, even if the buffer is larger.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Create a message to save to the SD card
  uint8_t message[] = "Hello from VEX!";

  // Save the message into "Text.txt"
  Brain.SDcard.savefile("Text.txt", message, sizeof(message));

  // Get the size of the file
  int fileSize = Brain.SDcard.size("Text.txt");

  // Make a buffer to hold the file data
  uint8_t buffer[fileSize];

  // Load the file from the SD card into the buffer
  Brain.SDcard.loadfile("Text.txt", buffer, sizeof(buffer));

  // Display the file contents on the screen
  Brain.Screen.print("%s", buffer);
}

appendfile#

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

Note: When appending text stored as a string (for example, “Hello”), use sizeof(text) - 1 to avoid saving the string’s null terminator (‘\0’). Including the terminator can cause later text to appear cut off when displaying the file contents.

Usage:
Brain.SDcard.appendfile(name, buffer, len);

Parameters

Description

name

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 pointer to the data you want to append.

len

The number of bytes to append from the buffer. This controls how much of the buffer gets written.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Create a message to save to a new file
  uint8_t message[] = "Hello";

  // Save only the characters, not the null terminator
  Brain.SDcard.savefile("Message.txt", message, sizeof(message) - 1);

  // Load the file
  int fileSize = Brain.SDcard.size("Message.txt");
  uint8_t buffer[fileSize + 1];
  Brain.SDcard.loadfile("Message.txt", buffer, fileSize);

  // Add null terminator so it can be printed safely
  buffer[fileSize] = 0;
  Brain.Screen.print("%s\n", buffer);

  // Create more text to append to the same file
  uint8_t moreText[] = " World!";

  // Append only the characters, not the null terminator
  Brain.SDcard.appendfile("Message.txt", moreText, sizeof(moreText) - 1);

  // Load the updated file
  fileSize = Brain.SDcard.size("Message.txt");
  uint8_t buffer2[fileSize + 1];
  Brain.SDcard.loadfile("Message.txt", buffer2, fileSize);

  // Add null terminator
  buffer2[fileSize] = 0;
  Brain.Screen.newLine();
  Brain.Screen.print("%s", buffer2);
}

drawImageFromFile#

drawImageFromFile 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.drawImageFromFile(name, x, y);

Parameters

Description

name

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

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.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Draw BMP image located on the SD card at (0, 0)
  Brain.Screen.drawImageFromFile("test_image.bmp", 0, 0);
}

Getter#

isInserted#

isInserted 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.isInserted()

Parameters

Description

This method has no parameters.

size#

size returns the size of a file in bytes as an integer.

Usage:
Brain.SDcard.size(name)

Parameters

Description

name

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

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(name)

Parameters

Description

name

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