/*
 * uom_tools.c
 *
 *  Created on: 13 нояб. 2024 г.
 *      Author: Evgeniy_Sokolov
 */



#include <edrk_main.h>

#include <params.h>
#include <params_alg.h>
#include <params_norma.h>
#include <params_pwm24.h>
#include <params_temper_p.h>
#include <project.h>
#include "IQmathLib.h"
#include "mathlib.h"
#include "CAN_Setup.h"
#include "uom_tools.h"


#pragma DATA_SECTION(uom_levels, ".slow_vars")
int uom_levels[9]    = {0, 0, 15, 30, 45, 60, 75, 90, 100};
#pragma DATA_SECTION(iq_uom_levels, ".slow_vars")
_iq iq_uom_levels[9] = {_IQ(1.0), _IQ(1.0), _IQ(0.85), _IQ(0.7), _IQ(0.55), _IQ(0.4), _IQ(0.25), _IQ(0.1), _IQ(0.016)};
void update_uom(void)
{
//    int index;
    static _iq max_nominal_power = _IQ(MAX_ZADANIE_LIMIT_POWER*1000.0/(NORMA_MZZ*NORMA_MZZ));
    static _iq super_max_nominal_power = _IQ(SUPER_MAX_ZADANIE_LIMIT_POWER*1000.0/(NORMA_MZZ*NORMA_MZZ));

    static unsigned int prev_CAN_count_cycle_input_units = 0, c_data = 0;
    unsigned int cur_can_cycle;

    static FROM_ZADAT4IK zad = {0}, zad_w = {0};
    static unsigned int temp_code = 0 , temp_code1 = 0, temp_code2 = 0, temp_code3 = 0;



    cur_can_cycle = unites_can_setup.CAN_count_cycle_input_units[0];

    if (prev_CAN_count_cycle_input_units != cur_can_cycle)
    {
        zad = edrk.from_zadat4ik;

        temp_code =  (zad.bits.UOM_READY_ACTIVE & 0x1) +
                (zad.bits.UOM_LIMIT_1 & 0x1) << 1 +
                (zad.bits.UOM_LIMIT_2 & 0x1) << 2 +
                (zad.bits.UOM_LIMIT_3 & 0x1) << 3 ;

        if (c_data == 0)
        {
            temp_code1 = temp_code;
            c_data = 1;
        }
        else
        if (c_data == 1)
        {
            temp_code2 = temp_code;
            c_data = 2;
        }
        else
        if (c_data == 2)
        {
            temp_code3 = temp_code;
            c_data = 0;
        }

    }

    prev_CAN_count_cycle_input_units = cur_can_cycle;

    if ((temp_code1 == temp_code2) && (temp_code2 == temp_code3))
        zad_w  = zad;


    edrk.from_uom.digital_line.bits.ready = zad_w.bits.UOM_READY_ACTIVE;
    edrk.from_uom.digital_line.bits.level0 = zad_w.bits.UOM_LIMIT_1;
    edrk.from_uom.digital_line.bits.level1 = zad_w.bits.UOM_LIMIT_2;
    edrk.from_uom.digital_line.bits.level2 = zad_w.bits.UOM_LIMIT_3;


    if (edrk.from_uom.digital_line.bits.ready && edrk.disable_uom==0)
    {
        edrk.from_uom.ready = 1;
//000 - 100
        if (edrk.from_uom.digital_line.bits.level0 == 0 &&
            edrk.from_uom.digital_line.bits.level1 == 0 &&
            edrk.from_uom.digital_line.bits.level2 == 0 )
            edrk.from_uom.code = 1;
//001 - 85
        if (edrk.from_uom.digital_line.bits.level0 == 1 &&
            edrk.from_uom.digital_line.bits.level1 == 0 &&
            edrk.from_uom.digital_line.bits.level2 == 0 )
            edrk.from_uom.code = 2;
//011  - 70
        if (edrk.from_uom.digital_line.bits.level0 == 1 &&
            edrk.from_uom.digital_line.bits.level1 == 1 &&
            edrk.from_uom.digital_line.bits.level2 == 0 )
            edrk.from_uom.code = 3;
//010 - 55
        if (edrk.from_uom.digital_line.bits.level0 == 0 &&
            edrk.from_uom.digital_line.bits.level1 == 1 &&
            edrk.from_uom.digital_line.bits.level2 == 0 )
            edrk.from_uom.code = 4;
//110 - 40
        if (edrk.from_uom.digital_line.bits.level0 == 0 &&
            edrk.from_uom.digital_line.bits.level1 == 1 &&
            edrk.from_uom.digital_line.bits.level2 == 1 )
            edrk.from_uom.code = 5;
//111 - 25
        if (edrk.from_uom.digital_line.bits.level0 == 1 &&
            edrk.from_uom.digital_line.bits.level1 == 1 &&
            edrk.from_uom.digital_line.bits.level2 == 1 )
            edrk.from_uom.code = 6;
//101 - 10
        if (edrk.from_uom.digital_line.bits.level0 == 1 &&
            edrk.from_uom.digital_line.bits.level1 == 0 &&
            edrk.from_uom.digital_line.bits.level2 == 1 )
            edrk.from_uom.code = 7;
//100 - 0
        if (edrk.from_uom.digital_line.bits.level0 == 0 &&
            edrk.from_uom.digital_line.bits.level1 == 0 &&
            edrk.from_uom.digital_line.bits.level2 == 1 )
            edrk.from_uom.code = 8;
    }
    else
    {
        edrk.from_uom.ready = 0;
        edrk.from_uom.code = 0;
    }

    edrk.from_uom.iq_level_value = iq_uom_levels[edrk.from_uom.code];

    if (edrk.from_uom.code<=1)
        edrk.from_uom.iq_level_value_kwt = super_max_nominal_power;
    else
        edrk.from_uom.iq_level_value_kwt = _IQmpy(max_nominal_power,
                                             edrk.from_uom.iq_level_value);

    edrk.from_uom.level_value = uom_levels[edrk.from_uom.code];



}

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