上傳檔案到micro:bit

在這裡講的不是把我們編寫好的MicroPython程式編譯成HEX檔案之後再上傳到micro:bit上執行,而是把一些其它的檔案,包括程式檔案、圖形檔案、或是資料檔案上傳到micro:bit上以備程式之需。

在之前的教學中,我們做的都是編寫micro:bit板子上唯一要被執行的程式腳本,然而,在一些程式執行的過程中可能會額外用到其它的資源檔案,或是引入其他人寫好的程式模組,此時就需要以另外的方式把這些會用到的資源檔、資料檔、或是模組程式檔案上傳。這些檔案可以選擇在 Mu Editor中的檔案功能來檢視及管理在micro:bit上的檔案,或是利用Python模組microfs來操作。先來說明後者,首先,請先在自己的電腦中的Python環境裡執行以下的安裝程序:

pip install microfs

順利安裝完成之後,即可利用ufs指令列示出目前在micro:bit上的所有的檔案:

ufs ls

如果你輸入ufs而不加上任何參數的話,會有如下所示的使用說明:

c:\>ufs
usage: ufs [-h] [command] [path] [target]

Interact with the basic filesystem on a connected BBC micro:bit device. You
may use the following commands: 'ls' - list files on the device (based on the
equivalent Unix command); 'rm' - remove a named file on the device (based on
the Unix command); 'put' - copy a named local file onto the device just like
the FTP command; and, 'get' - copy a named file from the device to the local
file system a la FTP. For example, 'ufs ls' will list the files on a connected
BBC micro:bit.

positional arguments:
  command     One of 'ls', 'rm', 'put' or 'get'.
  path        Use when a file needs referencing.
  target      Use to specify a target filename.

optional arguments:
  -h, --help  show this help message and exit

從上面的說明可以看到,它提供了ls、rm、put、以及get等四個命令,其中 ls用來列出所有在micro:bit上的檔案;rm用來刪除在micro:bit上的檔案;put可以把目前資料夾中的檔案上傳到micro:bit;get則是把檔案從micro:bit中取出。假設我們要上傳的檔案名稱是ssd1306.py,則上傳的方式如下:

C:\>ufs put ssd1306.py

請留意,上述的指令中,C:\>是命令提示字元,不需要輸入喔。

另外一個操作 micro:bit檔案的方式是利用Mu Editor的File(文件)功能。當你如下圖箭頭所指的地方按下File(文件)按鈕時:

在編輯器的下方即出現了兩個額外的視窗,左側的內容即為micro:bit上除了主程式之外的檔案,而右側則是在你的電腦中位於mu_code資料夾之下的所有檔案,你可以在兩個視窗間拖曳檔案,即可執行檔案上傳或下載的作業。

Last updated