SD-kort#
Introduktion#
Klassen sdcard är härledd från basklassen brain och ger åtkomst till filer lagrade på ett flyttbart SD-kort som sitter i EXP Brain.
Med hjälp av SD-kortet kan din robot ladda data som bilder, läsa och skriva filer och lagra information för senare användning eller överföring till en dator.
Access#
Brain.SDcard
Notes#
EXP Brain stöder SD-kort upp till 32 GB i storlek.
SD-kortet måste formateras som FAT32.
SD-kort större än 32 GB formateras vanligtvis som exFAT, vilket inte är kompatibelt med EXP Brain.
Alla filer måste placeras i SD-kortets rotkatalog för att vara tillgängliga.
SD-kortet måste sättas i Brain innan filoperationer kan utföras.
Filnamn och sökvägar är skiftlägeskänsliga.
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);
}
Medlemsfunktioner#
Klassen sdcard innehåller följande medlemsfunktioner:
loadfile— Laddar innehållet i en fil från SD-kortet till en användardefinierad minnesbuffert.savefile— Sparar data från en användardefinierad buffert till en fil på EXP Brains SD-kort.appendfile— Lägger till data från en användardefinierad buffert i slutet av en befintlig fil på EXP Brains SD-kort.isInserted— Returnerar om ett SD-kort är isatt i Brain.size— Returnerar storleken på en fil som lagrats på EXP Brains SD-kort.exists— Returnerar om en fil med det angivna namnet finns på EXP Brains SD-kort.
.bmp- och .png-filer som lagras på SD-kortet kan visas på EXP Brain-skärmen med hjälp av drawImageFromFile, som nås via Brain.Screen.
Innan några sdcard medlemsfunktioner anropas måste en brain instans skapas, som visas nedan:
// Create the Brain
brain Brain = brain();
loadfile#
Laddar innehållet i en fil från SD-kortet till en användardefinierad minnesbuffert.
Available Functionsint32_t loadfile(
const char* name,
uint8_t* buffer,
int32_t len );
Parameter |
Typ |
Beskrivning |
|---|---|---|
|
|
Namnet på filen som ska laddas från SD-kortet. Filnamnet måste innehålla filändelsen. |
|
|
En pekare till en buffert där fildata kommer att lagras. |
|
|
Det maximala antalet byte som ska läsas från filen, vanligtvis inställt på buffertens storlek. |
Returnerar int32_t som representerar antalet byte som lästs från filen:
Ett positivt värde anger hur många byte som laddades in i bufferten.
0indikerar att ingen data lästes.
Om filen är större än
lenläses endast de förstalenbyten.Bufferten måste vara tillräckligt stor för att lagra den begärda datan.
Den här funktionen läser råa byte och lägger inte till någon nullterminator.
// 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#
Sparar data från en användardefinierad buffert till en fil på EXP Brains SD-kort.
Available Functionsint32_t savefile(
const char* name,
uint8_t* buffer,
int32_t len );
Parameter |
Typ |
Beskrivning |
|---|---|---|
|
|
Namnet på filen som ska sparas som en sträng. Filnamnet måste innehålla filändelsen. |
|
|
En pekare till en minnesbuffert som innehåller data som ska skrivas till filen. |
|
|
Antalet byte som ska skrivas från bufferten till filen. |
Returnerar int32_t som representerar antalet byte som framgångsrikt skrivits till filen:
Ett positivt värde anger hur många byte som sparades.
0indikerar att inga data skrevs.
Om filen är större än
lenläses endast de förstalenbyten.Om en fil med samma namn redan finns kommer den att skrivas över.
Den här funktionen allokerar inte minne; bufferten måste skapas och hanteras av användaren.
Funktionen skriver råa byte och lägger inte till någon nullterminator.
// 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#
Lägger till data från en användardefinierad buffert i slutet av en befintlig fil på EXP Brains SD-kort.
Available Functionsint32_t appendfile(
const char* name,
uint8_t* buffer,
int32_t len );
Parameter |
Typ |
Beskrivning |
|---|---|---|
|
|
Namnet på filen som data ska läggas till på SD-kortet. Filnamnet måste innehålla filändelsen. |
|
|
En pekare till en minnesbuffert som innehåller de data som ska läggas till i filen. |
|
|
Antalet byte som ska läggas till från bufferten till filen. |
Returnerar int32_t som representerar antalet byte som framgångsrikt lagts till i filen:
Ett positivt värde anger hur många byte som har lagts till.
0indikerar att inga data har lagts till.
Om filen är större än
lenläses endast de förstalenbyten.Filen måste redan finnas på SD-kortet; den här funktionen skapar inte en ny fil.
Den här funktionen allokerar inte minne; bufferten måste skapas och hanteras av användaren.
Funktionen lägger till råa byte och lägger inte till någon nullterminator.
// 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#
Returnerar om ett SD-kort för närvarande är isatt i EXP Brain.
Available Functionsbool isInserted();
Den här funktionen accepterar inga parametrar.
Return ValuesReturnerar ett booleskt värde som anger om ett SD-kort är isatt:
true— Ett SD-kort är isatt i EXP Brain.false— Inget SD-kort är isatt.
Den här funktionen kontrollerar endast om det finns ett SD-kort; den verifierar inte att kortet är formaterat eller läsbart.
// 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#
Returnerar storleken på en fil som lagrats på EXP Brains SD-kort.
Available Functionsint32_t size(
const char* name );
Parameter |
Typ |
Beskrivning |
|---|---|---|
|
|
Namnet på filen på SD-kortet. Filnamnet måste innehålla filändelsen. |
Returnerar int32_t som representerar filens storlek i byte:
Ett positivt värde anger filstorleken i byte.
0indikerar en tom fil eller att filen inte kunde hittas.
Den returnerade storleken kan användas för att allokera en buffert innan
loadfileanropas.
// 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#
Returnerar om en fil med det angivna namnet finns på EXP Brains SD-kort.
Available Functionsbool exists(
const char* name );
Parameter |
Typ |
Beskrivning |
|---|---|---|
|
|
Namnet på filen som ska kontrolleras på SD-kortet. Filnamnet måste innehålla filändelsen. |
Returnerar ett booleskt värde som anger om filen finns:
true— En fil med det angivna namnet finns på SD-kortet.false— Ingen fil med det angivna namnet finns.
// 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);
}