4638| 1
|
[入门教程] 【向金老师原创】掌控来贺:“国庆快乐” |
本帖最后由 rzyzzxw 于 2018-10-3 00:13 编辑 掌控来贺“国庆快乐” 向金 掌控收到已经好几天了,但是由于忙于“世界人工智能大会”的分享事情,所以耽误好久没有动手做点东西。借着国庆,想看看能做点什么,看了官方的介绍后,想试试中文显示。结果第一次真的失败了:原因很简单,把PEPL交互模式和刷入下载模式开始搞混了。 好了,闲话少说,我们不如正题,首先看看视频效果: 制作过程: 一、 处理图片: 1、 随便网上搜索“国徽”、“天安门”然后下载到本地。 2、 将图片处理成45*45(国徽)、81*48(天安门)分辨率的图片,这个有很多工具,但是我的电脑上没有安装任何图片处理的软件,所以就在用了一个在线的图片处理平台:改图宝http://www.gaitubao.com/ (为什么使用这个像素:那是因为掌控屏幕的分辨率是128*64,考虑到第一行显示中文,占用了128*16的分辨率,所以只剩下128*48) 3、 利用取模工具对图片进行取模。网上有PCtoLCD、lcd image converter等取模软件,可根据自己喜好自行选择。以下使用的是Img2Lcd工具。 l 步骤1.导入刚才修改后的图片 l 步骤2.选择参数,输出数据类型[C语言数组]、 扫描模式[水平扫描]、输出灰度[单色]、宽高[128*64](注意:一定要根据实际图片效果来调整亮度和对比度,已达到最佳显示效果) l 步骤3.保存数据:点击保存,填写好文件名,自动会弹出图像的取模值。但是我们在使用的时候一定要注意:前面的注释部分(下图中的蓝色部分)不能写入一会儿的程序中。 二、 做好上面的工作后就可以开始编写程序,对于新手来说,可以逐步来编写。 1、第一个程序:显示“国庆快乐!”程序如下: from mpython import * #导入库文件 display.DispChar('国庆快乐!', 38, 0) #显示字符串(可以是中、英、日、韩文),后面两个参数代表显示的起始数列和横列的像素点的坐标 display.show() #执行显示命令 小编注:小白一定要注意正确的下载和运行方法: 首先:点击连上掌控板后,利用mPython软件编写好程序后点击上面的“刷入”按钮,如下图: 等待掌控板三次蜂鸣后在软件的下方会出现如下两个方框。 其次:选择刚才下载到掌控板的那个文件名,单机右键,选择运行,等待掌控板一次蜂鸣后开始运行程序。如下图: 2、第二个程序:显示“国庆快乐!”加国徽: 程序如下: 程序分为这几个部分 第一部分:调用库文件 第二部分:设置图片的数组,该部分数组来自图像取模工具,就是刚才我们生成的那个c文件, 第三部分:创建缓存,读取图片 第四部分:主程序部分 完整代码如下: [mw_shl_code=python,true]from mpython import * import framebuf import time #图片bitmap数组 bmp = bytearray([\ 0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X3D,0XE0,0X00,0X00,0X00,0X01,0XFD,0XFC, 0X00,0X00,0X00,0X06,0X7D,0XE7,0X00,0X00,0X00,0X19,0X70,0XF5,0XC0,0X00,0X00,0X2F, 0XE8,0XAE,0X20,0X00,0X00,0XFC,0X3F,0XE5,0XF0,0X00,0X01,0XF1,0XFD,0XFD,0X78,0X00, 0X03,0X43,0XFD,0XFE,0X1C,0X00,0X04,0XCF,0XE8,0XBF,0X9A,0X00,0X04,0X9F,0XE0,0X3F, 0XC9,0X00,0X09,0X7F,0XF0,0XFF,0XE5,0X00,0X0B,0X3D,0XF0,0XFF,0XEC,0X80,0X13,0X7C, 0XF6,0X71,0XF6,0X80,0X14,0XF8,0XFF,0XF9,0XFA,0X40,0X1E,0XFF,0XFF,0XFD,0XFB,0XC0, 0X3C,0XFF,0XFF,0XFF,0XF9,0XC0,0X39,0XFF,0XCF,0X1F,0XF8,0XC0,0X29,0XFF,0XC7,0X1F, 0XF9,0XE0,0X29,0XFF,0XCF,0X9F,0XFC,0XE0,0X21,0XFF,0XFF,0XFF,0XFC,0X20,0X21,0XFF, 0XFF,0XFF,0XFC,0X20,0X03,0XFF,0XFF,0XFF,0XFC,0X20,0X29,0XFF,0XFF,0XFF,0XFC,0X20, 0X31,0XFF,0X80,0X1F,0XFD,0XC0,0X18,0XFF,0X80,0X0F,0XF8,0X40,0X1E,0XFF,0X00,0X0F, 0XFB,0XC0,0X1C,0X7F,0X00,0X0F,0XF1,0X80,0X0E,0X7F,0X00,0X07,0XE3,0X80,0X06,0X00, 0X22,0X20,0X02,0X80,0X0A,0X00,0X00,0X20,0X02,0X00,0X04,0X2F,0XFF,0XFF,0X81,0X00, 0X00,0X4F,0XFF,0XFF,0X90,0X00,0X02,0XE3,0XF0,0X7C,0X3A,0X00,0X00,0XE8,0XE0,0X38, 0X30,0X00,0X00,0XF8,0X00,0X00,0XF0,0X00,0X00,0XFC,0X80,0X09,0XF8,0X00,0X00,0XDF, 0XFF,0XEF,0XD8,0X00,0X01,0XE7,0XF0,0X7E,0X78,0X00,0X01,0XF1,0XC0,0X1D,0XF8,0X00, 0X01,0X3B,0XC0,0X0C,0XE4,0X00,0X00,0X8F,0XF0,0X7F,0XE8,0X00,0X00,0X2D,0XFD,0XEF, 0X20,0X00,0X00,0X1C,0X07,0X01,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00, ]) #该部分代码来自取模工具 fb1 = framebuf.FrameBuffer(bmp,45,45, framebuf.MONO_HLSB) #创建128x64大小帧缓存区储存图片帧,水平扫描 #oled display.DispChar('国庆快乐!',38,0) display.blit(fb1,38,16) #将fb1帧缓存送至oled显示,起始位(38,16) display.show()[/mw_shl_code] 3、第三个程序:制作一个交替显示的动画。 其实这不是动画,而是两个“文字+图像”交替显示,程序如下: 这里和上一个程序最大的区别如下: (1)、多了一个图像的数组,所以第二个数组命名时我使用了bmp2(当然您可以自己命名,但不能有重名); (2)、缓存区也有对应的有两个,所以命名也不能重名; (3)、增加了循环、延时、清屏三个命令(见注释)。 完整的程序代码如下: [mw_shl_code=python,true]from mpython import * import framebuf,time #图片bitmap数组 bmp = bytearray([\ 0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X3D,0XE0,0X00,0X00,0X00,0X01,0XFD,0XFC, 0X00,0X00,0X00,0X06,0X7D,0XE7,0X00,0X00,0X00,0X19,0X70,0XF5,0XC0,0X00,0X00,0X2F, 0XE8,0XAE,0X20,0X00,0X00,0XFC,0X3F,0XE5,0XF0,0X00,0X01,0XF1,0XFD,0XFD,0X78,0X00, 0X03,0X43,0XFD,0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代码来自取模工具 fb1 = framebuf.FrameBuffer(bmp,45,45, framebuf.MONO_HLSB) #创建128x64大小帧缓存区储存图片帧,水平扫描 bmp2 = bytearray([\ 0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, 0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X60,0X00,0X14,0X00,0X00,0X00, 0X00,0X00,0X00,0X00,0X01,0XC0,0X00,0X30,0X00,0X00,0X00,0X00,0X00,0X00,0X0B,0X01, 0XC0,0X00,0X1A,0X00,0X00,0X00,0X00,0X00,0X1C,0X1F,0X01,0X60,0X00,0X1C,0X00,0X00, 0X00,0X00,0X00,0X1C,0X0E,0X01,0X80,0X00,0X02,0X00,0X00,0X00,0X00,0X00,0X30,0X00, 0X00,0X00,0X00,0X02,0X00,0X00,0X00,0X00,0X06,0X89,0X40,0X00,0X00,0X00,0X00,0X00, 0X00,0X00,0X00,0X03,0X41,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0XC3, 0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X43,0X40,0X00,0X00,0X00,0X00, 0X01,0X00,0X00,0X00,0X00,0X40,0X00,0X00,0X00,0X00,0X00,0X03,0X00,0X00,0X00,0X00, 0X00,0X00,0X20,0X00,0X00,0X08,0X17,0X00,0X00,0X00,0X00,0X00,0X00,0X30,0X00,0X00, 0X18,0X3B,0X00,0X00,0X00,0X00,0X00,0X00,0X3F,0XFF,0XFF,0XFC,0X0C,0X00,0X00,0X00, 0X00,0X00,0X00,0X6F,0XFF,0XFF,0XE4,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X40,0X00, 0X00,0X06,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XC0,0X00,0X00,0X03,0X00,0X00,0X00, 0X00,0X00,0X00,0X01,0X80,0X00,0X00,0X01,0X80,0X00,0X00,0X00,0X00,0X00,0X03,0X00, 0X00,0X00,0X00,0XC0,0X00,0X00,0X00,0X00,0X00,0X06,0X00,0X00,0X00,0X00,0XE0,0X00, 0X00,0X00,0X00,0X00,0X0F,0XFF,0XFF,0XFF,0XFF,0XE0,0X00,0X00,0X00,0X00,0X00,0X01, 0X80,0X02,0XC0,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X0F,0XFF,0XFF,0XFF,0XFF,0XE0, 0X00,0X00,0X00,0X00,0X00,0X0C,0X00,0X00,0X00,0X00,0X30,0X00,0X00,0X00,0X00,0X00, 0X18,0X00,0X00,0X00,0X00,0X18,0X00,0X00,0X00,0X00,0X00,0X7F,0XFF,0XFF,0XFF,0XFF, 0XFE,0X00,0X00,0X00,0X03,0X2F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X6F,0XC0,0X00,0X03, 0XFF,0X88,0XC4,0X00,0X66,0X31,0X0B,0X7F,0XC0,0X00,0X03,0XB6,0X88,0XC4,0X6C,0XE6, 0X31,0X0B,0X7E,0XC0,0X00,0X0F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XF8,0X00, 0X18,0X03,0XFF,0XFF,0X80,0X80,0X00,0X00,0X00,0X0C,0X00,0X18,0X07,0XFF,0XFF,0XC3, 0XC3,0XFF,0XFF,0XC0,0X0C,0X00,0X10,0X06,0X00,0X00,0X42,0X46,0X00,0X00,0X40,0X04, 0X00,0X10,0X07,0XFF,0XFF,0XC2,0X43,0XFF,0XFF,0XC0,0X04,0X00,0X10,0X00,0X00,0X00, 0X03,0XC0,0X00,0X00,0X00,0X04,0X00,0X10,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, 0X06,0X00,0X30,0X00,0X00,0X00,0X01,0XC0,0X00,0X00,0X00,0X06,0X00,0X30,0X00,0X00, 0X06,0X03,0XE0,0X00,0X00,0X00,0X06,0X00,0X20,0X00,0X1E,0X1F,0X06,0X20,0X70,0X7C, 0X00,0X02,0X00,0X20,0X00,0X33,0X11,0X84,0X30,0XD8,0X44,0X00,0X02,0X00,0X20,0X00, 0X23,0X31,0X84,0X30,0X88,0XC4,0X00,0X02,0X00,0X20,0X00,0X21,0X31,0X84,0X11,0X88, 0XC4,0X00,0X02,0X00,0X20,0X00,0X21,0X30,0X84,0X11,0X88,0XC4,0X00,0X03,0X00,0X60, 0X00,0X21,0X31,0X8C,0X11,0X88,0XC4,0X00,0X03,0X00,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF, 0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,0X00,0X00,0X40,0X00,0X00,0X00,0X00,0X00, ]) fb2 = framebuf.FrameBuffer(bmp2,81,48, framebuf.MONO_HLSB) #创建128x64大小帧缓存区储存图片帧,水平扫描 #oled display.fill(0) display.show() while True: display.DispChar('国庆快乐!',38,0) display.blit(fb1,38,16) #将fb1帧缓存送至oled显示,起始位(38,16) display.show() time.sleep(2) #延时2秒 display.fill(0) #清屏 display.DispChar('祖国万岁!',38,0) display.blit(fb2,20,16) #将fb1帧缓存送至oled显示,起始位(20,16) display.show() time.sleep(2) #延时2秒 display.fill(0) #清屏[/mw_shl_code] 希望能对大家学习有算个帮助,欢迎大家拍砖。 |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed