/*
 * message_terminals_can.c
 *
 *  Created on: 15 мая 2020 г.
 *      Author: yura
 */
#include <edrk_main.h>
#include <message_modbus.h>
#include <message2.h>
#include <vector.h>

#include "CAN_Setup.h"
#include "global_time.h"
#include "modbus_table_v2.h"
#include "oscil_can.h"
#include "DSP281x_Examples.h" // DSP281x Examples Include File
#include "DSP281x_Device.h"   // DSP281x Headerfile Include File
#include "CRC_Functions.h"
#include "RS_Function_terminal.h"
#include "TuneUpPlane.h"
#include "control_station.h"



#define TERMINALS_CAN_TIME_WAIT     500  // период передачи данных в терминалку
#pragma DATA_SECTION(buf_message_can_cmd,".slow_vars")
int buf_message_can_cmd[sizeof(CMD_TO_TMS_STRUCT)+10];
#pragma DATA_SECTION(buf_message_can_data,".slow_vars")
int buf_message_can_data[sizeof(TMS_TO_TERMINAL_STRUCT)+10];
#pragma DATA_SECTION(buf_message_can_data2,".slow_vars")
int buf_message_can_data2[sizeof(TMS_TO_TERMINAL_STRUCT)+10];

int *p_buf_message_can_data3;


void write_all_data_to_terminals_can(int run_force, unsigned int pause)
{
    static unsigned int old_time = 0;
    static unsigned int send_time = 0;
    static int prev_send_to_can = 0;

    static int count_sends = 0;

    unsigned long old_t;
    unsigned int i;
    int real_mbox;
    CMD_TO_TMS_STRUCT* pcommand = (CMD_TO_TMS_STRUCT *)(buf_message_can_cmd);


    real_mbox = get_real_out_mbox(TERMINAL_TYPE_BOX, edrk.number_can_box_terminal_cmd);

    // была команда на отправку посылки и она еще не ушла, тогда србасываем счетчик времени паузы между посылками,
    // т.е. TERMINALS_CAN_TIME_WAIT между концом отправки посылки и новой посылки.
    if (prev_send_to_can && CAN_cycle_full_free(real_mbox,CAN_BOX_STAT_OFF)==0)
    {
        old_time = (unsigned int)global_time.miliseconds;
        return;
    }
    prev_send_to_can = 0;

    if (!detect_pause_milisec(pause,&old_time))
        return;


    //func_fill_answer_to_TMS(&reply, pcommand);
    func_pack_answer_to_TMS(&reply);

    *(TMS_TO_TERMINAL_STRUCT*)buf_message_can_data2 = reply;    // Необходимы легкие приведениЯ



  //  reply.digit_data.byte01.byte_data = send_time;
/*    reply.digit_data.byte02.byte_data = 0x66;
    reply.analog_data.analog60_hi = 0x33;
    reply.analog_data.analog60_lo = 0x44;
*/
    reply.analog_data.analog58_hi = HIBYTE((unsigned int)control_station.raw_array_data[CONTROL_STATION_TERMINAL_CAN][0].all);
    reply.analog_data.analog58_lo = LOBYTE((unsigned int)control_station.raw_array_data[CONTROL_STATION_TERMINAL_CAN][0].all);

//    control_station.raw_array_data[CONTROL_STATION_TERMINAL_CAN][0].all

    reply.analog_data.analog59_hi = HIBYTE((unsigned int)global_time.miliseconds);
    reply.analog_data.analog59_lo = LOBYTE((unsigned int)global_time.miliseconds);

    reply.analog_data.analog60_hi = HIBYTE(count_sends);
    reply.analog_data.analog60_lo = LOBYTE(count_sends);
    count_sends++;
    if (count_sends>32768) count_sends=0;

//    reply.analog_data.analog1_hi = HIBYTE(send_time);
 //   reply.analog_data.analog1_lo = LOBYTE(send_time);

//    reply.analog_data.analog2_hi = HIBYTE(oscil_can.timer_send);
//    reply.analog_data.analog2_lo = LOBYTE(oscil_can.timer_send);

    p_buf_message_can_data3 = (int *)&reply.digit_data;


    for (i=0;i<sizeof(TMS_TO_TERMINAL_STRUCT)-5;i++)
    {

           if (i%2)
           {
               buf_message_can_data[i>>1] |= (  (*p_buf_message_can_data3++) << 8) & 0xff00;
           }
           else
               buf_message_can_data[i>>1] = (   (*p_buf_message_can_data3++)     ) & 0x00ff;

    }

    if (CAN_cycle_full_free(real_mbox,CAN_BOX_STAT_ON))
    {

        old_t = global_time.microseconds;
        CAN_cycle_send(
                TERMINAL_TYPE_BOX,
                edrk.number_can_box_terminal_cmd,
                            0,
                            &buf_message_can_data[0], ((sizeof(TMS_TO_TERMINAL_STRUCT)-5)>>1), CAN_BOX_STANDART_ADR, CAN_BOX_PRIORITY_NORMAL);

        prev_send_to_can = 1;


        send_time = (global_time.microseconds - old_t)/100;
    }


}
