本案例制作一个联网下载声音文件的MP3播放器。
本案例需要的硬件
器材 | 数量 |
---|---|
HaaS506开发板 | 1 |
JC025串口屏(480x320) | 1 |
触摸按键 | 1 |
SIM卡 | 1 |
喇叭 | 1 |
SD卡 | 1 |
1.代码总共4个文件
2.如果要显示中文代码烧录时选择GB2312编码
mian.py
import net_connect
import mp3
import utime
from driver import UART
from driver import GPIO#按键使能
key1=GPIO(10,10)
key2=GPIO(10,10)
key1.open("key1")
key2.open("key2")
#菜单目录
data = {'test1.mp3':'http://music.szyywdz.huntercat.cn/haas506.mp3','test2.mp3':'http://music.szyywdz.huntercat.cn/audio/m601test.mp3','test3.mp3':'http://music.szyywdz.huntercat.cn/audio/syserror.mp3'}
num = ['test1.mp3','test2.mp3','test3.mp3']
name = num[0]
#按键
buttons1 = 4
buttons2 = 0
buttons3 = 0
degree = 0
play_stop = True#切换键
def key1_callback(args):global degree,play_stop,buttons1,buttons2,buttons3print('-------- 切换 ---------')play_stop = True#设定按键次数degree += 1if degree > 2:degree = 0if degree == 0:buttons1 = 4buttons2 = 0buttons3 = 0if degree == 1:buttons1 = 0buttons2 = 4buttons3 = 0if degree == 2:buttons1 = 0buttons2 = 0buttons3 = 4#获取文件个数catalogue = mp3.search()UART_send("DCV32(120,5,'在线音乐播放器',1);BTN(40,50,400,60,%s,1,5,2,%d);BTN(40,110,400,60,%s,1,5,2,%d);BTN(40,170,400,60,%s,1,5,2,%d);BTN(60,230,360,40,'本地文件个数: %d',1,0,1,0);BTN(40,270,64,32,'下载播放',0,0,2,0);BTN(400,270,64,32,'切换',0,0,5,0);\r\n"%(num[0],buttons1,num[1],buttons2,num[2],buttons3,catalogue))utime.sleep_ms(100)key1.disableIrq()key1.clearIrq()#播放&删除键
def key2_callback(args):global degree,play_stop,buttons1,buttons2,buttons3print('======= 播放 =========')if play_stop: #播放print(data[num[degree]],num[degree],'----------------------------------------------')mp3.download(data[num[degree]],num[degree])catalogue = mp3.search()UART_send("DCV32(120,5,'在线音乐播放器',1);BTN(40,50,400,60,%s,1,5,2,%d);BTN(40,110,400,60,%s,1,5,2,%d);BTN(40,170,400,60,%s,1,5,2,%d);BTN(60,230,360,40,'本地文件个数: %d',1,0,1,0);BTN(40,270,64,32,'本地删除',0,0,1,0);BTN(400,270,64,32,'切换',0,0,5,0);\r\n"%(num[0],buttons1,num[1],buttons2,num[2],buttons3,catalogue))play_stop = Falsemp3.play(num[degree])else: #删除mp3.delete(num[degree])play_stop = Truecatalogue = mp3.search()UART_send("DCV32(120,5,'在线音乐播放器',1);BTN(40,50,400,60,%s,1,5,2,%d);BTN(40,110,400,60,%s,1,5,2,%d);BTN(40,170,400,60,%s,1,5,2,%d);BTN(60,230,360,40,'本地文件个数: %d',1,0,1,0);BTN(40,270,64,32,'下载播放',0,0,2,0);BTN(400,270,64,32,'切换',0,0,5,0);\r\n"%(num[0],buttons1,num[1],buttons2,num[2],buttons3,catalogue))utime.sleep_ms(100)key2.disableIrq()key2.clearIrq()def UART_send(data):writeBuf0=bytearray(data)uart0.write(writeBuf0)if __name__ == '__main__':uart0=UART()uart0.open("serial1")utime.sleep_ms(1000)UART_send("CLR(0);DIR(3);\r\n")net_connect.connect_network()key1.enableIrq(key1_callback)key2.enableIrq(key2_callback) catalogue = mp3.search()UART_send("DCV32(120,5,'在线音乐播放器',1);BTN(40,50,400,60,%s,1,5,2,%d);BTN(40,110,400,60,%s,1,5,2,%d);BTN(40,170,400,60,%s,1,5,2,%d);BTN(60,230,360,40,'本地文件个数: %d',1,0,1,0);BTN(40,270,64,32,'下载播放',0,0,2,0);BTN(400,270,64,32,'切换',0,0,5,0);\r\n"%(num[0],buttons1,num[1],buttons2,num[2],buttons3,catalogue))while True:utime.sleep_ms(10)
mp3.py
import uos
import utime
import http
from audio import Audiouos.mountsd()def download(url,name):print('filename in directory:',uos.listdir('/sdcard0'))htp=http.client()htp.download(url,'/sdcard0/'+name)print('filename in directory:',uos.listdir('/sdcard0'))catalogue = []
def search():global cataloguecatalogue = uos.listdir('/sdcard0')return len(catalogue)-1def delete(name):print('before removing files:',uos.listdir('/sdcard0'))uos.remove('/sdcard0/'+name)print('after removing files:',uos.listdir('/sdcard0'))def play(name):print('------------------------Audio play start--------------------')aud=Audio()aud.set_pa()aud.setVolume(5)utime.sleep(1)aud.play('/sdcard0/'+name)# utime.sleep(3)# aud.stop()# print('------------------------Audio play done--------------------')
net_connect.py
import network
import utimeg_connect_status = False
def on_4g_cb(args):global g_connect_statuspdp = args[0]netwk_sta = args[1]if netwk_sta == 1:g_connect_status = Trueelse:g_connect_status = Falsedef connect_network():global net,on_4g_cb,g_connect_statusnet = network.NetWorkClient()g_register_network = Falseif net._stagecode is not None and net._stagecode == 3 and net._subcode == 1:g_register_network = Trueelse:g_register_network = Falseif g_register_network:net.on(1,on_4g_cb)net.connect(None)else:print('!!! connect_network false !!!')while True:if g_connect_status:print('connect_network ok')breakutime.sleep_ms(20)
board.json
{"name": "haas506","version": "2.0.0","io": {"key1": {"type": "GPIO","port": 44,"dir": "irq","pull": "pullup","intMode":"falling"},"key2": {"type": "GPIO","port": 20,"dir": "irq","pull": "pullup","intMode":"falling"},"serial1":{"type":"UART","port":0,"dataWidth":8,"baudRate":115200,"stopBits":1,"flowControl":"disable","parity":"none"},"serial2":{"type":"UART","port":1,"dataWidth":8,"baudRate":115200,"stopBits":1,"flowControl":"disable","parity":"none"},"serial3":{"type":"UART","port":2,"dataWidth":8,"baudRate":115200,"stopBits":1,"flowControl":"disable","parity":"none"}},"debugLevel": "ERROR","repl":"enable","replPort":0}
1.上电后屏幕显示如下