esp32 +oled显示英文、中文

from machine import Pin, I2C
from oled import SSD1306_I2C
# 定义管脚
scl = Pin(22)
sda = Pin(21)
# 初始化
i2c = I2C(scl=scl, sda=sda)
# 像素 128*64
oled = SSD1306_I2C(128, 64, i2c)
# 清屏
oled.fill(0)
oled.show()

# 显示英文,从0,0像素开始
oled.text('hello', 0, 0)
oled.show()

# 以下为显示中文,重新显示需要清屏。fonts字体为自定义取模。
# 字索引为utf-8 16进制
fonts= {
    # 你
    0xe4bda0:
[0x11,0x11,0x11,0x23,0x22,0x64,0xA8,0x20,0x21,0x21,0x22,0x24,0x20,0x20,0x21,0x20,
0x00,0x00,0x00,0xFC,0x04,0x08,0x40,0x40,0x50,0x48,0x4C,0x44,0x40,0x40,0x40,0x80],
    # 好
    0xe5a5bd:
[0x10,0x11,0x10,0x10,0xFC,0x24,0x24,0x27,0x24,0x44,0x28,0x10,0x28,0x44,0x84,0x00,
0x00,0xFC,0x04,0x08,0x10,0x20,0x24,0xFE,0x20,0x20,0x20,0x20,0x20,0x20,0xA0,0x40]
    }

def chinese(ch_str, x_axis, y_axis): 
   offset_ = 0 
   y_axis = y_axis*8  # 中文高  
   x_axis = (x_axis*16)  # 中文宽
   for k in ch_str:
       code = 0x00  # 中文转成16进制编码 
       data_code = k.encode("utf-8")
       code |= data_code[0] << 16 
       code |= data_code[1] << 8
       code |= data_code[2]
       byte_data = fonts[code]
       for y in range(0, 16):
           a_ = bin(byte_data[y]).replace('0b', '')
           while len(a_) < 8:
               a_ = '0'+a_
           b_ = bin(byte_data[y+16]).replace('0b', '')
           while len(b_) < 8:
               b_ = '0'+b_
           for x in range(0, 8):
               oled.pixel(x_axis+offset_+x, y+y_axis, int(a_[x]))   
               oled.pixel(x_axis+offset_+x+8, y+y_axis, int(b_[x]))   
       offset_ += 16
       
chinese('你好',0,0)
oled.show()

 

你可能感兴趣的