#include "wav_pit.h"
#include "wav_dac.h"
#include "pit.h"
#include "data.h"
#include "dac_drv.h"
#include "pit32_drv.h"
#include "trans_port.h"


#if WAV_FLAG

UINT16 etARRValue(UINT16 sample)
{
	UINT16 arrValue;
	/* 更新OCA值以符合.WAV文件采样率 */
	switch (sample)
	{
	case 8000 :
	    arrValue = (uint16_t)(75000000/8000);
	    break; /* 8KHz = 75MHz / 9375 */
	case 11025:
	    arrValue = (uint16_t)(75000000/11025);
	    break; /* 11.025KHz = 75MHz / 6802 */
	case 16000:
	     arrValue = (uint16_t)(75000000/16000);
	    break; /* 16KHz = 75MHz / 4687 */
	case 22050:
	    arrValue = (uint16_t)(75000000/22050);
	    break; /* 22.05KHz = 75MHz / 3401 */
	case 44100:
	    arrValue = (uint16_t)(75000000/44100);
	    break; /* 44.1KHz = 75MHz / 1700 */
	case 48000:
	    arrValue = (uint16_t)(75000000/48000);
	    break; /* 48KHz = 75MHz / 1562 */
	default:
	    arrValue = 0;
	    break;
	}
	return arrValue - 1;
}

void DAC_WriteData(UINT16 data)       
{ 
	DAC->DAC_DR = data;
}

void DAC_SetData(UINT16 data)
{
	DAC_WriteData(data);           
	DAC->DAC_SWTR |= SW_TRIG;
	DAC_Wait_Load_Done();
}

void close_wav(void)
{
	bufPos = 0;
	gWavAddrFull_Flag = 0;
	gWavFinish_Flag = 0;
	PIT2->PCSR &=~PCSR_EN;
	gWavState = 0;

	gWavFlag = 0;
	
	WAV_amplifier_DIS();
}

UINT8 LT_PlayWav(void)
{
	UINT16 jj=0;
	if(gWavState == 1)
	{
//		printf("start\r\n");
		gWavState = 0;
		gTempWavFlashAddr = gWavFlashAddr;
		
		LT_ReadFlash(wavBuf0,gTempWavFlashAddr,bufflen);																//读取数据到 Buf0									//2019
		gTempWavFlashAddr += bufflen;
		wbuf0_Stat = 1;																																//Buf0 数据准备好

		LT_ReadFlash(wavBuf1,gTempWavFlashAddr,bufflen);																//读取数据到 Buf1									//2019
		gTempWavFlashAddr += bufflen;
		wbuf1_Stat = 1;																																//Buf1 数据准备好
		wbuf_select = 1;																															//选择 Buf0

		WAV_amplifier_EN();		//每次播放前使能功放IC
		gWavOpen = 1;					//因为IC使能后需要一段时间才能正常工作，这里若加延时会造成其他动效有停顿一下的情况，所以延时的工作交给定时器，这里负责打开这个延时的标志位
	}

	if(wbuf0_Stat == 0)
	{
//		if(gWavLen-gTempWavFlashAddr>=2048)
//		{
//			gWavDMA_Len_buf0 = 2048;
//		}
//		else
//		{
//			gWavDMA_Len_buf0 = gWavLen-gTempWavFlashAddr;
//		}
		LT_ReadFlash(wavBuf0,gTempWavFlashAddr,bufflen);
		gTempWavFlashAddr+=bufflen;
		wbuf0_Stat = 1; 

		if(gTempWavFlashAddr>=gWavLen)																								//读取数据地址超出 WAV 文件长度， 地址归零
		{
			gTempWavFlashAddr = gWavFlashAddr;
			
			gWavAddrFull_Flag=1;
		}
//		printf("111:%d\r\n",gWavDMA_Len_buf0);
	}
	if(wbuf1_Stat == 0)
	{
//		if(gWavLen-gTempWavFlashAddr>=2048)
//		{
//			gWavDMA_Len_buf1 = 2048;
//		}
//		else
//		{
//			gWavDMA_Len_buf1 = gWavLen-gTempWavFlashAddr;
//		}
		LT_ReadFlash(wavBuf1,gTempWavFlashAddr,bufflen);
		gTempWavFlashAddr+=bufflen;
		wbuf1_Stat = 1;

		if(gTempWavFlashAddr>=gWavLen)																								//读取数据地址超出 WAV 文件长度， 地址归零
		{
			gTempWavFlashAddr = gWavFlashAddr;
			gWavAddrFull_Flag=1;
		}
//		printf("222:%d\r\n",gWavDMA_Len_buf1);
	}
	
	if(gWavAddrFull_Flag&&gWavFinish_Flag&&gWavType == 0)
	{
//		printf("end\r\n");
		close_wav();	//判断DMA读的所有数据都通过DAC转换完，同时标志位为单曲循环，才关闭
	}
	
	return 0;	
}
#endif
