# 实现电子相册的全部功能 # 注意啊,pico的双线程模式存在问题,等官方修吧 import machine import os import sdcard import st7789py as st7789 import time import struct import _thread import MMA7660py # 硬件初始化部分--------------------------------------------- # 挂载sd卡 # 灵魂接线,我不得不使用了软件SPI!!! sd_spi=machine.SoftSPI(0,sck=machine.Pin(17,machine.Pin.OUT),mosi=machine.Pin(18,machine.Pin.OUT),miso=machine.Pin(19,machine.Pin.IN)) sd=sdcard.SDCard(sd_spi,cs=machine.Pin(22)) sd.init_spi(40_000_000) sd.init_card() x=os.mount(sd,'/sd') os.chdir('sd/bmpfile') print(os.listdir()) # 定义按键 旋转编码器的按键 thekey=machine.Pin(7,machine.Pin.IN) # 定义lcd_spi引脚 sck=machine.Pin(2,machine.Pin.OUT) mosi=machine.Pin(3,machine.Pin.OUT) rst=machine.Pin(0,machine.Pin.OUT) dc=machine.Pin(1,machine.Pin.OUT) # 定义屏幕信息 width=240 height=240 CENTER_Y = int(width/2) CENTER_X = int(height/2) # 初始化lcd_spi lcd_spi=machine.SPI(0,baudrate=40000000,polarity=1, phase =0,sck=sck,mosi=mosi) print(lcd_spi) # 实例化display对象 display=st7789.ST7789(lcd_spi,width,height,reset=rst,dc=dc,xstart=0,ystart=0,rotation=0) bmplist=os.listdir() print(bmplist) # def playmusic(): # music.play() # # _thread.start_new_thread(playmusic,()) global pwm pwm=machine.PWM(machine.Pin(16)) # 利用定时器中断放音乐 # c调音符的频率 list_freqC=[60000,261,293,329,349,392,440,493] #音符列表,音符和演奏时间 # 欢乐颂 list_music=([3,2],[3,2],[4,2],[5,2], [5,2],[4,2],[3,2],[2,2], [1,2],[1,2],[2,2],[3,2], [3,2],[2,2],[2,2],[0,2], [3,2],[3,2],[4,2],[5,2], [5,2],[4,2],[3,2],[2,2], [1,2],[1,2],[2,2],[3,2], [2,2],[1,2],[1,2],[0,2], [2,2],[2,2],[3,2],[1,2], [2,2],[3,2],[3,2],[1,2], [2,2],[3,2],[3,2],[2,2], [1,2],[2,2],[-5,2],[3,2], [3,2],[3,2],[4,2],[5,2], [5,2],[4,2],[3,2],[2,2], [1,2],[1,2],[2,2],[3,2], [2,2],[1,2],[1,2],[0,2] ) # 播放需要的变量,定时器1通过多次中断播放音乐 global mpos,volume,mlength mpos=0 mlength=len(list_music) volume=0 def interp1(timer): global mpos,mlength if mpos>=mlength: mpos=0 pwm.freq(list_freqC[list_music[mpos][0]]) pwm.duty_u16(volume) timer1.init(freq=list_music[mpos][1],callback=interp1_2) def interp1_2(timer): pwm.duty_u16(0) global mpos mpos=mpos+1 timer1.init(freq=100,callback=interp1) # 定时器2通过2秒的中断查询姿态 mma7660=MMA7660py.MMA7660() mma7660.init() # 初始方向 global pic_up pic_up=2 def interp2(timer): global pic_up result=mma7660.readtilt() # print(result) if result=='unknown!': pic_up=2 # 默认正方向 elif result=='Left': pic_up=3 elif result=='Rignt': pic_up=1 elif result=='Up': pic_up=2 elif result=='down': pic_up=0 timer1=machine.Timer() # 定时器1 timer1.init(freq=1,mode=machine.Timer.PERIODIC,callback=interp1) timer2=machine.Timer() # 定时器2 timer2.init(freq=0.5,mode=machine.Timer.PERIODIC,callback=interp2) # 显示图片 # 图片方向:0 下 2 上 3 右 4 下 #time.sleep(2) # 等待姿态传感器确定方向 #按键中断,按下改变图片刷新方式! pic_method=0 # 显示图片方法参数0,1,2三种 def keyint(irq): # 手册可没说要加irq参数 # display.fill(st7789.RED) global pic_method pic_method=pic_method+1 if pic_method>2: pic_method=0 print('pic_method==',pic_method) time.sleep_ms(100) irq_flag=1 thekey.irq(keyint,machine.Pin.IRQ_FALLING) # 死循环中显示图片和改变显示方向 while True: for xx in bmplist: fid=open(xx,'rb') # 图片显示方向选择 display.rotation(pic_up) # 图片显示方式选择 if pic_method==0: display.showMyBmp(fid) elif pic_method==1: display.showMyBmp2(fid) elif pic_method==2: display.showMyBmp3(fid)