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 Functions
int32_t loadfile(
  const char* name,
  uint8_t* buffer,
  int32_t len );

Parameters

Parametri

Tyyppi

Kuvaus

name

const char*

SD-kortilta ladattavan tiedoston nimi. Tiedostonimen on sisällettävä tiedostopääte.

buffer

uint8_t*

Osoitin puskuriin, johon tiedostotiedot tallennetaan.

len

int32_t

Tiedostosta luettavien tavujen enimmäismäärä, joka asetetaan tyypillisesti puskurin kooksi.

Return Values

Palauttaa arvon int32_t joka edustaa tiedostosta onnistuneesti luettujen tavujen määrää:

  • Positiivinen arvo osoittaa, kuinka monta tavua puskuriin ladattiin.

  • 0 tarkoittaa, että tietoja ei luettu.

Notes
  • Jos tiedosto on suurempi kuin len, vain ensimmäiset len tavua luetaan.

  • Puskurin on oltava riittävän suuri pyydetyn datan säilyttämiseen.

  • Tämä funktio lukee raakatavuja eikä lisää niihin null-terminaattoria.

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#

Tallentaa tiedot käyttäjän antamasta puskurista tiedostoon V5 Brainin SD-kortille.

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

Parameters

Parametri

Tyyppi

Kuvaus

name

const char*

Merkkijonona tallennettavan tiedoston nimi. Tiedostonimen on sisällettävä tiedostopääte.

buffer

uint8_t*

Osoitin muistipuskuriin, joka sisältää tiedostoon kirjoitettavat tiedot.

len

int32_t

Puskurimuistista tiedostoon kirjoitettavien tavujen määrä.

Return Values

Palauttaa arvon int32_t joka edustaa tiedostoon onnistuneesti kirjoitettujen tavujen määrää:

  • Positiivinen arvo osoittaa tallennettujen tavujen määrän.

  • 0 osoittaa, että tietoja ei ole kirjoitettu.

Notes
  • Jos tiedosto on suurempi kuin len, vain ensimmäiset len tavua 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.

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#

Lisää tietoja käyttäjän antamasta puskurista V5 Brainin SD-kortilla olevan tiedoston loppuun.

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

Parameters

Parametri

Tyyppi

Kuvaus

name

const char*

SD-kortilla olevan tiedoston nimi, johon tiedot liitetään. Tiedostonimen on sisällettävä tiedostopääte.

buffer

uint8_t*

Osoitin muistipuskuriin, joka sisältää tiedostoon liitettävät tiedot.

len

int32_t

Puskurista tiedostoon lisättävien tavujen määrä.

Return Values

Palauttaa arvon int32_t joka edustaa tiedostoon onnistuneesti liitettyjen tavujen määrää:

  • Positiivinen arvo osoittaa, kuinka monta tavua on lisätty.

  • 0 osoittaa, että tietoja ei ole liitetty.

Notes
  • Jos tiedosto on suurempi kuin len, vain ensimmäiset len tavua 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.

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#

Palauttaa, onko V5 Brainissa tällä hetkellä SD-kortti.

Available Functions
bool isInserted();

Parameters

Tämä funktio ei hyväksy parametreja.

Return Values

Palauttaa totuusarvon, joka ilmaisee, onko SD-kortti asetettu paikalleen:

  • true — SD-kortti on asetettu V5 Brainiin.

  • false — SD-korttia ei ole asetettu.

Notes
  • Tämä toiminto tarkistaa vain SD-kortin olemassaolon; se ei varmista, että kortti on alustettu tai luettavissa.

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#

Palauttaa V5 Brainin SD-kortille tallennetun tiedoston koon.

Available Functions
int32_t size(
  const char* name );

Parameters

Parametri

Tyyppi

Kuvaus

name

const char*

SD-kortilla olevan tiedoston nimi. Tiedostonimen on sisällettävä tiedostopääte.

Return Values

Palauttaa arvon int32_t joka edustaa tiedoston kokoa tavuina:

  • Positiivinen arvo ilmaisee tiedoston koon tavuina.

  • 0 tarkoittaa, että tiedosto on tyhjä tai että tiedostoa ei löytynyt.

Notes
  • Palautettua kokoa voidaan käyttää puskurin varaamiseen ennen loadfile-funktion kutsumista.

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#

Palauttaa, onko V5 Brainin SD-kortilla määritetyn nimistä tiedostoa.

Available Functions
bool exists(
  const char* name );

Parameters

Parametri

Tyyppi

Kuvaus

name

const char*

SD-kortilla tarkistettavan tiedoston nimi. Tiedostonimen on sisällettävä tiedostopääte.

Return Values

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.

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