“簡(jiǎn) 介: 完整的開源代碼以及移植說明正在整理當(dāng)中。建立共同的生態(tài)圈,使得更多人加入應(yīng)用模塊中,豐其羽翼、壯其筋骨。讓更多非專業(yè)人士體會(huì)編程快樂。
關(guān)鍵詞:
MM32,MicroPython,OLED
01 夸下海口
▲ 圖1 想當(dāng)年,第六屆智能車競(jìng)賽,吃雞腿這個(gè)男人(MindMotion, 蘇勇),在10月17在上海一家火鍋店 邊吃飯,邊回想當(dāng)年參加智能車競(jìng)賽[1] 的時(shí)候,邊夸下??冢衲昴軌蜃屚瑢W(xué)們?cè)贛indMotion單片機(jī)上使用MicroPython開發(fā)車模作品。
時(shí)間過去了一個(gè)多月了,他在原來空空蕩蕩的 MicroPython內(nèi)核上,針對(duì)MM32F3277單片機(jī)實(shí)現(xiàn)了那些功能呢?
下面是使用 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']
>>>
雖距離羽翼豐滿還有一段距離,但將自己裝進(jìn)OLED進(jìn)行顯示還是可以的了。
02 冰箱裝大象
一共分幾步?
一、MM32F3277 MicroPython
具有I2C接口 OLED接入MCU非常簡(jiǎn)單。只需要兩根信號(hào)線便可以。在 MindMotion MM32F3277 SoftI2C功能測(cè)試[2] 測(cè)試了OLED通過I2C接入MM32F3277 的 SoftI2C控制端口,通過MicroPython顯示字符信息。
▲ 圖1.1.1 OLED通過I2C總線連接到MM32F3277
1、OLED顯示圖片
OLED顯示像素為128×64,總共有1024個(gè)字節(jié)的數(shù)據(jù)表示顯示的圖片。每個(gè)像素只有兩個(gè)狀態(tài),所以只能顯示圖片的邊緣信息。
在顯示圖片之前需要將圖片轉(zhuǎn)換成邊緣信息。
▲ 圖1.1.2 OLED像素對(duì)應(yīng)的1024字節(jié)編碼
二、轉(zhuǎn)換圖片
對(duì)于顯示的圖片轉(zhuǎn)換包括有兩個(gè)過程:
-
將圖片轉(zhuǎn)換成給出邊緣圖片;
-
將圖片的尺寸切割成128×64;
轉(zhuǎn)換完之后,便可以將其編碼成1024 個(gè)字節(jié),傳送到MicroPython刷新到OLED中,或者將數(shù)據(jù)存儲(chǔ)在文件中,預(yù)先存儲(chǔ)在SD卡中,由MicroPython讀取顯示。
1、提取邊緣
使用cv2中的Canny算子提取圖片中的邊緣。
filename = os.path.join(gifdir, 'd.BMP'%i)
img = cv2.imread(filename)[:,:,::-1]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edge = cv2.Canny(gray, 100, 200)
▲ 圖1.2.1 提取邊緣結(jié)果
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 轉(zhuǎn)換成128×64點(diǎn)陣
4、轉(zhuǎn)換成字節(jié)
根據(jù)【1.1.1:OLED顯示圖片】中的OLED的數(shù)據(jù)編碼方式,將邊緣圖片轉(zhuǎn)換成1024個(gè)字節(jié)。
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
下面是其中衣服圖片轉(zhuǎn)換后的字節(jié)。
[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程序,將前面的數(shù)據(jù)顯示在OLED中。
1、將數(shù)據(jù)寫入SD卡文件
將上述數(shù)據(jù)寫入數(shù)據(jù)文件: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, 'd.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 'x'%d
strdata = strdata '\n'
outfile.write(strdata)
2、在OLED上顯示
在MicroPython通過讀取GIF.TXT中的數(shù)據(jù),顯示在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 顯示動(dòng)畫
03 開源在即
完整的開源代碼以及移植說明正在整理當(dāng)中。建立共同的生態(tài)圈,使得更多人加入應(yīng)用模塊中,豐其羽翼、壯其筋骨,讓更多非專業(yè)人士體會(huì)MicroPython編程快樂。
參考資料
[1]邊吃飯,邊回想當(dāng)年參加智能車競(jìng)賽: https://mp.weixin.qq.com/s?__biz=MzA5NjQyNjc2NQ
(全球TMT2022年2月24日訊)全球數(shù)字化轉(zhuǎn)型領(lǐng)導(dǎo)者M(jìn)icroland獲得卓越職場(chǎng)(Great Place to Work?)認(rèn)證。Great Place to Work?機(jī)構(gòu)授予這種認(rèn)可是基于從他們現(xiàn)有員工收到的...
關(guān)鍵字: Micro印度班加羅爾、英國(guó)倫敦和美國(guó)亞特蘭大2022年2月23日 /美通社/ -- 全球數(shù)字化轉(zhuǎn)型領(lǐng)導(dǎo)者M(jìn)icroland抵達(dá)了另一個(gè)里程碑 -- 獲得卓越職場(chǎng)(Great Plac...
關(guān)鍵字: Micro(全球TMT2022年2月2日訊)印度數(shù)字IT轉(zhuǎn)型公司Microland宣布Navneet Khandelwal將加入公司班加羅爾總部,擔(dān)任首席財(cái)務(wù)官。Navneet將接替S Raja Gopalan擔(dān)任公司首席財(cái)務(wù)官...
關(guān)鍵字: Micro(全球TMT2021年11月26日訊)全球數(shù)字化轉(zhuǎn)型領(lǐng)導(dǎo)企業(yè)Microland宣布,其在2021-2022年Avasant RadarView™智能自動(dòng)化服務(wù)報(bào)告中被評(píng)為“創(chuàng)新企業(yè)”。Avasant集團(tuán)評(píng)...
關(guān)鍵字: Micro倫敦、亞特蘭大和印度班加羅爾2021年11月26日 /美通社/ -- 全球數(shù)字化轉(zhuǎn)型領(lǐng)導(dǎo)企業(yè)Microland今日欣然宣布,其在2021-2022年Avasant Radar...
關(guān)鍵字: Micro倫敦、亞特蘭大和印度班加羅爾2021年11月19日 /美通社/ -- 全球數(shù)字化轉(zhuǎn)型服務(wù)公司Microland今天宣布,該公司被評(píng)為托管網(wǎng)絡(luò)服務(wù)領(lǐng)域Gartner®魔...
關(guān)鍵字: Micro