SD卡#
介绍#
使用 VEX V5 的 SD 卡,您可以将文件存储在插入主机 SD 卡插槽的可移动 SD 卡上,并随时访问这些文件。无论是保存传感器数据还是加载自定义图像,这些方法都能让您完全掌控文件操作。可移动 SD 卡还方便您在主机和计算机之间轻松传输数据。
V5 Brain 需要一张容量不超过 32GB 的 SD 卡,格式为 FAT32。容量大于 32GB 的 SD 卡默认使用 exFAT 格式,这与 V5 Brain 不兼容。请确保您的 SD 卡在使用前已正确格式化为 FAT32。
所有文件必须放置在SD卡的根文件夹中才能访问。
对于以下示例,构造的 Brain 包括对 SD 卡方法的访问,并且在本 API 文档中所有后续示例中引用这些方法时都将使用它。
以下是所有方法的列表:
操作 – 执行文件操作并显示 SD 卡中的图像。
loadfile– 将 SD 卡中的文件加载到字节数组中。savefile– 将字节数组保存到 SD 卡上。appendfile– 将字节数组追加到现有文件中。draw_image_from_file– 显示 SD 卡中的位图图像。
获取器 – 返回 SD 卡和文件信息。
is_inserted– 返回一个布尔值,指示 SD 卡是否已插入 Brain。filesize– 返回文件的大小(以字节为单位)。size– 返回文件的大小(以字节为单位)。exists– 返回一个布尔值,指示是否在 SD 卡上找到文件。
有关构建 Brain 以获取 sdcard 方法的信息,请参阅 Brain。
行动#
loadfile#
loadfile 将 SD 卡中的文件加载到字节数组中。
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 将字节数组保存到 SD 卡上。
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 将字节数组追加到现有文件,将新数据添加到文件末尾,而不更改当前内容。
Usage:
brain.sdcard.appendfile(filename, buffer)
参数 |
描述 |
|---|---|
|
要追加的文件名(字符串形式)。文件名必须包含文件扩展名。 |
|
包含要追加数据的字节数组。 |
# 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 显示存储在 SD 卡上的位图图像 (BMP)。
.bmp 文件限制为 5120 像素和 6KB 大小。
注意: V5 大脑仅支持显示 .bmp 文件。建议使用 8 位 RLE 编码以减小文件大小。
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 返回一个布尔值,指示当前是否将 SD 卡插入 Brain。
True- 大脑已插入 SD 卡。False- 大脑未插入 SD 卡。
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 返回文件的大小(以字节为单位)。
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 返回文件的大小(以字节为单位)。
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 返回一个布尔值,指示是否在 SD 卡上找到文件。
True- 文件在 SD 卡上。False- 文件不在 SD 卡上。
Usage:
brain.sdcard.exists(filename)
参数 |
描述 |
|---|---|
|
文件名(字符串形式)。文件名必须包含文件扩展名。 |
# Add more text and display file size
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)