0914

上级 05a89197
import array, time, gc
from math import *
from cmath import *
from machine import Pin, SoftI2C,I2S
from ssd1306 import SSD1306_I2C
import framebuf
import time
import uasyncio
i2c = SoftI2C(sda=Pin(18), scl=Pin(19))
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)
import framebuf
'''
左右各留 20px 的边
左右眼睛 36x36
左右眼中间有空隙看起来比较自然(空隙位16px)
嘴巴是 80x28
'''
eye_l_x=20
eye_l_y=0
eye_r_x=72
eye_r_y=0
eye_w=36
eye_h=36
mouth_x=24
mouth_y=36
mouth_w=80
mouth_h=28
def pbm_to_fb(filename):
fblist=[]
for i in range(1,30):
#print("//images//"+filename+str(i)+".pbm")
try:
with open("//images//"+filename+str(i)+".pbm",'rb') as f:
f.readline()
width,height=[int(v) for v in f.readline().split()]
data=bytearray(f.read())
#print(data)
#print(width,height)
fb=framebuf.FrameBuffer(data,width,height,framebuf.MONO_HLSB)
fblist.append(fb)
f.close()
except:
return fblist
eye_blink_list=pbm_to_fb("eye_blink")
mouth_talk_list=pbm_to_fb("mouth_talk")
async def eyes(action,delay):
print('eyes move')
for fb in action:
oled.blit(fb,eye_l_x,eye_l_y)
oled.blit(fb,eye_r_x,eye_r_y)
await uasyncio.sleep_ms(delay)
oled.show()
async def mouth(action,delay):
for fb in action:
oled.blit(fb,mouth_x,mouth_y)
await uasyncio.sleep_ms(delay)
oled.show()
async def main():
print('main start')
uasyncio.create_task(eyes(eye_blink_list, 10))
uasyncio.create_task(mouth(mouth_talk_list, 50))
print('out')
uasyncio.run(main())
文件已添加
此差异已折叠。
......@@ -36,3 +36,4 @@ with open("buf.txt","w") as f:
f.write(buf)
f.write('\r')
f.close()
\ No newline at end of file
import array, time, gc
from math import *
from cmath import *
from machine import Pin, SoftI2C,I2S
from ssd1306 import SSD1306_I2C
import framebuf
import time
import uasyncio
import _thread
import random
i2c = SoftI2C(sda=Pin(18), scl=Pin(19))
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)
import framebuf
'''
左右各留 20px 的边
左右眼睛 36x36
左右眼中间有空隙看起来比较自然(空隙位16px)
嘴巴是 80x28
'''
eye_l_x=20
eye_l_y=0
eye_r_x=72
eye_r_y=0
eye_w=36
eye_h=36
mouth_x=24
mouth_y=36
mouth_w=80
mouth_h=28
def pbm_to_fb(filename):
fblist=[]
for i in range(1,30):
#print("//images//"+filename+str(i)+".pbm")
try:
with open("//images//"+filename+str(i)+".pbm",'rb') as f:
f.readline()
width,height=[int(v) for v in f.readline().split()]
data=bytearray(f.read())
#print(data)
#print(width,height)
fb=framebuf.FrameBuffer(data,width,height,framebuf.MONO_HLSB)
fblist.append(fb)
f.close()
except:
return fblist
eye_blink_list=pbm_to_fb("eye_blink")
mouth_talk_list=pbm_to_fb("mouth_talk")
def eyes():
action=eye_blink_list
while 1:
delay=random.randint(1, 10)
for fb in action:
oled.blit(fb,eye_l_x,eye_l_y)
oled.blit(fb,eye_r_x,eye_r_y)
time.sleep_ms(delay)
oled.show()
time.sleep_ms(random.randint(500, 4000))
def mouth():
action=mouth_talk_list
while 1:
delay=random.randint(10, 50)
for fb in action:
oled.blit(fb,mouth_x,mouth_y)
time.sleep_ms(delay)
oled.show()
time.sleep_ms(random.randint(20, 1678))
_thread.start_new_thread(eyes, ())
_thread.start_new_thread(mouth, ())
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册