SD-kortti#
Johdanto#
sdcard -luokka on johdettu brain -perusluokasta ja tarjoaa pääsyn V5 Brainiin asetetulla irrotettavalla SD-kortilla tallennettuihin tiedostoihin.
SD-kortin avulla robotti voi ladata tietoja, kuten kuvia, lukea ja kirjoittaa tiedostoja sekä tallentaa tietoja myöhempää käyttöä tai tietokoneelle siirtämistä varten.
Access#
Brain.SDcard
Notes#
V5 Brain tukee jopa 32 Gt:n kokoisia SD-kortteja.
SD-kortti on alustettava FAT32-muotoon.
Yli 32 Gt:n SD-kortit alustetaan yleensä exFAT-muotoon, mikä ei ole yhteensopiva V5 Brainin kanssa.
Kaikki tiedostot on sijoitettava SD-kortin juurihakemistoon, jotta niihin pääsee käsiksi.
SD-kortti on asetettava aivoihin ennen kuin tiedostotoimintoja voidaan suorittaa.
Tiedostonimet ja polut ovat kirjainkokoriippuvaisia.
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);
}
Jäsenten toiminnot#
sdcard -luokka sisältää seuraavat jäsenfunktiot:
loadfile— Lataa tiedoston sisällön SD-kortilta käyttäjän määrittämään muistipuskuriin.savefile— Tallentaa tiedot käyttäjän antamasta puskurista tiedostoon V5 Brainin SD-kortille.appendfile— Lisää tietoja käyttäjän antamasta puskurista V5 Brainin SD-kortilla olevan tiedoston loppuun.isInserted— Palauttaa, onko SD-kortti asetettu aivoihin.size— Palauttaa V5 Brainin SD-kortille tallennetun tiedoston koon.exists— Palauttaa, onko V5 Brainin SD-kortilla määritetyn nimistä tiedostoa.
SD-kortille tallennettuja .bmp- ja .png-tiedostoja voidaan näyttää V5 Brain -näytössä käyttämällä drawImageFromFile-komentoa, johon päästään Brain.Screen-komennon kautta.
Ennen minkään sdcard -jäsenfunktion kutsumista on luotava brain -instanssi alla olevan mukaisesti:
// Create the V5 Brain
brain Brain = brain();
loadfile#
Lataa tiedoston sisällön SD-kortilta käyttäjän määrittämään muistipuskuriin.
Available Functionsint32_t loadfile(
const char* name,
uint8_t* buffer,
int32_t len );
Parametri |
Tyyppi |
Kuvaus |
|---|---|---|
|
|
SD-kortilta ladattavan tiedoston nimi. Tiedostonimen on sisällettävä tiedostopääte. |
|
|
Osoitin puskuriin, johon tiedostotiedot tallennetaan. |
|
|
Tiedostosta luettavien tavujen enimmäismäärä, joka asetetaan tyypillisesti puskurin kooksi. |
Palauttaa arvon int32_t joka edustaa tiedostosta onnistuneesti luettujen tavujen määrää:
Positiivinen arvo osoittaa, kuinka monta tavua puskuriin ladattiin.
0tarkoittaa, että tietoja ei luettu.
Jos tiedosto on suurempi kuin
len, vain ensimmäisetlentavua luetaan.Puskurin on oltava riittävän suuri pyydetyn datan säilyttämiseen.
Tämä funktio lukee raakatavuja eikä lisää niihin null-terminaattoria.
// 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#
Tallentaa tiedot käyttäjän antamasta puskurista tiedostoon V5 Brainin SD-kortille.
Available Functionsint32_t savefile(
const char* name,
uint8_t* buffer,
int32_t len );
Parametri |
Tyyppi |
Kuvaus |
|---|---|---|
|
|
Merkkijonona tallennettavan tiedoston nimi. Tiedostonimen on sisällettävä tiedostopääte. |
|
|
Osoitin muistipuskuriin, joka sisältää tiedostoon kirjoitettavat tiedot. |
|
|
Puskurimuistista tiedostoon kirjoitettavien tavujen määrä. |
Palauttaa arvon int32_t joka edustaa tiedostoon onnistuneesti kirjoitettujen tavujen määrää:
Positiivinen arvo osoittaa tallennettujen tavujen määrän.
0osoittaa, että tietoja ei ole kirjoitettu.
Jos tiedosto on suurempi kuin
len, vain ensimmäisetlentavua luetaan.Jos samanniminen tiedosto on jo olemassa, se korvataan.
Tämä funktio ei varaa muistia; käyttäjän on luotava ja hallittava puskuria.
Funktio kirjoittaa raakatavuja eikä lisää null-terminaattoria.
// 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#
Lisää tietoja käyttäjän antamasta puskurista V5 Brainin SD-kortilla olevan tiedoston loppuun.
Available Functionsint32_t appendfile(
const char* name,
uint8_t* buffer,
int32_t len );
Parametri |
Tyyppi |
Kuvaus |
|---|---|---|
|
|
SD-kortilla olevan tiedoston nimi, johon tiedot liitetään. Tiedostonimen on sisällettävä tiedostopääte. |
|
|
Osoitin muistipuskuriin, joka sisältää tiedostoon liitettävät tiedot. |
|
|
Puskurista tiedostoon lisättävien tavujen määrä. |
Palauttaa arvon int32_t joka edustaa tiedostoon onnistuneesti liitettyjen tavujen määrää:
Positiivinen arvo osoittaa, kuinka monta tavua on lisätty.
0osoittaa, että tietoja ei ole liitetty.
Jos tiedosto on suurempi kuin
len, vain ensimmäisetlentavua luetaan.Tiedoston on oltava jo SD-kortilla; tämä toiminto ei luo uutta tiedostoa.
Tämä funktio ei varaa muistia; käyttäjän on luotava ja hallittava puskuria.
Funktio lisää raakatavuja eikä lisää null-terminaattoria.
// 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#
Palauttaa, onko V5 Brainissa tällä hetkellä SD-kortti.
Available Functionsbool isInserted();
Tämä funktio ei hyväksy parametreja.
Return ValuesPalauttaa totuusarvon, joka ilmaisee, onko SD-kortti asetettu paikalleen:
true— SD-kortti on asetettu V5 Brainiin.false— SD-korttia ei ole asetettu.
Tämä toiminto tarkistaa vain SD-kortin olemassaolon; se ei varmista, että kortti on alustettu tai luettavissa.
// 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#
Palauttaa V5 Brainin SD-kortille tallennetun tiedoston koon.
Available Functionsint32_t size(
const char* name );
Parametri |
Tyyppi |
Kuvaus |
|---|---|---|
|
|
SD-kortilla olevan tiedoston nimi. Tiedostonimen on sisällettävä tiedostopääte. |
Palauttaa arvon int32_t joka edustaa tiedoston kokoa tavuina:
Positiivinen arvo ilmaisee tiedoston koon tavuina.
0tarkoittaa, että tiedosto on tyhjä tai että tiedostoa ei löytynyt.
Palautettua kokoa voidaan käyttää puskurin varaamiseen ennen
loadfile-funktion kutsumista.
// 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#
Palauttaa, onko V5 Brainin SD-kortilla määritetyn nimistä tiedostoa.
Available Functionsbool exists(
const char* name );
Parametri |
Tyyppi |
Kuvaus |
|---|---|---|
|
|
SD-kortilla tarkistettavan tiedoston nimi. Tiedostonimen on sisällettävä tiedostopääte. |
Palauttaa totuusarvon, joka ilmaisee, onko tiedostoa olemassa:
true— Määritetyn niminen tiedosto on jo SD-kortilla.false— Määritettyä nimeä käyttävää tiedostoa ei ole olemassa.
// 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);
}