📄 mp3_player_06.c
字号:
/********************************************************/
min = 0; /* clear the temp variable for time to 0 */
sec = 0;
ms = 0;
i = 0; /* for tag use, to ditermin tag like [XX:XX] & [XX:XX:XX] */
while(1)
{
temp = buffer[j++];
#if DEBUG
USART_putchar(temp);
#endif
/**********************************************************/
if(sector == totalsect && j == leftbytes + 1) goto end; /* file end */
if(j == 512) /* need to read next sector */
{
if(part == (SectorsPerClust - 1)) /* if need to read next cluster */
{
//p=FAT_NextCluster_NEW(p);//读下一簇数据 //read next cluster
p=FAT_NextCluster(p);
if(p == 0x0fffffff || p == 0x0ffffff8 || (FAT32_Enable == 0 && p == 0xffff))//如果无后续簇则结束, //no more cluster
{
#if DEBUG
printf_P(PSTR("\r\nNo next cluster or read next cluster error"));
#endif
goto end;
}
part = 0; /* clear the part count */
if(FAT_LoadPartCluster(p,part,buffer))
{
#if DEBUG
printf_P(PSTR("\r\nERROR: Faild to load a sector"));
#endif
free(buffer);
return 1;
}
}
else /* else just read the next part of the cluster */
{
part ++; /* part ++ */
if(FAT_LoadPartCluster(p,part,buffer))
{
#if DEBUG
printf_P(PSTR("\r\nERROR: Faild to load a sector"));
#endif
free(buffer);
return 1;
}
}
sector ++; /* sector count +=1 */
j = 0; /* loop value clear to 0*/
}
/***********************************************/
if((temp < '0' || temp > '9') && temp != ':' && temp != '.')break; /* end or not a leagal ch*/
if(temp == ':'){i=1;continue;}
if(temp == '.'){i=2;mscnt = 0;continue;}
/* calculate the value */
if(i==0)min = min*10 + (temp-0x30);
else if(i==1)sec = sec*10 + (temp-0x30);
else
{
if(mscnt == 0)
ms += (temp-0x30)*100;
else if(mscnt == 1)
ms += (temp - 0x30)*10;
else ms += temp-0x30;
mscnt ++;
}
}
if(temp == ']') /* if tag end */
{
#if DEBUG
printf_P(PSTR(" found time tag: "));
printf_P(PSTR("min = %d; "),min);
printf_P(PSTR("sec = %d; "),sec);
printf_P(PSTR("ms = %d; \r\n"),ms);
#endif
if(LrcStruct_p == 0)
{
printf_P(PSTR("\r\nWARNING: Sorry! no more space for the rest tag, som lryic can't display!"));
goto end;
}
/* set time value without offset */
LrcStruct_p->time = (uint32)(min*60 + sec)*1000 + ms;
#if DEBUG
printf_P(PSTR("\r\noffset : %d "),LrcStructHead.offset);
printf_P(PSTR(" total = %ld; "),LrcStruct_p->time);
#endif
/* recalculate the time, use offset */
if(LrcStructHead.sign)
{
if(LrcStruct_p->time<LrcStructHead.offset)LrcStruct_p->time = 0;
else LrcStruct_p->time -= LrcStructHead.offset;
}
else LrcStruct_p->time += LrcStructHead.offset;
#if DEBUG
printf_P(PSTR("\r\nafter calculate total = %ld; "),LrcStruct_p->time);
#endif
/* node operation, pointer to next node, and set address */
LrcStruct_p->eeaddr = lrcaddr;
temp = buffer[j];
LrcStruct_p = LrcStruct_p->next;
if(temp == '['); /* if two tag share one lyric, just jump and next loop it will process it */
else /* this is a sigle time tag */
{
while(1)
{
temp = buffer[j++];
#if DEBUG
USART_putchar(temp);
#endif
/*************************************************************/
if(sector == totalsect && j == leftbytes + 1) goto end; /* file end */
if(j == 512) /* need to read next sector */
{
if(part == (SectorsPerClust - 1)) /* if need to read next cluster */
{
//p=FAT_NextCluster_NEW(p);//读下一簇数据 //read next cluster
p=FAT_NextCluster(p);
if(p == 0x0fffffff || p == 0x0ffffff8 || (FAT32_Enable == 0 && p == 0xffff))//如果无后续簇则结束, //no more cluster
{
#if DEBUG
printf_P(PSTR("\r\nNo next cluster or read next cluster error"));
#endif
goto end;
}
part = 0; /* clear the part count */
if(FAT_LoadPartCluster(p,part,buffer))
{
#if DEBUG
printf_P(PSTR("\r\nERROR: Faild to load a sector"));
#endif
free(buffer);
return 1;
}
}
else /* else just read the next part of the cluster */
{
part ++; /* part ++ */
if(FAT_LoadPartCluster(p,part,buffer))
{
#if DEBUG
printf_P(PSTR("\r\nERROR: Faild to load a sector"));
#endif
free(buffer);
return 1;
}
}
sector ++; /* sector count +=1 */
j = 0; /* loop value clear to 0*/
}
/****************************************************************/
if(temp == 0x0d || temp == 0x0a) /* one lyric end, when meet next line*/
{
if(lrcaddr<MAXLRCDATSIZE) /* if lrcdatbuf is not full */
{
lrcdatbuf[lrcaddr++] ='\0';
}
else /* otherwise use the eeprom */
eeprom_write_byte(lrcaddr++ - MAXLRCDATSIZE,'\0');
break;
}
else /* else normal data */
{
if(lrcaddr == MAXLRCDATSIZE) /* if ram buffer run out, show some messages */
{
printf_P(PSTR("\r\n\r\nInternal RAM is not enough, some data will be stored at EEPROM!"));
printf_P(PSTR("\r\nThis may take little longer time. U know 8.4ms/Byte, pls wait!......"));
}
#if FIX_DIRECTORY
if(lrcaddr == MAXLRCDATSIZE + 2048)
#else
if(lrcaddr == MAXLRCDATSIZE + RECORD_ADDR_START)
#endif
{
printf_P(PSTR("\r\n\r\nWow! The lyric is so long, EEPROM is run out, sorry!\r\n"));
goto end;
}
else
{
if(lrcaddr<MAXLRCDATSIZE) /* use ram */
{
lrcdatbuf[lrcaddr++] = temp;
}
else /* use epprom */
eeprom_write_byte(lrcaddr++ - MAXLRCDATSIZE,temp);
}
}
}
}
}
}
}
else j++; /**/
}
sector ++; /**/
}
p=FAT_NextCluster(p);
//p=FAT_NextCluster_NEW(p);//读下一簇数据 //read next cluster
if(p == 0x0fffffff || p == 0x0ffffff8 || (FAT32_Enable == 0 && p == 0xffff))//如果无后续簇则结束, //no more cluster
{
#if DEBUG
printf_P(PSTR("\r\nNo next cluster or read next cluster error"));
#endif
break;
}
}
end:
/* in case that some lyc file don not have empty line at the last */
if(lrcaddr<MAXLRCDATSIZE)
{
lrcdatbuf[lrcaddr++] = '\0';
}
else
eeprom_write_byte(lrcaddr++ - MAXLRCDATSIZE,'\0');
/* end the node */
LrcStruct_p = LrcStruct_p - 1;
LrcStruct_p->next = 0;
/* free the buffer */
free(buffer);
/* below is a debug message */
/************************************************************/
#if DEBUG
printf_P(PSTR("\r\nLyric data stored completed!"));
printf_P(PSTR("\r\nThe data stored in RAM & EEPROM:\r\n"));
for(j=0;j<lrcaddr;j++)
{
if(j<MAXLRCDATSIZE)USART_putchar(lrcdatbuf[j]);
else USART_putchar(eeprom_read_byte(j - MAXLRCDATSIZE));
}
printf_P(PSTR("\r\nNo sorted data is : \r\n"));
LrcStruct_p = LrcStructHead.header;
while(1)
{
lrcaddr = LrcStruct_p->eeaddr;
while(1)
{
if(lrcaddr<MAXLRCDATSIZE)temp = lrcdatbuf[lrcaddr++];
else temp = eeprom_read_byte(lrcaddr++ - MAXLRCDATSIZE);
if(temp == 0)break;
USART_putchar(temp);
}
printf_P(PSTR("\r\n"));
if(LrcStruct_p->next == 0)break;
LrcStruct_p = LrcStruct_p->next;
}
#endif
#if DEBUG
printf_P(PSTR("\r\nSort begining ......"));
#endif
#if DEBUG
temp = 0;
LrcStruct_p = LrcStructHead.header;
while(1)
{
temp++;
if(LrcStruct_p->next == 0)break;
LrcStruct_p = LrcStruct_p->next;
}
printf_P(PSTR("\r\ntotal %s time tags"),temp);
#endif
/***************************************************************/
/* tag sort */
LrcStruct_p_header = LrcStruct_p = LrcStructHead.header;
while(1)
{
LrcStruct_p_header = LrcStruct_p;
LrcStruct_p = LrcStruct_p->next;
if(LrcStruct_p == 0)break;
if(LrcStruct_p_header->time > LrcStruct_p->time)
{
LrcStruct_p_temp = LrcStructHead.header;
j = 0;
while(1)
{
j++;
if(LrcStruct_p_temp->time < LrcStruct_p->time) /* do not need to insert node */
{
LrcStruct_p_up = LrcStruct_p_temp;
LrcStruct_p_temp = LrcStruct_p_temp->next;
LrcStruct_p_down = LrcStruct_p_temp->next;
}
else /* need insert */
{
#if DEBUG
printf_P(PSTR("\r\nLrcStruct_p_up->time = %ld"),LrcStruct_p_up->time);
printf_P(PSTR("\r\nLrcStruct_p_temp->time = %ld"),LrcStruct_p_temp->time);
printf_P(PSTR("\r\nLrcStruct_p_down->time = %ld"),LrcStruct_p_down->time);
printf_P(PSTR("\r\nLrcStruct_p->time = %ld \r\n"),LrcStruct_p->time);
#endif
if(j == 1) /* if the first need to replace */
{
LrcStructHead.header = LrcStruct_p;
LrcStruct_p_header->next = LrcStruct_p ->next;
LrcStruct_p ->next = LrcStruct_p_header;
LrcStruct_p = LrcStruct_p_header;
}
else /* other normal condition */
{
LrcStruct_p_header->next = LrcStruct_p->next;
LrcStruct_p_up->next = LrcStruct_p;
LrcStruct_p->next = LrcStruct_p_temp;
LrcStruct_p = LrcStruct_p_header;
#if DEBUG
printf_P(PSTR("\r\nLrcStruct_p->time = %ld \r\n\r\n"),LrcStruct_p->time);
#endif
}
break;
}
}
}
}
printf_P(PSTR("\r\n\r\nAnalyze complete!\r\n\r\nCode written by Bozai - 章其波"));
printf_P(PSTR("\r\n*****************************************************\r\n"));
/* debug message, show sorted data */
#if DEBUG
printf_P(PSTR("\r\nSort completed!"));
printf_P(PSTR("\r\nData after sort is : \r\n"));
printf_P(PSTR("\r\n\r\nLrc Data:\r\n\r\n"));
LrcStruct_p = LrcStructHead.header;
while(1)
{
lrcaddr = LrcStruct_p->eeaddr;
while(1)
{
if(lrcaddr<MAXLRCDATSIZE)temp = lrcdatbuf[lrcaddr++];
else temp = eeprom_read_byte(lrcaddr++ - MAXLRCDATSIZE);
if(temp == 0)break;
USART_putchar(temp);
}
printf_P(PSTR("\r\n"));
if(LrcStruct_p->next == 0)break;
LrcStruct_p = LrcStruct_p->next;
}
#endif
}
else lrc = 0;
}
#if 0
/* put the time and lyric info to the teminal, for speed consideration we did not use printf_P() */
/* format: [XX:XX] XXXXXXXXXXXXXXXX */
void putsLrc(unsigned char * time,unsigned char * lrcbuffer)
{
USART_putchar(0x0d);
USART_putchar(0x0a);
USART_putchar('[');
while(*time) /* time */
{
USART_putchar(*time++);
}
USART_putchar(']');
USART_putchar(0x20);
while(*lrcbuffer) /* lyric */
{
USART_putchar(*lrcbuffer++);
}
}
#endif
uint8 PlayMusicwithKey()//播放音乐函数,一旦执行不会退出
{
uint16 keylen; //用于键处理 //for key processing
uint16 count; //数据计数 //data counting
uint8 i; //循环变量 //loop variable
uint16 j; //循环变量 //loop variable
DWORD p; //簇指示值 //cluster
DWORD totalsect; //文件拥有的扇区数 //cotain the total sector number of a file
uint16 leftbytes; //剩余字节 //cotain the left bytes number of a file //the last cluster usually not fully occupied by the file
uint8 *buffer; //缓冲 //buffer
DWORD sector; //扇区 //recor the current sector to judge the end sector
uint8 flag; //播放/暂停标志 //flag of pause
uint16 vol= DEFAULT_VOLUME;//初始音量,应与vs1003函数里的初始音量相同 //default volume
uint8 vol_temp;
uint16 songs=1; //默认放第一首歌 //play the fist songs by default
uint16 songs_cnt = 0; //how many songs have been played
uint8 mode=0; //单曲重复 //repet all by default
unsigned long rand_val;
uint8 total[6]; /* for total songs indication */
uint8 current[12]; /* Current songs number */
uint16 temp = totalsongs;
uint8 time[6];
uint16 decodetime;
uint8 volume[3];
uint8 min,sec;
uint8 keycnt = 0;
uint8 blkeyflag = 0;
struct LrcStruct_s * LrcStruct_pp;
uint16 lrcaddr;
/* the display buffer (characters) */
#define MAX_LRC_DISP 52
uint8 lrcdata[MAX_LRC_DISP];
uint8 lrcDisFlag = 0; /* indicate if it need display lyric on the lcd */
uint8 lrcCnt = 0; /* lrcdata buffer count */
ClearTrackInfo();
printf_P(PSTR("\r\nCurrent mode : "));
switch(mode)
{
case REPET_ALL: printf_P(PSTR("repet all"));break;
case REPET_ONE:printf_P(PSTR("repet one"));break;
case RANDOM:printf_P(PSTR("shuffle"));break;
}
if(totalsongs==0)//如果没有歌曲则异常退出 //if no music file return
{
lcdClrDisBuf();
LCD_print12_P(0,20,PSTR("File not found!"));
lcdUpdateDisplay();
return;
}
/**********************************************/
for(i=0;i<5;i++)
{
total[4-i] = temp%10;
temp /= 10;
}
for(i=0;i<5;i++)
{
if(total[i])break;
}
for(j=0;j<5-i;j++)
{
total[j] = total[j+i] + 0x30;
}
total[j] = 0;
/**********************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -