การ์ด SD#

การแนะนำ#

คลาส sdcard ได้รับมาจากคลาสพื้นฐาน brain และให้สิทธิ์ในการเข้าถึงไฟล์ที่จัดเก็บไว้บนการ์ด SD แบบถอดได้ที่เสียบเข้ากับ V5 Brain

ด้วยการใช้การ์ด SD หุ่นยนต์ของคุณสามารถโหลดข้อมูล เช่น รูปภาพ อ่านและเขียนไฟล์ และจัดเก็บข้อมูลเพื่อใช้งานในภายหลังหรือถ่ายโอนไปยังคอมพิวเตอร์ได้

Access#

Brain.SDcard

Notes#

  • ชิปประมวลผล V5 Brain รองรับการ์ด SD ขนาดสูงสุด 32GB

  • ต้องฟอร์แมตการ์ด SD เป็น FAT32

    • โดยทั่วไป การ์ด SD ที่มีขนาดใหญ่กว่า 32GB จะถูกฟอร์แมตเป็น exFAT ซึ่งไม่สามารถใช้งานร่วมกับ V5 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 V5 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 ของ V5 Brain

  • appendfile — เพิ่มข้อมูลจากบัฟเฟอร์ที่ผู้ใช้จัดเตรียมไว้ไปยังส่วนท้ายของไฟล์ที่มีอยู่บนการ์ด SD ของ V5 Brain

  • isInserted — ส่งคืนว่ามีการเสียบการ์ด SD เข้าไปใน Brain หรือไม่

  • size — ส่งคืนขนาดของไฟล์ที่จัดเก็บไว้บนการ์ด SD ของ V5 Brain

  • exists — ตรวจสอบว่าไฟล์ที่มีชื่อที่ระบุมีอยู่ในการ์ด SD ของ V5 Brain หรือไม่

ไฟล์ .bmp และ .png ที่จัดเก็บไว้ในการ์ด SD สามารถแสดงบนหน้าจอ V5 Brain ได้โดยใช้ drawImageFromFile ซึ่งเข้าถึงได้ผ่าน Brain.Screen

ก่อนเรียกใช้ฟังก์ชันสมาชิก sdcard ใดๆ จะต้องสร้างอินสแตนซ์ brain ก่อน ดังแสดงด้านล่าง:

// Create the V5 Brain
brain Brain = brain();

loadfile#

โหลดเนื้อหาของไฟล์จาก SD การ์ดไปยังบัฟเฟอร์หน่วยความจำที่ผู้ใช้กำหนด

Available Functions
int32_t loadfile(
  const char* name,
  uint8_t* buffer,
  int32_t len );

Parameters

พารามิเตอร์

พิมพ์

คำอธิบาย

name

const char*

ชื่อไฟล์ที่จะโหลดจาก SD การ์ด ชื่อไฟล์ต้องมีนามสกุลไฟล์ด้วย

buffer

uint8_t*

ตัวชี้ไปยังบัฟเฟอร์ที่จะใช้เก็บข้อมูลไฟล์

len

int32_t

จำนวนไบต์สูงสุดที่จะอ่านจากไฟล์ โดยปกติจะตั้งค่าไว้ที่ขนาดของบัฟเฟอร์

Return Values

ส่งคืนค่า int32_t ซึ่งแสดงจำนวนไบต์ที่อ่านจากไฟล์ได้สำเร็จ:

  • ค่าบวกบ่งบอกจำนวนไบต์ที่ถูกโหลดเข้าไปในบัฟเฟอร์

  • 0 หมายถึงไม่มีการอ่านข้อมูล

Notes
  • ถ้าไฟล์มีขนาดใหญ่กว่า lenระบบจะอ่านเฉพาะ len ไบต์แรกเท่านั้น

  • บัฟเฟอร์ต้องมีขนาดใหญ่พอที่จะเก็บข้อมูลที่ร้องขอได้

  • ฟังก์ชันนี้อ่านไบต์ดิบและไม่เพิ่มตัวจบไบต์ที่เป็นค่าว่าง (null terminator)

Examples
// 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 ของ V5 Brain

Available Functions
int32_t savefile(
  const char* name,
  uint8_t* buffer,
  int32_t len );

Parameters

พารามิเตอร์

พิมพ์

คำอธิบาย

name

const char*

ชื่อไฟล์ที่จะบันทึกเป็นสตริง โดยชื่อไฟล์ต้องมีนามสกุลไฟล์ด้วย

buffer

uint8_t*

ตัวชี้ไปยังบัฟเฟอร์หน่วยความจำที่มีข้อมูลที่จะเขียนลงในไฟล์

len

int32_t

จำนวนไบต์ที่จะเขียนจากบัฟเฟอร์ไปยังไฟล์

Return Values

ส่งคืนค่า int32_t ซึ่งแสดงถึงจำนวนไบต์ที่เขียนลงไฟล์สำเร็จ:

  • ค่าบวกแสดงถึงจำนวนไบต์ที่ประหยัดได้

  • 0 หมายถึงไม่มีการเขียนข้อมูลใดๆ

Notes
  • ถ้าไฟล์มีขนาดใหญ่กว่า lenระบบจะอ่านเฉพาะ len ไบต์แรกเท่านั้น

  • หากมีไฟล์ชื่อเดียวกันอยู่แล้ว ไฟล์นั้นจะถูกเขียนทับ

  • ฟังก์ชันนี้ไม่ได้ทำการจัดสรรหน่วยความจำ ผู้ใช้ต้องสร้างและจัดการบัฟเฟอร์ด้วยตนเอง

  • ฟังก์ชันนี้เขียนข้อมูลดิบเป็นไบต์โดยไม่เพิ่มอักขระสิ้นสุด (null terminator)

Examples
// 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 ของ V5 Brain

Available Functions
int32_t appendfile(
  const char* name,
  uint8_t* buffer,
  int32_t len );

Parameters

พารามิเตอร์

พิมพ์

คำอธิบาย

name

const char*

ชื่อไฟล์ที่จะใช้เพิ่มข้อมูลลงในการ์ด SD โดยชื่อไฟล์ต้องมีนามสกุลไฟล์ด้วย

buffer

uint8_t*

ตัวชี้ไปยังบัฟเฟอร์หน่วยความจำที่บรรจุข้อมูลที่จะถูกเพิ่มเข้าไปในไฟล์

len

int32_t

จำนวนไบต์ที่จะเพิ่มจากบัฟเฟอร์ไปยังไฟล์

Return Values

ส่งคืนค่า int32_t ซึ่งแสดงถึงจำนวนไบต์ที่เพิ่มลงในไฟล์สำเร็จ:

  • ค่าบวกบ่งบอกถึงจำนวนไบต์ที่ถูกเพิ่มเข้าไป

  • 0 หมายถึงไม่มีการเพิ่มข้อมูลใดๆ

Notes
  • ถ้าไฟล์มีขนาดใหญ่กว่า lenระบบจะอ่านเฉพาะ len ไบต์แรกเท่านั้น

  • ไฟล์ต้องมีอยู่แล้วใน SD การ์ด ฟังก์ชันนี้ไม่ได้สร้างไฟล์ใหม่

  • ฟังก์ชันนี้ไม่ได้ทำการจัดสรรหน่วยความจำ ผู้ใช้ต้องสร้างและจัดการบัฟเฟอร์ด้วยตนเอง

  • ฟังก์ชันนี้จะเพิ่มไบต์ดิบโดยไม่เพิ่มตัวจบไบต์ที่เป็นค่าว่าง

Examples
// 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 อยู่ใน V5 Brain หรือไม่

Available Functions
bool isInserted();

Parameters

ฟังก์ชันนี้ไม่รับพารามิเตอร์ใดๆ

Return Values

ส่งคืนค่าบูลีนที่ระบุว่ามีการเสียบการ์ด SD หรือไม่:

  • true — มีการเสียบการ์ด SD ลงใน V5 Brain แล้ว

  • false — ไม่ได้เสียบการ์ด SD

Notes
  • ฟังก์ชันนี้ตรวจสอบเพียงว่ามีการ์ด SD อยู่หรือไม่เท่านั้น ไม่ได้ตรวจสอบว่าการ์ดได้รับการฟอร์แมตหรืออ่านข้อมูลได้หรือไม่

Examples
// 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 ของ V5 Brain

Available Functions
int32_t size(
  const char* name );

Parameters

พารามิเตอร์

พิมพ์

คำอธิบาย

name

const char*

ชื่อไฟล์บนการ์ด SD ชื่อไฟล์ต้องมีนามสกุลไฟล์ด้วย

Return Values

ส่งคืนค่า int32_t ซึ่งแสดงถึงขนาดของไฟล์ในหน่วยไบต์:

  • ค่าบวกแสดงถึงขนาดไฟล์เป็นไบต์

  • 0 หมายถึงไฟล์ว่างเปล่าหรือหาไฟล์ไม่พบ

Notes
  • ขนาดที่ส่งคืนสามารถใช้ในการจัดสรรบัฟเฟอร์ก่อนเรียก loadfile

Examples
// 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 ของ V5 Brain หรือไม่

Available Functions
bool exists(
  const char* name );

Parameters

พารามิเตอร์

พิมพ์

คำอธิบาย

name

const char*

ชื่อไฟล์ที่ต้องการตรวจสอบใน SD การ์ด ชื่อไฟล์ต้องมีนามสกุลไฟล์ด้วย

Return Values

ส่งคืนค่าบูลีนที่ระบุว่าไฟล์มีอยู่หรือไม่:

  • true — มีไฟล์ชื่อที่ระบุอยู่ในการ์ด SD

  • false — ไม่มีไฟล์ชื่อที่ระบุอยู่

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