Try   HackMD

陳大的 ming.py 試完筆記

tags: 字庫 MicroPython ESP32

PaoyungJun 08, 2022

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

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

前言

ming.py 是陳大分享的明體字庫模組,適合 Python 的資料型態使之能輕鬆取字,是目前我遇過最容易操作、最適合來產生適用於 MicroPython 的中文字檔。此字檔之智權為陳大,因時機之故因此前篇僅展示取字及顯示之結果,此篇為後續顯示之實作。

Paoyung ChangSun, Jun 12, 2022 9:31 PM

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 →
陳瑞隆大大已釋出完整的取字和秀字、秀圖程式喔!請到共學筆記中找找。
🙇 以下是我個人撰寫的版本。


秀出來

我己將秀出中文的方法寫在 chen_ming.py 模組中,它是以 SSD1306 模組來擴充,不影嚮原模組的操作,如果您已經閱覽 「陳大的 ming.py 試玩筆記」,也知道如何操作讓 SSD1306 跑出字來,那請瞧瞧 write_font 這個 function 即可。

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 →

# name: chen_ming.py, version: 0.1.1, author: paoyung.chang@gmail.com
# Copyright (c) 2022 Paoyung Chang. See the file LICENSE for copying permission.
# License link: https://gist.github.com/paoyung/7e465ad984a6cf24024508831ec54516
import ssd1306
from framebuf import FrameBuffer, MONO_HMSB
def write_font(display, wordsBuf, x, y, invert=False):
for buf in wordsBuf:
if invert: # 反白
buf = [b^0xff for b in buf]
width = len(buf) // 2 #判斷字寬
fbuf = FrameBuffer(bytearray(buf), width, 16, MONO_HMSB)
display.blit(fbuf, x, y, 0)
x += width
if x > 128:
break
class SSD1306_I2C(ssd1306.SSD1306_I2C):
def __init__(self, width, height, i2c, addr=0x3C, external_vcc=False):
super().__init__(width, height, i2c, addr, external_vcc)
def write_font(self, wordsBuf, x, y, invert=False):
write_font(self, wordsBuf, x, y, invert)
class SSD1306_SPI(ssd1306.SSD1306_SPI):
def __init__(self, width, height, spi, dc, res, cs, external_vcc=False):
super().__init__( width, height, spi, dc, res, cs, external_vcc)
def write_font(self, wordsBuf, x, y, invert=False):
write_font(self, wordsBuf, x, y, invert)
view raw chen_ming.py hosted with ❤ by GitHub

若想在載入 SSD1306 的同時就可以秀出中文,可用上述 chen_ming.py 來替代 ssd1306.py,但必須和 ssd1306.py 同時存於 MicroPython 中,請參考以下範例 demo 的內容。

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 →

ssd1306.py, chen_ming.py, myfont.py 需先上傳至 MicroPython。

# name: chen_ming_demo.py, version: 0.1.0, author: paoyung.chang@gmail.com
# Copyright (c) 2022 Paoyung Chang. See the file LICENSE for copying permission.
# License link: https://gist.github.com/paoyung/7e465ad984a6cf24024508831ec54516
from machine import Pin, I2C, SoftI2C, SPI, SoftSPI
import chen_ming as ssd1306 # <= 代替 import ssd1306
import time
import framebuf
from myfont import words
def get_display():
# 指定 IIC or SPI
useSPI = True
# 指定 hardware SPI/IIC or software SPI/IIC
useSoft = False
if useSPI:
dc = Pin(4) # data/command
rst = Pin(5) # reset
cs = Pin(15) # chip select, some modules do not have a pin for this
if useSoft:
spi = SoftSPI(baudrate=500000, polarity=1, phase=0,
sck=Pin(14), mosi=Pin(13), miso=Pin(12))
else:
spi = SPI(1) # sck=14 (scl), mosi=13 (sda), miso=12 (unused)
display = ssd1306.SSD1306_SPI(128, 64, spi, dc, rst, cs)
else:
if useSoft:
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000)
else:
# I2C(0, scl=22, sda=21, freq=400000)
# I2C(1, scl=25, sda=26, freq=400000)
i2c = I2C(0)
# using default address 0x3C
display = ssd1306.SSD1306_I2C(128, 64, i2c)
#display = ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3d)
return display
def demo():
disp = get_display()
disp.invert(0)
disp.rotate(True)
#
buf1 = [words[each] for each in 'MicroPy同學會']
buf2 = [words[each] for each in '感謝陳老師分享']
disp.write_font(buf1, 12, 10)
disp.write_font(buf2, 8, 30)
disp.show()

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 →

MicroPython程式語言