Κάρτα SD#

Εισαγωγή#

The sdcard class is derived from the brain base class and provides access to files stored on a removable SD card inserted into the V5 Brain.

Χρησιμοποιώντας την κάρτα SD, το ρομπότ σας μπορεί να φορτώσει δεδομένα όπως εικόνες, να διαβάσει και να γράψει αρχεία και να αποθηκεύσει πληροφορίες για μελλοντική χρήση ή μεταφορά σε υπολογιστή.

Access#

Brain.SDcard

Notes#

  • Το V5 Brain υποστηρίζει κάρτες SD μεγέθους έως 32GB.

  • Η κάρτα SD πρέπει να έχει μορφοποίηση FAT32.

    • Οι κάρτες SD μεγαλύτερες από 32GB συνήθως έχουν μορφοποίηση 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);
}

Συναρτήσεις Μελών#

The sdcard class includes the following member functions:

  • loadfile — Loads the contents of a file from the SD card into a user-provided memory buffer.

  • savefile — Saves data from a user-provided buffer to a file on the V5 Brain’s SD card.

  • appendfile — Appends data from a user-provided buffer to the end of an existing file on the V5 Brain’s SD card.

  • isInserted — Returns whether an SD card is inserted into the Brain.

  • size — Returns the size of a file stored on the V5 Brain’s SD card.

  • exists — Returns whether a file with the specified name exists on the V5 Brain’s SD card.

.bmp and .png files stored on the SD card can be displayed on the V5 Brain screen using drawImageFromFile, which is accessed through Brain.Screen.

Before calling any sdcard member functions, a brain instance must be created, as shown below:

// 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*

Ένας δείκτης σε ένα buffer όπου θα αποθηκευτούν τα δεδομένα του αρχείου.

len

int32_t

Ο μέγιστος αριθμός byte για ανάγνωση από το αρχείο, συνήθως ορίζεται στο μέγεθος του buffer.

Return Values

Returns an int32_t representing the number of bytes successfully read from the file:

  • Μια θετική τιμή υποδεικνύει πόσα byte φορτώθηκαν στο buffer.

  • 0 indicates that no data was read.

Notes
  • If the file is larger than len, only the first len bytes are read.

  • Το buffer πρέπει να είναι αρκετά μεγάλο για να χωρέσει τα ζητούμενα δεδομένα.

  • Αυτή η συνάρτηση διαβάζει ακατέργαστα byte και δεν προσθέτει έναν μηδενικό τερματιστή.

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#

Αποθηκεύει δεδομένα από ένα buffer που παρέχεται από τον χρήστη σε ένα αρχείο στην κάρτα SD του V5 Brain.

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

Parameters

Παράμετρος

Τύπος

Περιγραφή

name

const char*

Το όνομα του αρχείου που θα αποθηκευτεί ως συμβολοσειρά. Το όνομα του αρχείου πρέπει να περιλαμβάνει την επέκταση αρχείου.

buffer

uint8_t*

Ένας δείκτης σε μια προσωρινή μνήμη (buffer) που περιέχει τα δεδομένα που θα εγγραφούν στο αρχείο.

len

int32_t

Ο αριθμός των byte που θα εγγραφούν από την buffer στο αρχείο.

Return Values

Returns an int32_t representing the number of bytes successfully written to the file:

  • Μια θετική τιμή υποδεικνύει πόσα byte αποθηκεύτηκαν.

  • 0 indicates that no data was written.

Notes
  • If the file is larger than len, only the first len bytes are read.

  • Εάν υπάρχει ήδη ένα αρχείο με το ίδιο όνομα, θα αντικατασταθεί.

  • Αυτή η συνάρτηση δεν εκχωρεί μνήμη. Η προσωρινή μνήμη (buffer) πρέπει να δημιουργηθεί και να διαχειριστεί ο χρήστης.

  • Η συνάρτηση γράφει ακατέργαστα bytes και δεν προσθέτει έναν null τερματιστή.

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#

Προσθέτει δεδομένα από ένα buffer που παρέχεται από τον χρήστη στο τέλος ενός υπάρχοντος αρχείου στην κάρτα SD του V5 Brain.

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

Ο αριθμός των byte που θα προστεθούν από την buffer στο αρχείο.

Return Values

Returns an int32_t representing the number of bytes successfully appended to the file:

  • Μια θετική τιμή υποδεικνύει πόσα byte προστέθηκαν.

  • 0 indicates that no data was appended.

Notes
  • If the file is larger than len, only the first len bytes are read.

  • Το αρχείο πρέπει να υπάρχει ήδη στην κάρτα SD. Αυτή η λειτουργία δεν δημιουργεί νέο αρχείο.

  • Αυτή η συνάρτηση δεν εκχωρεί μνήμη. Η προσωρινή μνήμη (buffer) πρέπει να δημιουργηθεί και να διαχειριστεί ο χρήστης.

  • Η συνάρτηση προσαρτά ακατέργαστα bytes και δεν προσαρτά έναν τερματιστή null.

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#

Επιστρέφει εάν έχει τοποθετηθεί κάρτα SD στον εγκέφαλο V5.

Available Functions
bool isInserted();

Parameters

Αυτή η συνάρτηση δεν δέχεται καμία παράμετρο.

Return Values

Επιστρέφει μια λογική τιμή που υποδεικνύει εάν έχει εισαχθεί κάρτα SD:

  • true — An SD card is inserted in the V5 Brain.

  • false — No SD card is inserted.

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#

Επιστρέφει το μέγεθος ενός αρχείου που είναι αποθηκευμένο στην κάρτα SD του V5 Brain.

Available Functions
int32_t size(
  const char* name );

Parameters

Παράμετρος

Τύπος

Περιγραφή

name

const char*

Το όνομα του αρχείου στην κάρτα SD. Το όνομα του αρχείου πρέπει να περιλαμβάνει την επέκταση αρχείου.

Return Values

Returns an int32_t representing the size of the file in bytes:

  • Μια θετική τιμή υποδεικνύει το μέγεθος του αρχείου σε byte.

  • 0 indicates an empty file or that the file could not be found.

Notes
  • The returned size can be used to allocate a buffer before calling 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#

Επιστρέφει εάν υπάρχει ένα αρχείο με το καθορισμένο όνομα στην κάρτα SD του V5 Brain.

Available Functions
bool exists(
  const char* name );

Parameters

Παράμετρος

Τύπος

Περιγραφή

name

const char*

Το όνομα του αρχείου που θα ελεγχθεί στην κάρτα SD. Το όνομα του αρχείου πρέπει να περιλαμβάνει την επέκταση αρχείου.

Return Values

Επιστρέφει μια λογική τιμή που υποδεικνύει εάν το αρχείο υπάρχει:

  • true — A file with the specified name exists on the SD card.

  • false — No file with the specified name exists.

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