之前,记录了从新建项目到点灯,按钮控制彩灯测试!接下来看看在如何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!有兴趣的爱好者可以在此基础上实现更多或者自己更喜欢的方式!!!
欢迎留言点赞!
喷淋塔远程监控智能运维管理系统解决方案
区块链技术达到主流采用还需克服哪些挑战
人工智能技术助力银行业加快转型步伐进入数字化时代
GP8510是一款高性能PAC芯片,它的功能都有哪些
广和通与华大北斗达成全球战略合作,携手打造高精度GNSS定位解决方案
【RTT大赛作品连载】AB32VG1评估板 音乐播放器
110语音配线架连接方法指南
iRobot:正在研究补丁来修复i7和s9型号机器人
2021真无线蓝牙耳机推荐:618满意度超高的蓝牙耳机排名前十
技术 | 交流伺服电机振动故障的分析与解决方案
探究车企为什么要建设快充网络?
工业互联网平台如何真正为客户创造价值?
为超越台积电,三星将在美国新建晶圆工厂
【新品上市】FG28 SoC支持蓝牙/sub-GHz双频,实现远距离广覆盖Wi-SUN及专有协议连接!
锂电极片瑕疵检测设备功能需求是怎样的
AI芯片NovuTensor是除TPU 世界上跑得最快的单芯片
邻频调制器怎么调整_邻频调制器调试教程
安富利荣获工程师最喜爱的全球分销商
简单盘点一些世界上比较先进的机器人
100G QSFP28 AOC有源光缆的详细介绍