之前,記錄了從新建項目到點燈,按鈕控制彩燈測試!接下來看看在如何AB32VG1評估板實現音樂播放器!
硬件如圖:
要用到TF卡,和美標3.2mm耳機在RT-ThreadStudio的音樂播放器項目到對應開發配置!!!整體配置:
詳細配置如下:
內核設置如圖(注意不要多選,或者少選!!!)
組件如圖:
軟件包如圖:
硬件設置如圖:
以上就是在RT-ThreadStudio的設置,這是我驗證過的設置!!!其他的設置是否可用存在很大問題!!!我自己試過選擇MP3格式,結果異常一大堆!!!所以建議在設置項目的時候盡量要注意!!!我也希望用截圖這種直觀的方式,快速記錄項目設置的每個細節!盡量保證照著這個記錄重新建類似的項目一次成功!!!
下面就是軟件實現!有了前兩次的AB32VG1評估板項目調試經驗!這次相對就順利多了!
軟件邏輯是初始化后先獲取對應目錄下的.wav格式文件列表!在自動播放第一首歌!效果如圖!
接下來就是通過按鍵實現各種操作!如圖:
增加了兩個關聯變量,控制聲音,及播放完畢
配合以下源碼:
/* 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);
即可實現AB32VG1評估板 音樂播放器!
目前只支持.WAV格式,其實一個好的播放器應該支持多種音頻文件格式!如:MP3,AIFF!有興趣的愛好者可以在此基礎上實現更多或者自己更喜歡的方式!!!
歡迎留言點贊!
-
RTOS
+關注
關注
20文章
804瀏覽量
119117 -
RISC
+關注
關注
6文章
460瀏覽量
83566 -
RTThread
+關注
關注
7文章
132瀏覽量
40684 -
中科藍訊
+關注
關注
9文章
52瀏覽量
9799
發布評論請先 登錄
相關推薦
評論