การ์ด SD#
การแนะนำ#
คลาส sdcard ได้รับมาจากคลาสพื้นฐาน brain และให้สิทธิ์ในการเข้าถึงไฟล์ที่จัดเก็บไว้บนการ์ด SD แบบถอดได้ที่เสียบเข้าไปใน EXP Brain
ด้วยการใช้การ์ด SD หุ่นยนต์ของคุณสามารถโหลดข้อมูล เช่น รูปภาพ อ่านและเขียนไฟล์ และจัดเก็บข้อมูลเพื่อใช้งานในภายหลังหรือถ่ายโอนไปยังคอมพิวเตอร์ได้
Access#
Brain.SDcard
Notes#
EXP Brain รองรับการ์ด SD ขนาดสูงสุด 32GB
ต้องฟอร์แมตการ์ด SD เป็น FAT32
โดยทั่วไป การ์ด SD ที่มีขนาดใหญ่กว่า 32GB จะถูกฟอร์แมตเป็น exFAT ซึ่งไม่สามารถใช้งานร่วมกับ EXP Brain ได้
ไฟล์ทั้งหมดต้องอยู่ในไดเร็กทอรีหลักของ SD การ์ดจึงจะสามารถเข้าถึงได้
ต้องเสียบการ์ด SD เข้ากับ Brain ก่อนจึงจะสามารถดำเนินการกับไฟล์ได้
ชื่อไฟล์และเส้นทางไฟล์นั้นต้องตรงตามตัวพิมพ์ใหญ่เล็ก
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 Brainappendfile— เพิ่มข้อมูลจากบัฟเฟอร์ที่ผู้ใช้จัดเตรียมไว้ไปยังส่วนท้ายของไฟล์ที่มีอยู่บนการ์ด SD ของ EXP BrainisInserted— ส่งคืนว่ามีการเสียบการ์ด SD เข้าไปใน Brain หรือไม่size— ส่งคืนขนาดของไฟล์ที่จัดเก็บไว้บนการ์ด SD ของ EXP Brainexists— ตรวจสอบว่ามีไฟล์ที่มีชื่อที่ระบุอยู่ในการ์ด 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 );
พารามิเตอร์ |
พิมพ์ |
คำอธิบาย |
|---|---|---|
|
|
ชื่อไฟล์ที่จะโหลดจาก SD การ์ด ชื่อไฟล์ต้องมีนามสกุลไฟล์ด้วย |
|
|
ตัวชี้ไปยังบัฟเฟอร์ที่จะใช้เก็บข้อมูลไฟล์ |
|
|
จำนวนไบต์สูงสุดที่จะอ่านจากไฟล์ โดยปกติจะตั้งค่าไว้ที่ขนาดของบัฟเฟอร์ |
ส่งคืนค่า int32_t ซึ่งแสดงจำนวนไบต์ที่อ่านจากไฟล์ได้สำเร็จ:
ค่าบวกบ่งบอกจำนวนไบต์ที่ถูกโหลดเข้าไปในบัฟเฟอร์
0หมายถึงไม่มีการอ่านข้อมูล
ถ้าไฟล์มีขนาดใหญ่กว่า
lenระบบจะอ่านเฉพาะlenไบต์แรกเท่านั้นบัฟเฟอร์ต้องมีขนาดใหญ่พอที่จะเก็บข้อมูลที่ร้องขอได้
ฟังก์ชันนี้อ่านไบต์ดิบและไม่เพิ่มตัวจบไบต์ที่เป็นค่าว่าง (null terminator)
// 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 ซึ่งแสดงถึงจำนวนไบต์ที่เขียนลงไฟล์สำเร็จ:
ค่าบวกแสดงถึงจำนวนไบต์ที่ประหยัดได้
0หมายถึงไม่มีการเขียนข้อมูลใดๆ
ถ้าไฟล์มีขนาดใหญ่กว่า
lenระบบจะอ่านเฉพาะlenไบต์แรกเท่านั้นหากมีไฟล์ชื่อเดียวกันอยู่แล้ว ไฟล์นั้นจะถูกเขียนทับ
ฟังก์ชันนี้ไม่ได้ทำการจัดสรรหน่วยความจำ ผู้ใช้ต้องสร้างและจัดการบัฟเฟอร์ด้วยตนเอง
ฟังก์ชันนี้เขียนข้อมูลดิบเป็นไบต์โดยไม่เพิ่มอักขระสิ้นสุด (null terminator)
// 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 );
พารามิเตอร์ |
พิมพ์ |
คำอธิบาย |
|---|---|---|
|
|
ชื่อไฟล์ที่จะใช้เพิ่มข้อมูลลงในการ์ด SD โดยชื่อไฟล์ต้องมีนามสกุลไฟล์ด้วย |
|
|
ตัวชี้ไปยังบัฟเฟอร์หน่วยความจำที่บรรจุข้อมูลที่จะถูกเพิ่มเข้าไปในไฟล์ |
|
|
จำนวนไบต์ที่จะเพิ่มจากบัฟเฟอร์ไปยังไฟล์ |
ส่งคืนค่า int32_t ซึ่งแสดงถึงจำนวนไบต์ที่เพิ่มลงในไฟล์สำเร็จ:
ค่าบวกบ่งบอกถึงจำนวนไบต์ที่ถูกเพิ่มเข้าไป
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 Brainfalse— ไม่ได้เสียบการ์ด 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 );
พารามิเตอร์ |
พิมพ์ |
คำอธิบาย |
|---|---|---|
|
|
ชื่อไฟล์บนการ์ด SD ชื่อไฟล์ต้องมีนามสกุลไฟล์ด้วย |
ส่งคืนค่า 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 );
พารามิเตอร์ |
พิมพ์ |
คำอธิบาย |
|---|---|---|
|
|
ชื่อไฟล์ที่ต้องการตรวจสอบใน SD การ์ด ชื่อไฟล์ต้องมีนามสกุลไฟล์ด้วย |
ส่งคืนค่าบูลีนที่ระบุว่าไฟล์มีอยู่หรือไม่:
true— มีไฟล์ชื่อที่ระบุอยู่ในการ์ด SDfalse— ไม่มีไฟล์ชื่อที่ระบุอยู่
// 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);
}