树莓派pico驱动SSD1306显示英文和中文

树莓派pico驱动SSD1306显示英文和中文

Misery can be caused by someone being just weak and indecisive.
一个人仅仅因为软弱无能或优柔寡断就完全可能招致痛苦

前期准备

如果你还不会搭建开发环境,请看我这篇文章

1.购买配件(树莓派pico w,OLED屏幕)

2.接线

3.编写代码

接线

本次代码引脚用的是GP21,GP20,请看接线图,图中Ground代表着树莓派pico上的GND

20240318160243149-image

编写代码

创建库文件ssd1306.py

以下代码保存为ssd1306.py,然后上传到pico里

# MicroPython SSD1306 OLED driver, I2C and SPI interfaces
from micropython import const
import framebuf
# register definitions
SET_CONTRAST = const(0x81)
SET_ENTIRE_ON = const(0xA4)
SET_NORM_INV = const(0xA6)
SET_DISP = const(0xAE)
SET_MEM_ADDR = const(0x20)
SET_COL_ADDR = const(0x21)
SET_PAGE_ADDR = const(0x22)
SET_DISP_START_LINE = const(0x40)
SET_SEG_REMAP = const(0xA0)
SET_MUX_RATIO = const(0xA8)
SET_IREF_SELECT = const(0xAD)
SET_COM_OUT_DIR = const(0xC0)
SET_DISP_OFFSET = const(0xD3)
SET_COM_PIN_CFG = const(0xDA)
SET_DISP_CLK_DIV = const(0xD5)
SET_PRECHARGE = const(0xD9)
SET_VCOM_DESEL = const(0xDB)
SET_CHARGE_PUMP = const(0x8D)
# Subclassing FrameBuffer provides support for graphics primitives
# http://docs.micropython.org/en/latest/pyboard/library/framebuf.html
class SSD1306(framebuf.FrameBuffer):
def __init__(self, width, height, external_vcc):
self.width = width
self.height = height
self.external_vcc = external_vcc
self.pages = self.height // 8
self.buffer = bytearray(self.pages * self.width)
super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB)
self.init_display()
def init_display(self):
for cmd in (
SET_DISP, # display off
# address setting
SET_MEM_ADDR,
0x00, # horizontal
# resolution and layout
SET_DISP_START_LINE, # start at line 0
SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0
SET_MUX_RATIO,
self.height - 1,
SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0
SET_DISP_OFFSET,
0x00,
SET_COM_PIN_CFG,
0x02 if self.width > 2 * self.height else 0x12,
# timing and driving scheme
SET_DISP_CLK_DIV,
0x80,
SET_PRECHARGE,
0x22 if self.external_vcc else 0xF1,
SET_VCOM_DESEL,
0x30, # 0.83*Vcc
# display
SET_CONTRAST,
0xFF, # maximum
SET_ENTIRE_ON, # output follows RAM contents
SET_NORM_INV, # not inverted
SET_IREF_SELECT,
0x30, # enable internal IREF during display on
# charge pump
SET_CHARGE_PUMP,
0x10 if self.external_vcc else 0x14,
SET_DISP | 0x01, # display on
): # on
self.write_cmd(cmd)
self.fill(0)
self.show()
def poweroff(self):
self.write_cmd(SET_DISP)
def poweron(self):
self.write_cmd(SET_DISP | 0x01)
def contrast(self, contrast):
self.write_cmd(SET_CONTRAST)
self.write_cmd(contrast)
def invert(self, invert):
self.write_cmd(SET_NORM_INV | (invert & 1))
def rotate(self, rotate):
self.write_cmd(SET_COM_OUT_DIR | ((rotate & 1) << 3))
self.write_cmd(SET_SEG_REMAP | (rotate & 1))
def show(self):
x0 = 0
x1 = self.width - 1
if self.width != 128:
# narrow displays use centred columns
col_offset = (128 - self.width) // 2
x0 += col_offset
x1 += col_offset
self.write_cmd(SET_COL_ADDR)
self.write_cmd(x0)
self.write_cmd(x1)
self.write_cmd(SET_PAGE_ADDR)
self.write_cmd(0)
self.write_cmd(self.pages - 1)
self.write_data(self.buffer)
class SSD1306_I2C(SSD1306):
def __init__(self, width, height, i2c, addr=0x3C, external_vcc=False):
self.i2c = i2c
self.addr = addr
self.temp = bytearray(2)
self.write_list = [b"\x40", None] # Co=0, D/C#=1
super().__init__(width, height, external_vcc)
def write_cmd(self, cmd):
self.temp[0] = 0x80 # Co=1, D/C#=0
self.temp[1] = cmd
self.i2c.writeto(self.addr, self.temp)
def write_data(self, buf):
self.write_list[1] = buf
self.i2c.writevto(self.addr, self.write_list)
class SSD1306_SPI(SSD1306):
def __init__(self, width, height, spi, dc, res, cs, external_vcc=False):
self.rate = 10 * 1024 * 1024
dc.init(dc.OUT, value=0)
res.init(res.OUT, value=0)
cs.init(cs.OUT, value=1)
self.spi = spi
self.dc = dc
self.res = res
self.cs = cs
import time
self.res(1)
time.sleep_ms(1)
self.res(0)
time.sleep_ms(10)
self.res(1)
super().__init__(width, height, external_vcc)
def write_cmd(self, cmd):
self.spi.init(baudrate=self.rate, polarity=0, phase=0)
self.cs(1)
self.dc(0)
self.cs(0)
self.spi.write(bytearray([cmd]))
self.cs(1)
def write_data(self, buf):
self.spi.init(baudrate=self.rate, polarity=0, phase=0)
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(buf)
self.cs(1)
# MicroPython SSD1306 OLED driver, I2C and SPI interfaces
 
from micropython import const
import framebuf
 
# register definitions
SET_CONTRAST = const(0x81)
SET_ENTIRE_ON = const(0xA4)
SET_NORM_INV = const(0xA6)
SET_DISP = const(0xAE)
SET_MEM_ADDR = const(0x20)
SET_COL_ADDR = const(0x21)
SET_PAGE_ADDR = const(0x22)
SET_DISP_START_LINE = const(0x40)
SET_SEG_REMAP = const(0xA0)
SET_MUX_RATIO = const(0xA8)
SET_IREF_SELECT = const(0xAD)
SET_COM_OUT_DIR = const(0xC0)
SET_DISP_OFFSET = const(0xD3)
SET_COM_PIN_CFG = const(0xDA)
SET_DISP_CLK_DIV = const(0xD5)
SET_PRECHARGE = const(0xD9)
SET_VCOM_DESEL = const(0xDB)
SET_CHARGE_PUMP = const(0x8D)
 
# Subclassing FrameBuffer provides support for graphics primitives
# http://docs.micropython.org/en/latest/pyboard/library/framebuf.html
class SSD1306(framebuf.FrameBuffer):
    def __init__(self, width, height, external_vcc):
        self.width = width
        self.height = height
        self.external_vcc = external_vcc
        self.pages = self.height // 8
        self.buffer = bytearray(self.pages * self.width)
        super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB)
        self.init_display()
 
    def init_display(self):
        for cmd in (
            SET_DISP,  # display off
            # address setting
            SET_MEM_ADDR,
            0x00,  # horizontal
            # resolution and layout
            SET_DISP_START_LINE,  # start at line 0
            SET_SEG_REMAP | 0x01,  # column addr 127 mapped to SEG0
            SET_MUX_RATIO,
            self.height - 1,
            SET_COM_OUT_DIR | 0x08,  # scan from COM[N] to COM0
            SET_DISP_OFFSET,
            0x00,
            SET_COM_PIN_CFG,
            0x02 if self.width > 2 * self.height else 0x12,
            # timing and driving scheme
            SET_DISP_CLK_DIV,
            0x80,
            SET_PRECHARGE,
            0x22 if self.external_vcc else 0xF1,
            SET_VCOM_DESEL,
            0x30,  # 0.83*Vcc
            # display
            SET_CONTRAST,
            0xFF,  # maximum
            SET_ENTIRE_ON,  # output follows RAM contents
            SET_NORM_INV,  # not inverted
            SET_IREF_SELECT,
            0x30,  # enable internal IREF during display on
            # charge pump
            SET_CHARGE_PUMP,
            0x10 if self.external_vcc else 0x14,
            SET_DISP | 0x01,  # display on
        ):  # on
            self.write_cmd(cmd)
        self.fill(0)
        self.show()
 
    def poweroff(self):
        self.write_cmd(SET_DISP)
 
    def poweron(self):
        self.write_cmd(SET_DISP | 0x01)
 
    def contrast(self, contrast):
        self.write_cmd(SET_CONTRAST)
        self.write_cmd(contrast)
 
    def invert(self, invert):
        self.write_cmd(SET_NORM_INV | (invert & 1))
 
    def rotate(self, rotate):
        self.write_cmd(SET_COM_OUT_DIR | ((rotate & 1) << 3))
        self.write_cmd(SET_SEG_REMAP | (rotate & 1))
 
    def show(self):
        x0 = 0
        x1 = self.width - 1
        if self.width != 128:
            # narrow displays use centred columns
            col_offset = (128 - self.width) // 2
            x0 += col_offset
            x1 += col_offset
        self.write_cmd(SET_COL_ADDR)
        self.write_cmd(x0)
        self.write_cmd(x1)
        self.write_cmd(SET_PAGE_ADDR)
        self.write_cmd(0)
        self.write_cmd(self.pages - 1)
        self.write_data(self.buffer)
 
class SSD1306_I2C(SSD1306):
    def __init__(self, width, height, i2c, addr=0x3C, external_vcc=False):
        self.i2c = i2c
        self.addr = addr
        self.temp = bytearray(2)
        self.write_list = [b"\x40", None]  # Co=0, D/C#=1
        super().__init__(width, height, external_vcc)
 
    def write_cmd(self, cmd):
        self.temp[0] = 0x80  # Co=1, D/C#=0
        self.temp[1] = cmd
        self.i2c.writeto(self.addr, self.temp)
 
    def write_data(self, buf):
        self.write_list[1] = buf
        self.i2c.writevto(self.addr, self.write_list)
 
 
class SSD1306_SPI(SSD1306):
    def __init__(self, width, height, spi, dc, res, cs, external_vcc=False):
        self.rate = 10 * 1024 * 1024
        dc.init(dc.OUT, value=0)
        res.init(res.OUT, value=0)
        cs.init(cs.OUT, value=1)
        self.spi = spi
        self.dc = dc
        self.res = res
        self.cs = cs
        import time
 
        self.res(1)
        time.sleep_ms(1)
        self.res(0)
        time.sleep_ms(10)
        self.res(1)
        super().__init__(width, height, external_vcc)
 
    def write_cmd(self, cmd):
        self.spi.init(baudrate=self.rate, polarity=0, phase=0)
        self.cs(1)
        self.dc(0)
        self.cs(0)
        self.spi.write(bytearray([cmd]))
        self.cs(1)
 
    def write_data(self, buf):
        self.spi.init(baudrate=self.rate, polarity=0, phase=0)
        self.cs(1)
        self.dc(1)
        self.cs(0)
        self.spi.write(buf)
        self.cs(1)
# MicroPython SSD1306 OLED driver, I2C and SPI interfaces from micropython import const import framebuf # register definitions SET_CONTRAST = const(0x81) SET_ENTIRE_ON = const(0xA4) SET_NORM_INV = const(0xA6) SET_DISP = const(0xAE) SET_MEM_ADDR = const(0x20) SET_COL_ADDR = const(0x21) SET_PAGE_ADDR = const(0x22) SET_DISP_START_LINE = const(0x40) SET_SEG_REMAP = const(0xA0) SET_MUX_RATIO = const(0xA8) SET_IREF_SELECT = const(0xAD) SET_COM_OUT_DIR = const(0xC0) SET_DISP_OFFSET = const(0xD3) SET_COM_PIN_CFG = const(0xDA) SET_DISP_CLK_DIV = const(0xD5) SET_PRECHARGE = const(0xD9) SET_VCOM_DESEL = const(0xDB) SET_CHARGE_PUMP = const(0x8D) # Subclassing FrameBuffer provides support for graphics primitives # http://docs.micropython.org/en/latest/pyboard/library/framebuf.html class SSD1306(framebuf.FrameBuffer): def __init__(self, width, height, external_vcc): self.width = width self.height = height self.external_vcc = external_vcc self.pages = self.height // 8 self.buffer = bytearray(self.pages * self.width) super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB) self.init_display() def init_display(self): for cmd in ( SET_DISP, # display off # address setting SET_MEM_ADDR, 0x00, # horizontal # resolution and layout SET_DISP_START_LINE, # start at line 0 SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0 SET_MUX_RATIO, self.height - 1, SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0 SET_DISP_OFFSET, 0x00, SET_COM_PIN_CFG, 0x02 if self.width > 2 * self.height else 0x12, # timing and driving scheme SET_DISP_CLK_DIV, 0x80, SET_PRECHARGE, 0x22 if self.external_vcc else 0xF1, SET_VCOM_DESEL, 0x30, # 0.83*Vcc # display SET_CONTRAST, 0xFF, # maximum SET_ENTIRE_ON, # output follows RAM contents SET_NORM_INV, # not inverted SET_IREF_SELECT, 0x30, # enable internal IREF during display on # charge pump SET_CHARGE_PUMP, 0x10 if self.external_vcc else 0x14, SET_DISP | 0x01, # display on ): # on self.write_cmd(cmd) self.fill(0) self.show() def poweroff(self): self.write_cmd(SET_DISP) def poweron(self): self.write_cmd(SET_DISP | 0x01) def contrast(self, contrast): self.write_cmd(SET_CONTRAST) self.write_cmd(contrast) def invert(self, invert): self.write_cmd(SET_NORM_INV | (invert & 1)) def rotate(self, rotate): self.write_cmd(SET_COM_OUT_DIR | ((rotate & 1) << 3)) self.write_cmd(SET_SEG_REMAP | (rotate & 1)) def show(self): x0 = 0 x1 = self.width - 1 if self.width != 128: # narrow displays use centred columns col_offset = (128 - self.width) // 2 x0 += col_offset x1 += col_offset self.write_cmd(SET_COL_ADDR) self.write_cmd(x0) self.write_cmd(x1) self.write_cmd(SET_PAGE_ADDR) self.write_cmd(0) self.write_cmd(self.pages - 1) self.write_data(self.buffer) class SSD1306_I2C(SSD1306): def __init__(self, width, height, i2c, addr=0x3C, external_vcc=False): self.i2c = i2c self.addr = addr self.temp = bytearray(2) self.write_list = [b"\x40", None] # Co=0, D/C#=1 super().__init__(width, height, external_vcc) def write_cmd(self, cmd): self.temp[0] = 0x80 # Co=1, D/C#=0 self.temp[1] = cmd self.i2c.writeto(self.addr, self.temp) def write_data(self, buf): self.write_list[1] = buf self.i2c.writevto(self.addr, self.write_list) class SSD1306_SPI(SSD1306): def __init__(self, width, height, spi, dc, res, cs, external_vcc=False): self.rate = 10 * 1024 * 1024 dc.init(dc.OUT, value=0) res.init(res.OUT, value=0) cs.init(cs.OUT, value=1) self.spi = spi self.dc = dc self.res = res self.cs = cs import time self.res(1) time.sleep_ms(1) self.res(0) time.sleep_ms(10) self.res(1) super().__init__(width, height, external_vcc) def write_cmd(self, cmd): self.spi.init(baudrate=self.rate, polarity=0, phase=0) self.cs(1) self.dc(0) self.cs(0) self.spi.write(bytearray([cmd])) self.cs(1) def write_data(self, buf): self.spi.init(baudrate=self.rate, polarity=0, phase=0) self.cs(1) self.dc(1) self.cs(0) self.spi.write(buf) self.cs(1)

最后确保你的pico根目录下有ssd1306.py这个文件

20240320143022380-image

显示英文

创建main.py,并运行以下代码

from machine import Pin, I2C
import framebuf
i2c=I2C(0, scl=Pin(21), sda=Pin(20), freq=100000)
# i2c=I2C(1, scl=Pin(19),sda=Pin(18), freq=100000)
from ssd1306 import SSD1306_I2C
oled = SSD1306_I2C(128, 32, i2c)
oled.text('www.qingningz.cn', 0, 0, 8)
oled.show()
from machine import Pin, I2C
import framebuf
i2c=I2C(0, scl=Pin(21), sda=Pin(20), freq=100000)
# i2c=I2C(1, scl=Pin(19),sda=Pin(18), freq=100000)
from ssd1306 import SSD1306_I2C
oled = SSD1306_I2C(128, 32, i2c)
oled.text('www.qingningz.cn', 0, 0, 8)
oled.show()
from machine import Pin, I2C import framebuf i2c=I2C(0, scl=Pin(21), sda=Pin(20), freq=100000) # i2c=I2C(1, scl=Pin(19),sda=Pin(18), freq=100000) from ssd1306 import SSD1306_I2C oled = SSD1306_I2C(128, 32, i2c) oled.text('www.qingningz.cn', 0, 0, 8) oled.show()

20240323140718124-image

20240318162313608-image

显示中文

下载文字取模软件

 

第一种方法

汉字取模,下载上述提到的软件,按照我的设置

20240320143222923-image

这就是每个汉字

20240320143308654-image

编写代码

from machine import Pin, I2C
import framebuf
i2c=I2C(0, scl=Pin(21), sda=Pin(20), freq=100000)
# i2c=I2C(1, scl=Pin(19),sda=Pin(18), freq=100000)
from ssd1306 import SSD1306_I2C
oled = SSD1306_I2C(128, 32, i2c)
fontlib = {
'青':[0x01,0x00,0x01,0x00,0x7F,0xFC,0x01,0x00,0x3F,0xF8,0x01,0x00,0xFF,0xFE,0x00,0x00,
0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x10,0x10,0x10,0x50,0x10,0x20
],
'柠':[0x20,0x40,0x20,0x20,0x20,0x20,0x23,0xFE,0xFA,0x02,0x24,0x04,0x20,0x00,0x70,0x00,
0x6B,0xFE,0xA0,0x20,0xA0,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0xA0,0x20,0x40,
],
'博':[0x20,0x50,0x20,0x48,0x2F,0xFE,0x20,0x40,0x27,0xFC,0xFC,0x44,0x27,0xFC,0x24,0x44,
0x27,0xFC,0x24,0x44,0x20,0x08,0x2F,0xFE,0x22,0x08,0x21,0x08,0x21,0x28,0x20,0x10,
],
'客':[0x02,0x00,0x01,0x00,0x7F,0xFE,0x40,0x02,0x88,0x04,0x0F,0xF0,0x10,0x20,0x2C,0x40,
0x03,0x80,0x1C,0x70,0xE0,0x0E,0x1F,0xF0,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10,
],
}
def text_hz(hz, x, y):
zm = bytearray(fontlib[hz])
buf = framebuf.FrameBuffer(zm, 16, 16, framebuf.MONO_HLSB)
oled.blit(buf, x, y)
(x, y) = (0, 0)
for hz in "青柠博客":
text_hz(hz, x, y)
x += 16
oled.show()
from machine import Pin, I2C
import framebuf
i2c=I2C(0, scl=Pin(21), sda=Pin(20), freq=100000)
# i2c=I2C(1, scl=Pin(19),sda=Pin(18), freq=100000)
from ssd1306 import SSD1306_I2C
oled = SSD1306_I2C(128, 32, i2c)
fontlib = {
    '青':[0x01,0x00,0x01,0x00,0x7F,0xFC,0x01,0x00,0x3F,0xF8,0x01,0x00,0xFF,0xFE,0x00,0x00,
0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x10,0x10,0x10,0x50,0x10,0x20
],
        '柠':[0x20,0x40,0x20,0x20,0x20,0x20,0x23,0xFE,0xFA,0x02,0x24,0x04,0x20,0x00,0x70,0x00,
0x6B,0xFE,0xA0,0x20,0xA0,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0xA0,0x20,0x40,
],
        '博':[0x20,0x50,0x20,0x48,0x2F,0xFE,0x20,0x40,0x27,0xFC,0xFC,0x44,0x27,0xFC,0x24,0x44,
0x27,0xFC,0x24,0x44,0x20,0x08,0x2F,0xFE,0x22,0x08,0x21,0x08,0x21,0x28,0x20,0x10,
],
           '客':[0x02,0x00,0x01,0x00,0x7F,0xFE,0x40,0x02,0x88,0x04,0x0F,0xF0,0x10,0x20,0x2C,0x40,
0x03,0x80,0x1C,0x70,0xE0,0x0E,0x1F,0xF0,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10,
],
    }
def text_hz(hz, x, y):
    zm = bytearray(fontlib[hz])
    buf = framebuf.FrameBuffer(zm, 16, 16, framebuf.MONO_HLSB)
    oled.blit(buf, x, y)

(x, y) = (0, 0)
for hz in "青柠博客":
    text_hz(hz, x, y)
    x += 16
oled.show()
from machine import Pin, I2C import framebuf i2c=I2C(0, scl=Pin(21), sda=Pin(20), freq=100000) # i2c=I2C(1, scl=Pin(19),sda=Pin(18), freq=100000) from ssd1306 import SSD1306_I2C oled = SSD1306_I2C(128, 32, i2c) fontlib = { '青':[0x01,0x00,0x01,0x00,0x7F,0xFC,0x01,0x00,0x3F,0xF8,0x01,0x00,0xFF,0xFE,0x00,0x00, 0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x10,0x10,0x10,0x50,0x10,0x20 ], '柠':[0x20,0x40,0x20,0x20,0x20,0x20,0x23,0xFE,0xFA,0x02,0x24,0x04,0x20,0x00,0x70,0x00, 0x6B,0xFE,0xA0,0x20,0xA0,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0xA0,0x20,0x40, ], '博':[0x20,0x50,0x20,0x48,0x2F,0xFE,0x20,0x40,0x27,0xFC,0xFC,0x44,0x27,0xFC,0x24,0x44, 0x27,0xFC,0x24,0x44,0x20,0x08,0x2F,0xFE,0x22,0x08,0x21,0x08,0x21,0x28,0x20,0x10, ], '客':[0x02,0x00,0x01,0x00,0x7F,0xFE,0x40,0x02,0x88,0x04,0x0F,0xF0,0x10,0x20,0x2C,0x40, 0x03,0x80,0x1C,0x70,0xE0,0x0E,0x1F,0xF0,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10, ], } def text_hz(hz, x, y): zm = bytearray(fontlib[hz]) buf = framebuf.FrameBuffer(zm, 16, 16, framebuf.MONO_HLSB) oled.blit(buf, x, y) (x, y) = (0, 0) for hz in "青柠博客": text_hz(hz, x, y) x += 16 oled.show()

20240320143828908-3885ba7e6355a4dd9592503d0349ac5

第二种方法

首先你需要获取每个汉子的utf-8的值,比如我现在要在屏幕中输入“杨凡我爱你”这几个字,首先我需要获取“杨”这个字的utf-8的值

整个过程比较麻烦,所以我写了一个Python脚本

def char():
name = "杨"
for i in name:
unicode_value = ord(i)
utf_8_value = chr(unicode_value).encode("utf-8")
decoded_string = str(utf_8_value)[3:-1].replace("\\", "").replace("x", "")
foud = '0x'+decoded_string
print(foud)
char()
def char():
    
    name = "杨"
   
    for i in name:
        unicode_value = ord(i)
        utf_8_value = chr(unicode_value).encode("utf-8")
        decoded_string = str(utf_8_value)[3:-1].replace("\\", "").replace("x", "")
        foud = '0x'+decoded_string
        
        print(foud)

char()
def char(): name = "杨" for i in name: unicode_value = ord(i) utf_8_value = chr(unicode_value).encode("utf-8") decoded_string = str(utf_8_value)[3:-1].replace("\\", "").replace("x", "") foud = '0x'+decoded_string print(foud) char()

20240318163325116-image

0xe69da8这个就是“杨”字对应的utf-8的值

获取“杨”字的编码,打开文字取模软件按照我的设置

20240318164028842-image

获取“杨”字的编码

20240318164157725-image

0x10,0x11,0x10,0x10,0xFC,0x10,0x31,0x38,0x54,0x54,0x91,0x11,0x12,0x14,0x10,0x11,
0x00,0xF8,0x10,0x20,0x40,0x80,0xFE,0x92,0x92,0x92,0x12,0x22,0x22,0x42,0x94,0x08,
0x10,0x11,0x10,0x10,0xFC,0x10,0x31,0x38,0x54,0x54,0x91,0x11,0x12,0x14,0x10,0x11,
0x00,0xF8,0x10,0x20,0x40,0x80,0xFE,0x92,0x92,0x92,0x12,0x22,0x22,0x42,0x94,0x08,
0x10,0x11,0x10,0x10,0xFC,0x10,0x31,0x38,0x54,0x54,0x91,0x11,0x12,0x14,0x10,0x11, 0x00,0xF8,0x10,0x20,0x40,0x80,0xFE,0x92,0x92,0x92,0x12,0x22,0x22,0x42,0x94,0x08,

然后将“杨”字utf-8的地址和编码组合成一个字典

font16 = {
0xe69da8:
[0x10,0x11,0x10,0x10,0xFC,0x10,0x31,0x38,0x54,0x54,0x91,0x11,0x12,0x14,0x10,0x11,
0x00,0xF8,0x10,0x20,0x40,0x80,0xFE,0x92,0x92,0x92,0x12,0x22,0x22,0x42,0x94,0x08],
}
font16 = {
     0xe69da8:
        [0x10,0x11,0x10,0x10,0xFC,0x10,0x31,0x38,0x54,0x54,0x91,0x11,0x12,0x14,0x10,0x11,
0x00,0xF8,0x10,0x20,0x40,0x80,0xFE,0x92,0x92,0x92,0x12,0x22,0x22,0x42,0x94,0x08],  


}
font16 = { 0xe69da8: [0x10,0x11,0x10,0x10,0xFC,0x10,0x31,0x38,0x54,0x54,0x91,0x11,0x12,0x14,0x10,0x11, 0x00,0xF8,0x10,0x20,0x40,0x80,0xFE,0x92,0x92,0x92,0x12,0x22,0x22,0x42,0x94,0x08], }

整个杨凡我爱你的编码是这样的

font16 = {
0xe69da8:
[0x10,0x11,0x10,0x10,0xFC,0x10,0x31,0x38,0x54,0x54,0x91,0x11,0x12,0x14,0x10,0x11,
0x00,0xF8,0x10,0x20,0x40,0x80,0xFE,0x92,0x92,0x92,0x12,0x22,0x22,0x42,0x94,0x08],
0xe587a1:
[0x00,0x0F,0x08,0x08,0x08,0x0A,0x09,0x08,0x08,0x08,0x10,0x10,0x20,0x20,0x40,0x80,
0x00,0xE0,0x20,0x20,0x20,0x20,0x20,0xA0,0xA0,0x20,0x20,0x22,0x22,0x22,0x1E,0x00],
0xe68891:
[0x04,0x0E,0x78,0x08,0x08,0xFF,0x08,0x08,0x0A,0x0C,0x18,0x68,0x08,0x08,0x2B,0x10,
0x40,0x50,0x48,0x48,0x40,0xFE,0x40,0x44,0x44,0x48,0x30,0x22,0x52,0x8A,0x06,0x02],
0xe788b1:
[0x00,0x01,0x7E,0x22,0x11,0x7F,0x42,0x82,0x7F,0x04,0x07,0x0A,0x11,0x20,0x43,0x1C,
0x08,0xFC,0x10,0x10,0x20,0xFE,0x02,0x04,0xF8,0x00,0xF0,0x10,0x20,0xC0,0x30,0x0E],
0xe4bda0:
[0x08,0x08,0x08,0x11,0x11,0x32,0x34,0x50,0x91,0x11,0x12,0x12,0x14,0x10,0x10,0x10,
0x80,0x80,0x80,0xFE,0x02,0x04,0x20,0x20,0x28,0x24,0x24,0x22,0x22,0x20,0xA0,0x40],
}
font16 = {
     0xe69da8:
        [0x10,0x11,0x10,0x10,0xFC,0x10,0x31,0x38,0x54,0x54,0x91,0x11,0x12,0x14,0x10,0x11,
0x00,0xF8,0x10,0x20,0x40,0x80,0xFE,0x92,0x92,0x92,0x12,0x22,0x22,0x42,0x94,0x08],  
    0xe587a1:
        [0x00,0x0F,0x08,0x08,0x08,0x0A,0x09,0x08,0x08,0x08,0x10,0x10,0x20,0x20,0x40,0x80,
0x00,0xE0,0x20,0x20,0x20,0x20,0x20,0xA0,0xA0,0x20,0x20,0x22,0x22,0x22,0x1E,0x00],
     0xe68891:
        [0x04,0x0E,0x78,0x08,0x08,0xFF,0x08,0x08,0x0A,0x0C,0x18,0x68,0x08,0x08,0x2B,0x10,
0x40,0x50,0x48,0x48,0x40,0xFE,0x40,0x44,0x44,0x48,0x30,0x22,0x52,0x8A,0x06,0x02],  
    0xe788b1:
        [0x00,0x01,0x7E,0x22,0x11,0x7F,0x42,0x82,0x7F,0x04,0x07,0x0A,0x11,0x20,0x43,0x1C,
0x08,0xFC,0x10,0x10,0x20,0xFE,0x02,0x04,0xF8,0x00,0xF0,0x10,0x20,0xC0,0x30,0x0E],
     0xe4bda0:
        [0x08,0x08,0x08,0x11,0x11,0x32,0x34,0x50,0x91,0x11,0x12,0x12,0x14,0x10,0x10,0x10,
0x80,0x80,0x80,0xFE,0x02,0x04,0x20,0x20,0x28,0x24,0x24,0x22,0x22,0x20,0xA0,0x40],  

}
font16 = { 0xe69da8: [0x10,0x11,0x10,0x10,0xFC,0x10,0x31,0x38,0x54,0x54,0x91,0x11,0x12,0x14,0x10,0x11, 0x00,0xF8,0x10,0x20,0x40,0x80,0xFE,0x92,0x92,0x92,0x12,0x22,0x22,0x42,0x94,0x08], 0xe587a1: [0x00,0x0F,0x08,0x08,0x08,0x0A,0x09,0x08,0x08,0x08,0x10,0x10,0x20,0x20,0x40,0x80, 0x00,0xE0,0x20,0x20,0x20,0x20,0x20,0xA0,0xA0,0x20,0x20,0x22,0x22,0x22,0x1E,0x00], 0xe68891: [0x04,0x0E,0x78,0x08,0x08,0xFF,0x08,0x08,0x0A,0x0C,0x18,0x68,0x08,0x08,0x2B,0x10, 0x40,0x50,0x48,0x48,0x40,0xFE,0x40,0x44,0x44,0x48,0x30,0x22,0x52,0x8A,0x06,0x02], 0xe788b1: [0x00,0x01,0x7E,0x22,0x11,0x7F,0x42,0x82,0x7F,0x04,0x07,0x0A,0x11,0x20,0x43,0x1C, 0x08,0xFC,0x10,0x10,0x20,0xFE,0x02,0x04,0xF8,0x00,0xF0,0x10,0x20,0xC0,0x30,0x0E], 0xe4bda0: [0x08,0x08,0x08,0x11,0x11,0x32,0x34,0x50,0x91,0x11,0x12,0x12,0x14,0x10,0x10,0x10, 0x80,0x80,0x80,0xFE,0x02,0x04,0x20,0x20,0x28,0x24,0x24,0x22,0x22,0x20,0xA0,0x40], }

编写代码

import ssd1306py as lcd
font16 = {
0xe69da8:
[0x10,0x11,0x10,0x10,0xFC,0x10,0x31,0x38,0x54,0x54,0x91,0x11,0x12,0x14,0x10,0x11,
0x00,0xF8,0x10,0x20,0x40,0x80,0xFE,0x92,0x92,0x92,0x12,0x22,0x22,0x42,0x94,0x08],
0xe587a1:
[0x00,0x0F,0x08,0x08,0x08,0x0A,0x09,0x08,0x08,0x08,0x10,0x10,0x20,0x20,0x40,0x80,
0x00,0xE0,0x20,0x20,0x20,0x20,0x20,0xA0,0xA0,0x20,0x20,0x22,0x22,0x22,0x1E,0x00],
0xe68891:
[0x04,0x0E,0x78,0x08,0x08,0xFF,0x08,0x08,0x0A,0x0C,0x18,0x68,0x08,0x08,0x2B,0x10,
0x40,0x50,0x48,0x48,0x40,0xFE,0x40,0x44,0x44,0x48,0x30,0x22,0x52,0x8A,0x06,0x02],
0xe788b1:
[0x00,0x01,0x7E,0x22,0x11,0x7F,0x42,0x82,0x7F,0x04,0x07,0x0A,0x11,0x20,0x43,0x1C,
0x08,0xFC,0x10,0x10,0x20,0xFE,0x02,0x04,0xF8,0x00,0xF0,0x10,0x20,0xC0,0x30,0x0E],
0xe4bda0:
[0x08,0x08,0x08,0x11,0x11,0x32,0x34,0x50,0x91,0x11,0x12,0x12,0x14,0x10,0x10,0x10,
0x80,0x80,0x80,0xFE,0x02,0x04,0x20,0x20,0x28,0x24,0x24,0x22,0x22,0x20,0xA0,0x40],
}
lcd.init_i2c(21, 20, 128, 32, 0)
lcd.set_font(font16, 16)
lcd.text_cn('杨凡我爱你', 0, 0, 16)
lcd.show()
import ssd1306py as lcd

font16 = {
     0xe69da8:
        [0x10,0x11,0x10,0x10,0xFC,0x10,0x31,0x38,0x54,0x54,0x91,0x11,0x12,0x14,0x10,0x11,
0x00,0xF8,0x10,0x20,0x40,0x80,0xFE,0x92,0x92,0x92,0x12,0x22,0x22,0x42,0x94,0x08],  
    0xe587a1:
        [0x00,0x0F,0x08,0x08,0x08,0x0A,0x09,0x08,0x08,0x08,0x10,0x10,0x20,0x20,0x40,0x80,
0x00,0xE0,0x20,0x20,0x20,0x20,0x20,0xA0,0xA0,0x20,0x20,0x22,0x22,0x22,0x1E,0x00],
     0xe68891:
        [0x04,0x0E,0x78,0x08,0x08,0xFF,0x08,0x08,0x0A,0x0C,0x18,0x68,0x08,0x08,0x2B,0x10,
0x40,0x50,0x48,0x48,0x40,0xFE,0x40,0x44,0x44,0x48,0x30,0x22,0x52,0x8A,0x06,0x02],  
    0xe788b1:
        [0x00,0x01,0x7E,0x22,0x11,0x7F,0x42,0x82,0x7F,0x04,0x07,0x0A,0x11,0x20,0x43,0x1C,
0x08,0xFC,0x10,0x10,0x20,0xFE,0x02,0x04,0xF8,0x00,0xF0,0x10,0x20,0xC0,0x30,0x0E],
     0xe4bda0:
        [0x08,0x08,0x08,0x11,0x11,0x32,0x34,0x50,0x91,0x11,0x12,0x12,0x14,0x10,0x10,0x10,
0x80,0x80,0x80,0xFE,0x02,0x04,0x20,0x20,0x28,0x24,0x24,0x22,0x22,0x20,0xA0,0x40],  

}


lcd.init_i2c(21, 20, 128, 32, 0)
lcd.set_font(font16, 16)

lcd.text_cn('杨凡我爱你', 0, 0, 16)

lcd.show()
import ssd1306py as lcd font16 = { 0xe69da8: [0x10,0x11,0x10,0x10,0xFC,0x10,0x31,0x38,0x54,0x54,0x91,0x11,0x12,0x14,0x10,0x11, 0x00,0xF8,0x10,0x20,0x40,0x80,0xFE,0x92,0x92,0x92,0x12,0x22,0x22,0x42,0x94,0x08], 0xe587a1: [0x00,0x0F,0x08,0x08,0x08,0x0A,0x09,0x08,0x08,0x08,0x10,0x10,0x20,0x20,0x40,0x80, 0x00,0xE0,0x20,0x20,0x20,0x20,0x20,0xA0,0xA0,0x20,0x20,0x22,0x22,0x22,0x1E,0x00], 0xe68891: [0x04,0x0E,0x78,0x08,0x08,0xFF,0x08,0x08,0x0A,0x0C,0x18,0x68,0x08,0x08,0x2B,0x10, 0x40,0x50,0x48,0x48,0x40,0xFE,0x40,0x44,0x44,0x48,0x30,0x22,0x52,0x8A,0x06,0x02], 0xe788b1: [0x00,0x01,0x7E,0x22,0x11,0x7F,0x42,0x82,0x7F,0x04,0x07,0x0A,0x11,0x20,0x43,0x1C, 0x08,0xFC,0x10,0x10,0x20,0xFE,0x02,0x04,0xF8,0x00,0xF0,0x10,0x20,0xC0,0x30,0x0E], 0xe4bda0: [0x08,0x08,0x08,0x11,0x11,0x32,0x34,0x50,0x91,0x11,0x12,0x12,0x14,0x10,0x10,0x10, 0x80,0x80,0x80,0xFE,0x02,0x04,0x20,0x20,0x28,0x24,0x24,0x22,0x22,0x20,0xA0,0x40], } lcd.init_i2c(21, 20, 128, 32, 0) lcd.set_font(font16, 16) lcd.text_cn('杨凡我爱你', 0, 0, 16) lcd.show()

20240318164539171-image

20240318164613942-image

© 版权声明
THE END
喜欢就支持一下吧
点赞0赞赏 分享
I am ordinary yet unique.
我很平凡,但我独一无二
评论 共2条
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片快捷回复

    暂无评论内容