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();
// Load and display a text file
int fileSize = Brain.SDcard.size("Text.txt");
uint8_t buffer[fileSize];
Brain.SDcard.loadfile("Text.txt", buffer, sizeof(buffer));
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 new file and display its message
char message[] = "Hello";
Brain.SDcard.savefile("Text.txt", (uint8_t*) message, strlen(message));
int fileSize = Brain.SDcard.size("Text.txt");
char buffer[fileSize];
int length = Brain.SDcard.loadfile("Text.txt", (uint8_t*) buffer, sizeof(buffer));
buffer[length] = '\0';
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.
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();
char message[] = "Hello ";
Brain.SDcard.savefile("Text.txt", (uint8_t*) message, strlen(message));
// Append "IQ" to Text.txt and display it
char append_message[] = "IQ";
Brain.SDcard.appendfile("Text.txt", (uint8_t*) append_message, strlen(append_message));
int fileSize = Brain.SDcard.size("Text.txt");
char buffer[fileSize];
int length = Brain.SDcard.loadfile("Text.txt", (uint8_t*) buffer, sizeof(buffer));
buffer[length] = '\0';
Brain.Screen.print("%s", buffer);
}
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 |
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. |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Check if the SD card is inserted
if (Brain.SDcard.isInserted()) {
Brain.Screen.print("Inserted!");
}
}
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. |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Get filesize and then create the buffer to read file.
int fileSize = Brain.SDcard.size("Text.txt");
char buffer[fileSize];
int length = Brain.SDcard.loadfile("Text.txt", (uint8_t*) buffer, sizeof(buffer));
buffer[length] = '\0';
Brain.Screen.print("%s", buffer);
}
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. |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Check if the file exists
if (Brain.SDcard.exists("Text.txt")) {
Brain.Screen.print("File exists");
} else {
Brain.Screen.print("No file");
}
}