xucvai · 2021年12月27日

冰箱装大象,一共分几步?

简 介: 完整的开源代码以及移植说明正在整理当中。建立共同的生态圈,使得更多人加入应用模块中,丰其羽翼、壮其筋骨。让更多非专业人士体会编程快乐。
关键词MM32MicroPythonOLED

01 夸下海口

640.gif
▲ 图1  想当年,第六届智能车竞赛,吃鸡腿

这个男人(MindMotion, 苏勇),在10月17在上海一家火锅店 边吃饭,边回想当年参加智能车竞赛[1] 的时候,边夸下海口,今年能够让同学们在MindMotion单片机上使用MicroPython开发车模作品。

时间过去了一个多月了,他在原来空空荡荡的 MicroPython内核上,针对MM32F3277单片机实现了那些功能呢?

下面是使用 dir(machine) 看到的主要功能。

MicroPython v1.16 on 2021-11-25; MB_F3270 with MM32F3277G7P
>>> import machine
>>> dir(machine)
['__name__', 'ADC', 'DAC', 'PWM', 'Pin', 'SDCard', 'SPI', 'SoftI2C', 'SoftSPI', 'UART', 'freq', 'mem16', 'mem32', 'mem8']
>>> 

虽距离羽翼丰满还有一段距离,但将自己装进OLED进行显示还是可以的了。
image.png

02 冰箱装大象,一共分几步?

一、MM32F3277 MicroPython

具有I2C接口 OLED接入MCU非常简单。只需要两根信号线便可以。在 MindMotion MM32F3277 SoftI2C功能测试[2] 测试了OLED通过I2C接入MM32F3277 的 SoftI2C控制端口,通过MicroPython显示字符信息。
6.gif
▲ 图1.1.1  OLED通过I2C总线连接到MM32F3277

1、OLED显示图片

OLED显示像素为128×64,总共有1024个字节的数据表示显示的图片。每个像素只有两个状态,所以只能显示图片的边缘信息。

在显示图片之前需要将图片转换成边缘信息。
image.png
▲ 图1.1.2 OLED像素对应的1024字节编码

二、转换图片

对于显示的图片转换包括有两个过程:

  • 将图片转换成给出边缘图片;
  • 将图片的尺寸切割成128×64;

转换完之后,便可以将其编码成1024 个字节,传送到MicroPython刷新到OLED中,或者将数据存储在文件中,预先存储在SD卡中,由MicroPython读取显示。

1、提取边缘

使用cv2中的Canny算子提取图片中的边缘。

filename = os.path.join(gifdir, '%04d.BMP'%i)
img = cv2.imread(filename)[:,:,::-1]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edge = cv2.Canny(gray, 100, 200)

44.gif
▲ 图1.2.1  提取边缘结果

2、提取中心256×128图片

imgshape = shape(img)
imgcrop = img[imgshape[0]//2-64:imgshape[0]//2+64,
              0:255,:]

00.gif
▲ 图1.2.2  中心256×128图片

3、图片压缩到128×64

imgcrop = cv2.resize(imgcrop, dsize=(128, 64), interpolation=cv2.INTER_LINEAR)

640.gif
▲ 图1.2.3  转换成128×64点阵

4、转换成字节

根据【1.1.1:OLED显示图片】中的OLED的数据编码方式,将边缘图片转换成1024个字节。

def oleddata(d):
    outd = []

    d[d!=0] = 1

    h,w = shape(d)
    line = h//8
    c12 = [2**i for i in range(8)]
    printf(sum(d))

    for i in range(line):
        startline = i*8
        endline = startline+8
        for j in range(w):
            data = d[startline:endline, j]
            printf(data)
            num = sum([x*y for x,y in zip(data, c12)])
            outd.append(num)

   return outd

  下面是其中衣服图片转换后的字节。

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 224, 56, 12, 6, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 3, 4, 9, 11, 18, 50, 18, 4, 84, 84, 148, 20, 20, 36, 32, 40, 40, 40, 40, 40, 72, 64, 80, 208, 16, 224, 16, 80, 80, 80, 80, 80, 64, 15, 136, 143, 129, 129, 129, 2, 65, 129, 129, 128, 128, 128, 0, 0, 0, 0, 15, 0, 15, 16, 31, 16, 31, 0, 4, 7, 0, 255, 0, 176, 31, 16, 16, 31, 16, 16, 7, 4, 248, 0, 0, 0, 248, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 124, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 6, 28, 112, 192, 0, 0, 0, 0, 0, 0, 0, 255, 0, 254, 0, 2, 1, 13, 35, 221, 0, 129, 126, 0, 20, 58, 3, 1, 1, 238, 50, 14, 112, 216, 73, 197, 89, 17, 225, 2, 2, 254, 1, 2, 2, 252, 4, 8, 8, 9, 9, 1, 248, 8, 8, 8, 224, 32, 32, 32, 33, 33, 33, 33, 33, 32, 96, 64, 64, 64, 64, 0, 0, 0, 0, 0, 0, 0, 248, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 7, 124, 0, 0, 192, 96, 32, 223, 64, 255, 0, 128, 238, 138, 136, 255, 138, 139, 170, 168, 138, 250, 128, 128, 128, 179, 8, 8, 8, 11, 8, 8, 57, 192, 15, 0, 254, 1, 0, 0, 196, 187, 0, 0, 0, 0, 0, 0, 255, 0, 28, 226, 1, 251, 134, 48, 80, 136, 136, 64, 112, 24, 8, 8, 24, 160, 32, 0, 0, 0, 0, 0, 0, 192, 103, 56, 80, 80, 48, 32, 64, 192, 160, 160, 64, 64, 64, 128, 0, 0, 128, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 18, 18, 11, 73, 152, 113, 134, 56, 136, 56, 64, 80, 160, 50, 24, 14, 16, 56, 32, 68, 52, 36, 201, 121, 130, 113, 27, 17, 255, 128, 30, 0, 255, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 255, 0, 1, 246, 8, 161, 48, 28, 7, 1, 128, 128, 128, 128, 143, 137, 153, 180, 161, 128, 128, 128, 128, 128, 143, 240, 0, 0, 248, 136, 134, 132, 254, 64, 64, 1, 3, 6, 4, 4, 73, 99, 22, 36, 104, 72, 80, 144, 160, 64, 64, 128, 128, 0, 0, 0, 0, 3, 2, 2, 2, 2, 98, 178, 20, 36, 100, 132, 4, 12, 8, 216, 48, 0, 192, 120, 72, 68, 244, 146, 210, 2, 168, 41, 43, 2, 36, 38, 34, 34, 39, 44, 51, 32, 21, 38, 37, 36, 36, 208, 80, 156, 167, 160, 160, 160, 76, 48, 13, 9, 139, 46, 32, 32, 31, 32, 255, 0, 0, 0, 1, 254, 0, 0, 0, 0, 192, 120, 7, 128, 160, 119, 72, 73, 120, 74, 74, 105, 59, 72, 8, 248, 136, 122, 170, 8, 238, 194, 98, 58, 14, 0, 0, 0, 127, 192, 4, 8, 17, 16, 17, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 2, 2, 4, 5, 8, 16, 240, 16, 252, 68, 36, 32, 48, 21, 28, 10, 65, 243, 10, 98, 31, 64, 143, 243, 140, 118, 131, 137, 133, 134, 0, 128, 195, 162, 12, 20, 20, 20, 20, 20, 20, 20, 36, 36, 36, 36, 36, 36, 36, 39, 32, 35, 36, 40, 40, 47, 36, 68, 68, 70, 85, 85, 85, 85, 87, 81, 87, 80, 80, 64, 160, 191, 160, 160, 160, 160, 161, 175, 136, 144, 144, 159, 144, 16, 80, 80, 80, 80, 86, 81, 81, 82, 84, 80, 93, 81, 17, 0, 0, 0, 0, 0, 0, 0, 0, 3, 14, 24, 112, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 7, 4, 4, 4, 4, 64, 188, 6, 66, 59, 104, 78, 65, 0, 97, 142, 48, 14, 0, 32, 192, 112, 14, 225, 24, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 64, 64, 64, 64, 64, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 12, 8, 24, 48, 96, 192, 0, 0, 0, 0, 0, 1, 6, 28, 112, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 16, 16, 8, 15, 3, 2, 0, 0, 14, 176, 104, 15, 0, 0, 0, 0, 0, 255, 0, 128, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 56, 44, 0, 184, 196, 114, 10, 57, 99, 193, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

三、显示图片

编写MicroPython程序,将前面的数据显示在OLED中。

1、将数据写入SD卡文件

将上述数据写入数据文件:GIF.TXT。
然后将GIF.TXT 存入SD卡中。

pltgif = PlotGIF()
outfile = open(r'd:\temp\gif.txt', 'w')

#------------------------------------------------------------
for i in range(gifpage):

    filename = os.path.join(gifdir, '%04d.BMP'%i)
    printf(filename)
    img = cv2.imread(filename)[:,:,::-1]

    imgshape = shape(img)
    imgcrop = img[imgshape[0]//2-64:imgshape[0]//2+64,
                  0:255,:]

    imgcrop = cv2.resize(imgcrop, dsize=(128, 64), interpolation=cv2.INTER_LINEAR)
    gray = cv2.cvtColor(imgcrop, cv2.COLOR_BGR2GRAY)
    edge = cv2.Canny(gray, 80, 200)

    data = oleddata(edge)

    strdata = ''
    for d in data:
        strdata = strdata + '%02x'%d

    strdata = strdata + '\n'
    outfile.write(strdata)

2、在OLED上显示

在MicroPython通过读取GIF.TXT中的数据,显示在OLED上。

from machine                import SoftI2C,Pin
import utime
import i2coled

scl = Pin('PA0')
sda = Pin('PA1')

i2c = SoftI2C(scl, sda, freq=400000)

oled = i2coled.SSD1306_I2C(128, 64, i2c, addr=0x3c)
oled.setstring(0, 0, 'HELLO')
oled.show()

print("Receive OLED and show...")
count = 0

while True:
    count = 0
    with open('gif.txt', 'r') as f:
        for l in f:
            print(len(l))
            for i in range(1024):
                num = int(l[i*2:i*2+2], 16)
                oled.buffer[count] = num
                count += 1

            count = 0
            oled.show()

001.gif
▲ 图2.2.1  显示动画

03 开源在即

完整的开源代码以及移植说明正在整理当中。建立共同的生态圈,使得更多人加入应用模块中,丰其羽翼、壮其筋骨,让更多非专业人士体会MicroPython编程快乐。

参考资料
[1]边吃饭,边回想当年参加智能车竞赛: https://mp.weixin.qq.com/s?__biz=MzA5NjQyNjc2NQ&mid=2452244579&idx=1&sn=4c2327f742c0db6373950a8ca389a769&chksm=876ea601b0192f179c59fed9a2486615a5ede984da6e7bad9773daad65221fc0ce9f94e1455b&token=426555167&lang=zh_CN#rd*

[2]MindMotion MM32F3277 SoftI2C功能测试: https://zhuoqing.blog.csdn.net/article/details/121538923

原文链接:TsinghuaJoking
作者:卓晴

推荐阅读

推荐阅读
关注数
6143
内容数
276
灵动MM32 MCU相关技术知识,欢迎关注~
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息