聚豐項目 > DEMO板AB32VG1測試播放SD卡播放WAV音樂
用中科藍訊DEMO板AB32VG1 測試RT-thread 制作 SD卡播放WAV音樂和點燈
zsf6853
分享zsf6853
團隊成員
李朝暉 嵌入式研發經理
DEMO板AB32VG1一塊,一張32G SD卡 ,小型USB供電立體聲音箱
首先下載RT-Thread Studio 安裝成功
新建項目 test_SD_WAV 基于AB32VG1開發板,RT-Thread系統
編譯一下沒有問題,只能打印“hello world"
增加點燈程序,下面兩個文件
”RGB.c"
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-11-20 zsf the first version
*/
#include
#include "board.h"
#include "rgb.h"
struct Led_s Led;
void RGB_Init(void)
{
// 獲得 led
Led.LED_R = rt_pin_get("PE.1");
Led.LED_G = rt_pin_get("PE.4");
Led.LED_B = rt_pin_get("PA.2");
// 設置引腳為輸出方式
rt_pin_mode(Led.LED_R, PIN_MODE_OUTPUT);
rt_pin_mode(Led.LED_G, PIN_MODE_OUTPUT);
rt_pin_mode(Led.LED_B, PIN_MODE_OUTPUT);
}
//編寫 rgb 不同顏色點燈驅動,通過 rt_pin_write 來控制 gpio 口電平高低,點亮紅燈即
//拉低紅燈引腳,拉高其他兩個顏色燈的引腳
//傳入參數 on=1:對應亮,on=0:對應滅
//紅燈驅動
void RGB_Red(rt_bool_t on)
{
rt_pin_write(Led.LED_G, PIN_HIGH);
rt_pin_write(Led.LED_B, PIN_HIGH);
if (on)
{
rt_pin_write(Led.LED_R, PIN_LOW);
}
else
{
rt_pin_write(Led.LED_R, PIN_HIGH);
}
}
//藍燈驅動
void RGB_Blue(rt_bool_t on)
{
rt_pin_write(Led.LED_G, PIN_HIGH);
rt_pin_write(Led.LED_R, PIN_HIGH);
if (on)
{
rt_pin_write(Led.LED_B, PIN_LOW);
}
else
{
rt_pin_write(Led.LED_B, PIN_HIGH);
}
}
//綠燈驅動
void RGB_Green(rt_bool_t on)
{
rt_pin_write(Led.LED_R, PIN_HIGH);
rt_pin_write(Led.LED_B, PIN_HIGH);
if (on)
{
rt_pin_write(Led.LED_G, PIN_LOW);
}
else
{
rt_pin_write(Led.LED_G, PIN_HIGH);
}
}
"rgb.h"
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-11-20 zsf the first version
*/
#ifndef APPLICATIONS_RGB_H_
#define APPLICATIONS_RGB_H_
struct Led_s
{
uint8_t LED_R;
uint8_t LED_B;
uint8_t LED_G;
}; // 定義一個 RGB 結構體
void RGB_Init(void);
void RGB_Blue(rt_bool_t on);
void RGB_Green(rt_bool_t on);
void RGB_Red(rt_bool_t on);
#endif /* APPLICATIONS_RGB_H_ */
修改main.c
/*
* Copyright (c) 2020-2021, Bluetrum Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020/12/10 greedyhao The first version
*/
/**
* Notice!
* All functions or data that are called during an interrupt need to be in RAM.
* You can do it the way exception_isr() does.
*/
#include
#include "board.h"
#include "rgb.h"
static void rgb_thread_entry(void* p)
{
RGB_Init();
while(1)
{
rt_thread_mdelay(1000);
rt_kprintf("Blue\r\n");
RGB_Blue(1);
rt_thread_mdelay(1000);
rt_kprintf("Green\r\n");
RGB_Green(1);
rt_thread_mdelay(1000);
rt_kprintf("Red\r\n");
RGB_Red(1);
}
}
static int Thread_RGB(void)
{
rt_thread_t thread = RT_NULL;
thread = rt_thread_create("rgb", rgb_thread_entry, RT_NULL, 512, 10, 10);
if(thread == RT_NULL)
{
rt_kprintf("Thread_GRB Init ERROR");
return RT_ERROR;
}
rt_thread_startup(thread);
}
INIT_APP_EXPORT(Thread_RGB);
int main(void)
{
uint8_t pin = rt_pin_get("PE.1");
rt_pin_mode(pin, PIN_MODE_OUTPUT);
rt_kprintf("Hello, world\n");
while (1)
{
// rt_pin_write(pin, PIN_LOW);
rt_thread_mdelay(500);
// rt_pin_write(pin, PIN_HIGH);
rt_thread_mdelay(500);
}
}
編譯下載可以看到燈閃亮變色,第一步成功!!!
要播放SD卡中的wav文件 ,讓我們領略一下RT-Thread Studio友好和方便
第一步配置SD卡硬件驅動 test_SD_WAV項目管理器下第一行“RT-Thread Settings"->"組件”->"設備驅動程序“如下圖
勾選 ”使用SD/MMC 設備驅動程序“和”使用AUDIO 設備驅動程序”
再到“RT-Thread Settings"->"軟件包”->"multimedia packages“勾選 “WavPlayer..." 如下圖
再到“RT-Thread Settings"->"軟件包”->"miscellaneous packages“ 勾選 ”MultiButton:...."
增加一個wavplay.c 文件
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-12-08 zsf the first version
*/
#include
#ifdef BSP_USING_SDIO
#include
#include
#include
#include "drv_gpio.h"
#include "multi_button.h"
// #define DRV_DEBUG
#define DBG_TAG "app.card"
#include
#define BUTTON_PIN_0 rt_pin_get("PF.0")
#define BUTTON_PIN_1 rt_pin_get("PF.1")
static struct button btn_0;
static struct button btn_1;
uint32_t cnt_channels = 1;
uint32_t cnt_volume = 1;
uint32_t cnt_music = 0;
uint32_t start_flag = 1;
#define NUM_OF_SONGS 2
char *table[NUM_OF_SONGS] =
{
"/Try.wav",
"/Bad.wav",
};
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_0_callback(void *btn)
{
uint32_t btn_event_val;
btn_event_val = get_button_event((struct button *)btn);
rt_kprintf("button0clik\n");
switch(btn_event_val)
{
case SINGLE_CLICK:
if (cnt_volume < 11 )
{
if(start_flag)
{
start_flag = 0;
cnt_volume = (int)saia_volume_get()/10;
// pulse_pulse = 9000;
}
else
{
saia_volume_set(cnt_volume * 10);
//pulse_pulse = cnt_volume*9000;
}
}
else
{
saia_volume_set(10);
cnt_volume = 1;
rt_kprintf("The volume has been adjusted to maximum\n");
}
cnt_volume ++;
rt_kprintf("vol=%d\n", saia_volume_get());
rt_kprintf("button 0 single click\n");
break;
case DOUBLE_CLICK:
if (cnt_channels < 3)
{
saia_channels_set(cnt_channels);
}
else
{
saia_channels_set(cnt_channels);
cnt_channels = 1;
}
cnt_channels++;
rt_kprintf("button 0 double click\n");
break;
case LONG_PRESS_START:
rt_kprintf("button 0 long press start\n");
break;
case LONG_PRESS_HOLD:
rt_kprintf("button 0 long press hold\n");
break;
}
}
static void button_1_callback(void *btn)
{
uint32_t btn_event_val;
btn_event_val = get_button_event((struct button *)btn);
rt_kprintf("button1clik\n");
switch(btn_event_val)
{
case SINGLE_CLICK:
wavplayer_play(table[(cnt_music++) % NUM_OF_SONGS]);
rt_kprintf("button 1 single click\n");
break;
case DOUBLE_CLICK:
rt_kprintf("button 1 double click\n");
break;
case LONG_PRESS_START:
rt_kprintf("button 1 long press start\n");
break;
case LONG_PRESS_HOLD:
rt_kprintf("button 1 long press hold\n");
break;
}
}
static void btn_thread_entry(void* p)
{
while(1)
{
/* 5ms */
rt_thread_delay(RT_TICK_PER_SECOND/200);
button_ticks();
}
}
static int multi_button_wavplayer(void)
{
rt_thread_t thread = RT_NULL;
/* Create background ticks thread */
thread = rt_thread_create("btn", btn_thread_entry, RT_NULL, 512, 10, 10);
if(thread == RT_NULL)
{
return RT_ERROR;
}
rt_thread_startup(thread);
/* 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_0_callback);
button_attach(&btn_0, DOUBLE_CLICK, button_0_callback);
button_attach(&btn_0, LONG_PRESS_START, button_0_callback);
button_attach(&btn_0, LONG_PRESS_HOLD, button_0_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_1_callback);
button_attach(&btn_1, DOUBLE_CLICK, button_1_callback);
button_attach(&btn_1, LONG_PRESS_START, button_1_callback);
button_attach(&btn_1, LONG_PRESS_HOLD, button_1_callback);
button_start (&btn_1);
return RT_EOK;
}
INIT_APP_EXPORT(multi_button_wavplayer);
MSH_CMD_EXPORT(multi_button_wavplayer, button wavplayer)
#endif
這時項目文件情況如下
編譯下載,準備好兩首歌曲WAV模式,改名為Try.wav和Bad.wav,拷貝到SD卡根目錄
上電后,按供電的USB旁邊的鍵可以切換歌曲,相鄰的鍵可以調節音量大小!