SD卡#

介紹#

VEX IQ(第二代)的 SD 卡 API 可讓您儲存和存取插入 Brain SD 卡插槽中的可移動 SD 卡上的檔案。無論是保存感測器資料還是載入自訂影像,這些方法都能讓您完全控製檔案操作。可移動 SD 卡可讓您輕鬆地在 Brain 和電腦之間傳輸資料。

VEX IQ(第二代)主機需要容量不超過 32GB 的 SD 卡,格式為 FAT32。容量大於 32GB 的 SD 卡預設使用 exFAT 格式,這與 VEX IQ(第二代)主機不相容。請確保您的 SD 卡在使用前已正確格式化為 FAT32。

所有檔案必須放置在SD卡的根資料夾中才能存取。

本頁面使用 brain 作為範例 Brain 名稱。請根據需要將其替換為您自己配置的名稱。

以下是所有方法的清單:

操作 — 執行檔案操作並顯示 SD 卡中的影像。

  • loadfile — 將 SD 卡中的檔案內容載入到使用者提供的記憶體緩衝區中。

  • savefile — 將使用者提供的緩衝區中的資料儲存到 IQ(第二代)大腦的 SD 卡上的檔案中。

  • appendfile — 將使用者提供的緩衝區中的資料追加到 IQ(第二代)大腦 SD 卡上現有檔案的末端。

  • draw_image_from_file — 顯示 SD 卡中的點陣圖影像。

獲取器 — 返回 SD 卡和檔案資訊。

  • is_inserted — 傳回 SD 卡是否已插入 Brain。

  • filesize — 傳回檔案的大小。

  • size — 傳回檔案的大小。

  • exists — 返回 SD 卡上是否找到文件。

行動#

loadfile#

loadfile 將 SD 卡中的檔案載入到位元組數組中。

Usage:
brain.sdcard.loadfile(filename, buffer)

參數

描述

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)

參數

描述

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)

參數

描述

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 大小。

注意: IQ(第二代)Brain 僅支援顯示 .bmp 檔案。建議使用 8 位元 RLE 編碼以減少檔案大小。

Usage
brain.screen.draw_image_from_file(filename, x, y)

參數

描述

filename

圖片的檔名。

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)

獲取器#

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 傳回檔案的大小(以位元組為單位),以十進位數表示 (float)。

Usage:
brain.sdcard.filesize(filename)

參數

描述

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 傳回檔案的大小(以位元組為單位),以十進位數表示 (float)。

Usage:
brain.sdcard.size(filename)

參數

描述

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)

參數

描述

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)