之前,记录了从新建项目到点灯,按钮控制彩灯测试,再到如何AB32VG1评估板实现音乐播放器!今天通过对前边的经验提炼!实现炫酷音乐播放器!!!
硬件如图:
因用到彩灯需短接J8,J10,J12,TF卡需短接P2的对应pin!
应用RT-Thread Studio IDE,可快速实现对组件包的配置!
软件实现
led 彩灯的线程如下:
static void led_thread_entry(void* p)
{
uint32_t cnt = 0;
uint8_t pin = rt_pin_get("PE.1");
rt_pin_mode(pin, PIN_MODE_OUTPUT);
uint8_t pin1 = rt_pin_get("PE.4");
rt_pin_mode(pin1, PIN_MODE_OUTPUT);
uint8_t pin2 = rt_pin_get("PA.1");
rt_pin_mode(pin2, PIN_MODE_OUTPUT);
while (1)
{ rt_err_t result = rt_mutex_take(mutex1, 6);
if (result == RT_EOK) {
state = wavplayer_state_get();
rt_mutex_release(mutex1);
if (state== PLAYER_STATE_PLAYING) {
if (cnt % 8 == 0)
{
rt_pin_write(pin, PIN_LOW);
rt_pin_write(pin1, PIN_HIGH);
rt_pin_write(pin2, PIN_HIGH);
}
if (cnt % 8 == 1)
{
rt_pin_write(pin, PIN_HIGH);
rt_pin_write(pin1, PIN_LOW);
rt_pin_write(pin2, PIN_HIGH);
}
if (cnt % 8 == 2)
{
rt_pin_write(pin, PIN_HIGH);
rt_pin_write(pin1, PIN_HIGH);
rt_pin_write(pin2, PIN_LOW);
}
if (cnt % 8 == 3)
{
rt_pin_write(pin, PIN_LOW);
rt_pin_write(pin1, PIN_LOW);
rt_pin_write(pin2, PIN_HIGH);
}
if (cnt % 8 == 4)
{
rt_pin_write(pin, PIN_HIGH);
rt_pin_write(pin1, PIN_LOW);
rt_pin_write(pin2, PIN_LOW);
}
if (cnt % 8 == 5)
{
rt_pin_write(pin, PIN_LOW);
rt_pin_write(pin1, PIN_HIGH);
rt_pin_write(pin2, PIN_LOW);
}
if (cnt % 8 == 6)
{
rt_pin_write(pin, PIN_LOW);
rt_pin_write(pin1, PIN_LOW);
rt_pin_write(pin2, PIN_LOW);
}
if (cnt % 8 == 7)
{
rt_pin_write(pin, PIN_HIGH);
rt_pin_write(pin1, PIN_HIGH);
rt_pin_write(pin2, PIN_HIGH);
}
cnt++;
}else if (state== PLAYER_STATE_PAUSED) {
rt_pin_write(pin, PIN_LOW);
rt_pin_write(pin1, PIN_HIGH);
rt_pin_write(pin2, PIN_HIGH);
rt_thread_mdelay(400);
rt_pin_write(pin, PIN_LOW);
rt_pin_write(pin1, PIN_HIGH);
rt_pin_write(pin2, PIN_LOW);
rt_thread_mdelay(200);
}
else {
rt_pin_write(pin, PIN_LOW);
rt_thread_mdelay(500);
rt_pin_write(pin, PIN_HIGH);
rt_thread_mdelay(500);
}
rt_thread_mdelay(200);
}
}
}
音乐播放控制用到了两个线程。一个通过按钮事件线程控制音乐播放!一个通过音乐播放状态来检测是否切换歌曲,避免播放一首之后,因无按钮操作而停止!
按钮事件线程:
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, 6);
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);
if (result == RT_EOK) {
state = wavplayer_state_get();
rt_mutex_release(mutex1);
if (state == PLAYER_STATE_STOPED) {
EndState = 0;
rt_thread_mdelay(1000);
result = rt_mutex_take(mutex1, 2);
if (result == RT_EOK) {
state = wavplayer_state_get();
if ((state == PLAYER_STATE_STOPED)&& (EndState == 0)) {
EndState = 1;
if (currentSong == NUM_OF_SONGS) {
currentSong = 0;
}
GetCurrentPath();
wavplayer_play(table);
currentSong++;
}
rt_mutex_release(mutex1);}
}
}
}
}
通过以上主要的线程,在加上前面的【RTT大赛作品连载】AB32VG1评估板到货控制彩灯测试-电子发烧友网 (elecfans.com)的部分代码即可实现炫酷音乐播放器!
全部0条评论
快来发表一下你的评论吧 !