/*****************************************************************************\

  File Name: thumbwheel.c

  Description: .

  Copyright(c) 2007 Analog Devices Incorporated. All Rights Reserved.

  Revision History:
    


\*****************************************************************************/

/*****************************************************************************\

  Includes:

\*****************************************************************************/
#include <services/services.h>				/* system service includes 	*/
#include <drivers/adi_dev.h>			
#include <SDK-ezkitutilities.h>
#include "adi_ssl_init.h"
#include "mediaplayer.h"
#include <drivers/rotary/adi_cnt.h>			/* rotary counter driver includes   */


/*****************************************************************************\

  Defines:

\*****************************************************************************/

#define THUMBWHEEL_ARR_SIZE (10) /* number of TWHEEL commands that can be buffered */
#define		UP_STRIDE			3
#define		DOWN_STRIDE			-3
//#define		VOLUME_MAX			0x3FF


/*****************************************************************************\

  Declarations:

\*****************************************************************************/

static ADI_DEV_DEVICE_HANDLE RotaryDriverHandle = NULL;
OS_EVENT *thumbwheel_Q;						/* queue to hold TWHEEL commands */
void* thumbwheel_arr[THUMBWHEEL_ARR_SIZE]; 	/* Empty void* array to hold Q messages */
OS_EVENT *thumbwheel_event;

/*****************************************************************************\

  Externs:

\*****************************************************************************/
extern OS_EVENT *mgr_application_Q;

/*****************************************************************************\

  Function Prototypes:

\*****************************************************************************/

int init_thumbwheel(void);
static void callback_thumbwheel(void *AppHandle,u32  Event,void *pArg);
int close_thumbwheel(void);

/*********************************************************************

    Function:       ThumbwheelCallback

    Description:    Ez-Kit callback service routine
    				for Rotary-counter Events
*********************************************************************/
section ("callback_code")
static void callback_thumbwheel	(	
	void *AppHandle,
	u32  Event,
	void *pArg )
{
	
	MPPMESSAGE	MessageToApplication;
	u8 err;
	ADI_CNT_CBSTATUS *pResult;	                /* pointer to Rotary callback buffer   */
	pResult = (ADI_CNT_CBSTATUS *)pArg;         /* Pointer to rotary counter results */
	int iCounter;
	bool VolumeChange = FALSE;

	
    /* Case of (event type)*/
    switch (Event) {
    	
		case ADI_CNT_EVENT_CZMPIN_INT:
			MessageToApplication.uMID = MSGID_PLAYER_PLAY;
			if(OSQPost(mgr_application_Q, &MessageToApplication)!= OS_NO_ERR)
    			ezErrorCheck(1); 
    			
    	ezToggleLED(0);

		    break;

		/* CASE (Up/down count interrupt occurred) */
		case ADI_CNT_EVENT_UPCOUNT_INT:
		case ADI_CNT_EVENT_DOWNCOUNT_INT:
			iCounter = pResult->CntCounter;
			
			//printf("%d\n",iCounter);
			if (iCounter < 0)
			{
				if ((iCounter <= DOWN_STRIDE) && ((iCounter % DOWN_STRIDE) == 0))
					{
    					VolumeChange = TRUE;
					}
	
			}
			else if( iCounter > 0)
			{
				if ((iCounter >= UP_STRIDE) && ((iCounter % UP_STRIDE) == 0))
					{
						VolumeChange = TRUE;
					}
	
			}
			else /* iCounter == 0 */
			{
				VolumeChange = TRUE;
			}
		
			if (VolumeChange )
				{
					if(Event == ADI_CNT_EVENT_UPCOUNT_INT )
					{
						
						if (MPPState == STATE_IDLE)
						MessageToApplication.uMID = MSGID_PREV_ENTRY;
						else
						MessageToApplication.uMID = MSGID_VOLUME_UP;

					}
					else
					{
						if (MPPState == STATE_IDLE)
						MessageToApplication.uMID = MSGID_NEXT_ENTRY;
						else
						MessageToApplication.uMID = MSGID_VOLUME_DN;

					}
				
					MessageToApplication.Param1 = iCounter;
					if(OSQPost(mgr_application_Q, &MessageToApplication)!= OS_NO_ERR)
    				ezErrorCheck(1); /* wait for help */
				}
		
		    break; 
		    
		default:
        	ezTurnOnAllLEDs();
        	ezErrorCheck(1);
        	break;
          
    }
}
	    

/*********************************************************************

	Function:		Init_Thumbwheel

	Description:	Initializes the thumbwheel.  

*********************************************************************/
int init_thumbwheel(void) {
		    
	u32 Result;
	
	/************** Open Rotary counter driver ***************/
    
    if((Result = adi_dev_Open(  adi_dev_ManagerHandle,     		/* Dev manager Handle               */
                     	        &ADICNTEntryPoint,              /* Device Entry point               */
                                0,                              /* Device number                    */
	                            NULL,                           /* No client handle                 */
    	                        &RotaryDriverHandle,	        /* DevMgr handle for this device    */
				                ADI_DEV_DIRECTION_UNDEFINED,    /* data direction not used          */
            	                NULL,         	                /* Handle to DMA Manager            */
                	            adi_dcb_QueueHandle,            /* Handle to callback manager       */
                        	    callback_thumbwheel))		    /* Callback Function                */
	                	!= ADI_DEV_RESULT_SUCCESS) 
	{
    	APP_TRACE("Open Rotary counter driver Failed!, Error Code: 0x%08X\n",Result);
    }	
    
        
	/************ Rotary counter Configuration table ************/
	ADI_DEV_CMD_VALUE_PAIR  RotaryConfig[]=
    {
    	{ ADI_CNT_CMD_SET_CDG_POL_ALOW,     (void *)TRUE	},  /* set CDG polarity to active low                           */
        { ADI_CNT_CMD_SET_CUD_POL_ALOW,     (void *)TRUE    },  /* set CUD polarity to active low                           */
		{ ADI_CNT_CMD_SET_CZM_POL_ALOW,     (void *)TRUE    },  /* set CUD polarity to active low                           */            
/*		{ ADI_CNT_CMD_CZM_COUNTZERO_INT_EN,	(void *)TRUE    },
*/		{ ADI_CNT_CMD_CZMPIN_INT_EN,		(void *)TRUE	}, /* enable push button interrupt */
        { ADI_CNT_CMD_SET_CNTMODE, 			(void *)4		},	/* set for directional count mode							*/
        {ADI_CNT_CMD_SET_DEBOUNCE_REG,		(void *)7						},
        {ADI_CNT_CMD_SET_DEBOUNCE_ENABLE,	(void *)TRUE					},
        { ADI_DEV_CMD_END,					NULL            }   /* Terminate config table                                   */
	};

    /**************** Configure Rotary counter device ****************/
    if((Result = adi_dev_Control(RotaryDriverHandle, ADI_DEV_CMD_TABLE, (void *)RotaryConfig))!= ADI_DEV_RESULT_SUCCESS)
    {
		APP_TRACE("Failed to configure Rotary counter device: 0x%08X\n",Result);
    }

    /**************** Enable the Rotary counter device ****************/
    if((Result = adi_dev_Control(RotaryDriverHandle, ADI_CNT_CMD_SET_CNT_ENABLE, (void *)TRUE))!= ADI_DEV_RESULT_SUCCESS)
    {
		APP_TRACE("Failed to enable Rotary counter device: 0x%08X\n",Result);
    } 
    	    
	return (Result);
   
}

/*********************************************************************

	Function:		close_thumbwheel

	Description:	close the thumbwheel.  

*********************************************************************/
int close_thumbwheel(void)
{
	u32 Result= ADI_DEV_RESULT_SUCCESS;
	
	if (Result == ADI_DEV_RESULT_SUCCESS)	 
 		Result = adi_dev_Control(RotaryDriverHandle, ADI_CNT_CMD_SET_CNT_ENABLE, (void *)FALSE);
 	if (Result == ADI_DEV_RESULT_SUCCESS)	 
 		Result = adi_dev_Close(RotaryDriverHandle);
 		
   	return (Result);	
}











