简 介: 在测试了MicroPython对于OLED驱动的基础上,本文给出了将普通的视频转换成OLED可以显示的动画的过程。利用在SD卡中存储的文件,可以将大量视频过程进行播放。在实际过程中,由于MicroPython在读取数据过程中速度比较缓慢,因此在显示动画的动态特性中还不够流畅。
关键词 : OLED,MM32,GIF
I2C OLED
文章目录
MM32F3277
MicroPython
转换图片
显示图片
将数据写入文件
OLED显示
附 件
OLED程序
测试程序
?
§01 I2C OLED
一、MM32F3277 MicroPython
??具有I2C接口 OLED接入MCU非常简单。只需要两根信号线便可以。在 MindMotion MM32F3277 SoftI2C功能测试 测试了OLED通过I2C接入MM32F3277 的 SoftI2C控制端口,通过MicroPython显示字符信息。
▲ 图1.1.1 OLED通过I2C总线连接到MM32F3277
1、OLED显示图片
??OLED显示像素为128×64,总共有1024个字节的数据表示显示的图片。每个像素只有两个状态,所以只能显示图片的边缘信息。
??在显示图片之前需要将图片转换成边缘信息。
▲ 图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)
▲ 图1.2.1 提取边缘结果
2、提取中心256×128图片
imgshape = shape(img)
imgcrop = img[imgshape[0]//2-64:imgshape[0]//2+64,
0:255,:]
▲ 图1.2.2 中心256×128图片
3、图片压缩到128×64
imgcrop = cv2.resize(imgcrop, dsize=(128, 64), interpolation=cv2.INTER_LINEAR)
▲ 图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]
?
§02 显示图片
??编写MicroPython程序,将前面的数据显示在OLED中。
一、将数据写入文件
??将上述数据写入数据文件: 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)
二、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()
▲ 图2.2.1 显示动画
?
※ 附??件 ※
??在测试了MicroPython对于OLED驱动的基础上,本文给出了将普通的视频转换成OLED可以显示的动画的过程。
??利用在SD卡中存储的文件,可以将大量视频过程进行播放。在实际过程中,由于MicroPython在读取数据过程中速度比较缓慢,因此在显示动画的动态特性中还不够流畅。
一、OLED程序
from machine import Pin,SoftI2C
from micropython import const
import utime
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_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)
font8x16 = bytes((0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,
0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,
0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,
0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,
0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,
0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,
0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,
0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,
0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,
0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,
0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,
0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,
0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,
0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,
0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,
0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,
0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,
0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,
0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,
0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,
0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,
0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,
0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,
0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,
0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,
0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,
0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,
0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,
0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,
0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,
0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,
0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,
0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,
0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,
0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,
0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,
0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,
0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,
0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,
0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,
0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,
0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,
0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,
0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,
0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,
0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,
0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,
0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,
0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,
0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,
0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,
0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,
0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,
0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,
0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,
0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,
0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,
0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,
0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,
0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,
0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,
0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,
0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,
0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,
0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,
0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,
0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,
0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00))
class SSD1306():
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)
self.init_display()
def init_display(self):
for cmd in (
SET_DISP | 0x00,
SET_MEM_ADDR, 0x00,
SET_DISP_START_LINE | 0x00,
SET_SEG_REMAP | 0x01,
SET_MUX_RATIO, self.height - 1,
SET_COM_OUT_DIR | 0x08,
SET_DISP_OFFSET, 0x00,
SET_COM_PIN_CFG, 0x02 if self.height == 32 else 0x12,
SET_DISP_CLK_DIV, 0x80,
SET_PRECHARGE, 0x22 if self.external_vcc else 0xf1,
SET_VCOM_DESEL, 0x30,
SET_CONTRAST, 0xff,
SET_ENTIRE_ON,
SET_NORM_INV,
SET_CHARGE_PUMP, 0x10 if self.external_vcc else 0x14,
SET_DISP | 0x01):
self.write_cmd(cmd)
self.fill(0x0)
self.show()
def fill(self, c):
for i in range(len(self.buffer)):
self.buffer[i] = c
def poweroff(self):
self.write_cmd(SET_DISP | 0x00)
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 show(self):
x0 = 0
x1 = self.width - 1
if self.width == 64:
x0 += 32
x1 += 32
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)
def setpoint(self, x,y):
liney = y//8
yy = 1<<(y%8)
self.buffer[liney*self.width+x] |= yy
def setstring(self, x,y,str):
for c in str:
cid = ord(c) - 0x20
cid *= 16
for i in range(8):
ii = y*self.width+x*8+i
self.buffer[ii] = font8x16[cid+i]
self.buffer[ii+self.width] = font8x16[cid+i+8]
x += 1
if x >= self.width//8:
x = 0
y += 1
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)
super().__init__(width, height, external_vcc)
def write_cmd(self, cmd):
self.temp[0] = 0x80
self.temp[1] = cmd
self.i2c.writeto(self.addr, self.temp)
def write_data(self, buf):
self.temp[0] = self.addr << 1
self.temp[1] = 0x40
self.i2c.start()
self.i2c.write(self.temp)
self.i2c.write(buf)
self.i2c.stop()
二、测试程序
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, ' MM32F3277')
oled.setstring(0, 2, 'MicroPython:V2')
oled.show()
print("Test OLED.")
count = 0
while True:
oled.setstring(3, 6, 'Count:%04d'%count)
count += 1
oled.show()
utime.sleep_ms(100)
三、将GIF转换成OLED数据
from headm import *
import cv2
from tsmodule.tsstm32 import *
gifid = 17
giffile = tspgetdopfile(gifid)
gifpage = tspgetgifpage(gifid)
gifdir = os.path.dirname(giffile)
plt.draw()
plt.pause(.1)
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]
num = sum([x*y for x,y in zip(data, c12)])
outd.append(num)
return outd
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)
plt.clf()
plt.subplot(121)
plt.imshow(imgcrop)
plt.subplot(122)
plt.imshow(edge, cmap='gray')
plt.draw()
plt.pause(.1)
pltgif.append(plt)
pltgif.save()
outfile.close()
■ 相关文献链接:
● 相关图表链接:
|