from machine import I2C, Pin
from ssd1306 import SSD1306_I2C
import utime

oled = None

def check():
    global oled
    while True:
        i2c = I2C(scl=Pin(5), sda=Pin(4))
        addr = i2c.scan()
        if len(addr):
            oled = SSD1306_I2C(128, 64, i2c, addr=addr[0])
            break
        utime.sleep(1)
        print('connect oled module to esp8266')


fonts={
"王":[0x00,0x7F,0x04,0x04,0x04,0x04,0x7F,0x04,0x04,0x04,0x04,0xFF,0x00,0xC0,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,0x00,0x00,0xE0], #王",0
"静":[0x22,0xFB,0x24,0x79,0x27,0xF9,0x57,0x71,0x57,0x71,0x51,0x53,0x00,0x80,0x80,0x00,0xC0,0x40,0xE0,0x40,0xC0,0x40,0x00,0x00], #静",1
}




def chinese(ch_str, x_axis, y_axis, ch_size=12):
    ''' 刷单字到屏幕像素点
    Args:
        ch_str  单字或连接汉字
        x_axis,y_axis  定位点
        ch_size  单字大小,默认12,最大16
    '''
    global oled
    offset_ = 0
    for k in ch_str:
        byte_data = fonts[k]
        print(fonts[k], "offset=", offset_)
        for y in range(0, ch_size):
            # 进制转换、补全
            a_ = '{:0>8b}'.format(byte_data[y])
            b_ = '{:0>8b}'.format(byte_data[y+ch_size])
            # 绘制像素点 (按取模软件的行列式方式)
            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_ += ch_size

check()
chinese('王',0,0)
chinese('静',0,20)
oled.show()