📄 fat.c
字号:
unsigned char name[8];
unsigned char i=0;
WorkFlag&=~bLrc;
for(i=0;i<8;i++) name[i]=strName[i];
InitSetPath(&fp,dircluster);
ReadBlock(fp.Sector); //重新读取目录表
do
{
if(!ReadNextDirEntry(&fp)) {lyric.Clust=0; return;}
de=(DIRENTRY *) BUFFER;
de += fp.Index;
if(*de->deName != 0xe5){
if((de->deAttributes&ATTR_LONG_FILENAME)!=ATTR_LONG_FILENAME){
if((de->deAttributes& ATTR_DIRECTORY)!= ATTR_DIRECTORY){// is it a directory ?
for(i=0;i<8;i++) {
if(name[i]!=de->deName[i]) break;
}
if(i==8){ //主文件名比较正确
for(i=0;i<3;i++){
if(pgm_read_byte(filetype[LRC_TYPE]+i)!=de->deExtension[i]) break;
}
if(i==3) break;
}
} } }
de++;
fp.Index++;
if(de->deName[0]==0) { lyric.Clust=0; return;} //文件的最后一项
}while(1);
lyric.first = 1;
lyric.t_sec=lyric.t_ms=0;
WorkFlag|=bLrc;
lyric.so=0;
lyric.sl=0;
lyric.wl=0; //初始位置
lyric.filelen=(unsigned int)de->deFileSize; //保存歌词文件大小
lyric.Clust = (unsigned long) ((unsigned long)de->deHighClust << 16) + de->deStartCluster;
}
unsigned int FindDirIndex(unsigned long fatherdir,unsigned long dircluster)
{
FIND_FILE_INFO fp;
DIRENTRY *de=0;
uint fileNo=0;
unsigned long tempclust;
InitSetPath(&fp,fatherdir);
ReadBlock(fp.Sector); //读取目录表
do
{
if(!ReadNextDirEntry(&fp)) break;
de = (DIRENTRY *)BUFFER;
de+=fp.Index;
if(*de->deName != 0xe5){ //找到目录或文件
if((de->deAttributes&ATTR_LONG_FILENAME) != ATTR_LONG_FILENAME){
if((de->deAttributes&ATTR_DIRECTORY) == ATTR_DIRECTORY){// is it a directory ?
if(!IsCurDir(de->deName)){
fileNo++;
tempclust=((unsigned long)de->deHighClust << 16) + de->deStartCluster;
if(tempclust==dircluster) return fileNo;
}
}
}
}
de++;
fp.Index++;
if(de->deName[0]==0) break;
}while(1);
return 0;
}
//-----------------------------------------------------
// 记录第INDEX的后四项文件或目录内容: 内容首簇号,文件的名称(长文件名)
//
unsigned int SerarchFile(unsigned long dircluster,unsigned int index)
{
FIND_FILE_INFO fp;
WINENTRY *we;
unsigned char LONGNAME_BUFFER_ADDR[60]; //保存25个字符
unsigned char *LongNameBuffer = (unsigned char *) LONGNAME_BUFFER_ADDR;
unsigned char hasBuffer=0;
unsigned int b;
unsigned char line=0;
char *p;
DIRENTRY *de=0;
unsigned char i=0;
uint fileNo=0;
//================先查找目录=====================================
InitSetPath(&fp,dircluster);
ReadBlock(fp.Sector); //读取目录表
do
{
if(!ReadNextDirEntry(&fp)) break;
de = (DIRENTRY *)BUFFER;
de+=fp.Index;
if(*de->deName != 0xe5){ //找到目录或文件
if((de->deAttributes&ATTR_LONG_FILENAME) == ATTR_LONG_FILENAME){
we = (WINENTRY *) de;
b = 13 *( (we->weCnt-1) & 0x0f); // index into string
// if(b<50-13){
p = &LongNameBuffer[b];
for (i=0;i<10;i++) *p++ = we->wePart1[i]; // copy first part
for (i=0;i<12;i++) *p++ = we->wePart2[i]; // second part
for (i=0;i<4;i++) *p++ = we->wePart3[i]; // and third part
// }
if (we->weCnt & 0x40) *p = 0; // in case dirnamelength is multiple of 13
if ((we->weCnt & 0x0f) == 1) hasBuffer = 1; // mark that we have a long entry
}else{
if(hasBuffer){
hasBuffer = 0; // clear buffer
if((de->deAttributes&ATTR_DIRECTORY) == ATTR_DIRECTORY){// is it a directory ?
fileNo++;
if((fileNo>=index)&&(line<4)){
if(WorkFlag&bUnicode){
UniToGB(LongNameBuffer); //转成国标码
for(i=0;i<40;i++)
m_c[line].LongName[i]=LongNameBuffer[i];
}else{
for(i=0;i<11;i++)
m_c[line].LongName[i]=de->deName[i];
}
m_c[line].LongName[i]='\0';
for(i=0;i<11;i++)
m_c[line].ShortName[i]=de->deName[i];
m_c[line].ShortName[i]='\0';
m_c[line].Type=0;
m_c[line].Clust=((unsigned long)de->deHighClust << 16) + de->deStartCluster;
m_c[line++].FileLen=0;
}
}
}else{
if((de->deAttributes&ATTR_DIRECTORY) == ATTR_DIRECTORY){
if(!IsCurDir(de->deName)){
fileNo++;
if((fileNo>=index)&&(line<4)){
for(i=0;i<11;i++){
m_c[line].ShortName[i]=de->deName[i];
m_c[line].LongName[i]=de->deName[i];
}
m_c[line].ShortName[i]='\0';
m_c[line].LongName[i]='\0';
m_c[line].Type=0;
m_c[line].Clust=((unsigned long)de->deHighClust << 16) + de->deStartCluster;
m_c[line++].FileLen=0;
}
}
}
}
}
}
de++;
fp.Index++;
if(de->deName[0]==0) break; //文件的最后一项
}while(1);
//-------------------------再查找音乐文件-------------------------------------------
InitSetPath(&fp,dircluster);
ReadBlock(fp.Sector); //读取目录表
do
{
if(!ReadNextDirEntry(&fp)) break;
de = (DIRENTRY *)BUFFER;
de+=fp.Index;
if(*de->deName != 0xe5){ //找到目录或文件
if((de->deAttributes&ATTR_LONG_FILENAME) == ATTR_LONG_FILENAME)
{
we = (WINENTRY *) de;
b = 13 *( (we->weCnt-1) & 0x0f); // index into string
// if(b<50-13){
p = &LongNameBuffer[b];
for (i=0;i<10;i++) *p++ = we->wePart1[i]; // copy first part
for (i=0;i<12;i++) *p++ = we->wePart2[i]; // second part
for (i=0;i<4;i++) *p++ = we->wePart3[i]; // and third part
if (we->weCnt & 0x40) *p = 0; // in case dirnamelength is multiple of 13
// }
if ((we->weCnt & 0x0f) == 1) hasBuffer = 1; // mark that we have a long entry
}else{
if(hasBuffer)
{
hasBuffer = 0;
if((de->deAttributes&ATTR_DIRECTORY) != ATTR_DIRECTORY)// is it a directory ?
{
if(IsMusicFile(&de->deExtension[0])!=0){
fileNo++;
if((fileNo>=index)&&(line<4)){
if(WorkFlag&bUnicode){
UniToGB(LongNameBuffer); //转成GB码
for(i=0;i<40;i++)
m_c[line].LongName[i]=LongNameBuffer[i];
}else{
for(i=0;i<11;i++)
m_c[line].LongName[i]=de->deName[i];
}
m_c[line].LongName[i]='\0';
for(i=0;i<11;i++)
m_c[line].ShortName[i]=de->deName[i];
m_c[line].ShortName[i]='\0';
m_c[line].Type=2;
m_c[line].Clust=((unsigned long)de->deHighClust << 16) + de->deStartCluster;
m_c[line++].FileLen=de->deFileSize;
}
}
}
}else{
if((de->deAttributes&ATTR_DIRECTORY) != ATTR_DIRECTORY){
if(IsMusicFile(&de->deExtension[0])!=0){
fileNo++;
if((fileNo>=index)&&(line<4)){
for(i=0;i<11;i++)
m_c[line].ShortName[i]=de->deName[i];
m_c[line].ShortName[i]='\0';
for(i=0;i<11;i++)
m_c[line].LongName[i]=de->deName[i];
m_c[line].LongName[i]='\0';
m_c[line].Type=2;
m_c[line].Clust=((unsigned long)de->deHighClust << 16) + de->deStartCluster;
m_c[line++].FileLen=de->deFileSize;
}
}
}
}
}
}
de++;
fp.Index++;
if(de->deName[0]==0) break; //文件的最后一项
}while(1);
for(i=line;i<4;i++){
m_c[i].LongName[0]='\0';
m_c[i].ShortName[0]='\0';
m_c[i].Type=0xff;
}
//----------------------------------------------------------------------
return fileNo;
}
//取下一个目录项
unsigned char ReadNextDirEntry(FIND_FILE_INFO *fp)
{
if(fp->Index == 16)
{
if(!(fp->Nums&0x8000)){
if((fp->Nums&0x7FFF)==0) return 0;
fp->Nums--;
}else{
if(fp->NumSector>=SectorsPerCluster){
fp->Clust=fatNextCluster(fp->Clust); //取下一个目录簇
if(fp->Clust==CLUST_EOFE) return 0; // 目录内容全部找完
fp->Sector=fatClustToSect(fp->Clust);
fp->NumSector=0;
}
fp->NumSector++;
}
fp->Sector++;
ReadBlock(fp->Sector); // 读取下一个扇区的目录数据
fp->Index = 0;
}
return 1;
}
//代码转换unit code-> gb2312
void UniToGB(unsigned char *pbuf)
{
unsigned char wh;
unsigned int sector,clusor,secoff;
unsigned long foffset;
unsigned int code;
unsigned char i,m=0;
for(i=0;i<25;i++){
code= pbuf[i*2+1]*256+pbuf[i*2];
if((code==0)||(code==0xffff)) break;
if((code&0xff00)==0){
if((code>=0x20)&&(code<=0x7e)){
pbuf[m++]=(unsigned char)code;
}else{
pbuf[m++]='?';
}
continue;
}
if(code>=0x4e00){ //是汉字
code=(code-0x4e00)*2;
sector=code/BytesPerSector; //得到总的完整的扇区数
secoff= (unsigned int) code%BytesPerSector; //扇区内的字节数偏移
wh=(unsigned char) sector%SectorsPerCluster;
clusor=(unsigned int) sector/SectorsPerCluster;//得到总的簇数
eeprom_read_block(&foffset,(void*)(clusor*4+0x200),4); //查表得到首扇区
code=SD_Read_Word(foffset+wh,secoff);
pbuf[m++]=(uchar)code;
pbuf[m++]=code>>8;
}else{
pbuf[m++]='?';
}
}
pbuf[m]='\0';
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -