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

📄 main.c

📁 butterfly MP3源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/**	@file	main.c	@brief	Butterfly MP3 main	@author	Nick Lott 	@date	November 2004	Copyright (C) 2004 Nick Lott <brokentoaster@sf.net>	http://butterflymp3.sf.net/		This is a simple MP3 player based around the AVR Butterfly.It currently 	uses a VS1001 mp3 decoder, MMC card and Nokia LCD. It has been heavily 	influenced by the Yampp system by Jesper Hansen <jesperh@telia.com>.		This program is free software; you can redistribute it and/or	modify it under the terms of the GNU General Public License	as published by the Free Software Foundation; either version 2	of the License, or (at your option) any later version.	This program is distributed in the hope that it will be useful,	but WITHOUT ANY WARRANTY; without even the implied warranty of	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the	GNU General Public License for more details.	You should have received a copy of the GNU General Public License	along with this program; if not, write to the Free Software Foundation, 	Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.		Target(s)...: ATmega169	Compiler....: AVR-GCC 3.3.1; avr-libc 1.2	**/#include <avr/io.h>#include <avr/pgmspace.h>#include <avr/interrupt.h>#include "main.h"#include "types.h"#include "uart.h"#include "delay.h"#include "vs1001.h"#include "mmc.h"#include "avrfat16.h"#include "timer0.h"#include "button.h"#include "eeprom.h"#include "lcdgraphics.h"#include "power.h"#include "ADC.h"#include "utils.h"// tag rom with verion infoconst char  VERSIONINFO1[] PROGMEM = "ButterflyMP3_sf_net V0.5 ";const char  VERSIONINFO2[] PROGMEM = __DATE__ __TIME__ ;#ifdef NOKIAextern	uint8 fbXY[];		// LCD position array#endif// player option variablesuint8 	gmusic_vol	=5;		// initial music volume.uint8 	gbass_boost	=0;		// bass boost off.// player state variablesuint8 	gMode		=0;		// mode of player, idle, playing, off, etcBOOL 	gLCD_Refresh;		// Update LCD//TODO: move these into file structuint32 	gFAT_entry 	=0; 	// global current entry number in FAT table. (aka file number)uint32 	gSector; 			// current sector (cluster) in songuint32 	gFileSectorSize; 	// size of current file in complete sectors.uint32	gFileSectorsPlayed; // number of full sectors played. (aka position)BOOL   	gFile_good; 		// File has been read from Fat and ready to go.char	cluster_pos=0;		// position in cluster char	buff_pos=16;		// position in buffer//volatile uint16 gBatteryVoltage;	// last ADC battery Voltage readingvolatile uint16	gPlayTimeSeconds;	// # of seconds current file has been playing.uint8	gDisplayTimeOut=0; // # of seconds+1 before changing display;	//  setup volume levels				0	1	2	3	4	5	6	7	8	9const uint8	__attribute__ ((progmem)) volume_val[10] = {160,128,112, 96, 80, 64, 48, 32, 16, 0};// internal Function Prototypes//uint8	FAT_readRoot(uint32 filenumber);void 	flush_decoder(char reset_type);void 	cue_file(void);void 	streaming (void);void 	handle_interface(void);void 	update_LCD(void);void	mainTimer(void);uint8 	init_mmc_fat(void);uint8	handle_mmc(uint8 MMC_present);void DumpGlobals(void){#ifdef ALPHA	extern volatile signed char gScroll;	extern volatile char gScrollMode;#endif		UART_Printfu08(gmusic_vol);EOL();	UART_Printfu08(gbass_boost);EOL();	UART_Printfu08(gMode);EOL();	UART_Printfu08(gLCD_Refresh);EOL();	UART_Printfu08(gFile_good);EOL();	UART_Printfu08(gDisplayTimeOut);EOL();#ifdef ALPHA	UART_Printfu08(gScroll);EOL();	UART_Printfu08(gScrollMode);EOL();#endif	UART_Printfu16(gPlayTimeSeconds);EOL();		UART_Printfu32(gFAT_entry);EOL();	UART_Printfu32(gSector);EOL();	UART_Printfu32(gFileSectorSize);EOL();	UART_Printfu32(gFileSectorsPlayed);EOL();}/***   init_HW*	@return		none	**	Initialize basic hardware for the butterfly.**/void 	init_HW(void){	// boost IntRC to 8Mhz 	//OSCCAL_calibration();	CLKPR = (1<<CLKPCE);        // set Clock Prescaler Change Enable	CLKPR = (0<<CLKPS1)|(0<<CLKPS0); // set prescaler = 1, Internal RC 8Mhz       // Disable Analog Comparator (power save)    ACSR = (1<<ACD);		// Disable Analog to Digital converter (power save)	//ADCSRA = 0;    // Disable Digital input on PF0-7 (power save)    DIDR1 = 0xFF; #ifdef PWR_CHK_INTERVAL	Power_Init();		// init power monitoring 	// abort power up if battery is low.	if (Power_check()<PWR_MIN_V) { 		//Jump to Bootloader		WDTCR = (1<<WDCE) | (1<<WDE);     //Enable Watchdog Timer to give reset		while(1);   	}#endif		// setup IO	UART_Init();		// init RS-232 link	SpiInit();			// init spi pins (mmc & VS1001)	vs1001_init_io();	// init IO pins	vs1001_init_chip();	// Reset Chip	Timer0_Init();		// setup timers for debounce and scroll	Button_Init(); 		// setup joystick.	LCD_Initialize();	// SETUP lcd			//register  timer routine 	Timer0_RegisterCallbackFunction(mainTimer);		// Read settings (volume and stuff from eeprom or flash)	load_player_options();	vs_1001_setvolume(pgm_read_byte(volume_val+gmusic_vol), pgm_read_byte(volume_val+gmusic_vol));		// say hello on Serial   	PRINT("AVR ButterflyMP3 Player  "__DATE__" "__TIME__);EOL();			// set up the FAT variables	FAT_buffer = mmc_sbuf; 	// 512 byte buffer for sector reads/writes	FAT_scratch = mmc_scratch; 	FAT_read = MMC_Read; // pointer to read block function 	FAT_write = MMC_Write;// pointer to write block function 	FAT16_entryMAX =-1;	FAT16_entryMIN = 0;} void mainTimer(void){	static uint8 timeout_count=0; 	static uint8 second_timer=100;#ifdef PWR_CHK_INTERVAL	static uint8 power_timer=PWR_CHK_INTERVAL;	uint16 batteryVoltage;#endif	extern unsigned char gButtonTimeout;  	// Variable from "button.c" to prevent button-bouncing				/**************** Button timeout for the button.c, START ****************/    if(!gButtonTimeout)    {        timeout_count++;                if(timeout_count > 20)         {            gButtonTimeout = TRUE;            timeout_count = 0;			//long_timeout_count=0;        }		    }   // RA Sewell - other half a second   if (second_timer == 50)   {      gLCD_Refresh = TRUE;   }   	if (!second_timer--){/// do stuff every second in here		second_timer=100;		if (gMode != MODE_PLAY){			gPowerSaveTimer++;			//UART_Printfu08(gPowerSaveTimer);EOL();		}else{			gPlayTimeSeconds++;					}      gLCD_Refresh = TRUE;#ifdef PWR_CHK_INTERVAL		if (!power_timer--){			power_timer = PWR_CHK_INTERVAL;			batteryVoltage = Power_check();			if (batteryVoltage>PWR_GOOD_V){				//PRINT("Battery GOOD");				//EOL();			}else if (batteryVoltage>PWR_AVG_V){				//PRINT("Battery OK");				//EOL();			}else if (batteryVoltage>PWR_MIN_V){				//PRINT("Battery LOW");				//EOL();			}else if (batteryVoltage>PWR_ABS_MIN_V){				//PRINT("Battery FLAT");				//EOL();			}else {				PRINT("LOW BATTERY PWR DWN");				EOL();				// stop playing and power down				gMode = MODE_PAUSE;				// save everything to eeprom				//save current settings				save_player_options();								//TODO: Write MMC Serial Number to EEPROM								//write current song to EEPROM				save_player_state();												gPowerSaveTimer=AUTOPOWEROFF_TIMEOUT;			}		}#endif		if(gDisplayTimeOut){			if (gDisplayTimeOut==1){				#ifdef ALPHA				// Display back to normal				LCD_FileName(TRUE);				#endif					gDisplayTimeOut = 0;			}else{ 				// continue the count down				gDisplayTimeOut--;			}		}			}	/*	 if(!gButtonLongTimeout){ // start the hold down timer		 long_timeout_count++;		 		 if(long_timeout_count > 1000){			 gButtonLongTimeout = TRUE;			 KEY_SHIFT=TRUE;			 KEY_VALID=TRUE;			 timeout_count = 0;			 long_timeout_count=0;		 }		 	 }*/		/**************** Button timeout for the button.c, END ******************/}/***	restore_player*	@return		MMC Card Status**	Restore the player state from eeprom if possible**/uint8 restore_player(void){	uint8	MMC_present,mode;	uint16	time;	uint32	file,sector,played;		mode=0; file=0;	sector=0;	played=0;	time=0;	// check for an mmc on startup	MMC_present = handle_mmc(FALSE);		if (!MMC_present){ //Can't restore no MMC  		//PRINT("NO MMC. Rstre abortd");EOL();		return MMC_present; 	}		// read last song details from EEPROM	load_player_state(&mode,&file,&sector,&played,&time);		// Debug loading info//		PRINT("Loaded from EEPROM");EOL();//		UART_Printfu08(mode);EOL();//		UART_Printfu32(file);EOL();//		UART_Printfu16(time);EOL();//		UART_Printfu32(sector);EOL();//		UART_Printfu32(played);EOL();		if ((mode != MODE_PAUSE)&&( mode != MODE_IDLE)){ // nothing to restore		//PRINT("MODE != PAUSE");EOL();		return MMC_present;	}		// todo: check if mmc is same mmc		// check song is available	if ( FAT_getNextSong(file-1,FAT16_dir_first_cluster) == file){ // song found		gFAT_entry = file; // select file		cue_file(); // load the file into memory		gMode = mode;		gSector = sector; // move to last played position		gFileSectorsPlayed = played;		gPlayTimeSeconds = time;		gLCD_Refresh=TRUE;	}else{ // song not found		gFAT_entry =FAT16_entryMIN;	}	return MMC_present;}uint8 open_Dir(uint32 directory){	uint8 files =0;		// display mode on LCD	LCD_Scanning();	files=FAT_scanDir(directory);		gFAT_entry= FAT16_entryMIN;	cue_file(); // load the file into memory		// Display back to normal	LCD_FileName(TRUE);		//PRINT("1ST FLE @ ");UART_Printfu16(FAT16_entryMIN);	//PRINT("  LST FLE @ ");UART_Printfu16(FAT16_entryMAX);	//UART_Printfu08(files);PRINT("files");EOL();		return files;}/***   init_mmc_fat*	@return		error from MMC or fat initialization or 0 for everything okay**	initialize the mmc and read fat/partition details from the disk.*	Needs to be run everytime MMC is changed.**/uint8 	init_mmc_fat(void){ 	uint8 result;		//DumpGlobals();	gFAT_entry = 0;	FAT16_entryMIN = 0;	FAT16_entryMAX = -1;		result =  MMC_Reset();				// init mmc	if (result) return result;		result = FAT_initFat16();	if (result) return result; // abort on non-zero reply 		result = open_Dir(FAT16_root_dir_first_cluster);		//return 0 if files found, 1 if 0 files	return !result;}/***   flush_decoder*	@return		none

⌨️ 快捷键说明

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