#include <adc_tools.h>
#include <edrk_main.h>
#include <message2test.h>
#include <params.h>
#include <sync_tools.h>
#include <tk_Test.h>
#include <vector.h>

#include "CAN_Setup.h"
#include "IQmathLib.h"
#include "DSP281x_Device.h"     // DSP281x Headerfile Include File
#include "RS_Functions.h"
#include "xp_project.h"

#include "x_wdog.h"
#include "params_hwp.h"
#include "detect_errors.h"


//XilinxV2


void func_fill_answer_to_TMS_test(TMS_TO_TERMINAL_TEST_ALL_STRUCT* reply_ans, CMD_TO_TMS_TEST_ALL_STRUCT* pcommand)
{
	// онтрольнаy сумма
	unsigned int crc, DataOut, sinusImpulse, doubleImpulse,adc_plate;
	int Data,Data1,Data2/*,bitt, DataAnalog1, DataAnalog2*/, tk0,tk1,tk2,tk3,period1,period2, period3;
	
	//static int vs11,vs12,vs1;
	static int prev_Go = 0;
	static int prev_Prepare = 0;

	static int flag_prev_turn_on = 0;
	static int flag_prev_turn_off = 0;
	static int flag_prev_lamp_on_off = 0;
	static int soft_off_enable = 0, soft_on_enable = 0;
	static float tk_time_soft_off = 0;
	static unsigned long tk_time_soft_off_d = 0;
	static unsigned int i_af_protect_a = 0, i_af_protect_d = 0;
	int enable_line_err = 0, disable_tk_soft_off, disable_protect_tk_soft_off;


	stop_wdog();

	edrk.test_mode = 1;
	// const указатель на структуру стандартной команды
	// настроили на буфер приема
	
//	TMS_TO_TERMINAL_TEST_ALL_STRUCT* const pcommand = ((TMS_TO_TERMINAL_TEST_ALL_STRUCT*)reply);
	
	// -азбираем полy команды
	// ?се это надо пихнуть себе
	
//	f.RScount = SECOND*3;
	
	f.terminal_prepare	= pcommand->digit_data.byte05.bit_data.bit1;
	soft_off_enable = pcommand->digit_data.byte06.bit_data.bit0;
	soft_on_enable = pcommand->digit_data.byte06.bit_data.bit1;
//	edrk.direct_write_out = pcommand->digit_data.byte06.bit_data.bit2;

	disable_tk_soft_off = pcommand->digit_data.byte06.bit_data.bit3;
    disable_protect_tk_soft_off = pcommand->digit_data.byte06.bit_data.bit4;

    enable_line_err = pcommand->digit_data.byte06.bit_data.bit5;

	// Записали все выходы

#if (CHECK_IN_OUT_TERMINAL==1)

#if(C_cds_out_number>=1)
	project.cds_out[0].write.sbus.data_out.all = ~(pcommand->digit_data.byte07.byte_data | ((pcommand->digit_data.byte08.byte_data) << 8));
#endif
#if(C_cds_out_number>=2)
	project.cds_out[1].write.sbus.data_out.all = ~(pcommand->digit_data.byte09.byte_data | ((pcommand->digit_data.byte10.byte_data) << 8));
#endif
#if(C_cds_out_number>=3)
    project.cds_out[2].write.sbus.data_out.all = ~(pcommand->digit_data.byte11.byte_data | ((pcommand->digit_data.byte12.byte_data) << 8));
#endif

#endif //CHECK_IN_OUT

	if (pcommand->digit_data.byte05.bit_data.bit1 == 1)
	{
		
		//xreset_error_all();
	}


	
//	write_dig_out();

	//calc_norm_ADC(0);
	calc_norm_ADC_0(1);
	calc_norm_ADC_1(1);
	
	// проверка ключей
	tk0 = (pcommand->digit_data.byte01.byte_data);
	tk1 = (pcommand->digit_data.byte02.byte_data);
	tk2 = (pcommand->digit_data.byte03.byte_data);
	tk3 = (pcommand->digit_data.byte04.byte_data);
	
	Data1 = pcommand->analog_data.analog1_hi;
	Data2 = pcommand->analog_data.analog1_lo;
	Data = (Data2 + Data1*256);
	period1 = Data;

	Data1 = pcommand->analog_data.analog2_hi;
	Data2 = pcommand->analog_data.analog2_lo;
	Data = (Data2 + Data1*256);
	period2 = Data;
	
    Data1 = pcommand->analog_data.analog3_hi;
    Data2 = pcommand->analog_data.analog3_lo;
    Data = (Data2 + Data1*256);
    period3 = Data;

    Data1 = pcommand->analog_data.analog4_hi;
    Data2 = pcommand->analog_data.analog4_lo;
    Data = (Data2 + Data1*256);
 //   Data = 200;
    tk_time_soft_off = Data*100.0; // mks*10->ns
    if (tk_time_soft_off>1300000.0)
        tk_time_soft_off = 1300000.0;
    tk_time_soft_off_d = (unsigned long)(tk_time_soft_off / DIV_TIME_TK_SOFT_OFF);

    if (tk_time_soft_off_d>65535)
        tk_time_soft_off_d = 65535;


    Data1 = pcommand->analog_data.analog5_hi;
    Data2 = pcommand->analog_data.analog5_lo;
    Data = (Data2 + Data1*256);
    i_af_protect_a = Data;

    if (i_af_protect_a>LEVEL_HWP_I_AF) i_af_protect_a = LEVEL_HWP_I_AF;
    if (i_af_protect_a<10) i_af_protect_a = 10;
    i_af_protect_d = convert_real_to_mv_hwp(4,i_af_protect_a);
    if (i_af_protect_d>1500) i_af_protect_d = 1500; // max 1500 mV

    update_maz_level_i_af(0, i_af_protect_d);
    project.read_all_hwp();



	if(pcommand->digit_data.byte05.bit_data.bit3 == 1)
		doubleImpulse = 1;
	else
		doubleImpulse = 0;	
		
	if(pcommand->digit_data.byte05.bit_data.bit5 == 1)
		sinusImpulse = 1;
	else
		sinusImpulse = 0;	

	if ((pcommand->digit_data.byte05.bit_data.bit0 == 1) && (prev_Go == 0))
	{
	    if (pcommand->digit_data.byte05.bit_data.bit2 == 1) // при цикл импульсе предварительно даем квитирование
	    {
	        update_maz_level_i_af(0, 1500);
	        project.write_all_hwp();
	        clear_errors();
	        project.clear_errors_all_plates();
	        update_maz_level_i_af(0, i_af_protect_d);
	        project.write_all_hwp();

	    }


//		test_tk_ak_one_impulse( tk0,  tk1,  tk2,  tk3,  period1, period2);
#if (USE_TK_0)
        project.cds_tk[0].write.sbus.protect_error.bit.enable_soft_disconnect = !disable_tk_soft_off;
        project.cds_tk[0].write.sbus.protect_error.bit.detect_soft_disconnect = !disable_protect_tk_soft_off;
        project.cds_tk[0].write.sbus.protect_error.bit.enable_line_err = enable_line_err;
        project.cds_tk[0].write.sbus.time_after_err = tk_time_soft_off_d;//(int)(tk_time_soft_off / DIV_TIME_TK_SOFT_OFF);
#endif
#if (USE_TK_1)
        project.cds_tk[1].write.sbus.protect_error.bit.enable_soft_disconnect = !disable_tk_soft_off;
        project.cds_tk[1].write.sbus.protect_error.bit.detect_soft_disconnect = !disable_protect_tk_soft_off;
        project.cds_tk[1].write.sbus.protect_error.bit.enable_line_err = enable_line_err;
        project.cds_tk[1].write.sbus.time_after_err = tk_time_soft_off_d;//(int)(tk_time_soft_off / DIV_TIME_TK_SOFT_OFF);
#endif
#if (USE_TK_3)
        project.cds_tk[3].write.sbus.protect_error.bit.enable_soft_disconnect = !disable_tk_soft_off;
        project.cds_tk[3].write.sbus.protect_error.bit.detect_soft_disconnect = !disable_protect_tk_soft_off;
        project.cds_tk[3].write.sbus.protect_error.bit.enable_line_err = enable_line_err;
        project.cds_tk[3].write.sbus.time_after_err = tk_time_soft_off_d;//(int)(tk_time_soft_off / DIV_TIME_TK_SOFT_OFF);
#endif


        project.write_all_sbus();
        project.write_all_hwp();

		test_tk_ak_one_impulse( tk0,  tk1,  tk2,  tk3,  period1, period2, period3, doubleImpulse, sinusImpulse, soft_off_enable, soft_on_enable);
	}
	
	if ((pcommand->digit_data.byte05.bit_data.bit0 == 1) &&
		(pcommand->digit_data.byte05.bit_data.bit3 == 1) && (prev_Go == 0))
	{
//		test_tk_ak_sinus_period( tk0,  tk1,  tk2,  tk3,  period1, period2);
	}
	prev_Go = pcommand->digit_data.byte05.bit_data.bit0;
	
	f.Prepare = pcommand->digit_data.byte05.bit_data.bit1;
	if 	(pcommand->digit_data.byte05.bit_data.bit1 != prev_Prepare)
	{
	  if 	(pcommand->digit_data.byte05.bit_data.bit1==1)
	  {  
	    stop_wdog();
	    update_maz_level_i_af(0, 1500);
	    project.write_all_hwp();
	    clear_errors();
		project.clear_errors_all_plates();
		update_maz_level_i_af(0, i_af_protect_d);
		project.write_all_hwp();
	  }
	}
    prev_Prepare = pcommand->digit_data.byte05.bit_data.bit1;

    if (pcommand->digit_data.byte05.bit_data.bit2 == 1)
	{

		prev_Go = 0; // зациклили Go
	}

//    break_all_on_off(pcommand->digit_data.byte05.bit_data.bit5);

/*
	if (pcommand->digit_data.byte05.bit_data.bit5 != flag_prev_turn_off)  //turn off
	{
		if(pcommand->digit_data.byte05.bit_data.bit5 == 1)
		{
			project.cds_out[0].fpga.Write.Dout.bit.dout2 = 1;
			project.cds_out[0].fpga.Write.Dout.bit.dout12 = 1;
			cds_out_all(cds_out_WriteAll);
			pause_1000(100000);
			pause_1000(100000);
			pause_1000(100000);
			pause_1000(100000);
			project.cds_out[0].fpga.Write.Dout.bit.dout2 = 0;
			project.cds_out[0].fpga.Write.Dout.bit.dout12 = 0;
			cds_out_all(cds_out_WriteAll);
				//f.Ready2 = 0;
			f.On_Power_QTV = 0;
			edrk.Go = 0;
			
		}
		
		flag_prev_turn_off = pcommand->digit_data.byte05.bit_data.bit5;
		cds_out_all(cds_out_WriteAll);

	}
	if ((pcommand->digit_data.byte05.bit_data.bit6 != flag_prev_turn_on) && !f.Stop &&
//		((filter.iqU_1_long > 11184810) || (filter.iqU_3_long > 11184810)))  //turn_on
		((filter.iqU_1_long > 5590240) || (filter.iqU_3_long > 5590240)))
	{
		if(pcommand->digit_data.byte05.bit_data.bit6 == 1)
		{
			project.cds_out[0].fpga.Write.Dout.bit.dout7 = 0;
			cds_out_all(cds_out_WriteAll);
			pause_1000(100000);
			pause_1000(100000);
			pause_1000(100000);
			pause_1000(100000);
			pause_1000(100000);
			project.cds_out[0].fpga.Write.Dout.bit.dout7 = 1;
			cds_out_all(cds_out_WriteAll);
			//f.Ready2 = 1;
			f.On_Power_QTV = 1;
		}
		
		flag_prev_turn_on = pcommand->digit_data.byte05.bit_data.bit6;
		cds_out_all(cds_out_WriteAll);

	}
	if(project.cds_in[1].fpga.input_new.ChanalsPtr.ChanalPtr[7].rd_status != flag_prev_lamp_on_off) //turnig on lamp when power is on
	{
		if(project.cds_in[1].fpga.input_new.ChanalsPtr.ChanalPtr[7].rd_status == 1)
		{
			project.cds_out[1].fpga.Write.Dout.bit.dout1 = 0;
			cds_out_all(cds_out_WriteAll);
		}
		else
		{
			project.cds_out[1].fpga.Write.Dout.bit.dout1 = 1;
			cds_out_all(cds_out_WriteAll);
		}
		flag_prev_lamp_on_off = project.cds_in[1].fpga.input_new.ChanalsPtr.ChanalPtr[7].rd_status;
	}
	

*/

//	run_break = pcommand->digit_data.byte05.bit_data.bit4;


	if (pcommand->digit_data.byte05.bit_data.bit4 == 1)
	{
		adc_plate = 1;
			
	}
	else
	   adc_plate = 0;

    if (pcommand->digit_data.byte05.bit_data.bit6 == 1)
        i_sync_pin_on();
    else
        i_sync_pin_off();



	Data = project.adc[adc_plate].read.pbus.adc_value[0];//InternalADC[0];//0;//ADC_sf[0];//_IQtoF(analog.iqIa1_1_fir_n)*2000.0;//
	reply_test_all.analog_data.analog1_lo=LOBYTE(Data);
	reply_test_all.analog_data.analog1_hi=HIBYTE(Data);
	
	Data = project.adc[adc_plate].read.pbus.adc_value[1];// InternalADC[1];//0;//ADC_sf[1];//_IQtoF(analog.iqIa1_1_fir_n)*2000.0;//
	reply_test_all.analog_data.analog2_lo=LOBYTE(Data);
	reply_test_all.analog_data.analog2_hi=HIBYTE(Data);
	
	Data = project.adc[adc_plate].read.pbus.adc_value[2];// InternalADC[2];//0;//ADC_sf[2];//_IQtoF(analog.iqIa1_1_fir_n)*2000.0;//
	reply_test_all.analog_data.analog3_lo=LOBYTE(Data);
	reply_test_all.analog_data.analog3_hi=HIBYTE(Data);
	
	Data = project.adc[adc_plate].read.pbus.adc_value[3];// InternalADC[3];//0;//ADC_sf[3];//_IQtoF(analog.iqIa1_1_fir_n)*2000.0;//
	reply_test_all.analog_data.analog4_lo=LOBYTE(Data);
	reply_test_all.analog_data.analog4_hi=HIBYTE(Data);
	
	Data = project.adc[adc_plate].read.pbus.adc_value[4];// InternalADC[4];//0;//ADC_sf[4];//_IQtoF(analog.iqIa1_1_fir_n)*2000.0;//
	reply_test_all.analog_data.analog5_lo=LOBYTE(Data);
	reply_test_all.analog_data.analog5_hi=HIBYTE(Data);
	
	Data = project.adc[adc_plate].read.pbus.adc_value[5];// InternalADC[5];//0;//ADC_sf[5];//_IQtoF(analog.iqIa1_1_fir_n)*2000.0;//
	reply_test_all.analog_data.analog6_lo=LOBYTE(Data);
	reply_test_all.analog_data.analog6_hi=HIBYTE(Data);
	
	Data = project.adc[adc_plate].read.pbus.adc_value[6];// InternalADC[6];//0;//ADC_sf[6];//_IQtoF(analog.iqIa1_1_fir_n)*2000.0;//
	reply_test_all.analog_data.analog7_lo=LOBYTE(Data);
	reply_test_all.analog_data.analog7_hi=HIBYTE(Data);
	
	Data =  project.adc[adc_plate].read.pbus.adc_value[7];//InternalADC[7];//0;//ADC_sf[7];//_IQtoF(analog.iqIa1_1_fir_n)*2000.0;//
	reply_test_all.analog_data.analog8_lo=LOBYTE(Data);
	reply_test_all.analog_data.analog8_hi=HIBYTE(Data);
	
	Data =  project.adc[adc_plate].read.pbus.adc_value[8];//InternalADC[8];//0;//ADC_sf[8];//_IQtoF(analog.iqIa1_1_fir_n)*2000.0;//
	reply_test_all.analog_data.analog9_lo=LOBYTE(Data);
	reply_test_all.analog_data.analog9_hi=HIBYTE(Data);
	
	Data =  project.adc[adc_plate].read.pbus.adc_value[9];//InternalADC[9];//0;//ADC_sf[9];//_IQtoF(analog.iqIa1_1_fir_n)*2000.0;//
	reply_test_all.analog_data.analog10_lo=LOBYTE(Data);
	reply_test_all.analog_data.analog10_hi=HIBYTE(Data);
	
	Data =  project.adc[adc_plate].read.pbus.adc_value[10];//InternalADC[10];//0;//ADC_sf[10];//_IQtoF(analog.iqIa1_1_fir_n)*2000.0;//
	reply_test_all.analog_data.analog11_lo=LOBYTE(Data);
	reply_test_all.analog_data.analog11_hi=HIBYTE(Data);
	
	Data =  project.adc[adc_plate].read.pbus.adc_value[11];//InternalADC[11];//0;//ADC_sf[11];//_IQtoF(analog.iqIa1_1_fir_n)*2000.0;//
	reply_test_all.analog_data.analog12_lo=LOBYTE(Data);
	reply_test_all.analog_data.analog12_hi=HIBYTE(Data);
	
	Data =  project.adc[adc_plate].read.pbus.adc_value[12];//InternalADC[12];//0;//ADC_sf[12];//_IQtoF(analog.iqIa1_1_fir_n)*2000.0;//
	reply_test_all.analog_data.analog13_lo=LOBYTE(Data);
	reply_test_all.analog_data.analog13_hi=HIBYTE(Data);
	
	Data =  project.adc[adc_plate].read.pbus.adc_value[13];//InternalADC[13];//0;//ADC_sf[13];//_IQtoF(analog.iqIa1_1_fir_n)*2000.0;//
	reply_test_all.analog_data.analog14_lo=LOBYTE(Data);
	reply_test_all.analog_data.analog14_hi=HIBYTE(Data);
	
	Data =  project.adc[adc_plate].read.pbus.adc_value[14];//InternalADC[14];//0;//ADC_sf[14];//_IQtoF(analog.iqIa1_1_fir_n)*2000.0;//
	reply_test_all.analog_data.analog15_lo=LOBYTE(Data);
	reply_test_all.analog_data.analog15_hi=HIBYTE(Data);
	
	Data =  project.adc[adc_plate].read.pbus.adc_value[15];//InternalADC[15];//0;//ADC_sf[15];//_IQtoF(analog.iqIa1_1_fir_n)*2000.0;//
	reply_test_all.analog_data.analog16_lo=LOBYTE(Data);
	reply_test_all.analog_data.analog16_hi=HIBYTE(Data);


	Data = _IQtoF(analog.iqU_1) * NORMA_ACP;
	reply_ans->analog_data.analog17_lo=LOBYTE(Data);
	reply_ans->analog_data.analog17_hi=HIBYTE(Data);

	Data = _IQtoF(analog.iqU_2) * NORMA_ACP;
	reply_ans->analog_data.analog18_lo=LOBYTE(Data);
	reply_ans->analog_data.analog18_hi=HIBYTE(Data);

    Data = project.cds_tk[0].read.sbus.time_err_tk_all.bit.tk_3210;
	reply_ans->analog_data.analog19_lo=LOBYTE(Data);
	reply_ans->analog_data.analog19_hi=HIBYTE(Data);

    Data = project.cds_tk[0].read.sbus.time_err_tk_all.bit.tk_7654;
	reply_ans->analog_data.analog20_lo=LOBYTE(Data);
	reply_ans->analog_data.analog20_hi=HIBYTE(Data);

    Data = project.cds_tk[1].read.sbus.time_err_tk_all.bit.tk_3210;
    reply_ans->analog_data.analog21_lo=LOBYTE(Data);
    reply_ans->analog_data.analog21_hi=HIBYTE(Data);

    Data = project.cds_tk[1].read.sbus.time_err_tk_all.bit.tk_7654;
    reply_ans->analog_data.analog22_lo=LOBYTE(Data);
    reply_ans->analog_data.analog22_hi=HIBYTE(Data);

    Data = project.cds_tk[2].read.sbus.time_err_tk_all.bit.tk_3210;
    reply_ans->analog_data.analog23_lo=LOBYTE(Data);
    reply_ans->analog_data.analog23_hi=HIBYTE(Data);

    Data = project.cds_tk[2].read.sbus.time_err_tk_all.bit.tk_7654;
    reply_ans->analog_data.analog24_lo=LOBYTE(Data);
    reply_ans->analog_data.analog24_hi=HIBYTE(Data);

//    Data = project.cds_tk[3].read.sbus.time_err_tk_all.bit.tk_3210;
//    reply_ans->analog_data.analog25_lo=LOBYTE(Data);
//    reply_ans->analog_data.analog25_hi=HIBYTE(Data);
//
//    Data = project.cds_tk[3].read.sbus.time_err_tk_all.bit.tk_7654;
//    reply_ans->analog_data.analog26_lo=LOBYTE(Data);
//    reply_ans->analog_data.analog26_hi=HIBYTE(Data);

reply_ans->digit_data.byte01.byte_data = 0;
reply_ans->digit_data.byte02.byte_data = 0;
reply_ans->digit_data.byte03.byte_data = 0;
reply_ans->digit_data.byte04.byte_data = 0;
reply_ans->digit_data.byte05.byte_data = 0;
reply_ans->digit_data.byte06.byte_data = 0;
reply_ans->digit_data.byte07.byte_data = 0;
reply_ans->digit_data.byte08.byte_data = 0;
reply_ans->digit_data.byte09.byte_data = 0;
reply_ans->digit_data.byte10.byte_data = 0;
reply_ans->digit_data.byte11.byte_data = 0;
reply_ans->digit_data.byte12.byte_data = 0;
reply_ans->digit_data.byte13.byte_data = 0;
reply_ans->digit_data.byte14.byte_data = 0;
reply_ans->digit_data.byte15.byte_data = 0;
reply_ans->digit_data.byte16.byte_data = 0;
reply_ans->digit_data.byte17.byte_data = 0;
reply_ans->digit_data.byte18.byte_data = 0;
reply_ans->digit_data.byte19.byte_data = 0;
reply_ans->digit_data.byte20.byte_data = 0;
reply_ans->digit_data.byte21.byte_data = 0;
reply_ans->digit_data.byte22.byte_data = 0;
reply_ans->digit_data.byte23.byte_data = 0;
reply_ans->digit_data.byte24.byte_data = 0;


	reply_ans->digit_data.byte01.bit_data.bit0 = project.cds_tk[0].read.sbus.current_status_error.bit.err0_local;
	reply_ans->digit_data.byte01.bit_data.bit1 = project.cds_tk[1].read.sbus.current_status_error.bit.err0_local;
	reply_ans->digit_data.byte01.bit_data.bit2 = project.cds_tk[2].read.sbus.current_status_error.bit.err0_local;
	reply_ans->digit_data.byte01.bit_data.bit3 = project.cds_tk[3].read.sbus.current_status_error.bit.err0_local;

	reply_ans->digit_data.byte01.bit_data.bit4 = project.cds_tk[0].read.sbus.current_status_error.bit.err_power;
	reply_ans->digit_data.byte01.bit_data.bit5 = project.cds_tk[1].read.sbus.current_status_error.bit.err_power;
	reply_ans->digit_data.byte01.bit_data.bit6 = project.cds_tk[2].read.sbus.current_status_error.bit.err_power;
	reply_ans->digit_data.byte01.bit_data.bit7 = project.cds_tk[3].read.sbus.current_status_error.bit.err_power;

	reply_ans->digit_data.byte02.bit_data.bit0 = project.cds_in[0].read.sbus.current_status_error.bit.err_power;
	reply_ans->digit_data.byte02.bit_data.bit1 = project.cds_in[1].read.sbus.current_status_error.bit.err_power;
#if(C_cds_in_number>=3)
	reply_ans->digit_data.byte02.bit_data.bit2 = project.cds_in[2].read.sbus.current_status_error.bit.err_power;
#endif
	reply_ans->digit_data.byte02.bit_data.bit3 = project.cds_out[0].read.sbus.current_status_error.bit.err_power;

	reply_ans->digit_data.byte02.bit_data.bit4 = project.cds_out[1].read.sbus.current_status_error.bit.err_power;
	reply_ans->digit_data.byte02.bit_data.bit5 = 0;
	reply_ans->digit_data.byte02.bit_data.bit6 = project.cds_tk[0].read.sbus.current_status_error.bit.err_switch;
	reply_ans->digit_data.byte02.bit_data.bit7 = project.cds_tk[1].read.sbus.current_status_error.bit.err_switch;

	reply_ans->digit_data.byte03.bit_data.bit0 = project.cds_tk[2].read.sbus.current_status_error.bit.err_switch;
	reply_ans->digit_data.byte03.bit_data.bit1 = project.cds_tk[3].read.sbus.current_status_error.bit.err_switch;
	reply_ans->digit_data.byte03.bit_data.bit2 = project.cds_in[0].read.sbus.current_status_error.bit.err_switch;
	reply_ans->digit_data.byte03.bit_data.bit3 = project.cds_in[1].read.sbus.current_status_error.bit.err_switch;


#if(C_cds_in_number>=3)
	reply_ans->digit_data.byte03.bit_data.bit4 = project.cds_in[2].read.sbus.current_status_error.bit.err_switch;
#endif
	reply_ans->digit_data.byte03.bit_data.bit5 = project.cds_out[0].read.sbus.current_status_error.bit.err_switch;
	reply_ans->digit_data.byte03.bit_data.bit6 = project.cds_out[1].read.sbus.current_status_error.bit.err_switch;
	reply_ans->digit_data.byte03.bit_data.bit7 = 0;

	//TK0 acknolege-current
	reply_ans->digit_data.byte04.byte_data = project.cds_tk[0].read.sbus.status_protect_current_ack.all & 0xFF;
	reply_ans->digit_data.byte05.byte_data = (project.cds_tk[0].read.sbus.status_protect_current_ack.all >> 8) & 0xFF;

	//TK1 acknolege-current
	reply_ans->digit_data.byte06.byte_data = project.cds_tk[1].read.sbus.status_protect_current_ack.all & 0xFF;
	reply_ans->digit_data.byte07.byte_data = (project.cds_tk[1].read.sbus.status_protect_current_ack.all >> 8) & 0xFF;

	//TK2 acknolege-current
	reply_ans->digit_data.byte08.byte_data = project.cds_tk[2].read.sbus.status_protect_current_ack.all & 0xFF;
	reply_ans->digit_data.byte09.byte_data = (project.cds_tk[2].read.sbus.status_protect_current_ack.all >> 8) & 0xFF;

	//TK3 acknolege-current
	reply_ans->digit_data.byte10.byte_data = project.cds_tk[3].read.sbus.status_protect_current_ack.all & 0xFF;
	reply_ans->digit_data.byte11.byte_data = (project.cds_tk[3].read.sbus.status_protect_current_ack.all >> 8) & 0xFF;


//IN1
	reply_ans->digit_data.byte13.byte_data = project.cds_in[0].read.pbus.data_in.all & 0xFF;
	reply_ans->digit_data.byte14.byte_data = (project.cds_in[0].read.pbus.data_in.all >> 8) & 0xFF;
	
//IN2
	reply_ans->digit_data.byte15.byte_data = project.cds_in[1].read.pbus.data_in.all & 0xFF;
	reply_ans->digit_data.byte16.byte_data = (project.cds_in[1].read.pbus.data_in.all >> 8) & 0xFF;

//IN3
#if(C_cds_in_number>=3)
	reply_ans->digit_data.byte17.byte_data = project.cds_in[2].read.pbus.data_in.all & 0xFF;
	reply_ans->digit_data.byte18.byte_data = (project.cds_in[2].read.pbus.data_in.all >> 8) & 0xFF;
#endif

	reply_ans->digit_data.byte19.bit_data.bit0 = get_status_sync_line();//CAN_timeout[UKSS1_CAN_DEVICE];
	//reply.digit_data.byte21.bit_data.bit1 = CAN_timeout[UKSS4_CAN_DEVICE];
	reply_ans->digit_data.byte19.bit_data.bit2 = 0;// CAN_timeout[UKSS2_CAN_DEVICE];
	reply_ans->digit_data.byte19.bit_data.bit3 = 0;// CAN_timeout[UKSS3_CAN_DEVICE];
	reply_ans->digit_data.byte19.bit_data.bit4 = CAN_timeout[get_real_in_mbox(UNITS_TYPE_BOX,ZADATCHIK_CAN)];
	//reply_test_all.digit_data.byte19.bit_data.bit5 = CAN_timeout[VPU2_CAN_DEVICE];
	reply_ans->digit_data.byte19.bit_data.bit6 = CAN_timeout[get_real_in_mbox(UNITS_TYPE_BOX,UMU_CAN_DEVICE)];
	//reply.digit_data.byte21.bit_data.bit7 = CAN_timeout[VPU1_CAN_DEVICE];

	reply_ans->digit_data.byte20.bit_data.bit0 = project.controller.read.errors.bit.er0_out;
	reply_ans->digit_data.byte20.bit_data.bit1 = project.controller.read.errors.bit.er0_trig;
	reply_ans->digit_data.byte20.bit_data.bit2 = project.controller.read.errors.bit.errHWP;
	reply_ans->digit_data.byte20.bit_data.bit3 = project.controller.read.errors.bit.errHWP_trig;
	reply_ans->digit_data.byte20.bit_data.bit4 = project.controller.read.errors.bit.error_pbus;
	reply_ans->digit_data.byte20.bit_data.bit5 = project.controller.read.errors.bit.pwm_wdog;
	reply_ans->digit_data.byte20.bit_data.bit6 = project.controller.read.errors.bit.status_er0;
//	reply_ans->digit_data.byte20.bit_data.bit0 = project.controller.read.errors.bit.er0_out;
//	reply_ans->digit_data.byte20.bit_data.bit0 = CAN_timeout[get_real_in_mbox(UNITS_TYPE_BOX,VPU_CAN)];
//	reply_ans->digit_data.byte20.bit_data.bit1 = CAN_timeout[get_real_in_mbox(MPU_TYPE_BOX,0)];
	reply_ans->digit_data.byte20.bit_data.bit2 = 0;//READY_UKSS_6;
	reply_ans->digit_data.byte20.bit_data.bit3 = 0;//READY_UKSS_7;
	reply_ans->digit_data.byte20.bit_data.bit4 = 0;//READY_UKSS_8;
	//reply_test_all.digit_data.byte20.bit_data.bit5 = READY_UKSS_1;
	//reply_test_all.digit_data.byte20.bit_data.bit6 = READY_UKSS_2;
	//reply_test_all.digit_data.byte20.bit_data.bit7 = READY_UKSS_3;



	reply_ans->digit_data.byte21.bit_data.bit0 = !project.hwp[0].status;//XProject_balzam.IsReady_reg.bit.XPlaneHWP_Chanals_IsReady;
	reply_ans->digit_data.byte21.bit_data.bit2 = !project.adc[0].status;//XProject_balzam.IsReady_reg.bit.XPlaneHWP_Chanals_IsReady;
#if(C_cds_in_number>=1)
	reply_ans->digit_data.byte21.bit_data.bit3 = !project.cds_in[0].status;//XProject_balzam.IsReady_reg.bit.XPlaneIN0_IsReady;
#endif
#if(C_cds_in_number>=2)
	reply_ans->digit_data.byte21.bit_data.bit4 = !project.cds_in[1].status;//XProject_balzam.IsReady_reg.bit.XPlaneIN1_IsReady;
#endif
#if(C_cds_in_number>=3)
	reply_ans->digit_data.byte21.bit_data.bit5 = !project.cds_in[2].status;//XProject_balzam.IsReady_reg.bit.XPlaneIN2_IsReady;
#endif

#if(C_cds_out_number>=1)
	reply_ans->digit_data.byte21.bit_data.bit6 = !project.cds_out[0].status;//XProject_balzam.IsReady_reg.bit.XPlaneOUT0_IsReady;
#endif
#if(C_cds_out_number>=2)
	reply_ans->digit_data.byte21.bit_data.bit7 = !project.cds_out[1].status;//XProject_balzam.IsReady_reg.bit.XPlaneOUT1_IsReady;
#endif
#if(C_cds_out_number>=3)
	reply_ans->digit_data.byte22.bit_data.bit0 = !project.cds_out[2].status;//XProject_balzam.IsReady_reg.bit.XPlaneOUT2_IsReady;
#endif


	reply_ans->digit_data.byte22.bit_data.bit1 = !project.cds_tk[0].status;//XProject_balzam.IsReady_reg.bit.XPlaneTK0_IsReady;
	reply_ans->digit_data.byte22.bit_data.bit2 = !project.cds_tk[1].status;//XProject_balzam.IsReady_reg.bit.XPlaneTK1_IsReady;
	reply_ans->digit_data.byte22.bit_data.bit3 = !project.cds_tk[2].status;//XProject_balzam.IsReady_reg.bit.XPlaneTK2_IsReady;
	reply_ans->digit_data.byte22.bit_data.bit4 = !project.cds_tk[3].status;//!XProject_balzam.IsReady_reg.bit.XPlaneTK3_IsReady;
	reply_ans->digit_data.byte22.bit_data.bit5 = !project.adc[1].status;//XProject_balzam.IsReady_reg.bit.XPlaneHWP_Chanals_IsReady;
	
	reply_ans->digit_data.byte23.byte_data=project.hwp[0].read.comp_s.minus.all  & 0x00ff;
	reply_ans->digit_data.byte24.byte_data=((project.hwp[0].read.comp_s.minus.all >> 8) & 0x00ff);
	
	reply_ans->digit_data.byte23.byte_data|=project.hwp[0].read.comp_s.plus.all  & 0x00ff;
	reply_ans->digit_data.byte24.byte_data|=((project.hwp[0].read.comp_s.plus.all >> 8) & 0x00ff);

#if (USE_TK_0)
	if (project.cds_tk[0].useit)
	{
//	reply_ans->digit_data.byte22.bit_data.bit0 = project.cds_tk[0].read.sbus.lock_status_error.bit.err0_in;
        reply_ans->digit_data.byte25.bit_data.bit0 = project.cds_tk[0].read.sbus.lock_status_error.bit.err_hwp;
        reply_ans->digit_data.byte25.bit_data.bit1 = project.cds_tk[0].read.sbus.lock_status_error.bit.err0_local;
        reply_ans->digit_data.byte25.bit_data.bit2 = project.cds_tk[0].read.sbus.lock_status_error.bit.mintime_err_keys_7654;
        reply_ans->digit_data.byte25.bit_data.bit3 = project.cds_tk[0].read.sbus.lock_status_error.bit.mintime_err_keys_3210;
        reply_ans->digit_data.byte25.bit_data.bit4 = project.cds_tk[0].read.sbus.lock_status_error.bit.line_err_keys_7654;
        reply_ans->digit_data.byte25.bit_data.bit5 = project.cds_tk[0].read.sbus.lock_status_error.bit.line_err_keys_3210;
        reply_ans->digit_data.byte25.bit_data.bit6 = project.cds_tk[0].read.sbus.lock_status_error.bit.ErrorSoftShutdownFromErr0;
        reply_ans->digit_data.byte25.bit_data.bit7 = project.cds_tk[0].read.sbus.lock_status_error.bit.ErrorSoftShutdownForbidComb;
	}
    else
        reply_ans->digit_data.byte25.byte_data = 0;

#else
	    reply_ans->digit_data.byte25.byte_data = 0;
#endif

#if (USE_TK_1)
	if (project.cds_tk[1].useit)
	{
        reply_ans->digit_data.byte26.bit_data.bit0 = project.cds_tk[1].read.sbus.lock_status_error.bit.err_hwp;
        reply_ans->digit_data.byte26.bit_data.bit1 = project.cds_tk[1].read.sbus.lock_status_error.bit.err0_local;
        reply_ans->digit_data.byte26.bit_data.bit2 = project.cds_tk[1].read.sbus.lock_status_error.bit.mintime_err_keys_7654;
        reply_ans->digit_data.byte26.bit_data.bit3 = project.cds_tk[1].read.sbus.lock_status_error.bit.mintime_err_keys_3210;
        reply_ans->digit_data.byte26.bit_data.bit4 = project.cds_tk[1].read.sbus.lock_status_error.bit.line_err_keys_7654;
        reply_ans->digit_data.byte26.bit_data.bit5 = project.cds_tk[1].read.sbus.lock_status_error.bit.line_err_keys_3210;
        reply_ans->digit_data.byte26.bit_data.bit6 = project.cds_tk[1].read.sbus.lock_status_error.bit.ErrorSoftShutdownFromErr0;
        reply_ans->digit_data.byte26.bit_data.bit7 = project.cds_tk[1].read.sbus.lock_status_error.bit.ErrorSoftShutdownForbidComb;
	}
    else
        reply_ans->digit_data.byte26.byte_data = 0;
#else
        reply_ans->digit_data.byte26.byte_data = 0;
#endif

#if (USE_TK_2)
    if (project.cds_tk[2].useit)
    {
        reply_ans->digit_data.byte27.bit_data.bit0 = project.cds_tk[2].read.sbus.lock_status_error.bit.err_hwp;
        reply_ans->digit_data.byte27.bit_data.bit1 = project.cds_tk[2].read.sbus.lock_status_error.bit.err0_local;
        reply_ans->digit_data.byte27.bit_data.bit2 = project.cds_tk[2].read.sbus.lock_status_error.bit.mintime_err_keys_7654;
        reply_ans->digit_data.byte27.bit_data.bit3 = project.cds_tk[2].read.sbus.lock_status_error.bit.mintime_err_keys_3210;
        reply_ans->digit_data.byte27.bit_data.bit4 = project.cds_tk[2].read.sbus.lock_status_error.bit.line_err_keys_7654;
        reply_ans->digit_data.byte27.bit_data.bit5 = project.cds_tk[2].read.sbus.lock_status_error.bit.line_err_keys_3210;
        reply_ans->digit_data.byte27.bit_data.bit6 = project.cds_tk[2].read.sbus.lock_status_error.bit.ErrorSoftShutdownFromErr0;
        reply_ans->digit_data.byte27.bit_data.bit7 = project.cds_tk[2].read.sbus.lock_status_error.bit.ErrorSoftShutdownForbidComb;
    }
    else
        reply_ans->digit_data.byte27.byte_data = 0;
#else
        reply_ans->digit_data.byte27.byte_data = 0;
#endif

#if (USE_TK_3)
    if (project.cds_tk[3].useit)
    {
        reply_ans->digit_data.byte28.bit_data.bit0 = project.cds_tk[3].read.sbus.lock_status_error.bit.err_hwp;
        reply_ans->digit_data.byte28.bit_data.bit1 = project.cds_tk[3].read.sbus.lock_status_error.bit.err0_local;
        reply_ans->digit_data.byte28.bit_data.bit2 = project.cds_tk[3].read.sbus.lock_status_error.bit.mintime_err_keys_7654;
        reply_ans->digit_data.byte28.bit_data.bit3 = project.cds_tk[3].read.sbus.lock_status_error.bit.mintime_err_keys_3210;
        reply_ans->digit_data.byte28.bit_data.bit4 = project.cds_tk[3].read.sbus.lock_status_error.bit.line_err_keys_7654;
        reply_ans->digit_data.byte28.bit_data.bit5 = project.cds_tk[3].read.sbus.lock_status_error.bit.line_err_keys_3210;
        reply_ans->digit_data.byte28.bit_data.bit6 = project.cds_tk[3].read.sbus.lock_status_error.bit.ErrorSoftShutdownFromErr0;
        reply_ans->digit_data.byte28.bit_data.bit7 = project.cds_tk[3].read.sbus.lock_status_error.bit.ErrorSoftShutdownForbidComb;
    }
    else
        reply_ans->digit_data.byte28.byte_data = 0;
#else
        reply_ans->digit_data.byte28.byte_data = 0;
#endif


/*

//IN1 ready
	reply_ans->digit_data.byte21.byte_data = project.cds_in[0].read.pbus.ready_in.all & 0xFF;
	reply_ans->digit_data.byte22.byte_data = (project.cds_in[0].read.pbus.ready_in.all >> 8) & 0xFF;

//TK0 acknolege-current
	reply_ans->digit_data.byte23.byte_data = project.cds_tk[0].read.sbus.status_protect_current_ack.all & 0xFF;
	reply_ans->digit_data.byte24.byte_data = (project.cds_tk[0].read.sbus.status_protect_current_ack.all >> 8) & 0xFF;
*/
	return;
}





















