SD卡#

介绍#

VEX IQ(第二代)的 SD 卡 API 允许您将可移动 SD 卡插入 Brain 的 SD 卡槽,从而存储和访问其中的文件。无论您是保存传感器数据还是加载自定义图像,这些方法都能让您完全控制文件操作。可移动 SD 卡让您可以轻松地在 Brain 和计算机之间传输数据。

IQ(第二代)Brain 需要一张容量不超过 32GB 且格式为 FAT32 的 SD 卡。大于 32GB 的 SD 卡默认使用 exFAT 格式,这与 VEX IQ(第二代)Brain 不兼容。使用前请确保您的 SD 卡已正确格式化为 FAT32 格式。

所有文件必须放在 SD 卡的根文件夹中才可访问。

对于以下示例,构造的 Brain 包括对 SD 卡方法的访问,并且在本 API 文档的所有后续示例中引用这些方法时都将使用它。

以下是所有方法的列表:

行动

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

吸气剂

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

行动#

loadfile#

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

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

参数

描述

姓名

要加载的文件的名称(以字符串形式)。文件名必须包含文件扩展名。

缓冲

指向预分配内存缓冲区的指针,文件内容将加载到该缓冲区。此缓冲区必须足够大,以容纳预期的数据。如果文件大于缓冲区,则仅加载适合的部分。

len

读入缓冲区的最大字节数。如果文件大于此大小,则仅加载前 length 个字节。如果小于此大小,则缓冲区将被部分填充。

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);

参数

描述

姓名

要保存为字符串的文件名称。文件名必须包含文件扩展名。如果文件已存在,则会覆盖该文件。如果文件不存在,则会创建一个新文件。

缓冲

指向要写入的数据的指针。

len

从缓冲区写入文件的字节数。即使缓冲区更大,也只会保存这么多的字节。

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);

参数

描述

姓名

要附加的文件名(以字符串形式)。文件名必须包含文件扩展名。如果文件已存在,则会覆盖该文件。如果文件不存在,则会创建一个新文件。

缓冲

指向要附加的数据的指针。

len

要从缓冲区追加的字节数。这控制要写入的缓冲区大小。

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);

参数

描述

姓名

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

x

屏幕上图像左上角的 x 坐标。

y

屏幕上图像左上角的 y 坐标。

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);
}

盖特#

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

参数

描述

姓名

文件名(字符串形式)。文件名必须包含文件扩展名。

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)

参数

描述

姓名

文件名(字符串形式)。文件名必须包含文件扩展名。

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");
  }
}