SD卡#
介紹#
使用 VEX EXP 的 SD 卡,您可以將檔案儲存在插入 Brain 的 SD 卡插槽中的可移動 SD 卡上,並進行存取。無論是保存感測器資料還是載入自訂影像,這些方法都能讓您完全控製檔案操作。可移動 SD 卡也方便您在 Brain 和電腦之間傳輸資料。
EXP Brain 需要一張容量不超過 32GB 的 SD 卡,格式為 FAT32。容量大於 32GB 的 SD 卡預設使用 exFAT 格式,這與 EXP Brain 不相容。請確保您的 SD 卡在使用前已正確格式化為 FAT32。
所有檔案必須放置在SD卡的根資料夾中才能存取。
對於以下範例,建構的 Brain 包括對 SD 卡方法的訪問,並且在本 API 文件中所有後續範例中引用這些方法時都將使用它。
以下是所有方法的清單:
操作 — 執行檔案操作並顯示 SD 卡中的影像。
loadfile— 將 SD 卡中的檔案內容載入到使用者提供的記憶體緩衝區中。savefile— 將使用者提供的緩衝區中的資料儲存到 EXP Brain 的 SD 卡上的檔案中。appendfile— 將使用者提供的緩衝區中的資料追加到 EXP Brain 的 SD 卡上現有檔案的末端。draw_image_from_file— 顯示 SD 卡中的點陣圖影像。
獲取器 — 返回 SD 卡和檔案資訊。
is_inserted— 傳回 SD 卡是否已插入 Brain。filesize— 傳回檔案的大小。size— 傳回檔案的大小。exists— 返回 SD 卡上是否找到文件。
行動#
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 大小。
注意: EXP Brain 僅支援顯示 .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 傳回檔案的大小(以位元組為單位),以十進位數表示 (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 傳回檔案的大小(以位元組為單位),以十進位數表示 (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 傳回一個布林值,指示是否在 SD 卡上找到檔案。
True— 檔案在 SD 卡上。False— 檔案不在 SD 卡上。
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)