聚丰项目 > 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旁边的键可以切换歌曲,相邻的键可以调节音量大小!
初学习觉得RT-Thread Studio 还是比较容易上手,比较友好!