Tarjeta SD#

Introducción#

Usar una tarjeta SD con VEX V5 le permite almacenar y acceder a archivos en una tarjeta SD extraíble insertada en la ranura para tarjetas SD del Brain. Tanto si guarda datos del sensor como si carga imágenes personalizadas, estos métodos le ofrecen control total sobre las operaciones con los archivos. La tarjeta SD extraíble le permite transferir datos fácilmente entre su Brain y el ordenador.

El V5 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 V5 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 siguientes, 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:

Acciones: Realizar operaciones de archivos y mostrar imágenes desde la tarjeta SD.

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

  • savefile — Saves a bytearray onto the SD card.

  • appendfile — Appends a bytearray to an existing file.

  • draw_image_from_file — Displays a Bitmap Image from the SD card.

Getters — Devuelven información de la tarjeta SD y del archivo.

  • is_inserted — 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.

For information on constructing a Brain to gain access to the sdcard methods, see Brain.

Comportamiento#

loadfile#

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

Usage:
Brain.SDcard.loadfile(filename, buffer);

Parámetros

Descripción

filename

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

buffer

Un bytearray existente para cargar el contenido del archivo. Si el bytearray existente es mayor, su contenido se sobrescribirá. Si el bytearray existente es menor, es posible que el método no cargue todos los datos. Por defecto, se devolverá un nuevo bytearray si no se proporciona ningún búfer.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // 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#

savefile saves a bytearray onto the SD card.

Usage:
Brain.SDcard.savefile(filename, buffer);

Parámetros

Descripción

filename

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 bytearray para escribir en el archivo.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // 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#

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(filename, buffer);

Parámetros

Descripción

filename

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

buffer

Un bytearray que contiene los datos a agregar.

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

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

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 V5 Brain only supports displaying .bmp files. 8-bit RLE encoding is recommended to minimize file size.

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

Parámetros

Descripción

filename

El nombre del archivo de la imagen.

x

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 a bmp file on the Brain's screen at coordinate 0, 0
  Brain.Screen.drawImageFromFile("test_image.bmp", 0, 0);
}

Captadores#

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 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#

size returns the size of a file in bytes.

Usage:
Brain.SDcard.size(filename);

Parámetros

Descripción

filename

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 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#

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(filename);

Parámetros

Descripción

filename

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

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // 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);
  }
}