SD卡#
介绍#
sdcard 类派生自 brain 基类,并提供对插入 V5 Brain 的可移动 SD 卡上存储的文件的访问。
使用 SD 卡,您的机器人可以加载图像等数据、读取和写入文件,并将信息存储起来以供以后使用或传输到计算机。
Access#
Brain.SDcard
Notes#
V5 主板支持最大 32GB 的 SD 卡。
SD卡必须格式化为FAT32格式。
大于 32GB 的 SD 卡通常格式化为 exFAT,这与 V5 Brain 不兼容。
所有文件必须放置在 SD 卡的根目录中才能访问。
必须先将 SD 卡插入 Brain,才能执行文件操作。
文件名和路径区分大小写。
Example#
/* 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);
}
成员功能#
sdcard 类包含以下成员函数:
loadfile— 将 SD 卡中的文件内容加载到用户提供的内存缓冲区中。savefile— 将用户提供的缓冲区中的数据保存到 V5 大脑的 SD 卡上的文件中。appendfile— 将用户提供的缓冲区中的数据追加到 V5 Brain 的 SD 卡上现有文件的末尾。isInserted— 返回 SD 卡是否已插入 Brain。size— 返回存储在 V5 Brain 的 SD 卡上的文件的大小。exists— 返回 V5 Brain 的 SD 卡上是否存在指定名称的文件。
存储在 SD 卡上的 .bmp 和 .png 文件可以使用 drawImageFromFile 显示在 V5 Brain 屏幕上,该屏幕可通过 Brain.Screen 访问。
在调用任何 sdcard 成员函数之前,必须创建一个 brain 实例,如下所示:
// Create the V5 Brain
brain Brain = brain();
loadfile#
将 SD 卡中的文件内容加载到用户提供的内存缓冲区中。
Available Functionsint32_t loadfile(
const char* name,
uint8_t* buffer,
int32_t len );
范围 |
类型 |
描述 |
|---|---|---|
|
|
要从SD卡加载的文件名。文件名必须包含文件扩展名。 |
|
|
指向用于存储文件数据的缓冲区的指针。 |
|
|
要从文件中读取的最大字节数,通常设置为缓冲区的大小。 |
返回一个 int32_t ,表示已成功从文件中读取的字节数:
正值表示已将多少字节加载到缓冲区中。
0表示未读取任何数据。
如果文件大于
len,则只读取前len字节。缓冲区必须足够大,才能容纳请求的数据。
此函数读取原始字节,不附加空终止符。
// 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#
将用户提供的缓冲区中的数据保存到 V5 大脑的 SD 卡上的文件中。
Available Functionsint32_t savefile(
const char* name,
uint8_t* buffer,
int32_t len );
范围 |
类型 |
描述 |
|---|---|---|
|
|
要保存的文件名(字符串格式)。文件名必须包含文件扩展名。 |
|
|
指向包含要写入文件的数据的内存缓冲区的指针。 |
|
|
要从缓冲区写入文件的字节数。 |
返回 int32_t ,表示成功写入文件的字节数:
正值表示节省了多少字节。
0表示没有写入任何数据。
如果文件大于
len,则只读取前len字节。如果同名文件已存在,则会被覆盖。
该函数不分配内存;缓冲区必须由用户创建和管理。
该函数写入原始字节,不附加空终止符。
// 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#
将用户提供的缓冲区中的数据追加到 V5 Brain SD 卡上现有文件的末尾。
Available Functionsint32_t appendfile(
const char* name,
uint8_t* buffer,
int32_t len );
范围 |
类型 |
描述 |
|---|---|---|
|
|
要将数据追加到 SD 卡上的文件名。文件名必须包含文件扩展名。 |
|
|
指向包含要追加到文件的数据的内存缓冲区的指针。 |
|
|
要将缓冲区中的数据追加到文件的字节数。 |
返回一个 int32_t ,表示成功追加到文件的字节数:
正值表示追加的字节数。
0表示没有追加任何数据。
如果文件大于
len,则只读取前len字节。文件必须已存在于 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);
isInserted#
返回V5主机中当前是否插入了SD卡。
Available Functionsbool isInserted();
此函数不接受任何参数。
Return Values返回一个布尔值,指示是否已插入 SD 卡:
true— SD 卡已插入 V5 主控芯片。false— 未插入 SD 卡。
此功能仅检查 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");
}
size#
返回存储在 V5 大脑 SD 卡上的文件的大小。
Available Functionsint32_t size(
const char* name );
范围 |
类型 |
描述 |
|---|---|---|
|
|
SD卡上的文件名。文件名必须包含文件扩展名。 |
返回一个 int32_t 类型的值,表示文件大小(以字节为单位):
正值表示文件大小(以字节为单位)。
0表示文件为空或找不到该文件。
返回的大小可用于在调用
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");
}
exists#
返回 V5 Brain 的 SD 卡上是否存在具有指定名称的文件。
Available Functionsbool exists(
const char* name );
范围 |
类型 |
描述 |
|---|---|---|
|
|
要检查的SD卡上的文件名。文件名必须包含文件扩展名。 |
返回一个布尔值,指示文件是否存在:
true— SD 卡上存在指定名称的文件。false— 没有具有指定名称的文件。
// 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);
}