⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fat.c

📁 MG64+VS1003+SDCARD+nokia5110 之多的MP3
💻 C
📖 第 1 页 / 共 5 页
字号:
			for(cnt=0;cnt<SectorsPerClust;cnt++)
			{
				if(FAT_ReadSector(sector+cnt,buffer)){free(buffer);return 1;}
				for(offset=0;offset<512;offset+=32)
				{
					item=(struct direntry *)(&buffer[offset]);
					if((item->deName[0] != '.') && (item->deName[0] != 0x00) && (item->deName[0] != 0xe5))
					{				
						if(item->deAttributes == 0x0f)
						{
							we = (struct winentry *)(&buffer[offset]);
							j = 26 *( (we->weCnt-1) & WIN_CNT);
                            			if(j<MAX_LONG_NAME_SIZE-25)
						       {
							    p = &LongNameBuffer[j];
							    for (j=0;j<10;j++)	*p++ = we->wePart1[j];			
							    for (j=0;j<12;j++)	*p++ = we->wePart2[j];
							    for (j=0;j<4;j++)	*p++ = we->wePart3[j];	
							    if (we->weCnt & 0x40) (*(unsigned int *)p) = 0;				
							    if ((we->weCnt & WIN_CNT) == 1) LongNameFlag = 1;	
						       }
							LongNameBuffer[MAX_LONG_NAME_SIZE-1] = 0;
							LongNameBuffer[MAX_LONG_NAME_SIZE-2] = 0;
							/*p = &LongNameBuffer[j];
							for (j=0;j<10;j++)	*p++ = we->wePart1[j];			
							for (j=0;j<12;j++)	*p++ = we->wePart2[j];
							for (j=0;j<4;j++)	*p++ = we->wePart3[j];	
							if (we->weCnt & 0x40) (*(unsigned int *)p) = 0;				
							if ((we->weCnt & WIN_CNT) == 1) LongNameFlag = 1;	*/
						}
						else if((item->deExtension[0] == 'M')&&(item->deExtension[1] == 'P')&&(item->deExtension[2] == '3'))
						{
							CopyDirentruyItem(MusicInfo,item);
							*type = 1;
							i++;
							if(i==*Count){free(buffer);return 0;}
							else LongNameFlag = 0;	
						}
						else if((item->deExtension[0] == 'W')&&(item->deExtension[1] == 'M')&&(item->deExtension[2] == 'A'))
						{
							CopyDirentruyItem(MusicInfo,item);
							*type = 2;
							i++;
							if(i==*Count){free(buffer);return 0;}	
							else LongNameFlag = 0;
						}
						else if((item->deExtension[0] == 'M')&&(item->deExtension[1] == 'I')&&(item->deExtension[2] == 'D'))
						{
							CopyDirentruyItem(MusicInfo,item);
							*type = 3;
							i++;
							if(i==*Count){free(buffer);return 0;}
							else LongNameFlag = 0;	
						}
						else if((item->deExtension[0] == 'W')&&(item->deExtension[1] == 'A')&&(item->deExtension[2] == 'V'))
						{
							CopyDirentruyItem(MusicInfo,item);
							*type=4;
							i++;
							if(i==*Count){free(buffer);return 0;}
							else LongNameFlag = 0;	
						}
						else LongNameFlag = 0;
					}
				}
			}
			free(buffer);//release
			tempclust=FAT_NextCluster(tempclust);//next cluster
			if(tempclust == 0x0fffffff || tempclust == 0x0ffffff8 || (FAT32_Enable == 0 && tempclust == 0xffff))break;
		}
	}
	if(*Count==0)*Count=i;
	return 0;	
}

//search the file 
BYTE SearchLrc(BYTE *dir,BYTE * longnamebuffer,struct direntry *LrcInfo, uint8 isLongName)
{                                                         
	BYTE *buffer;
	BYTE longbuffertemp[130];
	BYTE * p1, * p2;
	BYTE match = 0;
	DWORD sector;
	DWORD cluster;
	DWORD tempclust;
	unsigned char cnt;
	unsigned int offset;
	//unsigned int i=0;
	unsigned char j;//long name buffer offset;
	unsigned char *p;//long name buffer pointer
	struct direntry *item = 0;
	struct winentry *we =0;
	cluster = FAT_OpenDir(dir);
	if(cluster == 1)return 1;
	if(cluster==0 && FAT32_Enable==0)// root directory
	{
		buffer=malloc(512);//apply memory
		if(buffer==0)return 1;//if failed
		for(cnt=0;cnt<RootDirSectors;cnt++)
		{
			if(FAT_ReadSector(FirstDirSector+cnt,buffer)){free(buffer);return 1;}
			for(offset=0;offset<512;offset+=32)
			{
				item=(struct direntry *)(&buffer[offset]);//pointer convert
				//find a valid item and display it
				if((item->deName[0] != '.') && (item->deName[0] != 0x00) && (item->deName[0] != 0xe5))
				{
					if(item->deAttributes == 0x0f)
					{
						we = (struct winentry *)(&buffer[offset]);
						j = 26 *( (we->weCnt-1) & WIN_CNT);
						if(j<MAX_LONG_NAME_SIZE-25)
						{
							p = &longbuffertemp[j];
							for (j=0;j<10;j++)	*p++ = we->wePart1[j];			
							for (j=0;j<12;j++)	*p++ = we->wePart2[j];
							for (j=0;j<4;j++)	*p++ = we->wePart3[j];	
							if (we->weCnt & 0x40) (*(unsigned int *)p) = 0;				
							if ((we->weCnt & WIN_CNT) == 1) LongNameFlag = 1;	
						}
						longbuffertemp[MAX_LONG_NAME_SIZE-1] = 0;
						longbuffertemp[MAX_LONG_NAME_SIZE-2] = 0;
					}
					else if((item->deExtension[0] == 'L')&&(item->deExtension[1] == 'R')&&(item->deExtension[2] == 'C'))
					{
						CopyDirentruyItem(LrcInfo,item);
						p1 = longnamebuffer;
						p2 = longbuffertemp;
						match = 1; 
						if(LongNameFlag && isLongName)
						{
							while(*((unsigned int*)p1))
							{
								if(*(unsigned int*)p1 != *(unsigned int*)p2)
								{
									match = 0;
									break;
								}
								p1 += 2;
								p2 += 2;	
							}
						}
						else if(!LongNameFlag && !isLongName)
						{
							p2 = item->deName;
							while(*p1)
							{
								if(*p1++ != *p2++)
								{
									match = 0;
									break;
								}
							}
						}
						else
						{
							match = 0;
						}
						if(match){free(buffer);return 0;}
						else LongNameFlag = 0;	
					}
					else LongNameFlag = 0;
				}
			}
		}
		free(buffer);//release
	}
	else//other folders
	{
		tempclust=cluster;
		while(1)
		{
			sector=FirstDataSector+(DWORD)(tempclust-2)*(DWORD)SectorsPerClust;//calculate the actual sector number
			buffer=malloc(512);//apply memory
			if(buffer==0)return 1;//if failed
			for(cnt=0;cnt<SectorsPerClust;cnt++)
			{
				if(FAT_ReadSector(sector+cnt,buffer)){free(buffer);return 1;}
				for(offset=0;offset<512;offset+=32)
				{
					item=(struct direntry *)(&buffer[offset]);
					if((item->deName[0] != '.') && (item->deName[0] != 0x00) && (item->deName[0] != 0xe5))
					{				
						if(item->deAttributes == 0x0f)
						{
							we = (struct winentry *)(&buffer[offset]);
							j = 26 *( (we->weCnt-1) & WIN_CNT);
							if(j<MAX_LONG_NAME_SIZE-25)
							{
								p = &longbuffertemp[j];
								for (j=0;j<10;j++)	*p++ = we->wePart1[j];			
								for (j=0;j<12;j++)	*p++ = we->wePart2[j];
								for (j=0;j<4;j++)	*p++ = we->wePart3[j];	
								if (we->weCnt & 0x40) (*(unsigned int *)p) = 0;				
								if ((we->weCnt & WIN_CNT) == 1) LongNameFlag = 1;	
							}
							longbuffertemp[MAX_LONG_NAME_SIZE-1] = 0;
							longbuffertemp[MAX_LONG_NAME_SIZE-2] = 0;
						}
						else if((item->deExtension[0] == 'L')&&(item->deExtension[1] == 'R')&&(item->deExtension[2] == 'C'))
						{
							CopyDirentruyItem(LrcInfo,item);
							p1 = longnamebuffer;
							p2 = longbuffertemp;
							match = 1; 
							if(LongNameFlag && isLongName)
							{
								while(*((unsigned int*)p1))
								{
									if(*(unsigned int*)p1 != *(unsigned int*)p2)
									{
										match = 0;
										break;
									}
										p1 += 2;
									p2 += 2;	
								}
							}
							else if(!LongNameFlag && !isLongName)
							{
								p2 = item->deName;
								while(*p1)
								{
									if(*p1++ != *p2++)
									{
										match = 0;
										break;
									}
								}
							}
							else
							{
								match = 0;
							}
							if(match){free(buffer);return 0;}
							else LongNameFlag = 0;	
						}
						else LongNameFlag = 0;
					}
				}
			}
			free(buffer);//release
			tempclust=FAT_NextCluster(tempclust);//next cluster
			if(tempclust == 0x0fffffff || tempclust == 0x0ffffff8 || (FAT32_Enable == 0 && tempclust == 0xffff))break;
		}
	}
	return 1;	/*can't find lrc file*/
}

#else

void WriteFolderCluster(WORD addr,DWORD cluster)
{
#if FAT_DEBUG
	printf_P(PSTR("\r\nWrite EEPROM address:%d with value:%ld"),addr,cluster);
#endif
	eeprom_write_byte(addr,cluster>>24);
	eeprom_write_byte(addr+1,cluster>>16);
	eeprom_write_byte(addr+2,cluster>>8);
	eeprom_write_byte(addr+3,cluster>>0);
}

DWORD GetFolderCluster(WORD addr)
{
	DWORD temp;
	temp = eeprom_read_byte(addr);
	temp <<= 8;
	temp += eeprom_read_byte(addr+1);
	temp <<= 8;
	temp += eeprom_read_byte(addr+2);
	temp <<= 8;
	temp += eeprom_read_byte(addr+3);
#if FAT_DEBUG
	printf_P(PSTR("\r\nRead EEPROM address: %d value is: %ld"),addr,temp);
#endif
	return temp;
}

BYTE SearchFolder(DWORD cluster,WORD *addr)
{
	BYTE *buffer;
	//BYTE buff[3];
	DWORD sector;
	//DWORD cluster;
	DWORD tempclust;
	unsigned char cnt;
	unsigned int offset;
	//unsigned int i=0;
	//unsigned char j;//long name buffer offset;
//	unsigned char *p;//long name buffer pointer
	struct direntry *item = 0;
	//struct winentry *we =0;
	
	if(cluster==0 && FAT32_Enable==0)// root directory
	{	
		buffer=malloc(512);//apply memory
		if(buffer==0)return 1;//if failed
		for(cnt=0;cnt<RootDirSectors;cnt++)
		{
			if(FAT_ReadSector(FirstDirSector+cnt,buffer)){free(buffer);return 1;}
			for(offset=0;offset<512;offset+=32)
			{
				item=(struct direntry *)(&buffer[offset]);//pointer convert
				//find a valid item and display it
				if((item->deName[0] != '.') && (item->deName[0] != 0x00) && (item->deName[0] != 0xe5))
				{
					if(item->deAttributes & ATTR_DIRECTORY )
					{
						#if FAT_DEBUG
							printf_P(PSTR("\r\nFound a folder!"));
						#endif
						if(*addr==RECORD_ADDR_END)return 0;
						else
						{
							WriteFolderCluster(*addr,item->deStartCluster+(((unsigned long)item->deHighClust)<<16));
							*addr+=4;
						}
					}
				}
			}
		}
		free(buffer);//release
	}
	else//other folders
	{
		tempclust=cluster;
		while(1)
		{
			sector=FirstDataSector+(DWORD)(tempclust-2)*(DWORD)SectorsPerClust;//calculate the actual sector number
			buffer=malloc(512);//apply memory
			if(buffer==0)return 1;//if failed
			for(cnt=0;cnt<SectorsPerClust;cnt++)
			{
				if(FAT_ReadSector(sector+cnt,buffer)){free(buffer);return 1;}
				for(offset=0;offset<512;offset+=32)
				{
					item=(struct direntry *)(&buffer[offset]);
					if((item->deName[0] != '.') && (item->deName[0] != 0x00) && (item->deName[0] != 0xe5))
					{				
						if(item->deAttributes & ATTR_DIRECTORY )
						{
							#if FAT_DEBUG
								printf_P(PSTR("\r\nFound a folder!"));
							#endif
							if(*addr==RECORD_ADDR_END)return 0;
							else
							{
								WriteFolderCluster(*addr,item->deStartCluster+(((unsigned long)item->deHighClust)<<16));
								*addr+=4;
							}
						}
					}
				}
			}
			free(buffer);//release
			tempclust=FAT_NextCluster(tempclust);//next cluster
			if(tempclust == 0x0fffffff || tempclust == 0x0ffffff8 || (FAT32_Enable == 0 && tempclust == 0xffff))break;
		}
	}
	return 0;		
}



BYTE SearchInit()
{	
	WORD addr = RECORD_ADDR_START;
	WORD temp_addr;
	DWORD cluster;
	
#if FAT_DEBUG
	printf_P(PSTR("\r\nSearchInit"));
#endif

	if(FAT32_Enable)
		WriteFolderCluster(addr,FirstDirClust);
	else 
		WriteFolderCluster(RECORD_ADDR_START,0);
	
	addr += 4;
	
	WriteFolderCluster(addr, 0xffffffff);

#if FAT_DEBUG
	printf_P(PSTR("\r\n"));
#endif
	temp_addr = addr;
	addr = RECORD_ADDR_START;
	while(1)
	{
		cluster = GetFolderCluster(addr);
		if(cluster == 0xffffffff)break;
		else
		{
		#if FAT_DEBUG
			printf_P(PSTR("\r\nSearch one folder start, cluster = %ld"),cluster);
		#endif

			if(SearchFolder(cluster,&temp_addr))
			{
				#if FAT_DEBUG
					printf_P(PSTR("\r\nERROR: search folder error"));
				#endif
			}
			if(GetFolderCluster(temp_addr) != 0xffffffff)
				WriteFolderCluster(temp_addr,0XFFFFFFFF);
			if(temp_addr == RECORD_ADDR_END)
			{
			#if FAT_DEBUG
				printf_P(PSTR("\r\nWARNING: EEPROM is full, no more space!"));
			#endif
				WriteFolderCluster(temp_addr - 4,0XFFFFFFFF);
				break;
			}
			#if FAT_DEBUG
				printf_P(PSTR("\r\nSearch one folder complete!\r\n"));
			#endif
		}
		addr+=4;
	}
	#if FAT_DEBUG
		printf_P(PSTR("\r\nSearch Completed!\r\n"));
	#endif
}


//search the file , when *count = 0 it will bring the number whole songs, when *cout != 0 the *MusicInfo will bring the infomation of the file
BYTE Search(/*BYTE *dir*/WORD *music_record_addr,struct direntry *MusicInfo,WORD *Count,BYTE *type)//当COUNT为零时,有它带回这个目录下总共有多少首音乐
{                                                            //不为零时有MusicInfo带回第Count首歌的详细文件信息
	BYTE *buffer;
	DWORD sector;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -