SDカード#

導入#

sdcard クラスは brain 基本クラスから派生しており、V5 Brain に挿入された取り外し可能な SD カードに保存されているファイルへのアクセスを提供します。

SDカードを使用することで、ロボットは画像などのデータを読み込んだり、ファイルの読み書きを行ったり、後で使用するため、あるいはコンピュータに転送するために情報を保存したりすることができます。

Access#

Brain.SDcard

Notes#

  • V5 Brainは最大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 Brainの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 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 の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#

ユーザーが指定したバッファからデータをV5 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 の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#

ユーザーが指定したバッファからデータを取り出し、V5 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 の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#

V5 BrainにSDカードが挿入されているかどうかを返します。

Available Functions
bool isInserted();

Parameters

この関数は引数を受け付けません。

Return Values

SDカードが挿入されているかどうかを示すブール値を返します。

  • true — SDカードがV5 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#

V5 BrainのSDカードに保存されているファイルのサイズを返します。

Available Functions
int32_t size(
  const char* name );

Parameters

パラメータ

タイプ

説明

name

const char*

SDカード上のファイル名。ファイル名にはファイル拡張子を含める必要があります。

Return Values

ファイルサイズ(バイト単位)を表す int32_t の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#

指定された名前のファイルがV5 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);
}