#include   <string.h>
#include "Speaker.h"
#include "DrvRT905.h"
#include "OsekCom.h"
#include "smartee.h"
/***************************************************************
copyright from private LiuXiao
if you have any question,you can contact me by email 461445092@qq.com
2022-10-05 17:56:24.020233
***************************************************************/
speaker_queue_str  speaker_queue;
Speaker_main_e_typ speaker_main_state_str;
STATE_UINT16       speaker_cycle_u16;
speaker_ctrl_Str   speaker_ctrl_buf_str;
static uint8_t DAC_Initialize_status = 0;
static uint8_t DAC_Initialize_RetryCnt = 3;
extern uint8 UDS_DAC_Ref_Voltage;
/************************************
Get Sw Fifo empty condition
************************************/

uint8_t speaker_queue_is_empty(void)
{
	uint8_t fl_temp_U8;
	__disable_irq();
	if(speaker_queue.queue_in == speaker_queue.queue_out)
	{
		fl_temp_U8 = TRUE;
	}
	else
	{
		fl_temp_U8 = FALSE;
	}
	__enable_irq();
	return fl_temp_U8;
}

/************************************
Get Sw Fifo Full condition
************************************/

uint8_t speaker_queue_is_full(void)
{
	uint8_t fl_temp_U8;
	__disable_irq();
	fl_temp_U8 = (speaker_queue.queue_in + 1) % (sizeof(speaker_queue.queue)/sizeof(speaker_queue.queue[0]));
	if(fl_temp_U8 == speaker_queue.queue_out)
	{
		fl_temp_U8 = TRUE;
	}
	else
	{
		fl_temp_U8 = FALSE;
	}
	__enable_irq();
	return fl_temp_U8;
}


/************************************
Push a element data to queue
************************************/

uint8_t speaker_queue_push_e(speaker_ctrl_Str fl_str_e)
{
	uint8_t fl_temp_U8;
	
	if(TRUE == speaker_queue_is_full())
	{
		return FALSE;
	}
	else
	{
	
		__disable_irq();
		fl_temp_U8 = (speaker_queue.queue_in + 1) % (sizeof(speaker_queue.queue)/sizeof(speaker_queue.queue[0]));
		speaker_queue.queue_in = fl_temp_U8;
		speaker_queue.queue[fl_temp_U8]  = fl_str_e;
		__enable_irq();
		return TRUE;
	}
}


/************************************
Get  Data from Rx Sw Fifo
return: TRUE->OK    FALSE->Fail
************************************/

uint8_t speaker_queue_pull_e(speaker_ctrl_Str *fl_str_e)
{
	uint8_t fl_temp_U8;

	if(TRUE == speaker_queue_is_empty())
	{
		return FALSE;
	}
	else
	{
	    __disable_irq();
		fl_temp_U8 = (speaker_queue.queue_out + 1) % (sizeof(speaker_queue.queue)/sizeof(speaker_queue.queue[0]));
		speaker_queue.queue_out = fl_temp_U8;
		fl_str_e[0] = 	speaker_queue.queue[fl_temp_U8];
	
		__enable_irq();

		return TRUE;
	}
}

/************************************
uart init
************************************/

void speaker_Init(void)
{
	//SA51024_Init();
    
	memset(&speaker_queue,0,sizeof(speaker_queue));	
	speaker_main_state_str = SPEAKER_INIT;
	DAC_Initialize_RetryCnt = 0x03;
	DAC_Initialize_status = 0;

    PORT_PinGPIOConfig(PORT_PIN_PA02);
    PORT_PinOutputEnable(PORT_PIN_PA02);
    PORT_PinWrite(PORT_PIN_PA02,1);/*EN = 1，turn on chip*/
    uint8_t eedata[5];
    SmartEE_Read(0x781, eedata, 1); 
    if(eedata[0]< 4)
    {
        UDS_DAC_Ref_Voltage = eedata[0];
    }else
    {
        UDS_DAC_Ref_Voltage = 0;
    }
}

/*
para level 0~3,other value force set to 3
return void
*/
/*level  ->0  ------ 1 ----------2 ---------3*/
/*gain0/1->00 ------ 10----------01---------11*/

void speaker_set_strenght(FA51024_ctrl_e_typ level)
{
	
	//PORT_PinWrite(PORT_PIN_PA13,1);/*SHUTDOWN = 1，turn on chip*/

	if(level == SA51024_STRENGTH_LEVEL0)
	{
		PORT_PinWrite(PORT_PIN_PB14,0);/*GAIN1*/
		PORT_PinWrite(PORT_PIN_PB15,0);/*GAIN0*/
	}
	else if(level == SA51024_STRENGTH_LEVEL1)
	{
		PORT_PinWrite(PORT_PIN_PB14,0);/*GAIN1*/
		PORT_PinWrite(PORT_PIN_PB15,1);/*GAIN0*/
	}
	else if(level == SA51024_STRENGTH_LEVEL2)
	{
		PORT_PinWrite(PORT_PIN_PB14,1);/*GAIN1*/
		PORT_PinWrite(PORT_PIN_PB15,0);/*GAIN0*/
	}
	else
	{
		PORT_PinWrite(PORT_PIN_PB14,1);/*GAIN1*/
		PORT_PinWrite(PORT_PIN_PB15,1);/*GAIN0*/
	}
}
uint8_t rxdata=0,rxerr=0;

/*
speaker main control state task
*/
void  speaker_MainTask(void)
{
	speaker_ctrl_Str fl_str_e;
	uint8_t ret,temp;

	switch(speaker_main_state_str)
	{
		case SPEAKER_INIT:
            //SA51024_Set_State(SA51024_STOP,speaker_ctrl_buf_str.strenght);
            PORT_PinGPIOConfig(PORT_PIN_PA02);
            PORT_PinOutputEnable(PORT_PIN_PA02);
            PORT_PinWrite(PORT_PIN_PA02,1);/*EN = 1，turn on chip*/

            if(speaker_cycle_u16++ >= 10)//20ms
			{
                ret = RT903_Init();
                if (ret == ERR_OK)
                {
                    speaker_set_strenght(SA51024_STRENGTH_LEVEL0);
                    speaker_main_state_str = SPEAKER_INIT_CYCLE;
                    speaker_cycle_u16 = 0;
                }
			}
            
            
		break;
		case SPEAKER_INIT_CYCLE:
		    if(speaker_cycle_u16++ >= 10)//20ms
			{
				if(DAC_Initialize_status == 0x00)
				{
                    ret = RT903_WriteWave();
					if(ret == ERR_OK || DAC_Initialize_RetryCnt == 0x00)
					{
						DAC_Initialize_status = 0x01;
					}else{
						if(DAC_Initialize_RetryCnt != 0x00){
							DAC_Initialize_RetryCnt--;
						}
					}
				}
			}

			if(speaker_cycle_u16++ >= ((SPEAKER_INTERVAL_CYCLE_TIMES)/(SPEAKER_TASK_PERIOD)))
			{
                speaker_main_state_str = SPEAKER_IDLE;
			}
		break;
		case SPEAKER_IDLE:
			if(TRUE == speaker_queue_pull_e(&fl_str_e))
			{
				speaker_ctrl_buf_str = fl_str_e;
				speaker_main_state_str = SPEAKER_SET_STRENGHT;
                speaker_cycle_u16 = 0;
			}
            else
            {
                //rxerr = RT903_ReadReg_block(0x00,&rxdata);//for test
                //InitMessage(SIG_STEERWHLTOUCHBDLECNTR, &rxerr);
                //InitMessage(SIG_STEERWHLTOUCHBDLECHKS, &rxdata);
            }
		break;

		case SPEAKER_SET_STRENGHT:
			{
                temp = speaker_ctrl_buf_str.strenght + UDS_DAC_Ref_Voltage*2;
                if (temp > 5)
                {
                    speaker_main_state_str = SPEAKER_UPDATE_CYCLE;
                    break;
                }
                
                if(RT903_SetWaveNo(temp) == ERR_OK)
                {
                    speaker_main_state_str = SPEAKER_START;
                    speaker_cycle_u16 = 0;
                }	
                else
                {
                    speaker_cycle_u16++;
                    if (speaker_cycle_u16 >= 3)
                    {
                        speaker_main_state_str = SPEAKER_UPDATE_CYCLE;
                    }
                    
                }		
			}
		break;
		
		case SPEAKER_INTERVAL:
			 if(speaker_cycle_u16++ >= 1)
			 {
				speaker_main_state_str = SPEAKER_START;
			 }
		break;
			
		case SPEAKER_START:
			{
                ret = RT903_PlayWave();
                if (ret == ERR_OK)
                {
                    speaker_main_state_str = SPEAKER_UPDATE_CYCLE;
                    speaker_cycle_u16 = 0;
                }
			}
		break;
		
		case SPEAKER_UPDATE_CYCLE:
			if(speaker_cycle_u16++ >= 9)
			{
				speaker_main_state_str = SPEAKER_STOP;
			}
		break;
			
		default:
		case SPEAKER_STOP:
			{
				SA51024_Set_State(SA51024_STOP,SA51024_STRENGTH_LEVEL0);
				speaker_main_state_str = SPEAKER_IDLE;
				speaker_cycle_u16 = 0;
			}
		break;


	}


}

/*
para1 cycle time unit ms
para2 level 0~3,other value force set to 3
return FALSE tigger fail,TRUE tigger sucess
*/
STATE_UINT8  speeker_Tig_Once(uint16_t cycle_time,FA51024_ctrl_e_typ level)
{
	speaker_ctrl_Str fl_str_e;
	STATE_UINT8 ret = FALSE;

	if(cycle_time <= (SPEAKER_MAX_CYCLE_TIMES))
	{
		if(level <= SA51024_STRENGTH_LEVEL3)
		{
			fl_str_e.cycle = cycle_time/SPEAKER_TASK_PERIOD;
			fl_str_e.strenght = level;
			if(TRUE == speaker_queue_push_e(fl_str_e))
			{
				ret = TRUE;
			}
		}
	}

	return ret;
}


Speaker_main_e_typ Get_Speaker_Main_State(void)
{
	return speaker_main_state_str;
}




