Κάρτα 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 EXP Brain.
Χρησιμοποιώντας την κάρτα SD, το ρομπότ σας μπορεί να φορτώσει δεδομένα όπως εικόνες, να διαβάσει και να γράψει αρχεία και να αποθηκεύσει πληροφορίες για μελλοντική χρήση ή μεταφορά σε υπολογιστή.
Access#
Brain.SDcard
Notes#
Το EXP Brain υποστηρίζει κάρτες SD μεγέθους έως 32GB.
Η κάρτα SD πρέπει να έχει μορφοποίηση FAT32.
Οι κάρτες SD μεγαλύτερες από 32GB συνήθως έχουν μορφοποίηση exFAT, η οποία δεν είναι συμβατή με την κάρτα γραφικών EXP 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 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);
}
Συναρτήσεις Μελών#
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 EXP Brain’s SD card.appendfile— Appends data from a user-provided buffer to the end of an existing file on the EXP 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 EXP Brain’s SD card.exists— Returns whether a file with the specified name exists on the EXP Brain’s SD card.
.bmp and .png files stored on the SD card can be displayed on the EXP 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 Brain
brain Brain = brain();
loadfile#
Φορτώνει τα περιεχόμενα ενός αρχείου από την κάρτα SD σε μια προσωρινή μνήμη που παρέχεται από τον χρήστη.
Available Functionsint32_t loadfile(
const char* name,
uint8_t* buffer,
int32_t len );
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
Το όνομα του αρχείου που θα φορτωθεί από την κάρτα SD. Το όνομα του αρχείου πρέπει να περιλαμβάνει την επέκταση αρχείου. |
|
|
Ένας δείκτης σε ένα buffer όπου θα αποθηκευτούν τα δεδομένα του αρχείου. |
|
|
Ο μέγιστος αριθμός byte για ανάγνωση από το αρχείο, συνήθως ορίζεται στο μέγεθος του buffer. |
Returns an int32_t representing the number of bytes successfully read from the file:
Μια θετική τιμή υποδεικνύει πόσα byte φορτώθηκαν στο buffer.
0indicates that no data was read.
If the file is larger than
len, only the firstlenbytes are read.Το buffer πρέπει να είναι αρκετά μεγάλο για να χωρέσει τα ζητούμενα δεδομένα.
Αυτή η συνάρτηση διαβάζει ακατέργαστα byte και δεν προσθέτει έναν μηδενικό τερματιστή.
// 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 του EXP Brain.
Available Functionsint32_t savefile(
const char* name,
uint8_t* buffer,
int32_t len );
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
Το όνομα του αρχείου που θα αποθηκευτεί ως συμβολοσειρά. Το όνομα του αρχείου πρέπει να περιλαμβάνει την επέκταση αρχείου. |
|
|
Ένας δείκτης σε μια προσωρινή μνήμη (buffer) που περιέχει τα δεδομένα που θα εγγραφούν στο αρχείο. |
|
|
Ο αριθμός των byte που θα εγγραφούν από την buffer στο αρχείο. |
Returns an int32_t representing the number of bytes successfully written to the file:
Μια θετική τιμή υποδεικνύει πόσα byte αποθηκεύτηκαν.
0indicates that no data was written.
If the file is larger than
len, only the firstlenbytes are read.Εάν υπάρχει ήδη ένα αρχείο με το ίδιο όνομα, θα αντικατασταθεί.
Αυτή η συνάρτηση δεν εκχωρεί μνήμη. Η προσωρινή μνήμη (buffer) πρέπει να δημιουργηθεί και να διαχειριστεί ο χρήστης.
Η συνάρτηση γράφει ακατέργαστα bytes και δεν προσθέτει έναν null τερματιστή.
// 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 του EXP Brain.
Available Functionsint32_t appendfile(
const char* name,
uint8_t* buffer,
int32_t len );
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
Το όνομα του αρχείου στο οποίο θα προστεθούν δεδομένα στην κάρτα SD. Το όνομα του αρχείου πρέπει να περιλαμβάνει την επέκταση αρχείου. |
|
|
Ένας δείκτης σε μια προσωρινή μνήμη που περιέχει τα δεδομένα που θα προστεθούν στο αρχείο. |
|
|
Ο αριθμός των byte που θα προστεθούν από την buffer στο αρχείο. |
Returns an int32_t representing the number of bytes successfully appended to the file:
Μια θετική τιμή υποδεικνύει πόσα byte προστέθηκαν.
0indicates that no data was appended.
If the file is larger than
len, only the firstlenbytes are read.Το αρχείο πρέπει να υπάρχει ήδη στην κάρτα SD. Αυτή η λειτουργία δεν δημιουργεί νέο αρχείο.
Αυτή η συνάρτηση δεν εκχωρεί μνήμη. Η προσωρινή μνήμη (buffer) πρέπει να δημιουργηθεί και να διαχειριστεί ο χρήστης.
Η συνάρτηση προσαρτά ακατέργαστα bytes και δεν προσαρτά έναν τερματιστή null.
// 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 έχει εισαχθεί αυτήν τη στιγμή στον εγκέφαλο EXP.
Available Functionsbool isInserted();
Αυτή η συνάρτηση δεν δέχεται καμία παράμετρο.
Return ValuesΕπιστρέφει μια λογική τιμή που υποδεικνύει εάν έχει εισαχθεί κάρτα SD:
true— An SD card is inserted in the EXP Brain.false— No SD card is inserted.
Αυτή η λειτουργία ελέγχει μόνο την παρουσία κάρτας 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#
Επιστρέφει το μέγεθος ενός αρχείου που είναι αποθηκευμένο στην κάρτα SD του EXP Brain.
Available Functionsint32_t size(
const char* name );
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
Το όνομα του αρχείου στην κάρτα SD. Το όνομα του αρχείου πρέπει να περιλαμβάνει την επέκταση αρχείου. |
Returns an int32_t representing the size of the file in bytes:
Μια θετική τιμή υποδεικνύει το μέγεθος του αρχείου σε byte.
0indicates an empty file or that the file could not be found.
The returned size can be used to allocate a buffer before calling
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#
Επιστρέφει εάν υπάρχει ένα αρχείο με το καθορισμένο όνομα στην κάρτα SD του EXP Brain.
Available Functionsbool exists(
const char* name );
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
Το όνομα του αρχείου που θα ελεγχθεί στην κάρτα SD. Το όνομα του αρχείου πρέπει να περιλαμβάνει την επέκταση αρχείου. |
Επιστρέφει μια λογική τιμή που υποδεικνύει εάν το αρχείο υπάρχει:
true— A file with the specified name exists on the SD card.false— No file with the specified name exists.
// 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);
}