SD卡#
介绍#
使用 VEX V5 的 SD 卡,您可以将文件存储在插入主机 SD 卡插槽的可移动 SD 卡上,并随时访问这些文件。无论是保存传感器数据还是加载自定义图像,这些方法都能让您完全掌控文件操作。可移动 SD 卡还方便您在主机和计算机之间轻松传输数据。
V5 Brain 需要一张容量不超过 32GB 的 SD 卡,格式为 FAT32。容量大于 32GB 的 SD 卡默认使用 exFAT 格式,这与 V5 Brain 不兼容。请确保您的 SD 卡在使用前已正确格式化为 FAT32。
所有文件必须放置在SD卡的根文件夹中才能访问。
对于以下示例,构造的 Brain 包括对 SD 卡方法的访问,并且在本 API 文档中所有后续示例中引用这些方法时都将使用它。
以下是所有方法的列表:
操作 — 执行文件操作并显示 SD 卡中的图像。
loadfile— Loads a file from the SD card into a bytearray.savefile— Saves a bytearray onto the SD card.appendfile— Appends a bytearray to an existing file.draw_image_from_file— Displays a Bitmap Image from the SD card.
获取器 — 返回 SD 卡和文件信息。
is_inserted— 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.
For information on constructing a Brain to gain access to the sdcard methods, see Brain.
行动#
loadfile#
loadfile loads a file from the SD card into a bytearray.
Usage:
Brain.SDcard.loadfile(filename, buffer);
参数 |
描述 |
|---|---|
|
要加载的文件名(字符串形式)。文件名必须包含文件扩展名。 |
|
一个用于加载文件内容的现有字节数组。如果现有字节数组大于目标数组,则会覆盖其内容。如果现有字节数组小于目标数组,则该方法可能无法加载所有数据。默认情况下,如果没有提供缓冲区,则会返回一个新的字节数组。 |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Check for SD card and show a file's text
if (Brain.SDcard.isInserted()) {
// 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);
} else {
Brain.Screen.print("Insert SD card");
}
}
savefile#
savefile saves a bytearray onto the SD card.
Usage:
Brain.SDcard.savefile(filename, buffer);
参数 |
描述 |
|---|---|
|
要保存的文件名(字符串类型)。文件名必须包含文件扩展名。如果文件已存在,则会覆盖它;如果文件不存在,则会创建一个新文件。 |
|
要写入文件的字节数组。 |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Check for SD card and show a file's text
if (Brain.SDcard.isInserted()) {
// 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);
} else {
Brain.Screen.print("Insert SD card");
}
}
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);
参数 |
描述 |
|---|---|
|
要追加的文件名(字符串形式)。文件名必须包含文件扩展名。 |
|
包含要追加数据的字节数组。 |
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 V5 Brain only supports displaying .bmp files. 8-bit RLE encoding is recommended to minimize file size.
Usage
Brain.Screen.drawImageFromFile(filename, x, y);
参数 |
描述 |
|---|---|
|
图片的文件名。 |
|
屏幕上图像左上角的 x 坐标。 |
|
屏幕上图像左上角的 y 坐标。 |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Draw a bmp file on the Brain's screen at coordinate 0, 0
Brain.Screen.drawImageFromFile("test_image.bmp", 0, 0);
}
获取器#
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()
参数 |
描述 |
|---|---|
此方法没有参数。 |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Check for SD card and show a file's text
if (Brain.SDcard.isInserted()) {
// 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);
} else {
Brain.Screen.print("Insert SD card");
}
}
size#
size returns the size of a file in bytes.
Usage:
Brain.SDcard.size(filename);
参数 |
描述 |
|---|---|
|
文件名(字符串形式)。文件名必须包含文件扩展名。 |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Check for SD card and show a file's text
if (Brain.SDcard.isInserted()) {
// 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);
} else {
Brain.Screen.print("Insert SD card");
}
}
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);
参数 |
描述 |
|---|---|
|
文件名(字符串形式)。文件名必须包含文件扩展名。 |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Add more text and display file size
if (Brain.SDcard.exists("Text.txt")) {
// 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);
}
}