SD카드#
소개#
sdcard 클래스는 brain 기본 클래스에서 파생되었으며 V5 Brain에 삽입된 이동식 SD 카드에 저장된 파일에 대한 액세스를 제공합니다.
SD 카드를 사용하면 로봇이 이미지와 같은 데이터를 로드하고, 파일을 읽고 쓰고, 나중에 사용하거나 컴퓨터로 전송할 정보를 저장할 수 있습니다.
Access#
Brain.SDcard
Notes#
V5 브레인은 최대 32GB 용량의 SD 카드를 지원합니다.
SD 카드는 FAT32 형식으로 포맷해야 합니다.
32GB보다 큰 SD 카드는 일반적으로 exFAT 형식으로 포맷되어 있는데, 이는 V5 Brain과 호환되지 않습니다.
모든 파일은 접근 가능하려면 SD 카드의 루트 디렉토리에 있어야 합니다.
파일 작업을 수행하기 전에 SD 카드를 브레인에 삽입해야 합니다.
파일 이름과 경로는 대소문자를 구분합니다.
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 카드가 브레인에 삽입되었는지 여부를 반환합니다.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 Brain의 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 ValuesSD 카드가 삽입되었는지 여부를 나타내는 부울 값을 반환합니다.
true— V5 브레인에 SD 카드가 삽입되었습니다.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 Brain의 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);
}