📄 main.c
字号:
LCD_LIGHT_ON();
// Init MMC/SD driver
MmcDiskInit();
// Init keys
KeyInit();
// Volume control init
AdcInit();
// init MP3 module
do
{
Mp3Init[0] = MP3_DMA_INTR_PRIORITY;
Mp3Init[1] = MP3_TMR_INTR_PRIORITY;
Mp3SendCmd(Mp3CmdPowerUp,Mp3Init);
MP3_Status = (MP3_Status_t)Mp3Init[0];
switch(MP3_Status)
{
case MP3_Pass:
HD44780_StrShow(1, 1, " IAR Systems ");
HD44780_StrShow(1, 2, " MP3 Player ");
break;
case MP3_WrongRev:
HD44780_StrShow(1, 1, " Unsupported ");
HD44780_StrShow(1, 2, " MP3 module ");
break;
default:
HD44780_StrShow(1, 1, " Can't find ");
HD44780_StrShow(1, 2, " MP3 module ");
}
}
while(MP3_Status != MP3_Pass);
ShowDly = LOGO_DLY;
while(1)
{
if(TickSysFlag)
{
TickSysFlag = FALSE;
// Display messages
if(Line2Update)
{
Line2Update = 0;
if(!FatOpen)
{
// show special symbols PLAY, WideStereo, Loudness
Line2[13] = ' ';
Line2[14] = ' ';
Line2[15] = ' ';
switch(SoundEffect)
{
case WideStereoEffect:
Line2[14] = WIDE_STEREO_CH;
break;
case LouldEffect:
Line2[13] = 'L';
Line2[14] = 'D';
}
if(Play)
{
Line2[15] = PLAY_CH;
}
}
HD44780_StrShow(1, 2, Line2);
}
// Implement the volume control
if(ADC_GetFlagStatus(ADC_FLAG_ECV) == SET)
{
// Get the conversion value
Tmp = ((ADC_GetConversionValue(ADC_Channel_5) >> 5) & 0x1F);
Tmp = Adc2Vol[Tmp];
if(Volume != Tmp)
{
// Update volume value and set flag (bit31)
Volume = Tmp | 0x80000000;
}
// Clear the end of conversion flag
ADC_ClearFlag(ADC_FLAG_ECV);
}
if(ShowDly)
{
--ShowDly;
}
else
{
ShowDly = REPEAT_DLY;
// Update MMC/SD card status
if (!Play)
{
MmcStatusUpdate();
}
pDiskCtrlBlk_t pMMCDiskStatus = MmcDiskStatus();
if(pMMCDiskStatus->DiskStatus == DiskCommandPass)
{
if (FatOpen)
{
FatOpen = efs_init(&efsl,NULL);
if (FatOpen == 0)
{
// free memory if this is necessary
while (Deep)
{
pDirTemp = pCurrDir;
pCurrDir = pCurrDir->pPrev;
free(pDirTemp);
--Deep;
}
// Open the root directory
ls_openDir( &list, &(efsl.myFs) , pCurrDir->DirName);
FileOpen = 0;
GenreteKeyPress(KeyNextMask);
}
else
{
// Not valid file system
strcpy((char *)Line2, "Pls, Insert Card");
Line2Update = 1;
}
}
}
else
{
// Can't find a card
strcpy((char *)Line2, "Pls, Insert Card");
Line2Update = 1;
FatOpen = -1;
}
}
}
// User Interface
KeyImpl();
PressedKey = GetKeys();
if (!FatOpen)
{
// valid a file systems
if (Play)
{
// Play a file
if (PressedKey.PlayStop)
{
Play = 0;
FatOpen = file_fclose(&Mp3File);
FileOpen = 0;
FillSize = 0;
// stop play
Mp3SendCmd(Mp3CmdPlayStop,&Tmp);
Line2[15] = ' ';
Line2Update = 1;
}
else if (PressedKey.Next)
{
// change sound effects
++SoundEffect;
switch(SoundEffect)
{
case WideStereoEffect:
Tmp = 1;
Mp3SendCmd(Mp3CmdWideStereo,&Tmp);
break;
case LouldEffect:
Tmp = 0;
Mp3SendCmd(Mp3CmdWideStereo,&Tmp);
Tmp = 1;
Mp3SendCmd(Mp3CmdLoudness,&Tmp);
break;
default:
Tmp = 0;
Mp3SendCmd(Mp3CmdWideStereo,&Tmp);
Mp3SendCmd(Mp3CmdLoudness,&Tmp);
SoundEffect = NoSoundEffect;
}
Line2Update = 1;
}
else
{
if(!FileOpen && !FillSize)
{
// file open
if((FatOpen = file_fopen(&Mp3File,&(efsl.myFs),(char *)list.currentEntry.FileName,MODE_READ)) == 0)
{
// Fill a buffer and send the first chunk to the MP3 decoder
FileOpen = 1;
FillSize = 0;
FillBlockOffset = 0;
DrainBlockOffset = 0;
DrainSizeHold = 0;
Line2[15] = PLAY_CH;
Line2Update = 1;
if((Mp3Stream.Size = file_read(&Mp3File,sizeof(Mp3Buffer),(pInt8U)Mp3Buffer)) != 0)
{
Mp3Stream.pStream = Mp3Buffer;
Mp3Stream.PlaySpeed = Mp3PlayNorm;
Mp3SendCmd(Mp3CmdPlay,(pInt32U)&Mp3Stream);
}
}
}
else
{
// Fill a buffer
if (FileOpen && (FillSize < 1024))
{
Tmp = file_read(&Mp3File,512,(pInt8U)(FillBlockOffset?(Mp3Buffer+(512/sizeof(Mp3Buffer[0]))):Mp3Buffer));
FillSize += Tmp;
if(Tmp == 512)
{
FillBlockOffset ^= 1;
}
else
{
// End of file
FileOpen = 0;
file_fclose(&Mp3File);
// find next MP3 file
while(1)
{
// open next MP3 file
if (ls_getNext(&list) == 0)
{
if (!(list.currentEntry.Attribute & (ATTR_VOLUME_ID | ATTR_DIRECTORY | ATTR_SYSTEM | ATTR_HIDDEN )))
{
// MP3 file is found
list.currentEntry.FileName[12-1] = '\0';
// prepare file name
if(!strcmp((char const *)&list.currentEntry.FileName[12-1-3],"MP3"))
{
pStr = (char*)strchr((const char*)list.currentEntry.FileName, ' ');
if(pStr != NULL)
{
*pStr = 0;
}
else
{
pStr = (char*)list.currentEntry.FileName;
list.currentEntry.FileName[12-1-3] = 0;
}
strcat(pStr, ".MP3");
strcpy((char*)Line2,(const char*)list.currentEntry.FileName);
while(strlen((const char *)Line2) < 16)
{
strcat((char *)Line2, " ");
}
break;
}
}
}
else
{
list.cEntry = 0xFFFF;
list.rEntry = 0;
}
}
}
}
// Drain a buffer
Mp3SendCmd(Mp3CmdPlayStatus,&Tmp);
// Wait until data was send
if(Tmp != Mp3DataTransferProgress)
{
FillSize -= DrainSizeHold;
if(FillSize)
{
DrainSizeHold = MIN(FillSize,512);
// calculate the offset
Mp3Stream.pStream = DrainBlockOffset?(Mp3Buffer+(512/sizeof(Mp3Buffer[0]))):Mp3Buffer;
Mp3Stream.Size = DrainSizeHold;
Mp3Stream.PlaySpeed = Mp3PlayNorm;
Mp3SendCmd(Mp3CmdPlay,(pInt32U)&Mp3Stream);
DrainBlockOffset ^= 1;
}
else
{
// The buffer is empty when end of file is reached or some error is appear
Mp3SendCmd(Mp3CmdPlayStop,&Tmp);
}
}
}
}
}
else
{
// Select a file
if (PressedKey.PlayStop)
{
if (Dir)
{
pDirTemp = malloc(sizeof(DirTree_t));
pDirTemp->pPrev = pCurrDir;
pCurrDir = pDirTemp;
strcpy(pCurrDir->DirName,(const char*)list.currentEntry.FileName);
ls_openDir( &list, &(efsl.myFs) , pCurrDir->DirName);
// enter in dir
GenreteKeyPress(KeyNextMask);
++Deep;
}
else if (PervDir)
{
pDirTemp = pCurrDir;
pCurrDir = pDirTemp->pPrev;
free(pDirTemp);
ls_openDir( &list, &(efsl.myFs) , pCurrDir->DirName);
// exit from dir
GenreteKeyPress(KeyNextMask);
--Deep;
}
else
{
// Play file
Play = 1;
}
}
else if (PressedKey.Next)
{
PervDir = Dir = 0;
while(1)
{
// open next directory or mp3 file
if (ls_getNext(&list) == 0)
{
if (list.currentEntry.Attribute & ATTR_DIRECTORY)
{
// Directory is found
Dir = 1;
list.currentEntry.FileName[12-1] = '\0';
strcpy((char*)Line2,(const char*)list.currentEntry.FileName);
break;
}
else if (!(list.currentEntry.Attribute & (ATTR_VOLUME_ID | ATTR_SYSTEM | ATTR_HIDDEN)))
{
// MP3 file is found
list.currentEntry.FileName[12-1] = '\0';
if(!strcmp((char const *)&list.currentEntry.FileName[12-1-3],"MP3"))
{
pStr = (char*)strchr((const char*)list.currentEntry.FileName, ' ');
if(pStr != NULL)
{
*pStr = 0;
}
else
{
pStr = (char*)list.currentEntry.FileName;
list.currentEntry.FileName[12-1-3] = 0;
}
strcat(pStr, ".MP3");
strcpy((char*)Line2,(const char*)list.currentEntry.FileName);
break;
}
}
}
else
{
if (Deep)
{
// show up dir entry
strcpy((char*)Line2,"../ ");
// return to begin of the list of the current open directory
PervDir = 1;
}
else
{
// if media is empty
strcpy((char*)Line2,"Root/ ");
GenreteKeyPress(KeyNextMask);
}
list.cEntry = 0xFFFF;
list.rEntry = 0;
break;
}
}
while(strlen((const char *)Line2) < 16)
{
strcat((char *)Line2, " ");
}
Line2Update = 1;
}
}
}
else
{
if(Play)
{
Mp3SendCmd(Mp3CmdPlayStop,&Tmp);
}
Dir = PervDir = Play = 0;
}
// Volume control
Mp3SendCmd(Mp3CmdPlayStatus,&Tmp);
// Wait until data was send
if((Tmp != Mp3DataTransferProgress) &&
(Volume & 0x80000000))
{
// Set the new volume value
Mp3SendCmd(Mp3CmdSetVol,&Volume);
Volume &= ~0x80000000;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -