from machine import Pin, I2C # 导入PIN和I2C
from ssd1306 import SSD1306_I2C # 导入屏幕驱动
#OLED=....
i2c = I2C(scl=Pin(22), sda=Pin(21)) # 创建I2C对象
OLED= SSD1306_I2C(128, 64, i2c) # 创建OLED对象
#fonts=....字使用字典,字典的key来放字的编码的十六进制,键来放字模
fonts= {
0xE5A5BD:
[0x00,0x04,0x08,0x10,0x20,0xFF,0x11,0x12,0x14,0x18,0x24,0x42,0x00,0x00,0x00,0x00,
0x00,0x00,0xF8,0x10,0x20,0xF8,0x40,0x20,0x10,0x08,0x28,0x10,0x00,0x00,0x00,0x00], # 好
0xE4BABA:
[0x00,0x01,0x01,0x01,0x01,0x02,0x04,0x08,0x10,0x20,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x00,0x00,0x00,0x00], # 人
0xE5A49A:
[0x00,0x02,0x04,0x0F,0x18,0x25,0x02,0x0D,0x12,0x04,0x00,0x01,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x80,0xA0,0x40,0xF8,0x08,0xD0,0x20,0x40,0x80,0x00,0x00,0x00,0x00], # 多
}
# 顶替中文函数部分
def chinese(ch_str, x_axis, y_axis): # 需要显示的中文,x轴的开始位置,y轴的开始位置
offset_ = 0 # 偏移量设为0,也可以设为其他值,俺需要修正
for k in ch_str: # for循环去除每个字
code = 0x00 # 将中文转成16进制编码
data_code = k.encode("utf-8") # 编码为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('好人多', 35, 4) # 需要显示的中文
OLED.show() # 显示
OLED.text('welcome to china', 0, 32) # 需要显示的英文
OLED.show() # 显示