/*********************************************************************************

Copyright(c) 2007 Analog Devices, Inc. All Rights Reserved. 

This software is proprietary and confidential.  By using this software you agree
to the terms of the associated Analog Devices License Agreement.  

Description:
        Contains functions to initialize and terminate the LCD attached to the BF548
        EZ-KIT Lite.
				
*********************************************************************************/

/*****************************************************************************\

  Includes:

\*****************************************************************************/

//#include <stdio.h>
#include <string.h>
//#include <ctype.h>

//#include <services/services.h>                  /* system service includes              */
//#include <drivers/adi_dev.h>                    /* device manager includes              */
#include "mediaplayer.h" 
//#include "devicecontrol.h" 
#include "ada_gfx/gfx.h"
//#include <adi_id3_parser.h>

#if defined(__ADSP_MOAB__)
	#include "lcd_nl6448.h"
#endif        

#if defined(__ADSP_KOOKABURRA__)
	#include <lcd_t350mcqb01.h>
#endif

/*****************************************************************************\

  Defines:

\*****************************************************************************/


/*****************************************************************************\

  Declarations:

\*****************************************************************************/

GFX_CANVAS Canvas,TextCanvas;
                        
static DRIVEENTRY *pDRIVEENTRY;

// LCD starting BOX position where the selection box is drawn
// LCD starting BOX position where the selection box is drawn
u32 Selection_Box [] = 
{
	SELECTION_BOX_START_Y1,
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*1),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*2),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*3),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*4),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*5),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*6),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*7),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*8),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*9),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*10),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*11),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*12),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*13),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*14),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*15),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*16),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*17),				
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*18),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*19),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*20),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*21),
	SELECTION_BOX_START_Y1+(SELECTION_BOX_SPACING*22),
};

/*****************************************************************************\

  Externs:

\*****************************************************************************/


/*****************************************************************************\

  Function Prototypes

\*****************************************************************************/

void display_fileselector	(u8 *pBuffer, DRIVEENTRY *pFileSystem, u32 FileSelectionIndex);
void display_filelist	 	(u8 *pBuffer, DRIVEENTRY *pFileSystem, u32 FileSelectionIndex);
void display_filetag	 	(u8 *pBuffer, lcd_taginfo_t *pTag, u32 Type);
void display_statusinfo	 	(u8 *pBuffer, u32 Status);

void display_filelistframe	(u8 *pBuffer);
void display_filetagframe 	(u8 *pBuffer);
void display_statusframe  	(u8 *pBuffer);
void display_test			(u8 *pBuffer);

void display_start_message(u8 *pBuffer);

/****************************************************************************
    Function:      display_start_message

    Description:   
    			    
    
   Return value:   
*****************************************************************************/
section ("mpp_nocache")
void display_start_message(u8 *pBuffer){
	u32 Colour, Fill;
	
	/* Set Background Canvas to Black in 24 bit VGA mode */
	gfx_init(&Canvas,GRAPHICS_MODE,(u16*)(pBuffer),0,0,0,0);
	Colour=gfx_getcolour(&Canvas,255,0,0);
	Fill=gfx_getcolour(&Canvas,0,0,0); /* BLACK */
	
	/* Set text Canvas */
	gfx_init(&TextCanvas,GRAPHICS_MODE,(u16*)(pBuffer),0,0,0,0);
	
	/* Set text colour and font */
	Colour=gfx_getcolour(&TextCanvas,0,0,255); /* BLUE */
	gfx_setcolour(&TextCanvas, Colour);
	gfx_setfont(&TextCanvas,4);

	/* Position and output text header field */
	gfx_goto(&TextCanvas, 60,20);
	gfx_printf(&TextCanvas,"ANALOG DEVICES");			

	Colour=gfx_getcolour(&TextCanvas,255,255,255); /* WHITE */
	gfx_setcolour(&TextCanvas, Colour);

	/* Position and output text header field */
	gfx_goto(&TextCanvas, 40,60);
	gfx_printf(&TextCanvas,"Loading........please wait");			

}

/****************************************************************************
    Function:      display_filelistframe

    Description:   Displays the frame for the file list
    			    
    
   Return value:   
*****************************************************************************/
section ("mpp_nocache")
void display_filelistframe(u8 *pBuffer){
	u32 Colour, Fill;
	
	/* Set Background Canvas to Black in 24 bit VGA mode */
	gfx_init(&Canvas,GRAPHICS_MODE,(u16*)(pBuffer),0,0,0,0);
	Colour=gfx_getcolour(&Canvas,255,0,0);
	Fill=gfx_getcolour(&Canvas,0,0,0); /* BLACK */
	/* File listing frame */
	gfx_boxf(&Canvas, 0, 0, LCD_WIDTH-1, BORDER_FILELISTING, Colour, Fill);
	/* Header line */
	gfx_line(&Canvas,0,BORDER_HEADER,LCD_WIDTH-1,BORDER_HEADER,Colour);
	/* File listing separation line*/
	gfx_line(&Canvas,0,BORDER_FILELISTING+1,LCD_WIDTH-1,BORDER_FILELISTING+1,Fill);	
	
	/* Set text Canvas */
	gfx_init(&TextCanvas,GRAPHICS_MODE,(u16*)(pBuffer),0,0,0,0);
	
	/* Set text colour and font */
	Colour=gfx_getcolour(&TextCanvas,255,255,255); /* WHITE */
	gfx_setcolour(&TextCanvas, Colour);
	gfx_setfont(&TextCanvas,4);
	
	/* Position and output text header field */
	gfx_goto(&TextCanvas, TEXT_CENTRED_HEADER,3);
	gfx_printf(&TextCanvas,"ADI - Multimedia Player");			

}

/****************************************************************************
    Function:      display_filetagframe

    Description:   Displays the frame for the file tag
    			    
    
   Return value:   
*****************************************************************************/
section ("mpp_nocache")
void display_filetagframe(u8 *pBuffer){
	u32 Colour, Fill;
	
	gfx_init(&Canvas,GRAPHICS_MODE,(u16*)(pBuffer),0,0,0,0);
	Colour=gfx_getcolour(&Canvas,255,0,0); /* RED */
	Fill=gfx_getcolour(&Canvas,0,0,0); /* BLACK	*/
	
	/* File tagging frame */
    gfx_boxf(&Canvas, 0, BORDER_FILETAG_TOP, LCD_WIDTH-1, BORDER_FILETAG_BOTTOM, Colour, Fill);
    /* File tagging separation line */
	gfx_line(&Canvas,0,BORDER_FILETAG_BOTTOM+1,LCD_WIDTH-1,BORDER_FILETAG_BOTTOM+1,Fill);

}

/****************************************************************************
    Function:      display_statusframe

    Description:   Displays the status frame
    			    
    
   Return value:   
*****************************************************************************/
section ("mpp_nocache")
void display_statusframe(u8 *pBuffer){
	u32 Colour, Fill;
	
	gfx_init(&Canvas,GRAPHICS_MODE,(u16*)(pBuffer),0,0,0,0);
	Colour=gfx_getcolour(&Canvas,255,0,0); /* RED */
	Fill=gfx_getcolour(&Canvas,0,0,0); /* BLACK	*/
	/* Status frame */
    gfx_boxf(&Canvas, 0, BORDER_STATUS_TOP, LCD_WIDTH-1, LCD_HEIGHT-1, Colour, Fill);
}

/****************************************************************************
    Function:      display_filelist

    Description:   This function updates the LCD data buffers with
    				graphical data for file listing of a directory read
    
   Return value:   
*****************************************************************************/
section ("mpp_nocache")
void display_filelist(u8 *pBuffer, DRIVEENTRY *pFileSystem, u32 FileSelectionIndex){

	u32 i,Colour, index;
	char filename[100];
	char	*file_extension;
	int		filename_length,x;
	
	if(FileSelectionIndex>=DISPLAY_FILELIST_MAX)
		FileSelectionIndex = FileSelectionIndex-DISPLAY_FILELIST_MAX+1;	
	else	
		FileSelectionIndex = 0;

	/* Set text Canvas */
	gfx_init(&TextCanvas,GRAPHICS_MODE,(u16*)(pBuffer),0,0,0,0);
	
	/* Set text colour and font */
	Colour=gfx_getcolour(&TextCanvas,255,255,255); /* WHITE */
	gfx_setcolour(&TextCanvas, Colour);		

	gfx_setfont(&TextCanvas,4);						
	
	/* if no files to list display message */	
	if(pFileSystem==NULL)
	{
		gfx_goto(&TextCanvas, TEXT_POSITION_OFFSET_X,TEXT_POSITION_OFFSET_Y);	
		gfx_printf(&TextCanvas,"No File List");
		return;
	}		
	
	/* Output the number of FileList entries that can fit on LCD Display */
	if(pFileSystem->indexnum > DISPLAY_FILELIST_MAX)
		index = DISPLAY_FILELIST_MAX;
	else
		index = pFileSystem->indexnum;
		
	for	(i=0; i<index; i++)
	{
		gfx_goto(&TextCanvas, TEXT_POSITION_OFFSET_X,TEXT_POSITION_OFFSET_Y+(TEXT_SPACING*i));	
		
		/* terminate string to LCD screen width*/
		strncpy(filename,pFileSystem->flist[FileSelectionIndex+i].entryname,40);
		filename[40]='\0';
		
		if( /* if selection is directory name, procede string with '\' character to identify directory 	*/
			/* if directory entry is a path to a volume i.e '..' or '.'  insert '\' character also		*/
			(pFileSystem->flist[FileSelectionIndex+i].type == FILETYPE_DIRECTORY) || 
		    (pFileSystem->flist[FileSelectionIndex+i].type == FILETYPE_DRIVE && pFileSystem->flist[FileSelectionIndex+i].entryname[0] =='.')) 
			{
		   		gfx_printf(&TextCanvas,"\\%s",filename);
		   	}	

		else
			{
				file_extension = strrchr(pFileSystem->flist[FileSelectionIndex+i].entryname,'.');
				filename_length = strlen(pFileSystem->flist[FileSelectionIndex+i].entryname);
				if (filename_length > MAX_FILENAME_LENGTH)
				{

					strncpy(filename,pFileSystem->flist[FileSelectionIndex+i].entryname, MAX_FILENAME_LENGTH-strlen(file_extension)-1);
					filename[MAX_FILENAME_LENGTH-strlen(file_extension)]='\0';	
					strcat(strcat(filename, "~"),file_extension);
				}	
				/* if selection is file name, just output string*/  
				gfx_printf(&TextCanvas,"%s", filename);
			}
	}
}

/****************************************************************************
    Function:      display_fileselector

    Description:   This function updates the LCD data buffers with
    			    - graphical data for the file selection highlight box
    				
    
   Return value:   
*****************************************************************************/
section ("mpp_nocache")
void display_fileselector(u8 *pBuffer, DRIVEENTRY *pFileSystem,u32 FileSelectionIndex)
{
	u32 Colour;
	if(FileSelectionIndex>=DISPLAY_FILELIST_MAX)
		FileSelectionIndex = DISPLAY_FILELIST_MAX-1;
		
	/*Set text Canvas */
	gfx_init(&Canvas,GRAPHICS_MODE,(u16*)(pBuffer),0,0,0,0);
	

	if(pFileSystem->flist[FileSelectionIndex].type == FILETYPE_DIRECTORY || 
	   pFileSystem->flist[FileSelectionIndex].type == FILETYPE_DRIVE)
	{
		/*Set text colour RED*/
		Colour=gfx_getcolour(&Canvas,255,0,0);
		gfx_setcolour(&Canvas, Colour);
	}
	else	
	{
		/*Set text colour BLUE*/
		Colour=gfx_getcolour(&Canvas,0,0,255);
		gfx_setcolour(&Canvas, Colour);
	}

	/* Fill and position selection box on LCD*/
	gfx_boxf(	&Canvas,
				SELECTION_BOX_X1,
				Selection_Box[FileSelectionIndex],
				SELECTION_BOX_X2,
				Selection_Box[FileSelectionIndex]+SELECTION_BOX_HEIGHT,
				Colour,
				Colour);


}

/****************************************************************************
    Function:      display_filetag

    Description:   This function outputs audio file tag information
    
   Return value:   
*****************************************************************************/
//section ("sdram0_bank3_app")
section ("mpp_nocache")
void display_filetag(u8 *pBuffer, lcd_taginfo_t *pTag, u32 Type ){
	u32 i,Colour;
	char Tag[MAX_TAG_LENGTH+1];
	int minutes, seconds;
	
	/* clear the file tag frame */
	display_filetagframe(pBuffer);

	/* Set text Canvas */
	gfx_init(&TextCanvas,GRAPHICS_MODE,(u16*)(pBuffer),0,0,0,0);
		
	gfx_setfont(&TextCanvas,3); /* small font*/
	Colour=gfx_getcolour(&TextCanvas,255,255,255);/*YELLOW*/
	gfx_setcolour(&TextCanvas, Colour);
	
	/* if TAG_CLEAR, just clear the tag frame */
	if(Type != TAG_CLEAR)
	{

	
		if(Type == TAG_DISPLAY)
		{	
			/* Graphics for song title tag */
			strncpy(Tag,pTag->song_title,MAX_TAG_LENGTH);
			Tag[MAX_TAG_LENGTH]='\0';
			gfx_goto(&TextCanvas, TEXT_POSITION_OFFSET_X,BORDER_FILETAG_TOP + 4);	
			gfx_printf(&TextCanvas,"Title: %s",Tag);
			
			/* Graphics for song artist tag */			
			strncpy(Tag,pTag->song_artist,MAX_TAG_LENGTH);
			Tag[MAX_TAG_LENGTH]='\0';
			gfx_goto(&TextCanvas, TEXT_POSITION_OFFSET_X,BORDER_FILETAG_TOP + 4 + TAG_SPACING);	
			gfx_printf(&TextCanvas,"Artist: %s",Tag);

			/* Graphics for album tag */						
			strncpy(Tag,pTag->song_album,MAX_TAG_LENGTH);
			Tag[MAX_TAG_LENGTH]='\0';
			gfx_goto(&TextCanvas, TEXT_POSITION_OFFSET_X,BORDER_FILETAG_TOP + 4 + TAG_SPACING*2);	
			gfx_printf(&TextCanvas,"Album: %s",Tag);
		
			/* Graphics for track time tag */
			if (pTag->file_type == MP3_TYPE)
			{								
				minutes = atoi(pTag->song_track_time)/60000;
				seconds = (atoi(pTag->song_track_time)-minutes*60000)/1000;
				gfx_goto(&TextCanvas, TEXT_POSITION_OFFSET_X,BORDER_FILETAG_TOP + 4 + TAG_SPACING*3);				
				gfx_printf(&TextCanvas,"Playtime: %d:%d",minutes,seconds);
			}
			else
			{
				gfx_goto(&TextCanvas, TEXT_POSITION_OFFSET_X,BORDER_FILETAG_TOP + 4 + TAG_SPACING*3);	
				gfx_printf(&TextCanvas,"Playtime Not Available");

			}
		}
		else
		{
			gfx_goto(&TextCanvas, TEXT_POSITION_OFFSET_X,BORDER_FILETAG_TOP + 4);	
			gfx_printf(&TextCanvas,"No Information Available");
		}	
	}	
		
}

/****************************************************************************
    Function:      display_statusinfo

    Description:   This function outputs Information regarding the status
    				of the media player
    
   Return value:   
*****************************************************************************/
//section ("sdram0_bank3_app")
section ("mpp_nocache")
void display_statusinfo(u8 *pBuffer, u32 Status){
	u32 i,Colour;
	
	/* clear the status frame */
	display_statusframe(pBuffer);

	/* Set Background Canvas to Black in 24 bit VGA mode */
	gfx_init(&Canvas,GRAPHICS_MODE,(u16*)(pBuffer),0,0,0,0);
	
	gfx_setfont(&TextCanvas,3); /* small font */
	Colour=gfx_getcolour(&TextCanvas,255,255,200);/* Yellow */
	gfx_setcolour(&TextCanvas, Colour);
	gfx_goto(&TextCanvas, TEXT_CENTRED_STATUS,BORDER_STATUS_TOP+7);
		
	if (Status == STATE_PAUSE)	
	{	
		gfx_printf(&TextCanvas,"PAUSED");
		gfx_goto(&TextCanvas, TEXT_CENTRED_WARNING,BORDER_STATUS_TOP+20);	
		gfx_printf(&TextCanvas,"Do not unplug media while playing");
	}	
	else if	(Status == STATE_PLAY)
	{
		gfx_printf(&TextCanvas,"PLAYING");
		gfx_goto(&TextCanvas, TEXT_CENTRED_WARNING,BORDER_STATUS_TOP+20);	
		gfx_printf(&TextCanvas,"Do not unplug media while playing");
	}	
	else if	(Status == STATE_IDLE)
	{
		gfx_printf(&TextCanvas," STOPPED");
		gfx_goto(&TextCanvas, TEXT_CENTRED_WARNING,BORDER_STATUS_TOP+20);
		gfx_printf(&TextCanvas,"Do not unplug media while playing");
	}	
}

section ("mpp_nocache")
void display_test(u8 *pBuffer){
	u32 Colour, Fill;
	
	gfx_init(&Canvas,GRAPHICS_MODE,(u16*)(pBuffer),0,0,0,0);
	Colour=gfx_getcolour(&Canvas,255,0,0); /* RED */
	Fill=gfx_getcolour(&Canvas,255,0,0); /* BLACK	*/
	/* Status frame */
    gfx_boxf(&Canvas, 0, 0, 319, 239, Colour, Fill);
}
