
#include "hid_online_set.h"
#include "config.h"

#define usb_online_debug(fmt, ...)      cdc_debug(fmt, ##__VA_ARGS__)
//#define usb_online_debug(fmt, ...)

void hid_get_adc_dac_cfg(uint8_t* buff,uint16_t len)
{
	uint32_t mic0_mix_gain,mic0_pga_gain;
	uint32_t mic1_mix_gain,mic1_pga_gain;
	uint32_t dac_vol;


	get_mic_mix_gain(MIC_0,(uint32_t*)&mic0_mix_gain);
	get_mic_mix_gain(MIC_1,(uint32_t*)&mic1_mix_gain);

	get_mic_gain(MIC_0,(uint32_t*)&mic0_pga_gain);
	get_mic_gain(MIC_1,(uint32_t*)&mic1_pga_gain);

	(void)audio_dac_get_volume((void*)&dac_vol,(uint32_t*)&dac_vol);

	buff[0] = mic0_pga_gain;
	buff[1] = mic0_mix_gain;
	buff[2] = mic1_pga_gain;
	buff[3] = mic1_mix_gain;
	buff[4] = dac_vol;
	usb_online_debug("mic0_pga_gain:%d\r\n",mic0_pga_gain);
	usb_online_debug("mic0_mix_gain:%d\r\n",mic0_mix_gain);
	usb_online_debug("mic1_pga_gain:%d\r\n",mic1_pga_gain);
	usb_online_debug("mic1_mix_gain:%d\r\n",mic1_mix_gain);
	usb_online_debug("dac_vol:%d\r\n",dac_vol);
}


void hid_get_algo_cfg(uint8_t* buff,uint16_t len)
{
	int16_t aec_level;
	int16_t ahs_level;
	int16_t enc_level;
	int16_t freq_level;
	get_algo_process_level((int16_t*)&aec_level,(int16_t*)&ahs_level,(int16_t*)&enc_level,(int16_t*)&freq_level);
	buff[0] = aec_level;
	buff[1] = aec_level>>8;

	buff[2] = ahs_level;
	buff[3] = ahs_level>>8;

	buff[4] = enc_level;
	buff[5] = enc_level>>8;

	buff[6] = freq_level;
	buff[7] = freq_level>>8;

	usb_online_debug("aec_level:%d\r\n",aec_level);
	usb_online_debug("ahs_level:%d\r\n",ahs_level);
	usb_online_debug("enc_level:%d\r\n",enc_level);
	usb_online_debug("freq_level:%d\r\n",freq_level);
}



#define CMD_GET_AUDIO_PARAM        0x00
#define CMD_SET_MIC0_PAG_GAIN      0x01
#define CMD_SET_MIC0_MIX_GAIN      0x02
#define CMD_SET_MIC1_PAG_GAIN      0x03
#define CMD_SET_MIC1_MIX_GAIN      0x04
#define CMD_SET_DAC_VOL            0x05

#define CMD_SET_AEC_PROCESS        0x06
#define CMD_SET_AHS_PROCESS        0x07
#define CMD_SET_ENC_PROCESS        0x08
#define CMD_SET_FREQ_PROCESS       0x09

void hid_adda_handle(struct hid_protocol *hid_pro)
{
	uint8_t adda_cfg_buff[64];
	uint32_t data_rxd;
    if (hid_pro->report_id == 0x4B)
    {
        if (hid_pro->bRequestType == 0x02)//写，pc->spv
        {
            if(hid_pro->bCode == CMD_SET_MIC0_PAG_GAIN)
            {
               usb_online_debug("set mic0 pga gain :%d\r\n",hid_pro->pay_load[0]);
               data_rxd = hid_pro->pay_load[0];
               set_mic_gain(MIC_0,data_rxd);

            }
            else if(hid_pro->bCode == CMD_SET_MIC0_MIX_GAIN)
			{
               usb_online_debug("set mic0 mix gain :%d\r\n",hid_pro->pay_load[0]);
               data_rxd = hid_pro->pay_load[0];
               set_mic_mix_gain(MIC_0,data_rxd);
			}
            else if(hid_pro->bCode == CMD_SET_MIC1_PAG_GAIN)
			{
            	usb_online_debug("set mic1 pga gain :%d\r\n",hid_pro->pay_load[0]);
            	data_rxd = hid_pro->pay_load[0];
            	set_mic_gain(MIC_1,data_rxd);
			}
            else if(hid_pro->bCode == CMD_SET_MIC1_MIX_GAIN)
			{
            	usb_online_debug("set mic1 mix gain :%d\r\n",hid_pro->pay_load[0]);
                data_rxd = hid_pro->pay_load[0];
                set_mic_mix_gain(MIC_0,data_rxd);
			}
            else if(hid_pro->bCode == CMD_SET_DAC_VOL)
            {
            	usb_online_debug("set dac gain :%d\r\n",hid_pro->pay_load[0]);
            	data_rxd = hid_pro->pay_load[0];
            	dac_set_volume(data_rxd);
            }
            else if(hid_pro->bCode == CMD_SET_AEC_PROCESS)
            {
            	int16_t aec_level;
            	if(hid_pro->bLength == 1)
            	{
            		aec_level = hid_pro->pay_load[0];
            	}
            	else
            	{
            		aec_level = hid_pro->pay_load[0] + hid_pro->pay_load[1]<<8;
            	}
            	usb_online_debug("aec_level:%d\r\n",aec_level);

            	set_algo_process_level(aec_level,1024,1024,1024);
            }
            else if(hid_pro->bCode == CMD_SET_AHS_PROCESS)
            {
            	int16_t ahs_level;
            	if(hid_pro->bLength == 1)
				{
            		ahs_level = hid_pro->pay_load[0];
				}
				else
				{
					ahs_level = hid_pro->pay_load[0] + hid_pro->pay_load[1]<<8;
				}
            	usb_online_debug("ahs_level:%d\r\n",ahs_level);
            	set_algo_process_level(1024,ahs_level,1024,1024);
            }
            else if(hid_pro->bCode == CMD_SET_ENC_PROCESS)
            {
            	int16_t enc_level;
            	if(hid_pro->bLength == 1)
				{
            		enc_level = hid_pro->pay_load[0];
				}
				else
				{
					enc_level = hid_pro->pay_load[0] + hid_pro->pay_load[1]<<8;
				}
            	usb_online_debug("enc_level:%d\r\n",enc_level);
            	set_algo_process_level(1024,1024,enc_level,1024);
            }
            else if(hid_pro->bCode == CMD_SET_FREQ_PROCESS)
		    {
				int16_t freq_level;
				if(hid_pro->bLength == 1)
				{
					freq_level = hid_pro->pay_load[0];
				}
				else
				{
					freq_level = hid_pro->pay_load[0] + hid_pro->pay_load[1]<<8;
				}
				usb_online_debug("freq_level:%d\r\n",freq_level);
				set_algo_process_level(1024,1024,1024,freq_level);
		   }
            else if(hid_pro->bCode == CMD_GET_AUDIO_PARAM)
            {

            	adda_cfg_buff[0] = 0x4B;
            	hid_get_adc_dac_cfg((uint8_t*)&adda_cfg_buff[1],63);
            	hid_get_algo_cfg((uint8_t*)&adda_cfg_buff[6],63);
            	hid_send_data(adda_cfg_buff,sizeof(adda_cfg_buff));
            }
        }
    }
}

__WEAK void set_algo_process_level(int16_t aec_level,int16_t ahs_level,int16_t enc_level,int16_t freq_level)
{
	return;
}
__WEAK void get_algo_process_level(int16_t* aec_level,int16_t* ahs_level,int16_t* enc_level,int16_t* freq_level)
{
	return;
}
