Tarjeta SD#

Introducción#

La API de tarjeta SD para VEX IQ (2.ª generación) permite almacenar y acceder a archivos en una tarjeta SD extraíble insertada en la ranura para tarjetas SD del Brain. Tanto si se guardan datos de sensores como si se cargan imágenes personalizadas, estos métodos ofrecen control total sobre las operaciones con los archivos. La tarjeta SD extraíble permite transferir datos fácilmente entre el Brain y el ordenador.

El IQ (2.ª generación) Brain requiere una tarjeta SD de hasta 32 GB formateada en FAT32. Las tarjetas SD de más de 32 GB utilizan el formato exFAT por defecto, que no es compatible con el VEX IQ (2.ª generación) Brain. Asegúrese de que su tarjeta SD esté formateada correctamente en FAT32 antes de usarla.

Todos los archivos deben colocarse en la carpeta raíz de la tarjeta SD para que sean accesibles.

Para los ejemplos a continuación, el cerebro construido incluye acceso a los métodos de la tarjeta SD y se utilizará en todos los ejemplos posteriores en esta documentación de API cuando se haga referencia a esos métodos.

A continuación se muestra una lista de todos los métodos:

Comportamiento

  • loadfile – Loads a file from the SD card into a bytearray.

  • savefile – Saves a bytearray into the SD card.

  • appendfile – Appends a bytearray to an existing file.

  • drawImageFromFile – Displays a Bitmap Image from the SD card.

Conseguidores

  • isInserted – Returns a Boolean indicating whether an SD card is inserted into the Brain.

  • size – Returns the size of a file in bytes.

  • exists – Returns a Boolean indicating whether a file is found on the SD card.

Acción#

loadfile#

loadfile loads a file from the SD card into a bytearray.

Usage:
Brain.SDcard.loadfile(name, buffer, len);

Parámetros

Descripción

nombre

El nombre del archivo que se cargará como cadena. El nombre del archivo debe incluir la extensión.

buffer

Un puntero a un búfer de memoria preasignado donde se cargará el contenido del archivo. Este búfer debe ser lo suficientemente grande como para contener los datos esperados. Si el archivo es más grande que el búfer, solo se cargará la parte que quepa.

lente

El número máximo de bytes que se pueden leer en el búfer. Si el archivo supera este tamaño, solo se cargarán los primeros bytes. Si es menor, el búfer se llenará parcialmente.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Load and display a text file
  int fileSize = Brain.SDcard.size("Text.txt");
  uint8_t buffer[fileSize];
  Brain.SDcard.loadfile("Text.txt", buffer, sizeof(buffer));
  Brain.Screen.print("%s", buffer);
}

savefile#

savefile saves a bytearray into the SD card.

Usage
Brain.SDcard.savefile(name, buffer, len);

Parámetros

Descripción

nombre

El nombre del archivo que se guardará como cadena. El nombre del archivo debe incluir la extensión. Si el archivo ya existe, se sobrescribirá. Si no existe, se creará uno nuevo.

buffer

Un puntero a los datos que desea escribir.

lente

El número de bytes que se escribirán desde el búfer al archivo. Solo se guardará esta cantidad de bytes, incluso si el búfer es mayor.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Create a new file and display its message
  char message[] = "Hello";
  Brain.SDcard.savefile("Text.txt", (uint8_t*) message, strlen(message));
  int fileSize = Brain.SDcard.size("Text.txt");
  char buffer[fileSize];
  int length = Brain.SDcard.loadfile("Text.txt", (uint8_t*) buffer, sizeof(buffer));
  buffer[length] = '\0';
  Brain.Screen.print("%s", buffer);
}

appendfile#

appendfile appends a bytearray to an existing file, adding new data to the end of the file without altering the current contents.

Usage:
Brain.SDcard.appendfile(name, buffer, len);

Parámetros

Descripción

nombre

El nombre del archivo que se añadirá como cadena. El nombre del archivo debe incluir la extensión. Si el archivo ya existe, se sobrescribirá. Si no existe, se creará uno nuevo.

buffer

Un puntero a los datos que desea agregar.

lente

El número de bytes que se añadirán del búfer. Esto controla la cantidad de búfer que se escribe.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  char message[] = "Hello ";
  Brain.SDcard.savefile("Text.txt", (uint8_t*) message, strlen(message));
  // Append "IQ" to Text.txt and display it
  char append_message[] = "IQ";
  Brain.SDcard.appendfile("Text.txt", (uint8_t*) append_message, strlen(append_message));

  int fileSize = Brain.SDcard.size("Text.txt");
  char buffer[fileSize];
  int length = Brain.SDcard.loadfile("Text.txt", (uint8_t*) buffer, sizeof(buffer));
  buffer[length] = '\0';
  Brain.Screen.print("%s", buffer);
}

drawImageFromFile#

drawImageFromFile displays a Bitmap Image (BMP) stored on the SD card.

.bmp files are limited to 5120 pixels and 6KB in size.

Note: The IQ (2nd gen) Brain only supports displaying .bmp files. 8-bit RLE encoding is recommended to minimize file size.

Usage
Brain.Screen.drawImageFromFile(name, x, y);

Parámetros

Descripción

nombre

The name of the image file as a string. The file’s name must include .bmp.

incógnita

La coordenada x de la esquina superior izquierda de la imagen en la pantalla.

y

La coordenada y de la esquina superior izquierda de la imagen en la pantalla.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Draw BMP image located on the SD card at (0, 0)
  Brain.Screen.drawImageFromFile("test_image.bmp", 0, 0);
}

Adquiridor#

isInserted#

isInserted returns a Boolean indicating whether an SD card is currently inserted into the Brain.

  • true - The Brain has an SD card inserted.

  • false - The Brain does not have an SD card inserted.

Usage:
Brain.SDcard.isInserted()

Parámetros

Descripción

Este método no tiene parámetros.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Check if the SD card is inserted
  if (Brain.SDcard.isInserted()) {
    Brain.Screen.print("Inserted!");
  }
}

size#

size returns the size of a file in bytes as an integer.

Usage:
Brain.SDcard.size(name)

Parámetros

Descripción

nombre

El nombre del archivo como cadena. El nombre del archivo debe incluir la extensión.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Get filesize and then create the buffer to read file.
  int fileSize = Brain.SDcard.size("Text.txt");
  char buffer[fileSize];
  int length = Brain.SDcard.loadfile("Text.txt", (uint8_t*) buffer, sizeof(buffer));
  buffer[length] = '\0';
  Brain.Screen.print("%s", buffer);
}

exists#

exists returns a Boolean indicating whether a file is found on the SD card.

  • true - The file is on the SD card.

  • false - The file is not on the SD card.

Usage:
Brain.SDcard.exists(name)

Parámetros

Descripción

nombre

El nombre del archivo como cadena. El nombre del archivo debe incluir la extensión.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Check if the file exists
  if (Brain.SDcard.exists("Text.txt")) {
    Brain.Screen.print("File exists");
  } else {
    Brain.Screen.print("No file");
  }
}