本帖最后由 lemon1394 于 2021-8-18 22:00 编辑
用帧缓冲区的方法来显示汉字或图片,算法变得很简单。
不光是显示,还可以做移动显示汉字,轮播特效,甚至简单游戏。
以一行“书山有路勤为径,学海无崖苦作舟。”为例:
先建一个能容纳这么多字的帧缓冲区,把每个汉字都按顺序用blit方法写进去,再把这个大的帧缓冲区blit到ssd1306的帧缓冲区,加上移动速度,就可以做出移动效果。
主程序:
from machine import I2C, Pin
import ssd1306
import framebuf
import font_chinese
i2c = I2C(scl=Pin(4), sda=Pin(5), freq=400000)
disp = ssd1306.SSD1306_I2C(128, 64, i2c)
big_buffer = bytearray(512)
big_framebuf = framebuf.FrameBuffer(big_buffer, 256, 16, framebuf.MONO_VLSB)
string = "书山有路勤为径,学海无涯苦作舟。"
offset = 0
speed = -2
x = 128
def chinese(string, x, y):
offset_ = 0
for i in string:
buf = bytearray(font_chinese.font)
font_framebuf = framebuf.FrameBuffer(buf, 16, 16, framebuf.MONO_VLSB)
disp.blit(font_framebuf, x + offset_, y)
offset_ += 16
chinese('中文演示', 32, 0)
disp.hline(32, 18, 64, 1)
disp.show()
for i in string:
temp_buffer = bytearray(font_chinese.font)
small_framebuf = framebuf.FrameBuffer(temp_buffer, 16, 16, framebuf.MONO_VLSB)
big_framebuf.blit(small_framebuf, offset, 0)
offset += 16
while True:
disp.blit(big_framebuf, x, 32)
disp.show()
x += speed
if x < -256:
x = 128
字库文件:
font = {
"书":[0x00,0x00,0x08,0x08,0x08,0x08,0xFF,0x08,0x08,0x08,0xF9,0x02,0x04,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0xFF,0x01,0x01,0x01,0x21,0x41,0x21,0x1F,0x00,0x00],#书0
"山":[0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x00,0x3F,0x20,0x20,0x20,0x20,0x3F,0x20,0x20,0x20,0x20,0x7F,0x00,0x00,0x00],#山1
"有":[0x04,0x04,0x04,0x84,0xE4,0x3C,0x27,0x24,0x24,0x24,0x24,0xE4,0x04,0x04,0x04,0x00,0x04,0x02,0x01,0x00,0xFF,0x09,0x09,0x09,0x09,0x49,0x89,0x7F,0x00,0x00,0x00,0x00],#有2
"路":[0x00,0x3E,0x22,0xE2,0x22,0x3E,0x00,0x10,0x88,0x57,0x24,0x54,0x8C,0x00,0x00,0x00,0x40,0x7E,0x40,0x3F,0x22,0x22,0x00,0x01,0xFE,0x42,0x42,0x42,0xFE,0x01,0x01,0x00],#路3
"勤":[0x04,0xC4,0x5F,0x54,0xF4,0x54,0x5F,0xC4,0x14,0x10,0xFF,0x10,0x10,0xF0,0x00,0x00,0x80,0x95,0x95,0x95,0x7F,0x55,0xD5,0x55,0x20,0x18,0x47,0x80,0x40,0x3F,0x00,0x00],#勤4
"为":[0x00,0x20,0x22,0x2C,0x20,0x20,0xE0,0x3F,0x20,0x20,0x20,0x20,0xE0,0x00,0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x06,0x01,0x00,0x01,0x46,0x80,0x40,0x3F,0x00,0x00,0x00],#为5
"径":[0x00,0x10,0x88,0xC4,0x33,0x80,0x82,0x42,0x22,0x12,0x1A,0x26,0x22,0x40,0x80,0x00,0x02,0x01,0x00,0xFF,0x00,0x40,0x42,0x42,0x42,0x7E,0x42,0x42,0x42,0x42,0x40,0x00],#径6
",":[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x58,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],#,7
"学":[0x40,0x30,0x11,0x96,0x90,0x90,0x91,0x96,0x90,0x90,0x98,0x14,0x13,0x50,0x30,0x00,0x04,0x04,0x04,0x04,0x04,0x44,0x84,0x7E,0x06,0x05,0x04,0x04,0x04,0x04,0x04,0x00],#8
"海":[0x10,0x60,0x02,0x0C,0xC0,0x10,0x08,0xF7,0x14,0x54,0x94,0x14,0xF4,0x04,0x00,0x00,0x04,0x04,0x7C,0x03,0x00,0x01,0x1D,0x13,0x11,0x55,0x99,0x51,0x3F,0x11,0x01,0x00],#海9
"无":[0x00,0x40,0x42,0x42,0x42,0xC2,0x7E,0x42,0xC2,0x42,0x42,0x42,0x40,0x40,0x00,0x00,0x80,0x40,0x20,0x10,0x0C,0x03,0x00,0x00,0x3F,0x40,0x40,0x40,0x40,0x70,0x00,0x00],#无10
"涯":[0x10,0x60,0x02,0x8C,0x00,0xFE,0x82,0x92,0x92,0x92,0xFE,0x92,0x92,0x92,0x82,0x00,0x04,0x04,0x7E,0x81,0x60,0x1F,0x40,0x48,0x48,0x48,0x7E,0x48,0x48,0x48,0x40,0x00],#涯11
"苦":[0x84,0x84,0x84,0x84,0x9F,0x84,0x84,0xF4,0x84,0x84,0x9F,0x84,0x84,0x84,0x84,0x00,0x00,0x00,0x00,0xFC,0x44,0x44,0x44,0x47,0x44,0x44,0x44,0xFC,0x00,0x00,0x00,0x00],#苦12
"作":[0x00,0x80,0x60,0xF8,0x07,0x40,0x30,0x0F,0xF8,0x88,0x88,0x88,0x88,0x08,0x08,0x00,0x01,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0xFF,0x08,0x08,0x08,0x08,0x08,0x00,0x00],#作13
"舟":[0x80,0x80,0x80,0xFC,0x84,0x86,0x95,0xE4,0x84,0x84,0x84,0xFC,0x80,0x80,0x80,0x00,0x80,0x40,0x30,0x0F,0x00,0x00,0x02,0x0C,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x00],#舟14
"。":[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x24,0x24,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],#舟15
"中":[0x00,0x00,0xF0,0x10,0x10,0x10,0x10,0xFF,0x10,0x10,0x10,0x10,0xF0,0x00,0x00,0x00,0x00,0x00,0x0F,0x04,0x04,0x04,0x04,0xFF,0x04,0x04,0x04,0x04,0x0F,0x00,0x00,0x00],#中0
"文":[0x08,0x08,0x08,0x38,0xC8,0x08,0x09,0x0E,0x08,0x08,0xC8,0x38,0x08,0x08,0x08,0x00,0x80,0x80,0x40,0x40,0x20,0x11,0x0A,0x04,0x0A,0x11,0x20,0x40,0x40,0x80,0x80,0x00],#文1
"演":[0x10,0x60,0x02,0x8C,0x00,0x0C,0xA4,0xA4,0xA5,0xE6,0xA4,0xA4,0xA4,0x0C,0x00,0x00,0x04,0x04,0x7E,0x01,0x00,0x80,0x4F,0x2A,0x0A,0x0F,0x0A,0x2A,0x4F,0x80,0x00,0x00],#演2
"示":[0x40,0x40,0x42,0x42,0x42,0x42,0x42,0xC2,0x42,0x42,0x42,0x42,0x42,0x40,0x40,0x00,0x20,0x10,0x08,0x06,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x02,0x04,0x08,0x30,0x00],#示3
}
esp8266演示:
这让我想起刚学python时,嵩天教授的pygame壁球小游戏,用这个也很容易实现。