之前,記錄了從新建項(xiàng)目到點(diǎn)燈,按鈕控制彩燈測(cè)試,再到如何AB32VG1評(píng)估板實(shí)現(xiàn)音樂(lè)播放器!今天通過(guò)對(duì)前邊的經(jīng)驗(yàn)提煉!實(shí)現(xiàn)炫酷音樂(lè)播放器?。?!
硬件如圖:
因用到彩燈需短接J8,J10,J12,TF卡需短接P2的對(duì)應(yīng)pin!
應(yīng)用RT-Thread Studio IDE,可快速實(shí)現(xiàn)對(duì)組件包的配置!
軟件實(shí)現(xiàn)
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);
}
}
}
音樂(lè)播放控制用到了兩個(gè)線程。一個(gè)通過(guò)按鈕事件線程控制音樂(lè)播放!一個(gè)通過(guò)音樂(lè)播放狀態(tài)來(lái)檢測(cè)是否切換歌曲,避免播放一首之后,因無(wú)按鈕操作而停止!
按鈕事件線程:
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);
}
}
}
音樂(lè)播放狀態(tài)來(lái)檢測(cè)線程:
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);}
}
}
}
}
通過(guò)以上主要的線程,在加上前面的【RTT大賽作品連載】AB32VG1評(píng)估板到貨控制彩燈測(cè)試-電子發(fā)燒友網(wǎng) (elecfans.com)的部分代碼即可實(shí)現(xiàn)炫酷音樂(lè)播放器!
-
RTOS
+關(guān)注
關(guān)注
20文章
804瀏覽量
119117 -
開發(fā)板
+關(guān)注
關(guān)注
25文章
4771瀏覽量
96187 -
代碼
+關(guān)注
關(guān)注
30文章
4671瀏覽量
67770 -
RT-Thread
+關(guān)注
關(guān)注
31文章
1239瀏覽量
39441 -
中科藍(lán)訊
+關(guān)注
關(guān)注
9文章
52瀏覽量
9799
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論