/* This file is part of X2C. web: x2c.lcm.at                                                                          */

/* Model: DemoApplication                                                                                             */
/* Date:  2025-11-27 13:58                                                                                            */

/* X2C-Version: 6.5.3948                                                                                              */
/* X2C-Edition: Development                                                                                           */

/* Common includes                                                                                                    */
#include <stddef.h>
#include "TableStruct.h"
/* Generated file includes                                                                                            */
#include "FlashTable.h"
#include "RamTable.h"
#include "X2C.h"

/**********************************************************************************************************************/
/**                                                       Model                                                      **/
/**********************************************************************************************************************/
struct x2cModel x2cModel;

/* Model identifier                                                                                                   */
const uint32 x2cModelIdentifier = 0xA0BD0CFBu;

/**********************************************************************************************************************/
/**                                                       Scope                                                      **/
/**********************************************************************************************************************/
SCOPE_MAIN x2cScope;

/* Block function table                                                                                               */
const tBlockFunctions blockFunctionTable[] = {
    FUNCTIONS,
    { 0u, NULL, NULL, NULL, NULL, NULL } /* End of table entry */
};

/* Parameter identifier table                                                                                         */
const tParameterTable parameterIdTable[] = {
    PARAMETER_TABLE,
    { 0u, NULL } /* End of table entry */
};

/* Inport Parameter identifier table                                                                                  */
const tIoParamIdEntry inportParamIdTable[] = {
    INPORT_PARAMID_TABLE, 
    { 0u, 0u, NULL } /* End of table entry */
};

/* Outport Parameter identifier table                                                                                 */
const tIoParamIdEntry outportParamIdTable[] = {
    OUTPORT_PARAMID_TABLE, 
    { 0u, 0u, NULL } /* End of table entry */
};

/* Mask Parameter identifier table                                                                                    */
const tMaskParameterEntry maskParamIdTable[] = {
    { 0u, NULL, NULL, NULL, NULL } /* End of table entry */
};

/* Extended Mask Parameter identifier table (used by Model API)                                                       */
const tMaskParamExtRecord maskParamExtTable[] = {
    { 0u, 0u, NULL, NULL, NULL, NULL, NULL } /* End of table entry */
};

/* Mask Parameter data table (used by Model API)                                                                      */
const tMaskParamDataRecord maskParamDataTable[] = {
    { 0u, 0u, NULL, 0u, 0u } /* End of table entry */
};

/**********************************************************************************************************************/
/** Model Sample Time                                                                                                **/
/**********************************************************************************************************************/
float_CoT getModelSampleTime()
{
    return ((float_CoT)1.0E-4);
}

/**********************************************************************************************************************/
/**                                                  Initialization                                                  **/
/**********************************************************************************************************************/
void X2C_Init(void)
{
    /******************************************************************************************************************/
    /**                                          Initialize Block Parameters                                         **/
    /******************************************************************************************************************/

    /* Block: Amplitude                                                                                               */
    /* Value = 50.0                                                                                                   */
    x2cModel.blocks.bAmplitude.K = 50;

    /* Block: AutoSwitch1                                                                                             */
    /* Thresh_up = 50.0                                                                                               */
    /* Thresh_down = 50.0                                                                                             */
    x2cModel.blocks.bAutoSwitch1.Thresh_up = 50;
    x2cModel.blocks.bAutoSwitch1.Thresh_down = 50;
    x2cModel.blocks.bAutoSwitch1.Status = &RamTable_float32[0];

    /* Block: AutoSwitch2                                                                                             */
    /* Thresh_up = 50.0                                                                                               */
    /* Thresh_down = 50.0                                                                                             */
    x2cModel.blocks.bAutoSwitch2.Thresh_up = 50;
    x2cModel.blocks.bAutoSwitch2.Thresh_down = 50;
    x2cModel.blocks.bAutoSwitch2.Status = &RamTable_float32[1];

    /* Block: AutoSwitch3                                                                                             */
    /* Thresh_up = 50.0                                                                                               */
    /* Thresh_down = 50.0                                                                                             */
    x2cModel.blocks.bAutoSwitch3.Thresh_up = 50;
    x2cModel.blocks.bAutoSwitch3.Thresh_down = 50;
    x2cModel.blocks.bAutoSwitch3.Status = &RamTable_float32[2];

    /* Block: DigOut1                                                                                                 */
    /* Value = 0.0                                                                                                    */
    x2cModel.blocks.bDigOut1.K = 0;

    /* Block: DigOut2                                                                                                 */
    /* Value = 0.0                                                                                                    */
    x2cModel.blocks.bDigOut2.K = 0;

    /* Block: DigOut3                                                                                                 */
    /* Value = 0.0                                                                                                    */
    x2cModel.blocks.bDigOut3.K = 0;

    /* Block: DigOut4                                                                                                 */
    /* Value = 0.0                                                                                                    */
    x2cModel.blocks.bDigOut4.K = 0;

    /* Block: LED off                                                                                                 */
    /* Value = 0.0                                                                                                    */
    x2cModel.blocks.bLED_off.K = 0;

    /* Block: LED on                                                                                                  */
    /* Value = 1.0                                                                                                    */
    x2cModel.blocks.bLED_on.K = 1;

    /* Block: LED_on_off                                                                                              */
    /* Value = 0.0                                                                                                    */
    x2cModel.blocks.bLED_on_off.K = 0;

    /* Block: Sin3Gen                                                                                                 */
    /* fmax = 1000.0                                                                                                  */
    /* Offset = 50.0                                                                                                  */
    /* ts_fact = 1.0                                                                                                  */
    x2cModel.blocks.bSin3Gen.delta_phi = 0.00062831853071796;
    x2cModel.blocks.bSin3Gen.offset = 50;
    x2cModel.blocks.bSin3Gen.phi = 0;

    /* Block: Start_Stop                                                                                              */
    /* Value = 1.0                                                                                                    */
    x2cModel.blocks.bStart_Stop.K = 1;

    /* Block: TriangleGenerator/AutoSwitch0                                                                           */
    /* Thresh_up = 500.0                                                                                              */
    /* Thresh_down = 0.0                                                                                              */
    x2cModel.blocks.sTriangleGenerator.bAutoSwitch0.Thresh_up = 500;
    x2cModel.blocks.sTriangleGenerator.bAutoSwitch0.Thresh_down = 0;
    x2cModel.blocks.sTriangleGenerator.bAutoSwitch0.Status = &RamTable_float32[3];

    /* Block: TriangleGenerator/I                                                                                     */
    /* Ki = 1.0                                                                                                       */
    /* ts_fact = 1.0                                                                                                  */
    x2cModel.blocks.sTriangleGenerator.bI.b0 = 0.0001;
    x2cModel.blocks.sTriangleGenerator.bI.i_old = 0;
    x2cModel.blocks.sTriangleGenerator.bI.enable_old = 0;

    /* Block: TriangleGenerator/Init                                                                                  */
    /* Value = 0.0                                                                                                    */
    x2cModel.blocks.sTriangleGenerator.bInit.K = 0;

    /* Block: TriangleGenerator/LoopBreaker                                                                           */

    /* Block: TriangleGenerator/Negation                                                                              */

    /* Block: TriangleGenerator/Slope                                                                                 */
    /* Value = 10.0                                                                                                   */
    x2cModel.blocks.sTriangleGenerator.bSlope.K = 10;


    /* Initialize RAM table content */
    initRamTables();

    /******************************************************************************************************************/
    /**                                              Initialize Inports                                              **/
    /******************************************************************************************************************/
    x2cModel.inports.bP8_10 = false;
    x2cModel.inports.bP8_12 = false;
    x2cModel.inports.bP8_16 = false;
    x2cModel.inports.bP8_8 = false;
    x2cModel.inports.bP9_33 = (float32)0;
    x2cModel.inports.bP9_35 = (float32)0;
    x2cModel.inports.bP9_36 = (float32)0;
    x2cModel.inports.bP9_37 = (float32)0;
    x2cModel.inports.bP9_38 = (float32)0;
    x2cModel.inports.bP9_39 = (float32)0;
    x2cModel.inports.bP9_40 = (float32)0;

    /******************************************************************************************************************/
    /**                                               Link Block Inputs                                              **/
    /******************************************************************************************************************/

    /* Block Amplitude                                                                                                */

    /* Block AutoSwitch1                                                                                              */
    x2cModel.blocks.bAutoSwitch1.In1 =
        &x2cModel.blocks.bLED_off.Out;
    x2cModel.blocks.bAutoSwitch1.Switch =
        &x2cModel.blocks.bSin3Gen.u;
    x2cModel.blocks.bAutoSwitch1.In3 =
        &x2cModel.blocks.bLED_on.Out;

    /* Block AutoSwitch2                                                                                              */
    x2cModel.blocks.bAutoSwitch2.In1 =
        &x2cModel.blocks.bLED_off.Out;
    x2cModel.blocks.bAutoSwitch2.Switch =
        &x2cModel.blocks.bSin3Gen.v;
    x2cModel.blocks.bAutoSwitch2.In3 =
        &x2cModel.blocks.bLED_on.Out;

    /* Block AutoSwitch3                                                                                              */
    x2cModel.blocks.bAutoSwitch3.In1 =
        &x2cModel.blocks.bLED_off.Out;
    x2cModel.blocks.bAutoSwitch3.Switch =
        &x2cModel.blocks.bSin3Gen.w;
    x2cModel.blocks.bAutoSwitch3.In3 =
        &x2cModel.blocks.bLED_on.Out;

    /* Block DigOut1                                                                                                  */

    /* Block DigOut2                                                                                                  */

    /* Block DigOut3                                                                                                  */

    /* Block DigOut4                                                                                                  */

    /* Block LEDoff                                                                                                   */

    /* Block LEDon                                                                                                    */

    /* Block LED_on_off                                                                                               */

    /* Block Sin3Gen                                                                                                  */
    x2cModel.blocks.bSin3Gen.A =
        &x2cModel.blocks.bAmplitude.Out;
    x2cModel.blocks.bSin3Gen.f =
        &x2cModel.blocks.sTriangleGenerator.bI.Out;

    /* Block Start_Stop                                                                                               */

    /* Block AutoSwitch0                                                                                              */
    x2cModel.blocks.sTriangleGenerator.bAutoSwitch0.In1 =
        &x2cModel.blocks.sTriangleGenerator.bNegation.Out;
    x2cModel.blocks.sTriangleGenerator.bAutoSwitch0.Switch =
        &x2cModel.blocks.sTriangleGenerator.bLoopBreaker.Out;
    x2cModel.blocks.sTriangleGenerator.bAutoSwitch0.In3 =
        &x2cModel.blocks.sTriangleGenerator.bSlope.Out;

    /* Block I                                                                                                        */
    x2cModel.blocks.sTriangleGenerator.bI.In =
        &x2cModel.blocks.sTriangleGenerator.bAutoSwitch0.Out;
    x2cModel.blocks.sTriangleGenerator.bI.I0 =
        &x2cModel.blocks.sTriangleGenerator.bInit.Out;
    x2cModel.blocks.sTriangleGenerator.bI.Enable =
        &x2cModel.blocks.bStart_Stop.Out;

    /* Block Init                                                                                                     */

    /* Block LoopBreaker                                                                                              */
    x2cModel.blocks.sTriangleGenerator.bLoopBreaker.In =
        &x2cModel.blocks.sTriangleGenerator.bI.Out;

    /* Block Negation                                                                                                 */
    x2cModel.blocks.sTriangleGenerator.bNegation.In =
        &x2cModel.blocks.sTriangleGenerator.bSlope.Out;

    /* Block Slope                                                                                                    */

    /******************************************************************************************************************/
    /**                                                 Link Outports                                                **/
    /******************************************************************************************************************/
    x2cModel.outports.bLED0 =
        &x2cModel.blocks.bLED_on_off.Out;
    x2cModel.outports.bLED1 =
        &x2cModel.blocks.bAutoSwitch1.Out;
    x2cModel.outports.bLED2 =
        &x2cModel.blocks.bAutoSwitch2.Out;
    x2cModel.outports.bLED3 =
        &x2cModel.blocks.bAutoSwitch3.Out;
    x2cModel.outports.bP8_11 =
        &x2cModel.blocks.bDigOut3.Out;
    x2cModel.outports.bP8_13_19 =
        &x2cModel.blocks.bSin3Gen.w;
    x2cModel.outports.bP8_15 =
        &x2cModel.blocks.bDigOut4.Out;
    x2cModel.outports.bP8_7 =
        &x2cModel.blocks.bDigOut1.Out;
    x2cModel.outports.bP8_9 =
        &x2cModel.blocks.bDigOut2.Out;
    x2cModel.outports.bP9_14_16 =
        &x2cModel.blocks.bSin3Gen.v;
    x2cModel.outports.bP9_21_22 =
        &x2cModel.blocks.bSin3Gen.u;

    /******************************************************************************************************************/
    /**                                           Run Block Init Functions                                           **/
    /******************************************************************************************************************/
    Constant_Float32_Init(&x2cModel.blocks.bAmplitude);
    AutoSwitch_Float32_Init(&x2cModel.blocks.bAutoSwitch1);
    AutoSwitch_Float32_Init(&x2cModel.blocks.bAutoSwitch2);
    AutoSwitch_Float32_Init(&x2cModel.blocks.bAutoSwitch3);
    Constant_Bool_Init(&x2cModel.blocks.bDigOut1);
    Constant_Bool_Init(&x2cModel.blocks.bDigOut2);
    Constant_Bool_Init(&x2cModel.blocks.bDigOut3);
    Constant_Bool_Init(&x2cModel.blocks.bDigOut4);
    Constant_Float32_Init(&x2cModel.blocks.bLED_off);
    Constant_Float32_Init(&x2cModel.blocks.bLED_on);
    Constant_Float32_Init(&x2cModel.blocks.bLED_on_off);
    Sin3Gen_Float32_Init(&x2cModel.blocks.bSin3Gen);
    Constant_Bool_Init(&x2cModel.blocks.bStart_Stop);
    AutoSwitch_Float32_Init(&x2cModel.blocks.sTriangleGenerator.bAutoSwitch0);
    I_Float32_Init(&x2cModel.blocks.sTriangleGenerator.bI);
    Constant_Float32_Init(&x2cModel.blocks.sTriangleGenerator.bInit);
    LoopBreaker_Float32_Init(&x2cModel.blocks.sTriangleGenerator.bLoopBreaker);
    Negation_Float32_Init(&x2cModel.blocks.sTriangleGenerator.bNegation);
    Constant_Float32_Init(&x2cModel.blocks.sTriangleGenerator.bSlope);
    Scope_Main_Init(&x2cScope);

    /* Initialize TableStruct tables                                                                                  */
    TableStruct->TFncTable = blockFunctionTable;
    TableStruct->TParamTable = parameterIdTable;
    TableStruct->inportParamTable = inportParamIdTable;
    TableStruct->outportParamTable = outportParamIdTable;
    TableStruct->maskParameterTable = maskParamIdTable;
}

/**********************************************************************************************************************/
/**                                            Run Block Update Functions                                            **/
/**********************************************************************************************************************/
void X2C_Update(void)
{
    X2C_Update_1();

}

/* X2C_Update for blocks with 1*Ts                                                                                    */
void X2C_Update_1(void)
{
    LoopBreaker_Float32_Update(&x2cModel.blocks.sTriangleGenerator.bLoopBreaker);
    Negation_Float32_Update(&x2cModel.blocks.sTriangleGenerator.bNegation);
    AutoSwitch_Float32_Update(&x2cModel.blocks.sTriangleGenerator.bAutoSwitch0);
    I_Float32_Update(&x2cModel.blocks.sTriangleGenerator.bI);
    Sin3Gen_Float32_Update(&x2cModel.blocks.bSin3Gen);
    AutoSwitch_Float32_Update(&x2cModel.blocks.bAutoSwitch1);
    AutoSwitch_Float32_Update(&x2cModel.blocks.bAutoSwitch2);
    AutoSwitch_Float32_Update(&x2cModel.blocks.bAutoSwitch3);
    Scope_Main_Update(&x2cScope);
}

