之前,記錄了從新建項(xiàng)目到點(diǎn)燈,按鈕控制彩燈測試!接下來看看在如何AB32VG1評估板實(shí)現(xiàn)音樂播放器!
硬件如圖:
要用到TF卡,和美標(biāo)3.2mm耳機(jī)在RT-ThreadStudio的音樂播放器項(xiàng)目到對應(yīng)開發(fā)配置?。。≌w配置:

詳細(xì)配置如下:
	內(nèi)核設(shè)置如圖(注意不要多選,或者少選?。。。?/p>


組件如圖:


軟件包如圖:


硬件設(shè)置如圖:

以上就是在RT-ThreadStudio的設(shè)置,這是我驗(yàn)證過的設(shè)置?。?!其他的設(shè)置是否可用存在很大問題?。?!我自己試過選擇MP3格式,結(jié)果異常一大堆?。?!所以建議在設(shè)置項(xiàng)目的時(shí)候盡量要注意?。。∥乙蚕M媒貓D這種直觀的方式,快速記錄項(xiàng)目設(shè)置的每個(gè)細(xì)節(jié)!盡量保證照著這個(gè)記錄重新建類似的項(xiàng)目一次成功?。。?/p>
下面就是軟件實(shí)現(xiàn)!有了前兩次的AB32VG1評估板項(xiàng)目調(diào)試經(jīng)驗(yàn)!這次相對就順利多了!
軟件邏輯是初始化后先獲取對應(yīng)目錄下的.wav格式文件列表!在自動(dòng)播放第一首歌!效果如圖!

接下來就是通過按鍵實(shí)現(xiàn)各種操作!如圖:

增加了兩個(gè)關(guān)聯(lián)變量,控制聲音,及播放完畢

配合以下源碼:
	/* Copyright (c) 2006-2021, RT-Thread Development Team
	*
	* SPDX-License-Identifier: Apache-2.0
	*
	* Change Logs:
	* Date      Author    Notes
	* 2021-11-12   panshi90   the first version
	*/
	#include 
	#include 
	#include "board.h"
	#include 
	#include "wavplayer.h"
	#include 
	#include 
	#include 
	#include 
	//S3
	#define BUTTON_PIN_0 rt_pin_get("PF.0")
	//S2
	#define BUTTON_PIN_1 rt_pin_get("PF.1")
	//#define NUM_OF_SONGS  (1000u)
	static struct button btn_0;
	static struct button btn_1;
	uint8_t cur_volume = 50;
	int stopstate = 0;
	rt_mutex_t mutex1;
	int EndState = 0;
	static uint32_t cnt_0 = 0;
	//static uint32_t cnt_1 = 0;
	uint16_t currentSong = 0;
	static uint32_t NUM_OF_SONGS = 0;
	char table[200];
	static struct dfs_fd fd1;
	static struct dirent dirent1;
	void readFileListos()
	{
	 struct stat stat;
	 int length, fd;
	 char* fullpath, * path;
	 fullpath = RT_NULL;
	 NUM_OF_SONGS = 1;
	 //system("cd .");
	 rt_thread_mdelay(10);
	 rm("wav.txt");
	 rt_thread_mdelay(50);
	 fd = open("wav.txt", O_WRONLY | O_CREAT | O_APPEND);
	 path = rt_strdup("/WAV");
	 if (dfs_file_open(&fd1, path, O_DIRECTORY) == 0)
	 {
	   //rt_kprintf("Directory %s:\n", path);
	   do
	   {
	     memset(&dirent1, 0, sizeof(dirent1));
	     length = dfs_file_getdents(&fd1, &dirent1, sizeof(dirent1));
	     if (length > 0)
	     {
	       memset(&stat, 0, sizeof(struct stat));
	       // build full path for each file
	       fullpath = dfs_normalize_path(path, dirent1.d_name);
	       if (fullpath == RT_NULL)
	         break;
	       if (dfs_file_stat(fullpath, &stat) == 0)
	       {
	         char songPath[200] = "WAV/";
	         strcat(songPath, dirent1.d_name);
	         strcat(songPath, "\n");
	         write(fd, songPath, sizeof(songPath));
	         //rt_kprintf("%s\n", songPath);
	         if (S_ISDIR(stat.st_mode))
	         {
	           rt_kprintf("%-25s\n", "
	   while (read(fd, songPath, sizeof(songPath)) > 0)
	   {
	     if (i == currentSong) {
	       int32_t len = strlen(songPath);
	       songPath[len - 1] = '\0';
	       strcpy(table, songPath);
	     }
	     i++;
	   }
	 }
	 close(fd);
	}
	void saia_channels_set(uint8_t channels);
	void saia_volume_set(rt_uint8_t volume);
	uint8_t saia_volume_get(void);
	static uint8_t button_read_pin_0(void)
	{
	 return rt_pin_read(BUTTON_PIN_0);
	}
	static uint8_t button_read_pin_1(void)
	{
	 return rt_pin_read(BUTTON_PIN_1);
	}
	static void button_S3_callback(void* btn)
	{
	 uint32_t btn_event_val;
	 btn_event_val = get_button_event((struct button*)btn);
	 switch (btn_event_val)
	 {
	 case SINGLE_CLICK:
	   cnt_0++;
	   cur_volume = cnt_0 * 10;
	   if (cnt_0 == 10)
	   {
	     cnt_0 = 1;
	   }
	   saia_volume_set(cur_volume);
	   rt_kprintf("vol=%d\n", saia_volume_get());
	   rt_kprintf("button S3 single click\n");
	   break;
	 case DOUBLE_CLICK:
	   if (cnt_0 > 1)
	   {
	     cnt_0--;
	   }
	   cur_volume = cnt_0 * 10;
	   wavplayer_volume_set(cur_volume);
	   rt_kprintf("button S3 double click\n");
	   break;
	 case LONG_PRESS_START:
	   rt_kprintf("button S3 long press start\n");
	   break;
	 case LONG_PRESS_HOLD:
	   rt_kprintf("button S3 long press hold\n");
	   break;
	 }
	}
	static void button_S2_callback(void* btn)
	{
	 uint32_t btn_event_val;
	 int state = 0;
	 EndState = 1;
	 btn_event_val = get_button_event((struct button*)btn);
	 switch (btn_event_val)
	 {
	 case SINGLE_CLICK:
	   if (currentSong == NUM_OF_SONGS) {
	     currentSong = 0;
	   }
	   GetCurrentPath();
	   stopstate = 0;
	   wavplayer_play(table);
	   currentSong++;
	   rt_kprintf("button S2 single click\n");
	   break;
	 case DOUBLE_CLICK:
	   state = wavplayer_state_get();
	   switch (state)
	   {
	   case PLAYER_STATE_PLAYING:
	     wavplayer_pause();
	     break;
	   case PLAYER_STATE_PAUSED:
	     wavplayer_resume();
	     break;
	   case PLAYER_STATE_STOPED:
	     GetCurrentPath();
	     wavplayer_play(table);
	     rt_kprintf("button S2 double click\n");
	     break;
	   default:
	     break;
	   }
	 default:
	   break;
	 }
	 stopstate = 0;
	 EndState = 0;
	}
	static void btn_thread_entry(void* p)
	{
	 while (1)
	 {
	   rt_thread_delay(RT_TICK_PER_SECOND / 500);
	   rt_err_t result = rt_mutex_take(mutex1, 2);
	   if (result == RT_EOK) {
	     button_ticks();
	     rt_mutex_release(mutex1);
	   }
	 }
	}
	static void endCheck_thread_entry(void* p)
	{
	 while (1)
	 {
	   rt_thread_mdelay(2500);
	   rt_err_t result = rt_mutex_take(mutex1, 2);
	   int  state = wavplayer_state_get();
	   if (result == RT_EOK) {
	     if ((state == PLAYER_STATE_STOPED) && (EndState == 0)) {
	       if ((stopstate == 1)) {
	         stopstate = 0;
	         if (currentSong == NUM_OF_SONGS) {
	           currentSong = 0;
	         }
	         GetCurrentPath();
	         wavplayer_play(table);
	         currentSong++;
	       }
	     }
	     rt_mutex_release(mutex1);
	   }
	 }
	}
	static int multi_button_test(void)
	{
	 rt_thread_t thread = RT_NULL, thread1 = RT_NULL;
	 mutex1 = rt_mutex_create("xx", RT_IPC_FLAG_PRIO);
	 //Create background ticks thread
	 rt_thread_mdelay(2000);
	 readFileListos();
	 GetCurrentPath();
	 saia_volume_set(cur_volume);
	 wavplayer_play(table);
	 currentSong++;
	 thread = rt_thread_create("btn", btn_thread_entry, RT_NULL, 2048, 10, 10);
	 thread1 = rt_thread_create("endCHeck", endCheck_thread_entry, RT_NULL, 2000, 11, 10);
	 if (thread == RT_NULL)
	 {
	   return RT_ERROR;
	 }
	 rt_thread_startup(thread);
	 if (thread1 == RT_NULL)
	 {
	   return RT_ERROR;
	 }
	 rt_thread_startup(thread1);
	 // low level drive
	 rt_pin_mode(BUTTON_PIN_0, PIN_MODE_INPUT_PULLUP);
	 button_init(&btn_0, button_read_pin_0, PIN_LOW);
	 button_attach(&btn_0, SINGLE_CLICK, button_S3_callback);
	 button_attach(&btn_0, DOUBLE_CLICK, button_S3_callback);
	 button_attach(&btn_0, LONG_PRESS_START, button_S3_callback);
	 button_attach(&btn_0, LONG_PRESS_HOLD, button_S3_callback);
	 button_start(&btn_0);
	 rt_pin_mode(BUTTON_PIN_1, PIN_MODE_INPUT_PULLUP);
	 button_init(&btn_1, button_read_pin_1, PIN_LOW);
	 button_attach(&btn_1, SINGLE_CLICK, button_S2_callback);
	 button_attach(&btn_1, DOUBLE_CLICK, button_S2_callback);
	 button_attach(&btn_1, LONG_PRESS_START, button_S2_callback);
	 button_attach(&btn_1, LONG_PRESS_HOLD, button_S2_callback);
	 button_start(&btn_1);
	 return RT_EOK;
	}
INIT_APP_EXPORT(multi_button_test);
即可實(shí)現(xiàn)AB32VG1評估板 音樂播放器!
目前只支持.WAV格式,其實(shí)一個(gè)好的播放器應(yīng)該支持多種音頻文件格式!如:MP3,AIFF!有興趣的愛好者可以在此基礎(chǔ)上實(shí)現(xiàn)更多或者自己更喜歡的方式?。?!
	歡迎留言點(diǎn)贊!
	
- 
                                RTOS
                                +關(guān)注
關(guān)注
24文章
858瀏覽量
122422 - 
                                RISC
                                +關(guān)注
關(guān)注
6文章
484瀏覽量
85863 - 
                                RTThread
                                +關(guān)注
關(guān)注
8文章
132瀏覽量
42412 - 
                                中科藍(lán)訊
                                +關(guān)注
關(guān)注
9文章
63瀏覽量
10486 
發(fā)布評論請先 登錄
【精選資料】開發(fā)板測評大賽作品合集!
    
【作品精選】2025電子發(fā)燒友開發(fā)板測評大賽作品合集!
【作品合集】創(chuàng)龍科技TLT113/TL3562-MiniEVM開發(fā)板測評
    Made with KiCad(135):Echo - 開源的音樂播放器
    
    【開源分享】用ESP32復(fù)刻一個(gè)iPod :便攜式音樂播放器Tangara
    
Made with KiCad:Tangara 便攜式音樂播放器
Made with KiCad(120):Tangara 便攜式音樂播放器
    
海貝HiBy R1播放器體驗(yàn)
    
云服務(wù)器 Flexus X 實(shí)例,Docker 集成搭建 YesPlayMusic 網(wǎng)易云音樂播放器
    
索尼發(fā)布空間現(xiàn)實(shí)顯示播放器新版本
畢業(yè)設(shè)計(jì)競賽選題推薦 | 嵌入式Linux應(yīng)用之音樂播放器項(xiàng)目實(shí)戰(zhàn)(含文檔及源碼)
    
海貝R1便攜音樂播放器開箱
    
          
        
        
【RTT大賽作品連載】AB32VG1評估板 音樂播放器
                
 
           
            
            
                
            
評論