SD卡#

介紹#

sdcard 類衍生自 brain 基類,並提供對插入 EXP Brain 的可移動 SD 卡上儲存的檔案的存取。

使用 SD 卡,您的機器人可以載入影像等資料、讀取和寫入文件,以及儲存資訊以供日後使用或傳輸到電腦。

Access#

Brain.SDcard

Notes#

  • EXP Brain 支援最大 32GB 的 SD 卡。

  • SD卡必須格式化為FAT32格式。

    • 大於 32GB 的 SD 卡通常格式化為 exFAT,這與 EXP 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 EXP 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 — 將使用者提供的緩衝區中的資料儲存到 EXP Brain 的 SD 卡上的檔案中。

  • appendfile — 將使用者提供的緩衝區中的資料追加到 EXP Brain 的 SD 卡上現有檔案的末端。

  • isInserted — 傳回 SD 卡是否已插入 Brain。

  • size — 傳回儲存在 EXP Brain 的 SD 卡上的檔案的大小。

  • exists — 傳回 EXP Brain 的 SD 卡上是否存在指定名稱的檔案。

儲存在 SD 卡上的 .bmp 和 .png 檔案可以使用 drawImageFromFile 顯示在 EXP Brain 螢幕上,該畫面可透過 Brain.Screen 存取。

在呼叫任何 sdcard 成員函數之前,必須建立一個 brain 實例,如下所示:

// Create the Brain
brain Brain = brain();

loadfile#

將 SD 卡中的檔案內容載入到使用者提供的記憶體緩衝區中。

Available Functions
int32_t loadfile(
  const char* name,
  uint8_t* buffer,
  int32_t len );

Parameters

範圍

類型

描述

name

const char*

要從SD卡載入的檔案名稱。檔案名稱必須包含檔案副檔名。

buffer

uint8_t*

指向用於儲存檔案資料的緩衝區的指標。

len

int32_t

要從檔案中讀取的最大位元組數,通常會設定為緩衝區的大小。

Return Values

傳回一個 int32_t ,表示已成功從檔案讀取的位元組數:

  • 正值表示已將多少位元組載入到緩衝區中。

  • 0 表示未讀取任何資料。

Notes
  • 如果檔案大於 len,則只讀取前 len 位元組。

  • 緩衝區必須足夠大,才能容納請求的資料。

  • 此函數讀取原始字節,不附加空終止符。

Examples
// 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#

將使用者提供的緩衝區中的資料儲存到 EXP Brain 的 SD 卡上的檔案中。

Available Functions
int32_t savefile(
  const char* name,
  uint8_t* buffer,
  int32_t len );

Parameters

範圍

類型

描述

name

const char*

要儲存的檔案名稱(字串格式)。檔案名稱必須包含檔案副檔名。

buffer

uint8_t*

指向包含要寫入檔案的資料的記憶體緩衝區的指標。

len

int32_t

要從緩衝區寫入檔案的位元組數。

Return Values

傳回 int32_t ,表示成功寫入檔案的位元組數:

  • 正值表示節省了多少位元組。

  • 0 表示沒有寫入任何資料。

Notes
  • 如果檔案大於 len,則只讀取前 len 位元組。

  • 如果同名檔案已存在,則會被覆寫。

  • 此函數不分配記憶體;緩衝區必須由使用者建立和管理。

  • 此函數寫入原始字節,不附加空終止符。

Examples
// 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#

將使用者提供的緩衝區中的資料追加到 EXP Brain SD 卡上現有檔案的末端。

Available Functions
int32_t appendfile(
  const char* name,
  uint8_t* buffer,
  int32_t len );

Parameters

範圍

類型

描述

name

const char*

要將資料追加到 SD 卡上的檔案名稱。檔案名稱必須包含檔案副檔名。

buffer

uint8_t*

指向包含要追加到檔案的資料的記憶體緩衝區的指標。

len

int32_t

要將緩衝區中的資料追加到檔案的位元組數。

Return Values

傳回一個 int32_t ,表示成功追加到檔案的位元組數:

  • 正值表示追加的位元組數。

  • 0 表示沒有追加任何資料。

Notes
  • 如果檔案大於 len,則只讀取前 len 位元組。

  • 文件必須已存在於 SD 卡上;此功能不會建立新文件。

  • 此函數不分配記憶體;緩衝區必須由使用者建立和管理。

  • 此函數會追加原始字節,而不會追加空終止符。

Examples
// 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#

返回 EXP Brain 中目前是否插入了 SD 卡。

Available Functions
bool isInserted();

Parameters

此函數不接受任何參數。

Return Values

傳回布林值,指示是否已插入 SD 卡:

  • true — SD 卡已插入 EXP Brain。

  • false — 未插入 SD 卡。

Notes
  • 此功能僅檢查 SD 卡是否存在;它不會驗證該卡是否已格式化或可讀。

Examples
// 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#

傳回儲存在 EXP Brain 的 SD 卡上的檔案的大小。

Available Functions
int32_t size(
  const char* name );

Parameters

範圍

類型

描述

name

const char*

SD卡上的檔名。檔案名稱必須包含檔案副檔名。

Return Values

傳回一個 int32_t 類型的值,表示檔案大小(以位元組為單位):

  • 正值表示檔案大小(以位元組為單位)。

  • 0 表示檔案為空或找不到該檔案。

Notes
  • 傳回的大小可用於在呼叫 loadfile 之前指派緩衝區。

Examples
// 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#

傳回 EXP Brain 的 SD 卡上是否存在指定名稱的檔案。

Available Functions
bool exists(
  const char* name );

Parameters

範圍

類型

描述

name

const char*

要檢查的SD卡上的檔案名稱。檔案名稱必須包含檔案副檔名。

Return Values

傳回布林值,指示文件是否存在:

  • true — SD 卡上存在指定名稱的檔案。

  • false — 沒有具有指定名稱的檔案。

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