بطاقة SD#
مقدمة#
يتم اشتقاق الفئة sdcard من الفئة الأساسية brain وتوفر الوصول إلى الملفات المخزنة على بطاقة SD قابلة للإزالة يتم إدخالها في EXP Brain.
باستخدام بطاقة SD، يمكن للروبوت الخاص بك تحميل البيانات مثل الصور، وقراءة وكتابة الملفات، وتخزين المعلومات لاستخدامها لاحقًا أو نقلها إلى جهاز كمبيوتر.
Access#
Brain.SDcard
Notes#
يدعم جهاز EXP Brain بطاقات SD بسعة تصل إلى 32 جيجابايت.
يجب تهيئة بطاقة الذاكرة بنظام FAT32.
عادةً ما تتم تهيئة بطاقات SD التي تزيد سعتها عن 32 جيجابايت بنظام exFAT، وهو نظام غير متوافق مع EXP Brain.
يجب وضع جميع الملفات في الدليل الرئيسي لبطاقة الذاكرة الخارجية (SD) حتى يمكن الوصول إليها.
يجب إدخال بطاقة الذاكرة SD في وحدة المعالجة المركزية قبل إجراء عمليات الملفات.
أسماء الملفات ومساراتها حساسة لحالة الأحرف.
Example#
/* This constructor is required when using VS Code.
A Brain is generated automatically at the start of
VEXcode projects. */
// Create the EXP 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);
}
الدوال الأعضاء#
تتضمن الفئة sdcard الدوال الأعضاء التالية:
loadfile— يقوم بتحميل محتويات ملف من بطاقة SD إلى مخزن مؤقت للذاكرة يوفره المستخدم.savefile— يحفظ البيانات من مخزن مؤقت يوفره المستخدم إلى ملف على بطاقة SD الخاصة بـ EXP Brain.appendfile— يقوم بإلحاق البيانات من مخزن مؤقت مقدم من المستخدم إلى نهاية ملف موجود على بطاقة SD الخاصة بـ EXP Brain.isInserted— تُرجع ما إذا كانت بطاقة SD قد تم إدخالها في الدماغ.size— يعيد حجم الملف المخزن على بطاقة SD الخاصة بـ EXP Brain.exists— إرجاع ما إذا كان ملف بالاسم المحدد موجودًا على بطاقة SD الخاصة بـ EXP Brain.
يمكن عرض ملفات .bmp و .png المخزنة على بطاقة SD على شاشة EXP Brain باستخدام drawImageFromFile، والتي يتم الوصول إليها من خلال Brain.Screen.
قبل استدعاء أي من وظائف الأعضاء sdcard ، يجب إنشاء مثيل brain ، كما هو موضح أدناه:
// Create the Brain
brain Brain = brain();
loadfile#
يقوم بتحميل محتويات ملف من بطاقة SD إلى مخزن مؤقت للذاكرة يوفره المستخدم.
Available Functionsint32_t loadfile(
const char* name,
uint8_t* buffer,
int32_t len );
المعلمة |
يكتب |
وصف |
|---|---|---|
|
|
اسم الملف المراد تحميله من بطاقة الذاكرة الخارجية. يجب أن يتضمن اسم الملف امتداد الملف. |
|
|
مؤشر إلى مخزن مؤقت حيث سيتم تخزين بيانات الملف. |
|
|
الحد الأقصى لعدد البايتات المراد قراءتها من الملف، ويتم تعيينه عادةً على حجم المخزن المؤقت. |
تُرجع الدالة قيمة من نوع int32_t تساوي int32_t تمثل عدد البايتات التي تمت قراءتها بنجاح من الملف:
تشير القيمة الموجبة إلى عدد البايتات التي تم تحميلها في المخزن المؤقت.
يشير
0إلى عدم قراءة أي بيانات.
إذا كان حجم الملف أكبر من
len، فسيتم قراءة أولlenبايت فقط.يجب أن تكون مساحة التخزين المؤقتة كبيرة بما يكفي لاستيعاب البيانات المطلوبة.
تقرأ هذه الدالة البايتات الخام ولا تضيف حرف إنهاء فارغ.
// 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#
يحفظ البيانات من مخزن مؤقت يوفره المستخدم إلى ملف على بطاقة SD الخاصة بجهاز EXP Brain.
Available Functionsint32_t savefile(
const char* name,
uint8_t* buffer,
int32_t len );
المعلمة |
يكتب |
وصف |
|---|---|---|
|
|
اسم الملف المراد حفظه كسلسلة نصية. يجب أن يتضمن اسم الملف امتداد الملف. |
|
|
مؤشر إلى مخزن مؤقت للذاكرة يحتوي على البيانات المراد كتابتها في الملف. |
|
|
عدد البايتات المراد كتابتها من المخزن المؤقت إلى الملف. |
يُرجع قيمة من نوع int32_t تساوي int32_t تمثل عدد البايتات التي تمت كتابتها بنجاح إلى الملف:
تشير القيمة الموجبة إلى عدد البايتات التي تم حفظها.
يشير
0إلى عدم كتابة أي بيانات.
إذا كان حجم الملف أكبر من
len، فسيتم قراءة أولlenبايت فقط.إذا كان هناك ملف بنفس الاسم موجود بالفعل، فسيتم استبداله.
لا تقوم هذه الوظيفة بتخصيص الذاكرة؛ يجب على المستخدم إنشاء المخزن المؤقت وإدارته.
تقوم الدالة بكتابة البايتات الخام ولا تضيف حرف إنهاء فارغ.
// 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#
يقوم بإلحاق البيانات من مخزن مؤقت يوفره المستخدم بنهاية ملف موجود على بطاقة SD الخاصة بجهاز EXP Brain.
Available Functionsint32_t appendfile(
const char* name,
uint8_t* buffer,
int32_t len );
المعلمة |
يكتب |
وصف |
|---|---|---|
|
|
اسم الملف المراد إضافة البيانات إليه على بطاقة الذاكرة الخارجية. يجب أن يتضمن اسم الملف امتداد الملف. |
|
|
مؤشر إلى مخزن مؤقت للذاكرة يحتوي على البيانات المراد إلحاقها بالملف. |
|
|
عدد البايتات المراد إضافتها من المخزن المؤقت إلى الملف. |
Returns an int32_t representing the number of bytes successfully appended to the file:
تشير القيمة الموجبة إلى عدد البايتات التي تمت إضافتها.
يشير
0إلى عدم إضافة أي بيانات.
إذا كان حجم الملف أكبر من
len، فسيتم قراءة أولlenبايت فقط.يجب أن يكون الملف موجودًا بالفعل على بطاقة الذاكرة الخارجية (SD)؛ فهذه الوظيفة لا تنشئ ملفًا جديدًا.
لا تقوم هذه الوظيفة بتخصيص الذاكرة؛ يجب على المستخدم إنشاء المخزن المؤقت وإدارته.
تقوم الدالة بإلحاق البايتات الخام ولا تقوم بإلحاق حرف إنهاء السلسلة الفارغ.
// 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#
يُرجع ما إذا كانت بطاقة SD مُدرجة حاليًا في وحدة EXP Brain.
Available Functionsbool isInserted();
لا تقبل هذه الدالة أي معلمات.
Return Valuesيُرجع قيمة منطقية تشير إلى ما إذا كانت بطاقة SD مُدخلة أم لا:
true— يتم إدخال بطاقة SD في وحدة EXP Brain.false— لم يتم إدخال بطاقة SD.
هذه الوظيفة تتحقق فقط من وجود بطاقة SD؛ فهي لا تتحقق من أن البطاقة مهيأة أو قابلة للقراءة.
// 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#
يُعيد حجم الملف المخزن على بطاقة SD الخاصة بجهاز EXP Brain.
Available Functionsint32_t size(
const char* name );
المعلمة |
يكتب |
وصف |
|---|---|---|
|
|
اسم الملف الموجود على بطاقة الذاكرة الخارجية. يجب أن يتضمن اسم الملف امتداد الملف. |
تُرجع الدالة قيمة من نوع int32_t int32_t تمثل حجم الملف بالبايت:
تشير القيمة الموجبة إلى حجم الملف بالبايت.
يشير
0إلى ملف فارغ أو إلى أنه تعذر العثور على الملف.
يمكن استخدام الحجم المُعاد لتخصيص مخزن مؤقت قبل استدعاء
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#
يُرجع ما إذا كان ملف بالاسم المحدد موجودًا على بطاقة SD الخاصة بجهاز EXP Brain.
Available Functionsbool exists(
const char* name );
المعلمة |
يكتب |
وصف |
|---|---|---|
|
|
اسم الملف المراد فحصه على بطاقة الذاكرة الخارجية. يجب أن يتضمن اسم الملف امتداد الملف. |
يُرجع قيمة منطقية تشير إلى ما إذا كان الملف موجودًا أم لا:
true— يوجد ملف بالاسم المحدد على بطاقة SD.false— لا يوجد ملف بالاسم المحدد.
// 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);
}