ming.py
試玩筆記字庫
MicroPython
ESP32
PaoyungJun 05, 2022
ℳ𝒾𝒸𝓇ℴ𝒫𝓎𝓉𝒽ℴ𝓃 隨手記Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
相關主題 👉 微控制器上字庫的選擇
因為陳大昨日(2022/06/05)的線上同學會來不及上線,因此這個明體中文字庫正確用法還沒公佈,或許已經有配套的工具和套件了。而我礙於撿到寶心急先做了一番測試。待陳大下週講解教學後,如有需要我再把一些內容記錄出來。也想要試試的人可以到 LINE 群組或共學筆記中尋找 ming.py
這個檔回來玩玩。
一般在 MCU 上若沒有合適的工具或套件,想要顯示中文不能算是個簡單任務,在 Arduino 社群間的做法就是使用 u8g2,益師父已經做過許多的教學,不然傑森創工也有詳細的解說,而黑大還寫了自動化工具。
之前曾提過我其實並不熟悉 Arduino IDE 的生態圈,反而喜歡在非 C/C++ 的語言間遊走,最常用的是 MicroPython 和 Espruino,在某次為了 Espruino 而購買 STM32F103RCT6 開發板時,賣家說開發板上的 W25 帶有字庫,然後從賣家給的文件發現那顆 Flash 其實是 Clone 了 GT30L24T3Y 這顆字庫 IC,因為它包含了不同大小的字型、以及繁體和簡體,可用 GB2312、GB12345、Big5 和 Unicode 來取碼,所以取字過程有點小複雜,不過使用它就不需要事先取字,隨時可以從它讀取出來,且這顆 IC 還非常的便宜,想說需要時掛上即可,因此就沒再找別的方案。
- 順道一提: ESP32 上的 4MB 也是來自於 W25
- GT30L24T3Y 文件
- GT30L24T3Y 價格
而前幾天在 LINE 群組 益師父 MicroPython 同學會 的討論中,陳大放出一個 ming.py
明體中文字庫模組,和字庫奮鬥過的人一看到這個簡直是挖到寶了,以下就來探索一下它的內容。
它是一個有點大的 Python dictionary (28922 items),初步觀察 key 值應該與內碼相關。首先來觀察第1筆,key '0x0020' 的內容是一整排的 0x00,以 bitmap 的觀點來看它就是個空白,而 ASCII 的 0x20 即是空白,這點是契合的。
>>> chr(int('0x0020'))
' '
>>>
照這個邏輯,key '0x0031' 不就該是 ASCII 的 1 嘛!要驗證這想法沒錯,就把數字 1 和英文的大寫 I、小寫 l 和符號 | 都取出來比對一下,若長得都有幾分像就代表思考的方向是正確的。
先找出對應的值:
>>> hex(ord('1'))
'0x31'
>>> hex(ord('I'))
'0x49'
>>> hex(ord('l'))
'0x6c'
>>> hex(ord('|'))
'0x7c'
>>>
以 bitmap 的觀點來看,中間段都是一個點連通的,所以方向正確,繼續 Go
再則中文應該都是以 unicode 來表示,而 key '0x0031'、'0x6211'…這樣來取中文字有點不順手,於是我有個大膽的想法,該不會 key 值就是 unicode 的值吧! 速速把 key 值前面的 '0x' 都換成 '\u' 來進行測試。
>>> print('\u0031', '\u0049', '\u006c', '\u007c')
1 I l |
>>> print('\u6211', '\u6709', '\u500b', '\u5927', '\u81bd', '\u7684', '\u60f3', '\u6cd5')
我 有 個 大 膽 的 想 法
沒錯,一旦的把 key 值換成了 unicode type,就可以用 chinese['中']、chinese['文'] 的方式來取字,這實在是太方便了。
Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
ming.py
對 MicroPython 來說檔案太大了,取字是用 Python 來操作。
Python 3.8.12 (default, Nov 26 2021, 23:35:35)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ming import chinese
>>> len(chinese)
28922
>>> # 有 28922 個字
>>> chinese['\u5bf6']
(0, 0, 4, 65, 252, 255, 4, 66, 250, 62, 32, 9, 248, 126, 32, 42, 252, 63, 16, 16, 240, 31, 16, 16, 240, 31, 16, 16, 240, 31, 28, 112)
>>> chinese['寶']
(0, 0, 4, 65, 252, 255, 4, 66, 250, 62, 32, 9, 248, 126, 32, 42, 252, 63, 16, 16, 240, 31, 16, 16, 240, 31, 16, 16, 240, 31, 28, 112)
>>> chinese['\u5bf6'] == chinese['寶']
True
>>> #取出內容相符
用 unicode 格式或直接輸入中文都是可以的。Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
整個字檔有 28922 個字,標準做法就是把需要的字取出另存檔案,再提供給微處理器使用,而取字用 Python 來做很輕鬆,不出幾行即可完成,日後寫成 function 就只要指定字串和檔名就完工了。
>>> # 把會用到的字先列進來
>>> word_list = '我有個大膽的想法MicroPython同學會'
>>> words = {}
>>> for each in word_list:
... words[each] = chinese[each]
...
>>> # 檢查看看
>>> words
{'我': (0, 0, 0, 12, 224, 36, 60, '...'), ('balah balah...太長了..省略...')}
>>> # 寫成檔案
>>> with open('myfont.py', 'w') as f:
... f.write('words=')
... f.write(str(words))
...
6
2109
>>>
而這個產生出來的 myfont.py
直接上傳到 MicroPython 就可供 import 來調用,字數如果較多可以再把 .py 編為 .mpy,其大小能再大幅縮減,若是以 bytes 來存,縮小的範圍會更大,在此先暫不細說,以下的檔案功能和效果是一樣的,但大小差了將近三倍。
(mpfs) ➜ MicroPythonChsFont ls -al myfont*.*
-rw-r--r-- 1 oholiab oholiab 739 6月 6 15:21 myfont_c.mpy
-rw-r--r-- 1 oholiab oholiab 1710 6月 6 15:15 myfont_c.py
-rw-r--r-- 1 oholiab oholiab 1093 6月 6 15:19 myfont.mpy
-rw-r--r-- 1 oholiab oholiab 2137 6月 6 12:48 myfont.py
(mpfs) ➜ MicroPythonChsFont
>>> # 這是 MicroPython
>>> # import 先前取出的字檔 myfont, 我們存成名為 words 的 dict
>>> from myfont import words
>>> # 取得 SSD1306 device
>>> disp = get_display()
>>> # 轉換成 list
>>> buf1 = [words[each] for each in 'MicroPy同學會']
>>> buf2 = [words[each] for each in '我有個大膽的想法']
>>> writeFont(disp, buf1, 12, 10)
116
>>> writeFont(disp, buf2, 0, 30)
128
>>> show(disp)
>>>
打完收工。
Paoyung ChangSat, Jun 11, 2022 11:49 PM
[續篇] 陳大的 ming.py 試完筆記Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported