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

📄 save.c

📁 基于stm32的MP3播放器
💻 C
📖 第 1 页 / 共 3 页
字号:
//复制记录项信息
void CopyDirentruyItem(struct direntry *Desti,struct direntry *Source)
{
	BYTE i;
	for(i=0;i<8;i++)Desti->deName[i] = Source->deName[i];
	for(i=0;i<3;i++)Desti->deExtension[i] = Source->deExtension[i];
	Desti->deAttributes = Source->deAttributes;
	Desti->deLowerCase = Source->deLowerCase;
	Desti->deCHundredth = Source->deCHundredth;
	for(i=0;i<2;i++)Desti->deCTime[i] = Source->deCTime[i];
	for(i=0;i<2;i++)Desti->deCDate[i] = Source->deCDate[i];
	for(i=0;i<2;i++)Desti->deADate[i] = Source->deADate[i];
	Desti->deHighClust = Source->deHighClust;
	for(i=0;i<2;i++)Desti->deMTime[i] = Source->deMTime[i];
	for(i=0;i<2;i++)Desti->deMDate[i] = Source->deMDate[i];
	Desti->deStartCluster = Source->deStartCluster;
	Desti->deFileSize = Source->deFileSize;
} 
   
//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,struct direntry *MusicInfo,WORD *Count,BYTE *type)//当COUNT为零时,有它带回这个目录下总共有多少首音乐
{                                                                //不为零时有MusicInfo带回第Count首歌的详细文件信息
	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;
	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(SD_ReadSingleBlock(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 = &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
	}
	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(SD_ReadSingleBlock(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)
{                                                         
	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(SD_ReadSingleBlock(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)
						{
							while(*((unsigned int*)p1))
							{
								if(*(unsigned int*)p1 != *(unsigned int*)p2)
								{
									match = 0;
									break;
								}
								p1 += 2;
								p2 += 2;	
							}
						}
						else
						{
							p2 = item->deName;
							while(*p1)
							{
								if(*p1++ != *p2++)
								{
									match = 0;
									break;
								}
							}
						}
						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(SD_ReadSingleBlock(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)
							{
								while(*((unsigned int*)p1))
								{
									if(*(unsigned int*)p1 != *(unsigned int*)p2)
									{
										match = 0;
										break;
									}
									p1 += 2;
									p2 += 2;	
								}
							}
							else
							{
								p2 = item->deName;
								while(*p1)
								{
									if(*p1++ != *p2++)
									{
										match = 0;
										break;
									}
								}
							}
							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
}


void WriteFolderCluster(WORD addr,DWORD cluster)
{
#if FAT_DEBUG
	printf("\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(SD_ReadSingleBlock(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(SD_ReadSingleBlock(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

⌨️ 快捷键说明

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