3163|5

28

帖子

1

TA的资源

一粒金砂(中级)

楼主
 

ssd1306汉字移动演示 [复制链接]

 
本帖最后由 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壁球小游戏,用这个也很容易实现。

 

最新回复

刷新速率还不错!   详情 回复 发表于 2021-8-19 11:44
点赞 关注(1)
 
 

回复
举报

375

帖子

0

TA的资源

一粒金砂(高级)

沙发
 
不错不错
 
 
 

回复

6802

帖子

0

TA的资源

五彩晶圆(高级)

板凳
 

先建一个能容纳这么多字的帧缓冲区,把每个汉字都按顺序用blit方法写进去,再把这个大的帧缓冲区blit到ssd1306的帧缓冲区,加上移动速度,就可以做出移动效果。

显示的速度应该很流利,没停顿

 
 
 

回复

1万

帖子

24

TA的资源

版主

4
 

全部放在内存里,显示内容少还行,多了就不方便了。最好做一个通用的读取汉字库的函数,实用性就比较强了。

 
 
 

回复

28

帖子

1

TA的资源

一粒金砂(中级)

5
 

字库全部放内存里的确不合理,汉字字库大得不行。

我看到大家一般都是把字库放TXT文件,按需要读取。

 
 
 

回复

1942

帖子

3

TA的资源

版主

6
 

刷新速率还不错!

 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/9 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表