# 3/21 Homework ## EZ_START kIT+ ESP32 OLED中文字 ### 作業流程 採用陳文盛老師提供的連結中的內容並在此之上進行操作,結合chatgpt的使用來完成此作業。 [https://hackmd.io/@PaoyungChang/mpy_ming_py_prelude](https://) [https://hackmd.io/@PaoyungChang/mpy_ming_py_march](https://) 作業流程大體上分為幾個步驟: 1.調整格式並提取ming.py中所需要的文字對應的ASCII編碼產生新文件。 2.下載chen_ming.py等文件並儲存於esp32中,依照連結文章的內容操作。 3.調整連結文章中的程式碼使其可以實際運用在手頭上的這台機器中(連結文章是採用SPI而非I2C)。 ### 程式碼 1.提取所需之中文字 [https://colab.research.google.com/drive/1UX6-iciLA66XFhpP6yVWvbEZOaHq-9vC#scrollTo=jXaipisRgMz-](https://) ``` from ming import chinese new_chinese = {} for k, v in chinese.items(): if k.startswith('0x'): new_key = chr(int(k, 16)) else: new_key = k new_chinese[new_key] = v word_list = '系級學號:41087教行姓名劉志軒' words = {} for each in word_list: words[each] = new_chinese[each] with open('myfont.py', 'w') as f: f.write('words=') f.write(str(words)) ``` 2.新文件內容(myfont) ``` words={ '系': (0, 0, 192, 0, 64, 2, 48, 1, 142, 12, 64, 4, 240, 3, 128, 8, 96, 16, 248, 63, 0, 33, 0, 1, 48, 9, 16, 49, 200, 97, 132, 64), '級': (0, 0, 24, 16, 136, 63, 4, 17, 52, 17, 18, 9, 30, 73, 8, 255, 36, 69, 114, 69, 78, 41, 0, 41, 74, 17, 146, 40, 148, 68, 68, 130), '學': (0, 0, 168, 34, 24, 121, 136, 34, 56, 56, 136, 34, 56, 57, 136, 34, 252, 255, 4, 64, 226, 47, 0, 4, 252, 127, 0, 2, 128, 3, 0, 1), '號': (0, 0, 36, 4, 60, 60, 164, 68, 164, 255, 188, 72, 164, 56, 128, 14, 254, 8, 132, 240, 188, 0, 164, 54, 162, 18, 160, 146, 92, 146, 72, 225), ':': (0, 0, 0, 0, 0, 0, 24, 24, 0, 0, 0, 0, 24, 24, 0, 0), '4': (0, 0, 32, 48, 48, 40, 40, 36, 36, 34, 126, 32, 32, 32, 0, 0), '1': (0, 0, 16, 24, 16, 16, 16, 16, 16, 16, 16, 16, 16, 56, 0, 0), '0': (0, 0, 24, 36, 66, 66, 66, 66, 66, 66, 66, 66, 36, 24, 0, 0), '8': (0, 0, 24, 36, 66, 66, 36, 24, 36, 66, 66, 66, 36, 24, 0, 0), '7': (0, 0, 126, 66, 66, 32, 32, 32, 16, 16, 16, 8, 8, 8, 0, 0), '教': (0, 0, 48, 24, 16, 11, 124, 9, 144, 8, 80, 252, 254, 69, 16, 68, 248, 74, 68, 40, 34, 40, 224, 17, 62, 16, 34, 40, 56, 68, 16, 131), '行': (0, 0, 48, 32, 144, 127, 8, 0, 100, 0, 34, 64, 144, 255, 24, 16, 20, 16, 18, 16, 16, 16, 16, 16, 16, 16, 16, 18, 16, 28, 16, 8), '姓': (0, 0, 24, 24, 8, 11, 72, 9, 126, 41, 72, 127, 76, 9, 68, 9, 162, 8, 38, 40, 40, 127, 16, 8, 40, 8, 40, 8, 4, 72, 130, 255), '名': (0, 0, 128, 1, 128, 16, 192, 63, 32, 8, 80, 4, 140, 2, 128, 1, 64, 32, 224, 127, 56, 32, 38, 32, 32, 32, 32, 32, 224, 63, 32, 32), '劉': (0, 0, 52, 128, 204, 135, 132, 180, 148, 148, 76, 148, 36, 147, 208, 144, 8, 147, 248, 146, 38, 144, 252, 131, 32, 129, 164, 144, 232, 227, 62, 64), '志': (0, 0, 0, 3, 0, 1, 0, 65, 254, 255, 0, 1, 0, 17, 248, 63, 0, 0, 0, 1, 96, 6, 36, 36, 36, 64, 38, 208, 34, 144, 192, 31), '軒': (0, 0, 48, 64, 16, 254, 254, 17, 16, 16, 252, 16, 148, 16, 252, 80, 148, 254, 252, 16, 148, 16, 16, 16, 254, 17, 16, 16, 16, 16, 16, 16)} ``` 3.調整過後之程式碼 ``` # 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 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 = False # 指定 hardware SPI/IIC or software SPI/IIC useSoft = True 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 = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000) # 使用硬件 I2C 初始化,并指定引脚和频率 # 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) # buf1 = [words[each] for each in '系級:教行4'] buf2 = [words[each] for each in '學號:410887040'] buf3 = [words[each] for each in '姓名:劉志軒'] disp.write_font(buf1, 0, 0) disp.write_font(buf2, 0, 20) disp.write_font(buf3, 0, 40) disp.show() if __name__ == '__main__': demo() ```