/*
 * Copyright (c) 2014, 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: 4463 $
 * $LastChangedDate:: 2026-08-17 13:59:56 +0200#$
 */
/* USERCODE-BEGIN:Description */
/* Description: */
/* USERCODE-END:Description */

package at.lcm.x2c.library.general;

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

/* USERCODE-BEGIN:Imports */
/* USERCODE-END:Imports */

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

    @Override
    public void convertMaskToImplementation() throws Exception {
        MaskDouble Delay1MaskVal = (MaskDouble)this.getMaskParameter("Delay1").getMaskDataType();
        TNumeric Delay1MaskData = (TNumeric)Delay1MaskVal.getData();
        double mp_Delay1 = Delay1MaskData.getReal(0, 0);
        MaskDouble Delay2MaskVal = (MaskDouble)this.getMaskParameter("Delay2").getMaskDataType();
        TNumeric Delay2MaskData = (TNumeric)Delay2MaskVal.getData();
        double mp_Delay2 = Delay2MaskData.getReal(0, 0);
        MaskDouble Delay3MaskVal = (MaskDouble)this.getMaskParameter("Delay3").getMaskDataType();
        TNumeric Delay3MaskData = (TNumeric)Delay3MaskVal.getData();
        double mp_Delay3 = Delay3MaskData.getReal(0, 0);
        MaskDouble Delay4MaskVal = (MaskDouble)this.getMaskParameter("Delay4").getMaskDataType();
        TNumeric Delay4MaskData = (TNumeric)Delay4MaskVal.getData();
        double mp_Delay4 = Delay4MaskData.getReal(0, 0);
        MaskDouble ts_factMaskVal = (MaskDouble)this.getMaskParameter("ts_fact").getMaskDataType();
        TNumeric ts_factMaskData = (TNumeric)ts_factMaskVal.getData();
        double mp_ts_fact = ts_factMaskData.getReal(0, 0);

        TNumeric delay1CtrVal = this.getImplementationParameterData("delay1");
        double cp_delay1 = Double.NaN;
        TNumeric delay2CtrVal = this.getImplementationParameterData("delay2");
        double cp_delay2 = Double.NaN;
        TNumeric delay3CtrVal = this.getImplementationParameterData("delay3");
        double cp_delay3 = Double.NaN;
        TNumeric delay4CtrVal = this.getImplementationParameterData("delay4");
        double cp_delay4 = Double.NaN;

        /* Check if sample time factor is an integer value */
        if (Math.rint(mp_ts_fact) != mp_ts_fact) {
            throw new Exception("Sample time factor must be a whole number");
        }
        /* Check if sample time factor is within the valid range (upper limit defined by unsigned 16-bit integer range) */
        if ((mp_ts_fact < 1.0) || (mp_ts_fact > 65535.0)) {
            throw new Exception("Sample time factor must be at least 1 and at most 65535");
        }
        /* Block sample time */
        double bp_ts = mp_ts_fact * this.getDedicatedBlock().getModel().getSampleTime();
        /* Check if Block sample time has a valid value */
        if (bp_ts <= 0.0) {
            throw new Exception("Block sample time must be greater than zero");
        }

        /* 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_Delay1 OR Delay1MaskVal (Number) */
        /* mp_Delay2 OR Delay2MaskVal (Number) */
        /* mp_Delay3 OR Delay3MaskVal (Number) */
        /* mp_Delay4 OR Delay4MaskVal (Number) */
        /* mp_ts_fact OR ts_factMaskVal (Number) */

        /* USERCODE-BEGIN:ConvMaskToImplementation */
        double delay1, delay2, delay3, delay4;
        double Ts = 0;
        int ts_fact;
        final int UINT16_MAX = 65535;

        // get parameter values
        delay1 = Double.valueOf(Delay1MaskVal.getValue());
        delay2 = Double.valueOf(Delay2MaskVal.getValue());
        delay3 = Double.valueOf(Delay3MaskVal.getValue());
        delay4 = Double.valueOf(Delay4MaskVal.getValue());

        // calculate sample time
        ts_fact = Double.valueOf(ts_factMaskVal.getValue()).intValue();
        Ts = ts_fact * this.getDedicatedBlock().getModel().getSampleTime();

        // validate controller parameters
        if (delay1 < 0) {
            delay1 = 0;
        }
        if (delay2 < 0) {
            delay2 = 0;
        }
        if (delay3 < 0) {
            delay3 = 0;
        }
        if (delay4 < 0) {
            delay4 = 0;
        }
        if (ts_fact <= 0) {
            ts_fact = 1;
        }

        // calculate delay values
        delay1 = delay1 / Ts;
        delay2 = delay2 / Ts;
        delay3 = delay3 / Ts;
        delay4 = delay4 / Ts;

        // check delay values
        if (delay1 > UINT16_MAX) {
            throw new Exception("Sequencer-Block: Delay 1 is too high!");
        }
        if (delay2 > UINT16_MAX) {
            throw new Exception("Sequencer-Block: Delay 2 is too high!");
        }
        if (delay3 > UINT16_MAX) {
            throw new Exception("Sequencer-Block: Delay 3 is too high!");
        }
        if (delay4 > UINT16_MAX) {
            throw new Exception("Sequencer-Block: Delay 4 is too high!");
        }

        // set delay values
        delay1CtrVal.setReal(0, 0, delay1);
        delay2CtrVal.setReal(0, 0, delay2);
        delay3CtrVal.setReal(0, 0, delay3);
        delay4CtrVal.setReal(0, 0, delay4);

        /* USERCODE-END:ConvMaskToImplementation */

        /* Implementation Parameter values */
        /* cp_delay1 OR delay1CtrVal (Value) */
        /* cp_delay2 OR delay2CtrVal (Value) */
        /* cp_delay3 OR delay3CtrVal (Value) */
        /* cp_delay4 OR delay4CtrVal (Value) */

        if (Double.isNaN(cp_delay1)) {
            this.setImplementationParameterData("delay1", delay1CtrVal);
        } else {
            this.setImplementationParameterData("delay1", new TNumeric(cp_delay1));
        }
        if (Double.isNaN(cp_delay2)) {
            this.setImplementationParameterData("delay2", delay2CtrVal);
        } else {
            this.setImplementationParameterData("delay2", new TNumeric(cp_delay2));
        }
        if (Double.isNaN(cp_delay3)) {
            this.setImplementationParameterData("delay3", delay3CtrVal);
        } else {
            this.setImplementationParameterData("delay3", new TNumeric(cp_delay3));
        }
        if (Double.isNaN(cp_delay4)) {
            this.setImplementationParameterData("delay4", delay4CtrVal);
        } else {
            this.setImplementationParameterData("delay4", new TNumeric(cp_delay4));
        }
    }

    @Override
    public void convertImplementationToMask() throws Exception {
        MaskDouble Delay1MaskVal = (MaskDouble)this.getMaskParameter("Delay1").getMaskDataType();
        TNumeric Delay1MaskData = (TNumeric)Delay1MaskVal.getData();
        double mp_Delay1 = Double.NaN;
        MaskDouble Delay2MaskVal = (MaskDouble)this.getMaskParameter("Delay2").getMaskDataType();
        TNumeric Delay2MaskData = (TNumeric)Delay2MaskVal.getData();
        double mp_Delay2 = Double.NaN;
        MaskDouble Delay3MaskVal = (MaskDouble)this.getMaskParameter("Delay3").getMaskDataType();
        TNumeric Delay3MaskData = (TNumeric)Delay3MaskVal.getData();
        double mp_Delay3 = Double.NaN;
        MaskDouble Delay4MaskVal = (MaskDouble)this.getMaskParameter("Delay4").getMaskDataType();
        TNumeric Delay4MaskData = (TNumeric)Delay4MaskVal.getData();
        double mp_Delay4 = Double.NaN;
        MaskDouble ts_factMaskVal = (MaskDouble)this.getMaskParameter("ts_fact").getMaskDataType();
        TNumeric ts_factMaskData = (TNumeric)ts_factMaskVal.getData();
        double mp_ts_fact = ts_factMaskData.getReal(0, 0);

        TNumeric delay1CtrVal = this.getImplementationParameterData("delay1");
        double cp_delay1 = delay1CtrVal.getReal(0, 0);
        TNumeric delay2CtrVal = this.getImplementationParameterData("delay2");
        double cp_delay2 = delay2CtrVal.getReal(0, 0);
        TNumeric delay3CtrVal = this.getImplementationParameterData("delay3");
        double cp_delay3 = delay3CtrVal.getReal(0, 0);
        TNumeric delay4CtrVal = this.getImplementationParameterData("delay4");
        double cp_delay4 = delay4CtrVal.getReal(0, 0);

        /* Check if sample time factor is an integer value */
        if (Math.rint(mp_ts_fact) != mp_ts_fact) {
            throw new Exception("Sample time factor must be a whole number");
        }
        /* Check if sample time factor is within the valid range (upper limit defined by unsigned 16-bit integer range) */
        if ((mp_ts_fact < 1.0) || (mp_ts_fact > 65535.0)) {
            throw new Exception("Sample time factor must be at least 1 and at most 65535");
        }
        /* Block sample time */
        double bp_ts = mp_ts_fact * this.getDedicatedBlock().getModel().getSampleTime();
        /* Check if Block sample time has a valid value */
        if (bp_ts <= 0.0) {
            throw new Exception("Block sample time must be greater than zero");
        }

        /* 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_delay1 OR delay1CtrVal (Value) */
        /* cp_delay2 OR delay2CtrVal (Value) */
        /* cp_delay3 OR delay3CtrVal (Value) */
        /* cp_delay4 OR delay4CtrVal (Value) */

        /* USERCODE-BEGIN:ConvImplementationToMask */
        double Ts = 0;
        int ts_fact;
        double delay1, delay2, delay3, delay4;

        // get controller parameter
        delay1 = delay1CtrVal.getReal(0, 0);
        delay2 = delay2CtrVal.getReal(0, 0);
        delay3 = delay3CtrVal.getReal(0, 0);
        delay4 = delay4CtrVal.getReal(0, 0);

        // get sample time information from Simulink/Xcos block/model (sample time information can't be obtained from target)
        ts_fact = Double.valueOf(ts_factMaskVal.getValue()).intValue();
        Ts = ts_fact * this.getDedicatedBlock().getModel().getSampleTime();

        // set mask parameter
        Delay1MaskData.setReal(0, 0, delay1 * Ts);
        Delay2MaskData.setReal(0, 0, delay2 * Ts);
        Delay3MaskData.setReal(0, 0, delay3 * Ts);
        Delay4MaskData.setReal(0, 0, delay4 * Ts);

        /* USERCODE-END:ConvImplementationToMask */

        /* Mask parameter values */
        /* mp_Delay1 OR Delay1MaskVal (Number) */
        /* mp_Delay2 OR Delay2MaskVal (Number) */
        /* mp_Delay3 OR Delay3MaskVal (Number) */
        /* mp_Delay4 OR Delay4MaskVal (Number) */
        /* mp_ts_fact OR ts_factMaskVal (Number) */

        if (Double.isNaN(mp_Delay1)) {
            this.getMaskParameter("Delay1").setValue(Delay1MaskData.toFullString());
        } else {
            this.getMaskParameter("Delay1").setValue(new TNumeric(mp_Delay1).toFullString());
        }
        if (Double.isNaN(mp_Delay2)) {
            this.getMaskParameter("Delay2").setValue(Delay2MaskData.toFullString());
        } else {
            this.getMaskParameter("Delay2").setValue(new TNumeric(mp_Delay2).toFullString());
        }
        if (Double.isNaN(mp_Delay3)) {
            this.getMaskParameter("Delay3").setValue(Delay3MaskData.toFullString());
        } else {
            this.getMaskParameter("Delay3").setValue(new TNumeric(mp_Delay3).toFullString());
        }
        if (Double.isNaN(mp_Delay4)) {
            this.getMaskParameter("Delay4").setValue(Delay4MaskData.toFullString());
        } else {
            this.getMaskParameter("Delay4").setValue(new TNumeric(mp_Delay4).toFullString());
        }
    }
}
