Tarjeta SD#
Introducción#
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.
Mediante la tarjeta SD, tu robot puede cargar datos como imágenes, leer y escribir archivos, y almacenar información para su uso posterior o para transferirla a un ordenador.
Acceso#
Brain.SDcard
Notas#
El V5 Brain admite tarjetas SD de hasta 32 GB.
La tarjeta SD debe estar formateada como FAT32.
Las tarjetas SD de más de 32 GB suelen estar formateadas en exFAT, que no es compatible con el V5 Brain.
Para poder acceder a los archivos, estos deben estar ubicados en el directorio raíz de la tarjeta SD.
La tarjeta SD debe insertarse en el dispositivo antes de poder realizar operaciones con archivos.
Los nombres y las rutas de archivo distinguen entre mayúsculas y minúsculas.
Ejemplo#
/* 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);
}
Funciones de los miembros#
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 a Boolean indicating whether an SD card is inserted into the Brain.size— Returns the size, in bytes, of a file stored on the V5 Brain’s SD card.exists— Returns a Boolean indicating 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();
cargar archivo#
Carga el contenido de un archivo desde la tarjeta SD en un búfer de memoria proporcionado por el usuario.
Available Functionsint32_t loadfile(
const char* name,
uint8_t* buffer,
int32_t len );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
El nombre del archivo que se cargará desde la tarjeta SD. El nombre del archivo debe incluir la extensión. |
|
|
Un puntero a un búfer donde se almacenarán los datos del archivo. |
|
|
El número máximo de bytes que se leerán del archivo, que normalmente se establece al tamaño del búfer. |
Devuelve el número de bytes leídos correctamente del archivo:
Un valor positivo indica cuántos bytes se cargaron en el búfer.
0indicates that no data was read.
If the file is larger than
len, only the firstlenbytes are read.El búfer debe ser lo suficientemente grande como para contener los datos solicitados.
Esta función lee bytes sin procesar y no agrega un terminador nulo.
// 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");
}
archivo de guardado#
Guarda los datos de un búfer proporcionado por el usuario en un archivo de la tarjeta SD del V5 Brain.
Available Functionsint32_t savefile(
const char* name,
uint8_t* buffer,
int32_t len );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
El nombre del archivo que se guardará como una cadena de texto. El nombre del archivo debe incluir la extensión. |
|
|
Un puntero a un búfer de memoria que contiene los datos que se escribirán en el archivo. |
|
|
El número de bytes que se escribirán desde el búfer al archivo. |
Devuelve el número de bytes escritos correctamente en el archivo:
Un valor positivo indica cuántos bytes se guardaron.
0indicates that no data was written.
If the file is larger than
len, only the firstlenbytes are read.Si ya existe un archivo con el mismo nombre, se sobrescribirá.
Esta función no asigna memoria; el búfer debe ser creado y administrado por el usuario.
La función escribe bytes sin procesar y no añade un terminador nulo.
// 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");
}
archivo adjunto#
Agrega datos desde un búfer proporcionado por el usuario al final de un archivo existente en la tarjeta SD del V5 Brain.
Available Functionsint32_t appendfile(
const char* name,
uint8_t* buffer,
int32_t len );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
El nombre del archivo al que se añadirán datos en la tarjeta SD. El nombre del archivo debe incluir la extensión. |
|
|
Un puntero a un búfer de memoria que contiene los datos que se añadirán al archivo. |
|
|
El número de bytes que se añadirán desde el búfer al archivo. |
Devuelve el número de bytes añadidos correctamente al archivo:
Un valor positivo indica cuántos bytes se añadieron.
0indicates that no data was appended.
If the file is larger than
len, only the firstlenbytes are read.El archivo debe existir previamente en la tarjeta SD; esta función no crea un archivo nuevo.
Esta función no asigna memoria; el búfer debe ser creado y administrado por el usuario.
La función agrega bytes sin procesar y no agrega un terminador nulo.
// 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);
está insertado#
Indica si hay una tarjeta SD insertada en el V5 Brain.
Available Functionsbool isInserted();
Esta función no acepta ningún parámetro.
Return ValuesDevuelve un valor booleano que indica si hay una tarjeta SD insertada:
true— An SD card is inserted in the V5 Brain.false— No SD card is inserted.
Esta función solo comprueba la presencia de una tarjeta SD; no verifica que la tarjeta esté formateada o sea legible.
// 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");
}
tamaño#
Devuelve el tamaño, en bytes, de un archivo almacenado en la tarjeta SD del V5 Brain.
Available Functionsint32_t size(
const char* name );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
El nombre del archivo en la tarjeta SD. El nombre del archivo debe incluir la extensión. |
Returns an int32_t representing the size of the file in bytes:
Un valor positivo indica el tamaño del archivo en bytes.
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");
}
existe#
Indica si existe un archivo con el nombre especificado en la tarjeta SD del V5 Brain.
Available Functionsbool exists(
const char* name );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
El nombre del archivo que se va a comprobar en la tarjeta SD. El nombre del archivo debe incluir la extensión. |
Devuelve un valor booleano que indica si el archivo existe:
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);
}