alienwalker 3 лет назад
Родитель
Сommit
60e7aa2f75
1 измененных файлов с 156 добавлено и 2 удалено
  1. 156 2
      demo/multimedia/Air105/music_demo.lua

+ 156 - 2
demo/multimedia/Air105/music_demo.lua

@@ -27,6 +27,7 @@ local MSG_MD = "moreData"   -- 播放缓存有空余
 local MSG_PD = "playDone"   -- 播放完成所有数据
 local MSG_NEW = "audioNew"  -- 播放新的歌曲
 
+--[[
 audio.on(0, function(id, event)
     if event == audio.MORE_DATA then
         sysplus.sendMsg(taskName, MSG_MD)
@@ -126,8 +127,8 @@ local function audio_task()
         end
         log.info("总共", tFiles)
         prePlay = tFiles - 1
-        log.info("等待切换歌曲")
-        sysplus.waitMsg(taskName, MSG_NEW)
+        -- log.info("等待切换歌曲")
+        -- sysplus.waitMsg(taskName, MSG_NEW)
         while true do
             nowPlay = curPlay
             log.info("开始播放")
@@ -210,5 +211,158 @@ local function audio_task()
     end
     sysplus.taskDel(taskName)
 end
+]]
+
+audio.on(0, function(id, event)
+    sysplus.sendMsg(taskName, MSG_PD)
+end)
+
+
+function moveNext()
+    prePlay = curPlay
+    curPlay = curPlay + 1
+    if curPlay >= tFiles then
+        curPlay = 0
+    end
+    nextPlay = curPlay + 1
+    if nextPlay >= tFiles then
+        nextPlay = 0
+    end
+    log.info("选择下一首")
+    log.info("上一首", prePlay, playList[prePlay])
+    log.info("选择", curPlay, playList[curPlay])
+    log.info("下一首", nextPlay, playList[nextPlay])
+end
+
+function movePre()
+    nextPlay = curPlay
+    curPlay = prePlay
+    if prePlay > 0 then
+        prePlay = prePlay - 1
+    else
+        prePlay = tFiles - 1
+    end
+    log.info("选择上一首")
+    log.info("上一首", prePlay, playList[prePlay])
+    log.info("选择", curPlay, playList[curPlay])
+    log.info("下一首", nextPlay, playList[nextPlay])
+end
+
+local function task_cb(msg)
+    log.info(msg[1], msg[2], msg[3], msg[4])
+end
+
+-- 用软件行列键盘,底层已经做好防抖了,如果用单独按键,自行处理
+sys.subscribe("SOFT_KB_INC", function(port, data, state)
+    -- log.info(data, state)
+    -- 按下起效
+    if state > 0 then
+        if data == 0 then
+            -- 开始播放或者立刻结束当前歌曲并播放下一首
+            sysplus.sendMsg(taskName, MSG_NEW)
+        elseif data == 1 then
+            movePre()
+            -- 如果需要立刻就切换歌曲就取消注释
+            -- sysplus.sendMsg(taskName, MSG_NEW)
+        elseif data == 16 then
+            moveNext()
+            -- 如果需要立刻就切换歌曲就取消注释
+            -- sysplus.sendMsg(taskName, MSG_NEW)
+        elseif data == 17 then
+            isPause = not isPause
+            audio.pause(0, isPause)
+            log.info("暂停状态", isPause)
+        end
+    end
+end)
+
+local function audio_task()
+    softkb.init(0, {pin.PD12, pin.PD13}, {pin.PE1, pin.PE2})
+    local spiId = 2
+    local nowPlay
+    local result = spi.setup(
+        spiId,--串口id
+        255, -- 不使用默认CS脚
+        0,--CPHA
+        0,--CPOL
+        8,--数据宽度
+        400*1000  -- 初始化时使用较低的频率
+    )
+    local TF_CS = pin.PB3
+    gpio.setup(TF_CS, 1)
+    -- fatfs.debug(1) -- 若挂载失败,可以尝试打开调试信息,查找原因
+    fatfs.mount("SD", spiId, TF_CS, 24000000)
+    -- fatfs.mount("SD", 0, TF_CS, 24000000)
+    local data, err = fatfs.getfree("SD")
+    local msg
+    if data then
+        log.info("fatfs", "getfree", json.encode(data))
+        local dir_nums, dir_info = fatfs.lsdir(musicDir)
+        for k,v in pairs(dir_info) do
+            if k:find(".mp3") or k:find(".MP3") or k:find(".wav") or k:find(".WAV") then
+                log.info("找到",k)
+                playList[tFiles] = k
+                tFiles = tFiles + 1
+            end
+        end
+        log.info("总共", tFiles)
+        prePlay = tFiles - 1
+        -- log.info("等待切换歌曲")
+        -- sysplus.waitMsg(taskName, MSG_NEW)
+        while true do
+            nowPlay = curPlay
+            log.info("开始播放")
+            log.info("上一首", prePlay, playList[prePlay])
+            log.info("当前", curPlay, playList[curPlay])
+            log.info("下一首", nextPlay, playList[nextPlay])
+            result = audio.play(0, "/sd".. musicDir .. playList[curPlay])
+            log.info(result)
+            if result then
+                --等待音频通道的回调消息,或者切换歌曲的消息
+                while true do
+                    msg = sysplus.waitMsg(taskName, nil)
+                    if type(msg) == 'table' then
+                        if msg[1] == MSG_PD then
+                            log.info("播放结束")
+                            while not audio.isEnd(0) do
+                                log.info("等待真正结束")    --确保异步进程里没有操作了
+                                sys.wait(100)
+                            end
+                            break
+                        elseif msg[1] == MSG_NEW then
+                            log.info("切换歌曲")
+                            audio.play(0)   -- 发送停止播放请求,但是必须等到播放结束
+                        end
+                    else
+                        if not isPause then
+                            log.error(type(msg), msg)
+                        end
+                    end
+                end
+            else
+                log.debug("/sd".. musicDir .. playList[curPlay], "解码失败!")
+                sys.wait(1000)
+            end
+            audio.stop(0)
+            isPause = false
+            log.info(rtos.meminfo("sys"))
+            log.info(rtos.meminfo("lua"))
+            if nowPlay == curPlay then
+                prePlay = curPlay
+                curPlay = curPlay + 1
+                if curPlay >= tFiles then
+                    curPlay = 0
+                end
+                nextPlay = curPlay + 1
+                if nextPlay >= tFiles then
+                    nextPlay = 0
+                end
+            end
+        end
+    else
+        log.info("fatfs", "err", err)
+    end
+    sysplus.taskDel(taskName)
+end
 
 sysplus.taskInitEx(audio_task, taskName, task_cb)