/*
 * Copyright (c) 2013, Linz Center of Mechatronics GmbH (LCM), web: www.lcm.at
 * All rights reserved.
 */
/*
 * This file is licensed according to the BSD 3-clause license as follows:
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the "Linz Center of Mechatronics GmbH" and "LCM" nor
 *       the names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL "Linz Center of Mechatronics GmbH" BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * This file is part of X2C. web: x2c.lcm.at
 * $LastChangedRevision: 4457 $
 * $LastChangedDate:: 2026-08-13 18:59:23 +0200#$
 */
/* USERCODE-BEGIN:Description */
//*     Description:                                                           *
/* USERCODE-END:Description */

package at.lcm.x2c.library.control;

import at.lcm.x2c.core.structure.*;
import at.lcm.bu21.general.dtypes.*;

/* USERCODE-BEGIN:Imports */
import at.lcm.x2c.utils.QFormat;
/* USERCODE-END:Imports */

@SuppressWarnings("unused")
public class ConvFnc_TDSystemO1_FiP16 extends JavaConversionFunction {
    private static final long serialVersionUID = 1L;

    @Override
    public void convertMaskToImplementation() throws Exception {
        MaskDouble AMaskVal = (MaskDouble)this.getMaskParameter("A").getMaskDataType();
        TNumeric AMaskData = (TNumeric)AMaskVal.getData();
        double mp_A = AMaskData.getReal(0, 0);
        MaskDouble BMaskVal = (MaskDouble)this.getMaskParameter("B").getMaskDataType();
        TNumeric BMaskData = (TNumeric)BMaskVal.getData();
        double mp_B = BMaskData.getReal(0, 0);
        MaskDouble CMaskVal = (MaskDouble)this.getMaskParameter("C").getMaskDataType();
        TNumeric CMaskData = (TNumeric)CMaskVal.getData();
        double mp_C = CMaskData.getReal(0, 0);
        MaskDouble DMaskVal = (MaskDouble)this.getMaskParameter("D").getMaskDataType();
        TNumeric DMaskData = (TNumeric)DMaskVal.getData();
        double mp_D = DMaskData.getReal(0, 0);

        TNumeric a11CtrVal = this.getImplementationParameterData("a11");
        double cp_a11 = Double.NaN;
        TNumeric b11CtrVal = this.getImplementationParameterData("b11");
        double cp_b11 = Double.NaN;
        TNumeric c11CtrVal = this.getImplementationParameterData("c11");
        double cp_c11 = Double.NaN;
        TNumeric d11CtrVal = this.getImplementationParameterData("d11");
        double cp_d11 = Double.NaN;
        TNumeric sfra11CtrVal = this.getImplementationParameterData("sfra11");
        double cp_sfra11 = Double.NaN;
        TNumeric sfrb11CtrVal = this.getImplementationParameterData("sfrb11");
        double cp_sfrb11 = Double.NaN;
        TNumeric sfrc11CtrVal = this.getImplementationParameterData("sfrc11");
        double cp_sfrc11 = Double.NaN;
        TNumeric sfrd11CtrVal = this.getImplementationParameterData("sfrd11");
        double cp_sfrd11 = Double.NaN;

        /* ATTENTION: */
        /* The M2I conversion function may either use the mp_* or *MaskVal value to get the Mask Parameter value */
        /* For new or updated implementations, the mp_* value shall be used where possible */
        /* Currently, the mp_* value cannot be used for getting non-scalar Mask Parameter data but will return only the first element for these */
        /* The Implementation Parameter assignment variables cp_* can and shall be used for all Implementation Parameter data types */

        /* INFO: Implementation data assignment for array types when using the cp_* variables */
        /* If the cp_* parameter is a FLASH ARRAY, the cp_* variable is assigned an array type but without an actual array */
        /* The M2I conversion function has to assign an array with the correct size and fill it with content */
        /* If the cp_* parameter is a RAM ARRAY, the cp_* variable is assigned an array with the correct size */
        /* The first element is assigned an invalid value while the other elements are (auto)initialized with zero */
        /* The M2I conversion function has to fill the array with content */

        /* Mask parameter values */
        /* mp_A OR AMaskVal (Number) */
        /* mp_B OR BMaskVal (Number) */
        /* mp_C OR CMaskVal (Number) */
        /* mp_D OR DMaskVal (Number) */

        /* USERCODE-BEGIN:ConvMaskToImplementation */
		double a11, b11, c11, d11;
		final int BITS = 16;


		// get controller parameter values
		a11 = Double.valueOf(AMaskVal.getValue());
		b11 = Double.valueOf(BMaskVal.getValue());
		c11 = Double.valueOf(CMaskVal.getValue());
		d11 = Double.valueOf(DMaskVal.getValue());


		// set shift factors and Q values
		sfra11CtrVal.setReal(0, 0, QFormat.getQFormat(a11, BITS, true));
		sfrb11CtrVal.setReal(0, 0, QFormat.getQFormat(b11, BITS, true));
		sfrc11CtrVal.setReal(0, 0, QFormat.getQFormat(c11, BITS, true));
		sfrd11CtrVal.setReal(0, 0, QFormat.getQFormat(d11, BITS, true));
		a11CtrVal.setReal(0,0, Double.valueOf(QFormat.getQValue(a11, (int)(sfra11CtrVal.getReal(0,0)), BITS, true)));
		b11CtrVal.setReal(0,0, Double.valueOf(QFormat.getQValue(b11, (int)(sfrb11CtrVal.getReal(0,0)), BITS, true)));
		c11CtrVal.setReal(0,0, Double.valueOf(QFormat.getQValue(c11, (int)(sfrc11CtrVal.getReal(0,0)), BITS, true)));
		d11CtrVal.setReal(0,0, Double.valueOf(QFormat.getQValue(d11, (int)(sfrd11CtrVal.getReal(0,0)), BITS, true)));

        /* USERCODE-END:ConvMaskToImplementation */

        /* Implementation Parameter values */
        /* cp_a11 OR a11CtrVal (Value) */
        /* cp_b11 OR b11CtrVal (Value) */
        /* cp_c11 OR c11CtrVal (Value) */
        /* cp_d11 OR d11CtrVal (Value) */
        /* cp_sfra11 OR sfra11CtrVal (Value) */
        /* cp_sfrb11 OR sfrb11CtrVal (Value) */
        /* cp_sfrc11 OR sfrc11CtrVal (Value) */
        /* cp_sfrd11 OR sfrd11CtrVal (Value) */

        if (Double.isNaN(cp_a11)) {
            this.setImplementationParameterData("a11", a11CtrVal);
        } else {
            this.setImplementationParameterData("a11", new TNumeric(cp_a11));
        }
        if (Double.isNaN(cp_b11)) {
            this.setImplementationParameterData("b11", b11CtrVal);
        } else {
            this.setImplementationParameterData("b11", new TNumeric(cp_b11));
        }
        if (Double.isNaN(cp_c11)) {
            this.setImplementationParameterData("c11", c11CtrVal);
        } else {
            this.setImplementationParameterData("c11", new TNumeric(cp_c11));
        }
        if (Double.isNaN(cp_d11)) {
            this.setImplementationParameterData("d11", d11CtrVal);
        } else {
            this.setImplementationParameterData("d11", new TNumeric(cp_d11));
        }
        if (Double.isNaN(cp_sfra11)) {
            this.setImplementationParameterData("sfra11", sfra11CtrVal);
        } else {
            this.setImplementationParameterData("sfra11", new TNumeric(cp_sfra11));
        }
        if (Double.isNaN(cp_sfrb11)) {
            this.setImplementationParameterData("sfrb11", sfrb11CtrVal);
        } else {
            this.setImplementationParameterData("sfrb11", new TNumeric(cp_sfrb11));
        }
        if (Double.isNaN(cp_sfrc11)) {
            this.setImplementationParameterData("sfrc11", sfrc11CtrVal);
        } else {
            this.setImplementationParameterData("sfrc11", new TNumeric(cp_sfrc11));
        }
        if (Double.isNaN(cp_sfrd11)) {
            this.setImplementationParameterData("sfrd11", sfrd11CtrVal);
        } else {
            this.setImplementationParameterData("sfrd11", new TNumeric(cp_sfrd11));
        }
    }

    @Override
    public void convertImplementationToMask() throws Exception {
        MaskDouble AMaskVal = (MaskDouble)this.getMaskParameter("A").getMaskDataType();
        TNumeric AMaskData = (TNumeric)AMaskVal.getData();
        double mp_A = Double.NaN;
        MaskDouble BMaskVal = (MaskDouble)this.getMaskParameter("B").getMaskDataType();
        TNumeric BMaskData = (TNumeric)BMaskVal.getData();
        double mp_B = Double.NaN;
        MaskDouble CMaskVal = (MaskDouble)this.getMaskParameter("C").getMaskDataType();
        TNumeric CMaskData = (TNumeric)CMaskVal.getData();
        double mp_C = Double.NaN;
        MaskDouble DMaskVal = (MaskDouble)this.getMaskParameter("D").getMaskDataType();
        TNumeric DMaskData = (TNumeric)DMaskVal.getData();
        double mp_D = Double.NaN;

        TNumeric a11CtrVal = this.getImplementationParameterData("a11");
        double cp_a11 = a11CtrVal.getReal(0, 0);
        TNumeric b11CtrVal = this.getImplementationParameterData("b11");
        double cp_b11 = b11CtrVal.getReal(0, 0);
        TNumeric c11CtrVal = this.getImplementationParameterData("c11");
        double cp_c11 = c11CtrVal.getReal(0, 0);
        TNumeric d11CtrVal = this.getImplementationParameterData("d11");
        double cp_d11 = d11CtrVal.getReal(0, 0);
        TNumeric sfra11CtrVal = this.getImplementationParameterData("sfra11");
        double cp_sfra11 = sfra11CtrVal.getReal(0, 0);
        TNumeric sfrb11CtrVal = this.getImplementationParameterData("sfrb11");
        double cp_sfrb11 = sfrb11CtrVal.getReal(0, 0);
        TNumeric sfrc11CtrVal = this.getImplementationParameterData("sfrc11");
        double cp_sfrc11 = sfrc11CtrVal.getReal(0, 0);
        TNumeric sfrd11CtrVal = this.getImplementationParameterData("sfrd11");
        double cp_sfrd11 = sfrd11CtrVal.getReal(0, 0);

        /* ATTENTION: */
        /* The I2M conversion function may either use the cp_* or *CtrlVal value to get the Implementation Parameter value */
        /* For new or updated implementations, the cp_* value shall be used for all Implementation Parameter data types */
        /* The Mask Parameter assignment variables cp_* shall be used where possible */
        /* Currently, the mp_* variable assignment value cannot be used for setting non-scalar Mask Parameter data */

        /* Implementation Parameter values */
        /* cp_a11 OR a11CtrVal (Value) */
        /* cp_b11 OR b11CtrVal (Value) */
        /* cp_c11 OR c11CtrVal (Value) */
        /* cp_d11 OR d11CtrVal (Value) */
        /* cp_sfra11 OR sfra11CtrVal (Value) */
        /* cp_sfrb11 OR sfrb11CtrVal (Value) */
        /* cp_sfrc11 OR sfrc11CtrVal (Value) */
        /* cp_sfrd11 OR sfrd11CtrVal (Value) */

        /* USERCODE-BEGIN:ConvImplementationToMask */
		/* get controller values */
		double a11 = a11CtrVal.getReal(0, 0);
		double b11 = b11CtrVal.getReal(0, 0);
		double c11 = c11CtrVal.getReal(0, 0);
		double d11 = d11CtrVal.getReal(0, 0);
		double sfra11 = sfra11CtrVal.getReal(0, 0);
		double sfrb11 = sfrb11CtrVal.getReal(0, 0);
		double sfrc11 = sfrc11CtrVal.getReal(0, 0);
		double sfrd11 = sfrd11CtrVal.getReal(0, 0);

		/* set mask parameter values */
		AMaskData.setReal(0, 0, a11 / Math.pow(2, sfra11));
		BMaskData.setReal(0, 0, b11 / Math.pow(2, sfrb11));
		CMaskData.setReal(0, 0, c11 / Math.pow(2, sfrc11));
		DMaskData.setReal(0, 0, d11 / Math.pow(2, sfrd11));

        /* USERCODE-END:ConvImplementationToMask */

        /* Mask parameter values */
        /* mp_A OR AMaskVal (Number) */
        /* mp_B OR BMaskVal (Number) */
        /* mp_C OR CMaskVal (Number) */
        /* mp_D OR DMaskVal (Number) */

        if (Double.isNaN(mp_A)) {
            this.getMaskParameter("A").setValue(AMaskData.toFullString());
        } else {
            this.getMaskParameter("A").setValue(new TNumeric(mp_A).toFullString());
        }
        if (Double.isNaN(mp_B)) {
            this.getMaskParameter("B").setValue(BMaskData.toFullString());
        } else {
            this.getMaskParameter("B").setValue(new TNumeric(mp_B).toFullString());
        }
        if (Double.isNaN(mp_C)) {
            this.getMaskParameter("C").setValue(CMaskData.toFullString());
        } else {
            this.getMaskParameter("C").setValue(new TNumeric(mp_C).toFullString());
        }
        if (Double.isNaN(mp_D)) {
            this.getMaskParameter("D").setValue(DMaskData.toFullString());
        } else {
            this.getMaskParameter("D").setValue(new TNumeric(mp_D).toFullString());
        }
    }
}
