📄 player.c
字号:
#include "player.h"
#include "ff.h"
#include "diskio.h"
#include "vs1003.h"
#include "ch375.h"
#define BUF_SIZE (512)
/* FatFs structures */
FATFS fatSD; /* FatFs structure for SD */
DIR dirSD;
FILINFO finfoSD;
FIL filSD;
FATFS fatUSB; /* FatFs structure for USB */
DIR dirUSB;
FILINFO finfoUSB;
FIL filUSB;
FIL filHND;
unsigned int cur_file_size = 0;
unsigned char wBuff[BUF_SIZE] = {0};
/* test.txt */
/* test.mp3 */
void Player_Init(void)
{
unsigned char res = 0;
unsigned long len = 0;
unsigned short br = 0;
FATFS *fatf = &fatSD;
FATFS *fatfUSB = &fatUSB;
/* VS 1003 init */
/* NOTE: SPI should be initialized */
/* when init, the SPI clk speed shoud be low */
/* no more than (1/6 * CLKI) */
VS1003_Init();
/* init done, the SPI clk speed can be changed up for fast operation */
/****************************************************************************/
/* SD */
f_mount(DRV_SD, &fatUSB);
/* disk_initialize(DRV_USB) will be DONE in FatFs */
/* GET disk info for test */
disk_ioctl(DRV_SD, MMC_GET_CSD, wBuff);
disk_ioctl(DRV_SD, MMC_GET_CID, wBuff);
disk_ioctl(DRV_SD, MMC_GET_OCR, wBuff);
disk_ioctl(DRV_SD, ATA_GET_MODEL, wBuff);
disk_ioctl(DRV_SD, GET_SECTOR_COUNT, wBuff);
f_getfree("0:/", &len, &fatf);
len = len;
/* get all the dir/file under root */
/* test purpose */
res = f_opendir(&dirSD, "0:/");
/* try to find all the folders/files under ROOT */
if (FR_OK == res)
{
while(1)
{
res = f_readdir(&dirSD, &finfoSD);
if ((res != FR_OK) || !finfoSD.fname[0])
{
// end of operation
break;
}
}
}
#if 0
/* test 1 : read txt file */
res = f_open(&filSD, "0:/test.txt", FA_OPEN_EXISTING | FA_READ);
if (FR_OK == res)
{
f_read(&filSD, wBuff, 10, &br);
f_close(&filSD);
}
/* test 3 : create file */
for (res = 0; res < 26; res ++) wBuff[res] = res + 0x41; // A ~ Z
res = f_open(&filSD, "0:/write.txt", FA_CREATE_ALWAYS | FA_WRITE);
if (FR_OK == res)
{
f_write(&filSD, wBuff, 26, &br);
f_close(&filSD);
}
#endif
Player_play_file("0:/test.mp3"); /* qqqg 128kbps */
Player_play_file("0:/test1.mp3"); /* wlzmk 320kbps */
Player_play_file("0:/test2.mp3"); /* jht */
//Player_play_file("0:/test3.mp3"); /* wbhh */
f_mount(DRV_SD, 0); /* unmount DRV_SD */
VS1003_WAIT_RDY();
len = VS1003_SCI_Read(SPI_HDAT0);
VS1003_WAIT_RDY();
len = VS1003_SCI_Read(SPI_HDAT1);
len = len;
/****************************************************************************/
/* USB */
#if 1
//VS1003_Init();
f_mount(DRV_USB, &fatUSB);
/* disk_initialize(DRV_USB) will be DONE in FatFs */
/* test_dr1.txt 1.mp3 */
res = disk_ioctl(DRV_USB, USB_GET_SIZE, wBuff);
res = f_getfree("1:/", &len, &fatfUSB);
len = len;
/* get all the dir/file under root */
/* test purpose */
res = f_opendir(&dirUSB, "1:/");
/* try to find all the folders/files under ROOT */
if (FR_OK == res)
{
while(1)
{
res = f_readdir(&dirUSB, &finfoUSB);
if ((res != FR_OK) || !finfoUSB.fname[0])
{
// end of operation
break;
}
}
}
Player_play_file("1:/duandian.mp3");
f_mount(DRV_USB, 0); /* unmount DRV_USB */
VS1003_WAIT_RDY();
len = VS1003_SCI_Read(SPI_HDAT0);
VS1003_WAIT_RDY();
len = VS1003_SCI_Read(SPI_HDAT1);
len = len;
#endif
}
/****************************************************************************/
/* play a complete audio file */
void Player_play_file(const char *file)
{
unsigned char res = 0;
unsigned long len = 0;
unsigned short br = 0;
unsigned long idx = 0;
unsigned long flen = 0;
res = f_open(&filHND, file, FA_OPEN_EXISTING | FA_READ);
if (FR_OK == res)
{
flen = filHND.fsize;
while (flen > 0)
{
len = 0;
br = 0;
idx = 0;
f_read(&filHND, wBuff, BUF_SIZE / 2, &br); // 512
flen -= br;
while(br > 0)
{
VS1003_WAIT_RDY();
len = (br > 32)? 32 : br ;
VS1003_SDI_Write(&wBuff[idx], len);
br -= len;
idx += len;
}
}
f_close(&filHND);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -