Try   HackMD

陳大 ming.py ESP32 字量極限

tags: 字庫 | ESP32 | MicroPython

PaoyungJun 15, 2022

ℳ𝒾𝒸𝓇ℴ𝒫𝓎𝓉𝒽ℴ𝓃 隨手記

相關主題 👉 微控制器上字庫的選擇

Pinyin

在玩轉了幾回陳大 ming.py 後,突然想起之前的某個 POC 要顯示台灣 368 個行政區名稱和郵遞區號,本來打算要用 SSD1306 來顯示中文,但因為不想把手上僅剩不多的字庫 IC 給出去,索性將其換成 LCD1602,中文就轉成拼音(pinyin)。而現在有了字庫檔,於是重新思考該如何將其應用在類似的需求上了。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

使用字庫時需留意其授權之使用範圍,並依規定使用。
明體中文字庫前情提要: 陳大的 ming.py 試玩筆記 | 陳大的 ming.py 試完筆記

字量

首先要考量的是台灣 368 行政區加上郵遞區號會用上多少字呢? 出乎意料的只有 326 個字,加上額外的英文、數字及符號也才 388 個 ,於是馬上做出字庫檔來試試看。

>>> 
MicroPython v1.18 on 2022-01-17; ESP32 module with ESP32
Type "help()" for more information.
>>> from ming_ac368 import words
>>> len(words)
388
>>> # 字庫字量為 388
>>> for each in '台北桃園新竹苗栗台中南投嘉義雲林高雄台南':
    print(words[each])
    
b'\x00\x00\x80\x01\x80.....\x10\xf0\x1f\x10\x10'
b'\x00\x00\xc0\x06@\x02....\x02\xc0BpB\x0e\xc2\x04|'
b'\x00\x00\x18\n\x08\nH....\t\x08\x89\x88\x88H\xf0'
b':::::::'
b'\x00\x00\x18\n\x08\x12H\.....\xfaR\x92\xfe\x02\x02'
b'\x00\x00\x80\x01\x80.....\x10\x10\xf0\x1f\x10\x10'
b'\x00\x00\x00\x03\x00A.....\xe4O\x04A\x04A\x04q\x04 '
>>> 

載入字檔 OK、上網也正常,這結果真是令人興奮,即代表之前的 POC 使用這個字庫檔是可以勝任的。但同時也再萌生了另一個想法,即 ESP32 可以載入的字量極限是多少呢?

挑戰

為方便測試可達字量,除了上述的台灣 368 字檔外,另外製作了不同字量的字庫檔以利測試,字檔內容以台灣 368 為基底,再從教育部 4808 常用字中隨機抽取以補足至設定之字量。測試程序如下:

  1. 載入字庫檔。
  2. 連上 WiFi。
  3. 至網站 A 讀取資料:從 ifconfig.co 網站取得 IP Address,資料為少量文字資料。
  4. 至網站 B 讀取資料:從環保署使用 API 取得空氣品質數據(aqx_p_432),資料為 json 格式資料。
  5. 在 SSD1306 上顯示出數據。

使用 ESP32 + MicroPython v1.18。
輸出之字庫檔有經過再處理,請參閱 陳大的 ming.py 試玩筆記

測試

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
第一回合

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
功能正常      
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
失敗      
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
未測

字量 載入字庫 連上 WiFi 網站A 網站B SSD1306
台灣368
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
500
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
1000
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
1500
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

此回合的重點在於找出大概的區間,於 1500 字時載入即以記憶體不足之錯誤訊息作收。而在 1000 字時尚能瀏覽少量資料的網站,但當訪問到環保署 OpenData 之類較多資料的網站時也已招架不住,故下回合要往下做調整。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
第二回合

字量 載入字庫 連上 WiFi 網站A 網站B SSD1306
950
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
960
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
975
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

第二回合 600 至 900 的區段皆通過測試,直至 950 左右才出現分岐點,最後測得安全的字量落在 950~960 左右,代表若有較多記憶體的需求,則需酙酌字數的調整,但整體來說,已能滿足一般的應用。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
第三回合
上兩回合使用的是常見的 ESP32 WROOM 版本,而市面上的 ESP32 還有一個 WROVER 的版本,它與一般的 ESP32 不同之處在於加載了 8MB 的 SPI PSRAM。

常見的ESP32

MicroPython v1.18 on 2022-01-17; ESP32 module with ESP32
Type "help()" for more information.
>>> from micropython import mem_info
>>> mem_info()
stack: 672 out of 15360
GC: total: 111168, used: 2528, free: 108640
 No. of 1-blocks: 22, 2-blocks: 8, max blk sz: 32, max free sz: 6776
>>>

ESP32 with SPIRAM

MicroPython v1.18 on 2022-01-17; ESP32 module (spiram) with ESP32
Type "help()" for more information.
>>> from micropython import mem_info
>>> mem_info()
stack: 704 out of 15360
GC: total: 4098240, used: 1568, free: 4096672
 No. of 1-blocks: 15, 2-blocks: 8, max blk sz: 18, max free sz: 255999
>>> 

由上可輕易的看出兩者之間記憶體有明顯的差別,而這回合要試探具備 SPIRAM 的 ESP32 能承受的字量又是多少呢?

字量 載入字庫 連上 WiFi 網站A 網站B SSD1306
1000
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
2000
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
3000
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
4000
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
4903
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

字量 4903 字庫為教育部 4808 常用字加上英數字符號 95 個集合所成。

看起來很完美吧!至此已經達到非常驚人的字數量

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
,但為何不再繼續往上測試呢? 因為光是載入 4903 字庫就要花費約 25 秒的時間,除非有特殊的使用需求,不然這樣的用法並不符合實用性
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
,所以就在這個常用字加英數符號的 4903 字庫劃下了句點。載入字庫所需耗費的時間記錄如下:

字庫檔 台灣368 1000 2000 3000 4000 4903
秒數 0.8 2.4 6.4 11.7 18.3 25.5

秒數為手按碼表測得之數據,非精確數值,僅供參考。

結論

對於擁有大容量的 ESP32 來說,字庫檔不失為一個在有限字量需求下解決中文顯示的好方法,雖然 WROVER 版本的 ESP32 能夠承載更多的字量,但執行速度的損耗使其並不具備絕對的優勢,亦或使用字庫 IC 更能取得平衡而達到較好的使用者體驗,希望本篇內容對有字庫需求的同好在進行評估時能有一些幫助。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

使用字庫時需留意其授權之使用範圍,並依規定使用。

關於作者  

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

我是 𝙋𝙖𝙤𝙮𝙪𝙣𝙜,是 MicroPythonEspruino 的愛好者,一直認為:

「Maker 應該把重心放在應用上,而不該被語言工具限制了創意」

所以想藉由較為簡捷且詳細的解說讓更多人可以學習進而自由發揮,而非淪為只能將程式碼複製貼上的複製人,如果你認同這樣的理念,請一起為此目標努力,如果我的文章內容對你有幫助,請轉發或協助需要的人。

若有專案需求,請利用 連絡我,謝謝!