之前,記錄了從新建項目到點燈,按鈕控制彩燈測試,再到如何AB32VG1評估板實現音樂播放器!今天通過對前邊的經驗提煉!實現炫酷音樂播放器!!!
硬件如圖:
![image.png](https://file.elecfans.com/web2/M00/1E/BA/pYYBAGGXglqAaih4AAurcQeVKQE062.png)
因用到彩燈需短接J8,J10,J12,TF卡需短接P2的對應pin!
應用RT-Thread Studio IDE,可快速實現對組件包的配置!
![image.png](https://file.elecfans.com/web2/M00/1E/BB/pYYBAGGXgymAKaRcAADqbgNsJ7w026.png)
軟件實現
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)的部分代碼即可實現炫酷音樂播放器!
-
RTOS
+關注
關注
22文章
819瀏覽量
119890 -
開發板
+關注
關注
25文章
5121瀏覽量
98214 -
代碼
+關注
關注
30文章
4828瀏覽量
69058 -
RT-Thread
+關注
關注
31文章
1305瀏覽量
40388 -
中科藍訊
+關注
關注
9文章
57瀏覽量
9933
發布評論請先 登錄
相關推薦
RTT大賽作品:AB32VG1開發板OLED顯示實驗
![<b class='flag-5'>RTT</b><b class='flag-5'>大賽</b><b class='flag-5'>作品</b>:<b class='flag-5'>AB32VG1</b>開發<b class='flag-5'>板</b>OLED顯示實驗](https://file.elecfans.com/web2/M00/1B/88/pYYBAGGE5W6ANiFfAADG95L24Nc028.png)
【RTT大賽作品連載】中科藍訊AB32VG1開發板開箱篇
![【<b class='flag-5'>RTT</b><b class='flag-5'>大賽</b><b class='flag-5'>作品</b><b class='flag-5'>連載</b>】中科藍訊<b class='flag-5'>AB32VG1</b>開發<b class='flag-5'>板</b>開箱篇](https://file.elecfans.com//web2/M00/1C/FD/pYYBAGGOg5-AaQa2AA-J9MBxhQg611.png)
評論