SD卡#
介绍#
VEX IQ(第二代)的 SD 卡 API 允许您将可移动 SD 卡插入 Brain 的 SD 卡槽,从而存储和访问其中的文件。无论您是保存传感器数据还是加载自定义图像,这些方法都能让您完全控制文件操作。可移动 SD 卡让您可以轻松地在 Brain 和计算机之间传输数据。
IQ(第二代)Brain 需要一张容量不超过 32GB 且格式为 FAT32 的 SD 卡。大于 32GB 的 SD 卡默认使用 exFAT 格式,这与 VEX IQ(第二代)Brain 不兼容。使用前请确保您的 SD 卡已正确格式化为 FAT32 格式。
所有文件必须放在 SD 卡的根文件夹中才可访问。
以下是所有方法的列表:
loadfile – 将文件从 SD 卡加载到字节数组中。
savefile – 将字节数组保存到 SD 卡中。
appendfile – 将字节数组附加到现有文件。
is_inserted – 返回一个布尔值,指示 SD 卡是否已插入 Brain。
filesize – 返回文件的大小(以字节为单位)。
size – 返回文件的大小(以字节为单位)。
exists – 返回一个布尔值,指示是否在 SD 卡上找到某个文件。
draw_image_from_file – 显示来自 SD 卡的位图图像。
加载文件#
loadfile
将文件从 SD 卡加载到字节数组中。
用法:
brain.sdcard.loadfile(filename, buffer)
参数 |
描述 |
---|---|
文件名 |
要加载的文件的名称(以字符串形式)。文件名必须包含文件扩展名。 |
缓冲 |
可选。一个现有的字节数组,用于加载文件内容。如果现有字节数组较大,其内容将被覆盖。如果现有字节数组较小,该方法可能无法加载所有数据。默认情况下,如果未提供缓冲区,则将返回一个新的字节数组。 |
# Display a .txt file's contents on the brain
# Change the name of the file
text_file = brain.sdcard.loadfile("Text.txt")
brain.screen.print(text_file.decode("utf-8"))
保存文件#
savefile
将字节数组保存到 SD 卡中。
用法
brain.sdcard.savefile(filename, buffer)
参数 |
描述 |
---|---|
文件名 |
要保存为字符串的文件名称。文件名必须包含文件扩展名。如果文件已存在,则会覆盖该文件。如果文件不存在,则会创建一个新文件。 |
缓冲 |
可选。要写入文件的字节数组。 |
# Overwrite an existing txt file and display the new text
brain.sdcard.savefile("Text.txt", bytearray("Hello "))
text_file = brain.sdcard.loadfile("Text.txt")
brain.screen.print(text_file.decode("utf-8"))
附加文件#
appendfile
将字节数组附加到现有文件,将新数据添加到文件末尾而不改变当前内容。
用法:
brain.sdcard.appendfile(filename, buffer)
参数 |
描述 |
---|---|
文件名 |
要附加的文件名(以字符串形式)。文件名必须包含文件扩展名。如果文件已存在,则会覆盖该文件。如果文件不存在,则会创建一个新文件。 |
缓冲 |
包含要附加的数据的字节数组。 |
# Append "World" to an existing txt file then display it
brain.sdcard.appendfile('Text.txt', bytearray("World "))
text_file = brain.sdcard.loadfile("Text.txt")
brain.screen.print(text_file.decode("utf-8"))
已插入#
is_inserted
返回一个布尔值,指示 SD 卡当前是否插入 Brain。
“True”——大脑已插入 SD 卡。
False
-大脑没有插入 SD 卡。
用法:
brain.sdcard.is_inserted()
参数 |
描述 |
---|---|
该方法没有参数。 |
# Display a message if the SDcard is inserted
if brain.sdcard.is_inserted():
brain.screen.print("Inserted!")
文件大小#
filesize
以浮点数形式返回文件的大小(以字节为单位)。
用法:
brain.sdcard.filesize(filename)
参数 |
描述 |
---|---|
文件名 |
文件名(字符串形式)。文件名必须包含文件扩展名。 |
# Display the amount of bytes in a txt file
brain.screen.print(brain.sdcard.filesize("Text.txt"))
尺寸#
size
以浮点数形式返回文件的大小(以字节为单位)。
用法:
brain.sdcard.size(filename)
参数 |
描述 |
---|---|
文件名 |
文件名(字符串形式)。文件名必须包含文件扩展名。 |
# Display the amount of bytes in a txt file
brain.screen.print(brain.sdcard.size("Text.txt"))
存在#
exists
返回一个布尔值,指示是否在 SD 卡上找到文件。
True
-文件在 SD 卡上。False
- 文件不在 SD 卡上。
用法:
brain.sdcard.exists(filename)
参数 |
描述 |
---|---|
文件名 |
文件名(字符串形式)。文件名必须包含文件扩展名。 |
# Determine if a requested file exists
if brain.sdcard.exists("Text.txt"):
brain.screen.print("File exists")
else:
brain.screen.print("Create file")
从文件绘制图像#
draw_image_from_file
显示存储在 SD 卡上的位图图像 (BMP)。
.bmp
文件大小限制为 5120 像素和 6KB。
**注意:**IQ(第二代)Brain 仅支持显示 .bmp
文件。建议使用 8 位 RLE 编码以最小化文件大小。
用法
brain.screen.draw_image_from_file(filename, x, y)
参数 |
描述 |
---|---|
文件名 |
图像的文件名。 |
x |
屏幕上图像左上角的 x 坐标。 |
y |
屏幕上图像左上角的 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)