SD卡#
介绍#
The sdcard class is derived from the brain base class and provides access to files stored on a removable SD card inserted into the V5 Brain.
使用 SD 卡,您的机器人可以加载图像等数据、读取和写入文件,并将信息存储起来以供以后使用或传输到计算机。
使用权#
Brain.SDcard
笔记#
V5 主板支持最大 32GB 的 SD 卡。
SD卡必须格式化为FAT32格式。
大于 32GB 的 SD 卡通常格式化为 exFAT,这与 V5 Brain 不兼容。
所有文件必须放置在 SD 卡的根目录中才能访问。
必须先将 SD 卡插入 Brain,才能执行文件操作。
文件名和路径区分大小写。
例子#
/* This constructor is required when using VS Code.
A Brain is generated automatically at the start of
VEXcode projects. */
// Create the V5 Brain
brain Brain = brain();
// 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);
}
成员功能#
The sdcard class includes the following member functions:
loadfile— Loads the contents of a file from the SD card into a user-provided memory buffer.savefile— Saves data from a user-provided buffer to a file on the V5 Brain’s SD card.appendfile— Appends data from a user-provided buffer to the end of an existing file on the V5 Brain’s SD card.isInserted— Returns a Boolean indicating whether an SD card is inserted into the Brain.size— Returns the size, in bytes, of a file stored on the V5 Brain’s SD card.exists— Returns a Boolean indicating whether a file with the specified name exists on the V5 Brain’s SD card.
.bmp and .png files stored on the SD card can be displayed on the V5 Brain screen using drawImageFromFile, which is accessed through Brain.Screen.
Before calling any sdcard member functions, a brain instance must be created, as shown below:
// Create the V5 Brain
brain Brain = brain();
加载文件#
将 SD 卡中的文件内容加载到用户提供的内存缓冲区中。
Available Functionsint32_t loadfile(
const char* name,
uint8_t* buffer,
int32_t len );
范围 |
类型 |
描述 |
|---|---|---|
|
|
要从SD卡加载的文件名。文件名必须包含文件扩展名。 |
|
|
指向用于存储文件数据的缓冲区的指针。 |
|
|
要从文件中读取的最大字节数,通常设置为缓冲区的大小。 |
返回从文件中成功读取的字节数:
正值表示已将多少字节加载到缓冲区中。
0indicates that no data was read.
If the file is larger than
len, only the firstlenbytes are read.缓冲区必须足够大,才能容纳请求的数据。
此函数读取原始字节,不附加空终止符。
// 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");
}
保存文件#
将用户提供的缓冲区中的数据保存到 V5 大脑的 SD 卡上的文件中。
Available Functionsint32_t savefile(
const char* name,
uint8_t* buffer,
int32_t len );
范围 |
类型 |
描述 |
|---|---|---|
|
|
要保存的文件名(字符串格式)。文件名必须包含文件扩展名。 |
|
|
指向包含要写入文件的数据的内存缓冲区的指针。 |
|
|
要从缓冲区写入文件的字节数。 |
返回成功写入文件的字节数:
正值表示节省了多少字节。
0indicates that no data was written.
If the file is larger than
len, only the firstlenbytes are read.如果同名文件已存在,则会被覆盖。
该函数不分配内存;缓冲区必须由用户创建和管理。
该函数写入原始字节,不附加空终止符。
// 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");
}
追加文件#
将用户提供的缓冲区中的数据追加到 V5 Brain SD 卡上现有文件的末尾。
Available Functionsint32_t appendfile(
const char* name,
uint8_t* buffer,
int32_t len );
范围 |
类型 |
描述 |
|---|---|---|
|
|
要将数据追加到 SD 卡上的文件名。文件名必须包含文件扩展名。 |
|
|
指向包含要追加到文件的数据的内存缓冲区的指针。 |
|
|
要将缓冲区中的数据追加到文件的字节数。 |
返回成功追加到文件中的字节数:
正值表示追加的字节数。
0indicates that no data was appended.
If the file is larger than
len, only the firstlenbytes are read.文件必须已存在于 SD 卡上;此功能不会创建新文件。
该函数不分配内存;缓冲区必须由用户创建和管理。
该函数会追加原始字节,而不会追加空终止符。
// 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);
已插入#
返回V5主机中当前是否插入了SD卡。
Available Functionsbool isInserted();
此函数不接受任何参数。
Return Values返回一个布尔值,指示是否已插入 SD 卡:
true— An SD card is inserted in the V5 Brain.false— No SD card is inserted.
此功能仅检查 SD 卡是否存在;它不会验证该卡是否已格式化或可读。
// 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");
}
尺寸#
返回存储在 V5 Brain 的 SD 卡上的文件的大小(以字节为单位)。
Available Functionsint32_t size(
const char* name );
范围 |
类型 |
描述 |
|---|---|---|
|
|
SD卡上的文件名。文件名必须包含文件扩展名。 |
Returns an int32_t representing the size of the file in bytes:
正值表示文件大小(以字节为单位)。
0indicates an empty file or that the file could not be found.
The returned size can be used to allocate a buffer before calling
loadfile.
// 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");
}
存在#
返回 V5 Brain 的 SD 卡上是否存在具有指定名称的文件。
Available Functionsbool exists(
const char* name );
范围 |
类型 |
描述 |
|---|---|---|
|
|
要检查的SD卡上的文件名。文件名必须包含文件扩展名。 |
返回一个布尔值,指示文件是否存在:
true— A file with the specified name exists on the SD card.false— No file with the specified name exists.
// 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);
}