/*****************************************************************************\

  File Name: mgr_display.c

  Description: This module contains DISPLAY thread specific functions.

  Copyright(c) 2007 Analog Devices Incorporated. All Rights Reserved.

  Revision History:
    .

\*****************************************************************************/

/*****************************************************************************\

  Includes:

\*****************************************************************************/

#include <drivers/adi_dev.h>			// Device manager includes
#include <SDK-ezkitutilities.h>
#include "mediaplayer.h"

/*****************************************************************************\

  Defines:

\*****************************************************************************/


/*****************************************************************************\

  Declarations:

\*****************************************************************************/
OS_EVENT *lcd_Q;
OS_EVENT *lcd_event;
void* lcd_arr[SZ_MSGQ_APP_CMD];

section("sdram0_bank2_stack") static OS_STK stack[THRD_STACKSIZE * sizeof(OS_STK)];

/*****************************************************************************\

  Externs:

\*****************************************************************************/

extern MPPMESSAGE	CmdStatusToApp;
extern OS_EVENT *mgr_application_Q;
extern OS_EVENT *mgr_display_Q;
extern void* mgr_display_arr[SZ_MSGQ_APP_CMD];
extern semaphore_t mpEvents[];
extern bool updateLCD;

/*****************************************************************************\

  Function Prototypes:

\*****************************************************************************/
void init_mgr_display(void);
void launch_mgr_display(void);
void thread_mgr_display(void *pEvent);

/*********************************************************************

	Function:	init_mgr_display																			
	
 	Parameters:	None 
																		
 	Return:		None														
																			
 	Description:	Initializes the display manager.
 	
*********************************************************************/
//section ("sdram0_bank3_app")
void init_mgr_display(void)
{
    /* launch a task to receive messages the media player application*/	
    launch_mgr_display();	
}
			
/*********************************************************************

	Function:	launch_mgr_display																			
	
 	Parameters:	None 
																		
 	Return:		None														
																			
 	Description:	Launches a task thread which handles LCD display.
*********************************************************************/
section ("sdram0_bank2_thread")
void launch_mgr_display(void) 
{
	/*OS_STK *stack;*/
	int Result;
	INT32U i;
	
	
	/* Create display message Queue */
	mgr_display_Q = OSQCreate(mgr_display_arr, SZ_MSGQ_APP_INPUTDEV);
	mpEvents[3].sem = mgr_display_Q;
	
	
	Result = OSTaskCreateExt(thread_mgr_display, 
						(void *)&mpEvents[3], 
						&stack[THRD_STACKSIZE -1], 
						THRD_PRIORITY_MANAGER_DISPLAY, 
						MANAGER_DISPLAY_ID, 
						&stack[0], 
						THRD_STACKSIZE , 
						(void *)0, 
						OS_TASK_OPT_STK_CHK );
						
	if(Result && APP_TRACE_LEVEL)
	{					
    	APP_TRACE("OSTaskCreateExt thread_mgr_display failed %d\n",Result);
    	ezErrorCheck(1);/* wait for help */
	}	
}// end of launch_threads


/*********************************************************************

	Function:	thread_mgr_display																			
	
 	Parameters:	None 
																		
 	Return:		None														
																			
 	Description:	execute command to display to LCD
*********************************************************************/
section ("sdram0_bank2_thread")
void thread_mgr_display(void *pEvent)
{
	INT8U err;
	MPPMESSAGE	*pMessage;
	MPPMESSAGE	MessageToLCD;
	semaphore_t *p;

	char file_path[MAX_PATH_NAME_LEN];
	u32 slen ;
	
	p = (semaphore_t *)pEvent;
	
	OS_EVENT *queue = p->sem;

	
    while(1)
	{
		/* pend on message queue */
	    pMessage = (MPPMESSAGE *) OSQPend (queue, 0, &err);
		if (err && APP_TRACE_LEVEL)
		{					
    		APP_TRACE("thread_mgr_display OSQPend error %d\n",err);
    		    	ezErrorCheck(1);/* wait for help */
		}	
	
		switch(pMessage->uMID)
		{
				/* up / down event update LCD for new file selection */
				case MSGID_PREV_ENTRY:
				case MSGID_NEXT_ENTRY:
					
					if (MPPMode != MODE_IMAGE)
						/* If up / down during Audio decode update selector		*/
						MessageToLCD.uMID = MSGID_LCD_FILESELECT;
					else
						{
							/* if JPEG image playing refresh LCD by updating	*/ 
							/* LCD file selection, clear LCD tag and status  	*/
							MessageToLCD.uMID = MSGID_LCD_REFRESH_RESUME;
							MPPMode = MODE_NONE;		
						}
					/* Send message to LCD */	
					MessageToLCD.Param1 = pMessage->Param1;
    				MessageToLCD.Param2 = pMessage->Param2;
					err = OSQPost(lcd_Q, &MessageToLCD);				
					break;				
				
				case MSGID_LCD_REFRESH_RESET:
				/* send message to LCD to update LCD to default */	
    				MessageToLCD.uMID = MSGID_LCD_REFRESH_RESET;
    				MessageToLCD.Param1 = pMessage->Param1;
					MessageToLCD.Param2 = pMessage->Param2;
					err = OSQPost(lcd_Q, &MessageToLCD);
					break;				
					
				case MSGID_LCD_TAG:
					/* Update LCD file tag field */
    				MessageToLCD.uMID = MSGID_LCD_TAG;
    				MessageToLCD.Param1 = pMessage->Param1;
    				MessageToLCD.Param2 = pMessage->Param2;
					err = OSQPost(lcd_Q, &MessageToLCD);
					break;
				
				case MSGID_LCD_STATUS:
					/* Update LCD status field */
    				MessageToLCD.uMID = MSGID_LCD_STATUS;
    				MessageToLCD.Param1 = pMessage->Param1;
					err = OSQPost(lcd_Q, &MessageToLCD);
					break;
				
				default:/* ignore all other commands */
					break;
			
		}/* end of switch */
		
		if(err && APP_TRACE_LEVEL)
		{
			APP_TRACE("thread_mgr_display OSQPost err =%d\n",err);
			ezErrorCheck(1);
		}
	
		
	}
}




