SD karte#
Ievads#
Klase sdcard ir atvasināta no bāzes klases brain un nodrošina piekļuvi failiem, kas glabājas noņemamā SD kartē, kas ievietota IQ (2. paaudzes) Brain ierīcē.
Izmantojot SD karti, robots var ielādēt datus, piemēram, attēlus, lasīt un rakstīt failus, kā arī saglabāt informāciju vēlākai izmantošanai vai pārsūtīšanai uz datoru.
Access#
Brain.SDcard
Notes#
IQ (2. paaudzes) smadzenes atbalsta SD kartes līdz 32 GB ietilpībai.
SD kartei jābūt formatētai kā FAT32.
SD kartes, kas lielākas par 32 GB, parasti tiek formatētas kā exFAT, kas nav saderīgas ar IQ (2. paaudzes) Brain.
Lai failiem varētu piekļūt, tie jāievieto SD kartes saknes direktorijā.
Pirms failu operāciju veikšanas SD karte ir jāievieto ierīcē.
Failu nosaukumi un ceļi ir reģistrjutīgi.
Example#
/* This constructor is required when using VS Code.
A Brain is generated automatically at the start of
VEXcode projects. */
// Create the 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);
}
Dalībnieku funkcijas#
Klasē sdcard ir iekļautas šādas dalībfunkcijas:
loadfile— Ielādē faila saturu no SD kartes lietotāja norādītā atmiņas buferī.savefile— Saglabā datus no lietotāja norādītās bufera failā IQ (2. paaudzes) Brain SD kartē.appendfile— Pievieno datus no lietotāja nodrošinātas bufera esoša faila beigās IQ (2. paaudzes) Brain SD kartē.isInserted— Atgriež, vai ierīcē Brain ir ievietota SD karte.size— Atgriež IQ (2. paaudzes) smadzeņu SD kartē saglabātā faila izmēru.exists— Atgriež, vai IQ (2. paaudzes) Brain SD kartē pastāv fails ar norādīto nosaukumu.
SD kartē saglabātos .bmp un .png failus var parādīt IQ (2. paaudzes) Brain ekrānā, izmantojot drawImageFromFile, kam var piekļūt, izmantojot Brain.Screen.
Pirms jebkuru sdcard dalībfunkciju izsaukšanas ir jāizveido brain instance, kā parādīts zemāk:
// Create the Brain
brain Brain = brain();
loadfile#
Ielādē faila saturu no SD kartes lietotāja nodrošinātā atmiņas buferī.
Available Functionsint32_t loadfile(
const char* name,
uint8_t* buffer,
int32_t len );
Parametrs |
Tips |
Apraksts |
|---|---|---|
|
|
No SD kartes ielādējamā faila nosaukums. Faila nosaukumā jāiekļauj faila paplašinājums. |
|
|
Rādītājs uz buferi, kurā tiks glabāti faila dati. |
|
|
Maksimālais no faila nolasāmo baitu skaits, kas parasti tiek iestatīts uz bufera lielumu. |
Atgriež int32_t , kas apzīmē no faila veiksmīgi nolasīto baitu skaitu:
Pozitīva vērtība norāda, cik baitu tika ielādēti buferī.
0norāda, ka dati netika nolasīti.
Ja fails ir lielāks par
len, tiek nolasīti tikai pirmielenbaiti.Buferim jābūt pietiekami lielam, lai tajā varētu ietilpt pieprasītie dati.
Šī funkcija nolasa neapstrādātus baitus un nepievieno nulles terminatoru.
// 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#
Saglabā datus no lietotāja sniegtas bufera failā IQ (2. paaudzes) smadzeņu SD kartē.
Available Functionsint32_t savefile(
const char* name,
uint8_t* buffer,
int32_t len );
Parametrs |
Tips |
Apraksts |
|---|---|---|
|
|
Faila nosaukums, kas jāsaglabā kā virkne. Faila nosaukumā jāiekļauj faila paplašinājums. |
|
|
Rādītājs uz atmiņas buferi, kurā atrodas failā ierakstāmie dati. |
|
|
Baitu skaits, kas jāieraksta no bufera failā. |
Atgriež vērtību int32_t kas apzīmē failā veiksmīgi ierakstīto baitu skaitu:
Pozitīva vērtība norāda, cik baitu tika saglabāti.
0norāda, ka dati nav ierakstīti.
Ja fails ir lielāks par
len, tiek nolasīti tikai pirmielenbaiti.Ja fails ar tādu pašu nosaukumu jau pastāv, tas tiks pārrakstīts.
Šī funkcija nepiešķir atmiņu; buferis ir jāizveido un jāpārvalda lietotājam.
Funkcija raksta neapstrādātus baitus un nepievieno nulles terminatoru.
// 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#
Pievieno datus no lietotāja sniegtas bufera esoša faila beigās IQ (2. paaudzes) Brain SD kartē.
Available Functionsint32_t appendfile(
const char* name,
uint8_t* buffer,
int32_t len );
Parametrs |
Tips |
Apraksts |
|---|---|---|
|
|
SD kartē esošā faila nosaukums, kuram pievienot datus. Faila nosaukumā jāiekļauj faila paplašinājums. |
|
|
Rādītājs uz atmiņas buferi, kurā ir dati, kas jāpievieno failam. |
|
|
Baitu skaits, kas jāpievieno no bufera failam. |
Atgriež vērtību int32_t kas apzīmē failam veiksmīgi pievienoto baitu skaitu:
Pozitīva vērtība norāda, cik baitu tika pievienoti.
0norāda, ka dati netika pievienoti.
Ja fails ir lielāks par
len, tiek nolasīti tikai pirmielenbaiti.Failam jau ir jābūt SD kartē; šī funkcija neveido jaunu failu.
Šī funkcija nepiešķir atmiņu; buferis ir jāizveido un jāpārvalda lietotājam.
Funkcija pievieno neapstrādātus baitus un nepievieno nulles terminatoru.
// 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#
Atgriež, vai IQ (2. paaudzes) smadzenēs pašlaik ir ievietota SD karte.
Available Functionsbool isInserted();
Šī funkcija nepieņem nekādus parametrus.
Return ValuesAtgriež Būla vērtību, kas norāda, vai ir ievietota SD karte:
true— IQ (2. paaudzes) smadzenēs ir ievietota SD karte.false— SD karte nav ievietota.
Šī funkcija pārbauda tikai SD kartes esamību; tā nepārbauda, vai karte ir formatēta vai nolasāma.
// 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#
Atgriež IQ (2. paaudzes) Brain SD kartē saglabātā faila lielumu.
Available Functionsint32_t size(
const char* name );
Parametrs |
Tips |
Apraksts |
|---|---|---|
|
|
SD kartē esošā faila nosaukums. Faila nosaukumā jāiekļauj faila paplašinājums. |
Atgriež int32_t , kas apzīmē faila lielumu baitos:
Pozitīva vērtība norāda faila lielumu baitos.
0norāda, ka fails ir tukšs vai ka failu nevarēja atrast.
Atgriezto izmēru var izmantot, lai piešķirtu buferi pirms
loadfileizsaukšanas.
// 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#
Atgriež, vai IQ (2. paaudzes) Brain SD kartē pastāv fails ar norādīto nosaukumu.
Available Functionsbool exists(
const char* name );
Parametrs |
Tips |
Apraksts |
|---|---|---|
|
|
SD kartē pārbaudāmā faila nosaukums. Faila nosaukumā jāiekļauj faila paplašinājums. |
Atgriež Būla vērtību, kas norāda, vai fails pastāv:
true— SD kartē jau ir fails ar norādīto nosaukumu.false— Nav faila ar norādīto nosaukumu.
// 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);
}