/*
 * message2can.c
 *
 *  Created on: 3 июн. 2020 г.
 *      Author: Yura
 */
#include <adc_tools.h>
#include <edrk_main.h>
#include <message2can.h>
#include <params.h>
#include <params_norma.h>
#include <v_rotor.h>
#include <vector.h>

#include "control_station.h"
#include "CAN_Setup.h"
#include "global_time.h"
#include "IQmathLib.h"
#include "DSP281x_Device.h"
#include "x_basic_types.h"
#include "xp_cds_in.h"
#include "xp_hwp.h"
#include "xp_project.h"


void detecting_cmd_from_can(void)
{

    if (control_station.alive_control_station[CONTROL_STATION_TERMINAL_CAN])
    {
        func_unpack_answer_from_TMS_CAN((TERMINAL_UNITES_STRUCT_handle)&TerminalUnites[edrk.number_can_box_terminal_cmd][0]);
    }

}


void func_unpack_answer_from_TMS_CAN(TERMINAL_UNITES_STRUCT_handle unites_t)
{
    // Контрольнаy сумма
    unsigned int DataOut,h;
    int Data, Data1, Data2, DataAnalog1, DataAnalog2, DataAnalog3, DataAnalog4,DataAnalog5,DataAnalog6, i;
    unsigned char *pByte;
    //  static int vs11,vs12,vs1;
    //  static int DataCnt=0;
    //  int GoT,Assemble_scheme;
    //  static int prev_temp_Rele1=0, temp_Rele1=0, prev_temp_Rele2=0, temp_Rele2=0;

    static int flag_prev_turn_on = 0;
    static int flag_prev_turn_off = 0;
    static int prev_byte01_bit4 = 0;
    static int prev_byte01_bit1 = 0;
    static int flag_wait_revers_sbor = 1;
    static int flag_wait_revers_go = 1;

    static unsigned int count_transmited = 0;


 //

    if (TERMINAL_UNIT_LEN>CONTROL_STATION_MAX_RAW_DATA)
        xerror(main_er_ID(2),(void *)0);



    // перепакуем из байтов в слова, т.к. в RS232 данные идут байтами
 //   pByte = (unsigned char *)(pcommand);//->analog_data.analog1_lo;

    for (h=0;h<TERMINAL_UNIT_LEN;h++)
    {
           control_station.raw_array_data[CONTROL_STATION_TERMINAL_CAN][h].all = unites_t->buf[h].all;
    }

 /*

    if (control_station.alive_control_station[CONTROL_STATION_TERMINAL_CAN]==0)
    {
        edrk.from_can.bits.ACTIVE = 0;
        return;
    }

    if ((control_station.active_control_station[CONTROL_STATION_TERMINAL_RS232]==1))
    {
        edrk.from_can.bits.ACTIVE = 0;
        return;
    }

   // unites_t->buf[0].bits.bit0

    edrk.test_mode = 0;

    edrk.from_can.bits.ACTIVE = unites_t->buf[6].bits.bit3;

    if (edrk.from_can.bits.ACTIVE==0)
        return;

//  f.RScount = SECOND * 3;

  //  unites_t->
// StartGED

    if (edrk.summ_errors)
    {
        flag_wait_revers_go = 1;
    }

    if (flag_wait_revers_go==1)
      edrk.StartGEDRS = 0;
    if (unites_t->buf[6].bits.bit0 && flag_wait_revers_go)
      edrk.StartGEDRS = 0;
    if (unites_t->buf[6].bits.bit0==0)
      edrk.StartGEDRS = 0;
    if (unites_t->buf[6].bits.bit0==0 && flag_wait_revers_go)
      flag_wait_revers_go = 0;
    if (unites_t->buf[6].bits.bit0==1 && flag_wait_revers_go==0)
       edrk.StartGEDRS    = 1;

//  edrk.StartGEDRS = pcommand->digit_data.Byte01.bit_data.bit0;

// end StartGED


    edrk.Mode_UFConst = unites_t->buf[6].bits.bit2;


////////////////

    if (unites_t->buf[6].bits.bit1 && prev_byte01_bit1==0)
      edrk.KvitirRS = 1;

    prev_byte01_bit1 = unites_t->buf[6].bits.bit1;


//  edrk.from_rs.bits.RAZBOR_SHEMA  = pcommand->digit_data.Byte01.bit_data.bit5;



// SBOR SHEMA
    if (edrk.summ_errors)
    {
        flag_wait_revers_sbor = 1;
    }

    if (flag_wait_revers_sbor==1)
      edrk.from_can.bits.SBOR_SHEMA = 0;

    if (unites_t->buf[6].bits.bit4 && flag_wait_revers_sbor)
      edrk.from_can.bits.SBOR_SHEMA = 0;

    if (unites_t->buf[6].bits.bit4==0)
      edrk.from_can.bits.SBOR_SHEMA = 0;

    if (unites_t->buf[6].bits.bit4==0 && flag_wait_revers_sbor)
      flag_wait_revers_sbor = 0;

    if (unites_t->buf[6].bits.bit4==1 && flag_wait_revers_sbor==0)
       edrk.from_can.bits.SBOR_SHEMA    = unites_t->buf[6].bits.bit4;

    prev_byte01_bit4 = unites_t->buf[6].bits.bit4;

// end SBOR SHEMA



//    if (edrk.from_rs.bits.RAZBOR_SHEMA)
 //     edrk.from_rs.bits.SBOR_SHEMA = 0;

    //edrk.SborRS   = pcommand->digit_data.Byte01.bit_data.bit4;


    edrk.SelectPump0_1   = unites_t->buf[6].bits.bit6;
    edrk.DirectOUT       = unites_t->buf[6].bits.bit7;

    edrk.DirectNagrevOff = unites_t->buf[6].bits.bit8;
    edrk.DirectBlockKeyOff = unites_t->buf[6].bits.bit9;
    edrk.DirectPumpON = unites_t->buf[6].bits.bit10;
    edrk.DirectZaryadOn = unites_t->buf[6].bits.bit11;

#ifdef STENDD

  //  edrk.to_shema.bits.QTV_ON = pcommand->digit_data.Byte02.bit_data.bit3;

#endif

    edrk.Status_Ready.bits.ImitationReady2 = unites_t->buf[6].bits.bit12;

    edrk.SetSpeed = unites_t->buf[6].bits.bit13;



//  edrk.RemouteFromRS = pcommand->digit_data.Byte01.bit_data.bit3;




//  edrk.VozbudOnOffFromRS = pcommand->digit_data.Byte01.bit_data.bit1;
//  edrk.enable_set_vozbud = pcommand->digit_data.Byte01.bit_data.bit1;
//  edrk.SborRS = pcommand->digit_data.Byte01.bit_data.bit2;
//  edrk.RazborRS = pcommand->digit_data.Byte01.bit_data.bit3;
//  edrk.DirectOUT = pcommand->digit_data.Byte01.bit_data.bit4;

//  edrk.StartGED   = pcommand->digit_data.Byte01.bit_data.bit6;


//  f.flag_distance = pcommand->digit_data.Byte01.bit_data.bit6;
//  f.Set_power = pcommand->digit_data.Byte01.bit_data.bit7;

    f.Obmotka1 = unites_t->buf[6].bits.bit15;
    f.Obmotka2 = unites_t->buf[7].bits.bit0;

    edrk.disable_alg_u_disbalance = unites_t->buf[7].bits.bit1;

    //  f.Down50   = pcommand->digit_data.Byte02.bit_data.bit2;
//  f.Up50 = pcommand->digit_data.Byte02.bit_data.bit3;
//  f.Ciclelog = pcommand->digit_data.Byte02.bit_data.bit4;

    //  if (SPEED_SELECT_ZADAT==1)
//  f.Provorot = pcommand->digit_data.Byte02.bit_data.bit5;


//    Data1 = pcommand->analog_data.analog1_hi;
//    Data2 = pcommand->analog_data.analog1_lo;
//    Data = (Data2 + Data1 * 256);
//    if (Data > 32767)
//        Data = Data - 65536;
    DataAnalog1 = unites_t->buf[0].all;

//    Data1 = pcommand->analog_data.analog2_hi;
//    Data2 = pcommand->analog_data.analog2_lo;
//    Data = (Data2 + Data1 * 256);
//    if (Data > 32767)
//        Data = Data - 65536;
    DataAnalog2 = unites_t->buf[1].all;

//    Data1 = pcommand->analog_data.analog3_hi;
//    Data2 = pcommand->analog_data.analog3_lo;
//    Data = (Data2 + Data1 * 256);
//    if (Data > 32767)
//        Data = Data - 65536;
    DataAnalog3 = unites_t->buf[2].all;

//    Data1 = pcommand->analog_data.analog4_hi;
//    Data2 = pcommand->analog_data.analog4_lo;
//    Data = (Data2 + Data1 * 256);
//    if (Data > 32767)
//        Data = Data - 65536;
    DataAnalog4 = unites_t->buf[3].all;
    DataAnalog5 = unites_t->buf[4].all;
    DataAnalog6 = unites_t->buf[5].all;


    edrk.W_from_RS = DataAnalog1;
    edrk.I_zad_vozb_add_from_RS = 0;//DataAnalog4;


    if (!edrk.SetSpeed)
    {
     if (DataAnalog3<0)
       edrk.ZadanieU_Charge_RS = 0;
     else
       edrk.ZadanieU_Charge_RS = DataAnalog3;
    }

    if (edrk.SetSpeed)
    {
      edrk.fzad = DataAnalog1/100.0;
      edrk.kzad = DataAnalog2/10000.0;
      edrk.k_u_disbalance = _IQ(DataAnalog4/100.0);
      edrk.kplus_u_disbalance = _IQ(DataAnalog3/1000.0);

    }
*/
    return;
}





