SD卡#
介绍#
The SD card API for VEX IQ (2nd gen) enables you to store and access files on a removable SD card inserted into the Brain’s SD card slot. Whether you’re saving sensor data or loading custom images, these methods provide full control over file operations. The removable SD card allows you to easily transfer data between your Brain and computer.
IQ(第二代)Brain 需要一张容量不超过 32GB 且格式为 FAT32 的 SD 卡。大于 32GB 的 SD 卡默认使用 exFAT 格式,这与 VEX IQ(第二代)Brain 不兼容。使用前请确保您的 SD 卡已正确格式化为 FAT32 格式。
所有文件必须放在 SD 卡的根文件夹中才可访问。
This page uses brain as the example Brain name. Replace it with your own configured name as needed.
以下是所有方法的列表:
Actions — Perform file operations and display images from the SD card.
loadfile— Loads a file from the SD card into a bytearray.savefile— Saves a bytearray into the SD card.appendfile— Appends a bytearray to an existing file.draw_image_from_file— Displays a Bitmap Image from the SD card.
Getters — Return SD card and file information.
is_inserted— Returns a Boolean indicating whether an SD card is inserted into the Brain.filesize— Returns the size of a file.size— Returns the size of a file.exists— Returns a Boolean indicating whether a file is found on the SD card.
行动#
loadfile#
loadfile loads a file from the SD card into a bytearray.
Usage:
brain.sdcard.loadfile(filename, buffer)
参数 |
描述 |
|---|---|
|
要加载的文件的名称(以字符串形式)。文件名必须包含文件扩展名。 |
|
可选。一个现有的字节数组,用于加载文件内容。如果现有字节数组较大,其内容将被覆盖。如果现有字节数组较小,该方法可能无法加载所有数据。默认情况下,如果未提供缓冲区,则将返回一个新的字节数组。 |
# Check for SD card and show a file's text
if brain.sdcard.is_inserted():
# Save new text to a file
brain.sdcard.savefile("Note.txt", bytearray("VEX"))
# Load the file and print it
data = brain.sdcard.loadfile("Note.txt")
brain.screen.print(data.decode("utf-8"))
else:
brain.screen.print("Insert SD card")
savefile#
savefile saves a bytearray into the SD card.
Usage:
brain.sdcard.savefile(filename, buffer)
参数 |
描述 |
|---|---|
|
要保存为字符串的文件名称。文件名必须包含文件扩展名。如果文件已存在,则会覆盖该文件。如果文件不存在,则会创建一个新文件。 |
|
可选。要写入文件的字节数组。 |
# Check for SD card and show a file's text
if brain.sdcard.is_inserted():
# Save new text to a file
brain.sdcard.savefile("Note.txt", bytearray("VEX"))
# Load the file and print it
data = brain.sdcard.loadfile("Note.txt")
brain.screen.print(data.decode("utf-8"))
else:
brain.screen.print("Insert SD card")
appendfile#
appendfile appends a bytearray to an existing file, adding new data to the end of the file without altering the current contents.
Usage:
brain.sdcard.appendfile(filename, buffer)
参数 |
描述 |
|---|---|
|
The name of the file to append as a string. The file’s name must include the file extension. |
|
包含要附加的数据的字节数组。 |
# Add more text and display file size
if brain.sdcard.exists("Note.txt"):
brain.sdcard.appendfile("Note.txt", bytearray("code"))
size = brain.sdcard.size("Note.txt")
brain.screen.print("Size: ")
brain.screen.print(size)
draw_image_from_file#
draw_image_from_file displays a Bitmap Image (BMP) stored on the SD card.
.bmp files are limited to 5120 pixels and 6KB in size.
Note: The IQ (2nd gen) Brain only supports displaying .bmp files. 8-bit RLE encoding is recommended to minimize file size.
Usage
brain.screen.draw_image_from_file(filename, x, y)
参数 |
描述 |
|---|---|
|
图像的文件名。 |
|
屏幕上图像左上角的 x 坐标。 |
|
屏幕上图像左上角的 y 坐标。 |
# Draw a bmp file on the Brain's screen at coordinate 0, 0
brain.screen.draw_image_from_file('test_image.bmp', 0, 0)
吸气剂#
is_inserted#
is_inserted returns a Boolean indicating whether an SD card is currently inserted into the Brain.
True— The Brain has an SD card inserted.False— The Brain does not have an SD card inserted.
Usage:
brain.sdcard.is_inserted()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Check for SD card and show a file's text
if brain.sdcard.is_inserted():
# Save new text to a file
brain.sdcard.savefile("Note.txt", bytearray("VEX"))
# Load the file and print it
data = brain.sdcard.loadfile("Note.txt")
brain.screen.print(data.decode("utf-8"))
else:
brain.screen.print("Insert SD card")
filesize#
filesize returns the size of a file in bytes as a float.
Usage:
brain.sdcard.filesize(filename)
参数 |
描述 |
|---|---|
|
文件名(字符串形式)。文件名必须包含文件扩展名。 |
# Add more text and display file size
if brain.sdcard.exists("Note.txt"):
brain.sdcard.appendfile("Note.txt", bytearray("code"))
size = brain.sdcard.filesize("Note.txt")
brain.screen.print("Size: ")
brain.screen.print(size)
size#
size returns the size of a file in bytes as a float.
Usage:
brain.sdcard.size(filename)
参数 |
描述 |
|---|---|
|
文件名(字符串形式)。文件名必须包含文件扩展名。 |
# Add more text and display file size
if brain.sdcard.exists("Note.txt"):
brain.sdcard.appendfile("Note.txt", bytearray("code"))
size = brain.sdcard.size("Note.txt")
brain.screen.print("Size: ")
brain.screen.print(size)
exists#
exists returns a Boolean indicating whether a file is found on the SD card.
True— The file is on the SD card.False— The file is not on the SD card.
Usage:
brain.sdcard.exists(filename)
参数 |
描述 |
|---|---|
|
文件名(字符串形式)。文件名必须包含文件扩展名。 |
# Add more text and display file size if the file exists
if brain.sdcard.exists("Note.txt"):
brain.sdcard.appendfile("Note.txt", bytearray(" World"))
size = brain.sdcard.size("Note.txt")
brain.screen.print("Size: ")
brain.screen.print(size)