SD kortelė#
Įvadas#
sdcard klasė yra kilusi iš brain bazinės klasės ir suteikia prieigą prie failų, saugomų išimamoje SD kortelėje, įdėtoje į IQ (2-osios kartos) Brain.
Naudodamas SD kortelę, jūsų robotas gali įkelti duomenis, pvz., vaizdus, skaityti ir rašyti failus bei saugoti informaciją vėlesniam naudojimui arba perkėlimui į kompiuterį.
Access#
Brain.SDcard
Notes#
„IQ“ (2-osios kartos) smegenys palaiko iki 32 GB talpos SD korteles.
SD kortelė turi būti suformatuota kaip FAT32.
Didesnės nei 32 GB SD kortelės paprastai formatuojamos kaip „exFAT“, kuri nesuderinama su „IQ (2-osios kartos) Brain“.
Visi failai turi būti patalpinti SD kortelės šakniniame kataloge, kad būtų pasiekiami.
Prieš atliekant failų operacijas, SD kortelė turi būti įdėta į „Brain“.
Failų pavadinimuose ir keliuose skiriamos didžiosios ir mažosios raidės.
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);
}
Narių funkcijos#
sdcard klasė apima šias narines funkcijas:
loadfile– Įkelia failo turinį iš SD kortelės į vartotojo pateiktą atminties buferį.savefile– Įrašo duomenis iš vartotojo pateikto buferio į failą IQ (2-osios kartos) smegenų SD kortelėje.appendfile– Prideda duomenis iš vartotojo pateikto buferio prie esamo failo pabaigos IQ (2-osios kartos) smegenų SD kortelėje.isInserted— Grąžina, ar į „Brain“ įdėta SD kortelė.size— Grąžina IQ (2-osios kartos) smegenų SD kortelėje saugomo failo dydį.exists— Grąžina, ar IQ (2-osios kartos) smegenų SD kortelėje yra failas nurodytu pavadinimu.
SD kortelėje saugomus .bmp ir .png failus galima peržiūrėti IQ (2-osios kartos) smegenų ekrane naudojant drawImageFromFile, prie kurios galima prisijungti per Brain.Screen.
Prieš iškviečiant bet kokias sdcard narių funkcijas, reikia sukurti brain egzempliorių, kaip parodyta žemiau:
// Create the Brain
brain Brain = brain();
loadfile#
Įkelia failo turinį iš SD kortelės į vartotojo pateiktą atminties buferį.
Available Functionsint32_t loadfile(
const char* name,
uint8_t* buffer,
int32_t len );
Parametras |
Tipas |
Aprašymas |
|---|---|---|
|
|
Iš SD kortelės įkeliamo failo pavadinimas. Failo pavadinime turi būti failo plėtinys. |
|
|
Rodyklė į buferį, kuriame bus saugomi failo duomenys. |
|
|
Didžiausias baitų skaičius, kurį galima nuskaityti iš failo, paprastai nustatomas pagal buferio dydį. |
Grąžina int32_t , nurodantį sėkmingai iš failo nuskaitytų baitų skaičių:
Teigiama reikšmė rodo, kiek baitų buvo įkelta į buferį.
0reiškia, kad duomenys nebuvo nuskaityti.
Jei failas yra didesnis nei
len, nuskaitomi tik pirmiejilenbaitai.Buferis turi būti pakankamai didelis, kad tilptų prašomi duomenys.
Ši funkcija nuskaito neapdorotus baitus ir neprideda nulinio terminatoriaus.
// 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#
Įrašo duomenis iš vartotojo pateikto buferio į failą IQ (2-osios kartos) smegenų SD kortelėje.
Available Functionsint32_t savefile(
const char* name,
uint8_t* buffer,
int32_t len );
Parametras |
Tipas |
Aprašymas |
|---|---|---|
|
|
Failo, kurį reikia išsaugoti kaip eilutę, pavadinimas. Failo pavadinime turi būti failo plėtinys. |
|
|
Rodyklė į atminties buferį, kuriame yra į failą įrašomi duomenys. |
|
|
Baitų skaičius, kurį reikia įrašyti iš buferio į failą. |
Grąžina int32_t , nurodantį sėkmingai į failą įrašytų baitų skaičių:
Teigiama reikšmė rodo, kiek baitų buvo išsaugota.
0reiškia, kad duomenys nebuvo įrašyti.
Jei failas yra didesnis nei
len, nuskaitomi tik pirmiejilenbaitai.Jei failas tuo pačiu pavadinimu jau yra, jis bus perrašytas.
Ši funkcija neskiria atminties; buferį turi sukurti ir valdyti vartotojas.
Funkcija rašo neapdorotus baitus ir neprideda nulinio terminatoriaus.
// 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#
Prideda duomenis iš vartotojo pateikto buferio prie esamo failo pabaigos IQ (2-osios kartos) smegenų SD kortelėje.
Available Functionsint32_t appendfile(
const char* name,
uint8_t* buffer,
int32_t len );
Parametras |
Tipas |
Aprašymas |
|---|---|---|
|
|
Failo, prie kurio SD kortelėje bus pridėti duomenys, pavadinimas. Failo pavadinime turi būti failo plėtinys. |
|
|
Rodyklė į atminties buferį, kuriame yra prie failo pridėtini duomenys. |
|
|
Baitų skaičius, kurį reikia pridėti iš buferio prie failo. |
Grąžina int32_t , nurodantį sėkmingai prie failo pridėtų baitų skaičių:
Teigiama reikšmė rodo, kiek baitų buvo pridėta.
0reiškia, kad nebuvo pridėta jokių duomenų.
Jei failas yra didesnis nei
len, nuskaitomi tik pirmiejilenbaitai.Failas jau turi būti SD kortelėje; ši funkcija nesukuria naujo failo.
Ši funkcija neskiria atminties; buferį turi sukurti ir valdyti vartotojas.
Funkcija prideda neapdorotus baitus ir neprideda nulinio terminatoriaus.
// 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#
Grąžina, ar SD kortelė šiuo metu įdėta į IQ (2-osios kartos) smegenis.
Available Functionsbool isInserted();
Ši funkcija nepriima jokių parametrų.
Return ValuesGrąžina loginę reikšmę, nurodančią, ar įdėta SD kortelė:
true— Į IQ (2-osios kartos) smegenis įdėta SD kortelė.false— SD kortelė neįdėta.
Ši funkcija tikrina tik SD kortelės buvimą; ji netikrina, ar kortelė suformatuota arba ar ją galima nuskaityti.
// 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#
Grąžina IQ (2-osios kartos) smegenų SD kortelėje saugomo failo dydį.
Available Functionsint32_t size(
const char* name );
Parametras |
Tipas |
Aprašymas |
|---|---|---|
|
|
SD kortelėje esančio failo pavadinimas. Failo pavadinime turi būti failo plėtinys. |
Grąžina int32_t , nurodantį failo dydį baitais:
Teigiama reikšmė nurodo failo dydį baitais.
0reiškia, kad failas yra tuščias arba kad jo nerasta.
Grąžintą dydį galima naudoti buferiui paskirstyti prieš iškviečiant
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#
Grąžina, ar IQ (2-osios kartos) Brain SD kortelėje yra failas nurodytu pavadinimu.
Available Functionsbool exists(
const char* name );
Parametras |
Tipas |
Aprašymas |
|---|---|---|
|
|
SD kortelėje esančio tikrinamo failo pavadinimas. Failo pavadinime turi būti nurodytas failo plėtinys. |
Grąžina loginę reikšmę, nurodančią, ar failas egzistuoja:
true— SD kortelėje yra failas nurodytu pavadinimu.false— Failo nurodytu pavadinimu nėra.
// 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);
}