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

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

/* 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 = 0xDD3A5BF0u;

/**********************************************************************************************************************/
/**                                                       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 = 0.5                                                                                                    */
    x2cModel.blocks.bAmplitude.K = 16384;

    /* Block: AutoSwitch                                                                                              */
    /* Thresh_up = 0.0                                                                                                */
    /* Thresh_down = 0.0                                                                                              */
    x2cModel.blocks.bAutoSwitch.Thresh_up = 0;
    x2cModel.blocks.bAutoSwitch.Thresh_down = 0;
    x2cModel.blocks.bAutoSwitch.Status = &RamTable_int16[0];

    /* Block: AutoSwitch1                                                                                             */
    /* Thresh_up = 0.5                                                                                                */
    /* Thresh_down = 0.5                                                                                              */
    x2cModel.blocks.bAutoSwitch1.Thresh_up = 16384;
    x2cModel.blocks.bAutoSwitch1.Thresh_down = 16384;
    x2cModel.blocks.bAutoSwitch1.Status = &RamTable_int16[1];

    /* Block: Frequency                                                                                               */
    /* Value = 0.005                                                                                                  */
    x2cModel.blocks.bFrequency.K = 164;

    /* Block: Gain                                                                                                    */
    /* Gain = 2.0                                                                                                     */
    x2cModel.blocks.bGain.V = 16384;
    x2cModel.blocks.bGain.sfr = 13;

    /* Block: LD2_on                                                                                                  */
    /* Value = 0.0                                                                                                    */
    x2cModel.blocks.bLD2_on.K = 0;

    /* Block: LD3_on                                                                                                  */
    /* Value = 0.0                                                                                                    */
    x2cModel.blocks.bLD3_on.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 = 32767;

    /* Block: Oscillator/AutoSwitch                                                                                   */
    /* Thresh_up = 0.5                                                                                                */
    /* Thresh_down = -0.5                                                                                             */
    x2cModel.blocks.sOscillator.bAutoSwitch.Thresh_up = 16384;
    x2cModel.blocks.sOscillator.bAutoSwitch.Thresh_down = -16384;
    x2cModel.blocks.sOscillator.bAutoSwitch.Status = &RamTable_int16[2];

    /* Block: Oscillator/Constant                                                                                     */
    /* Value = 1.0                                                                                                    */
    x2cModel.blocks.sOscillator.bConstant.K = 32767;

    /* Block: Oscillator/Enable                                                                                       */
    /* Value = 1.0                                                                                                    */
    x2cModel.blocks.sOscillator.bEnable.K = 1;

    /* Block: Oscillator/Init                                                                                         */
    /* Value = 0.5                                                                                                    */
    x2cModel.blocks.sOscillator.bInit.K = 16384;

    /* Block: Oscillator/Integrator                                                                                   */
    /* Ki = 50.0                                                                                                      */
    /* ts_fact = 1.0                                                                                                  */
    x2cModel.blocks.sOscillator.bIntegrator.b0 = 164;
    x2cModel.blocks.sOscillator.bIntegrator.sfr = 15;
    x2cModel.blocks.sOscillator.bIntegrator.i_old = 0;
    x2cModel.blocks.sOscillator.bIntegrator.enable_old = 0;

    /* Block: Oscillator/LoopBreaker                                                                                  */

    /* Block: Oscillator/Negation                                                                                     */

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

    /* Block: SinGen                                                                                                  */
    /* fmax = 1000.0                                                                                                  */
    /* Offset = 0.0                                                                                                   */
    /* Phase = 0.0                                                                                                    */
    /* ts_fact = 1.0                                                                                                  */
    x2cModel.blocks.bSinGen.delta_phi = 6554;
    x2cModel.blocks.bSinGen.phase = 0;
    x2cModel.blocks.bSinGen.offset = 0;
    x2cModel.blocks.bSinGen.phi = 0;


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

    /******************************************************************************************************************/
    /**                                              Initialize Inports                                              **/
    /******************************************************************************************************************/
    x2cModel.inports.bB1_User = (int16)0;
    x2cModel.inports.bCPU_Temp = (float32)0;

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

    /* Block Amplitude                                                                                                */

    /* Block AutoSwitch                                                                                               */
    x2cModel.blocks.bAutoSwitch.In1 =
        &x2cModel.blocks.bLED_off.Out;
    x2cModel.blocks.bAutoSwitch.Switch =
        &x2cModel.blocks.bSinGen.u;
    x2cModel.blocks.bAutoSwitch.In3 =
        &x2cModel.blocks.bLED_on.Out;

    /* Block AutoSwitch1                                                                                              */
    x2cModel.blocks.bAutoSwitch1.In1 =
        &x2cModel.blocks.bGain.Out;
    x2cModel.blocks.bAutoSwitch1.Switch =
        &x2cModel.inports.bB1_User;
    x2cModel.blocks.bAutoSwitch1.In3 =
        &x2cModel.blocks.bFrequency.Out;

    /* Block Frequency                                                                                                */

    /* Block Gain                                                                                                     */
    x2cModel.blocks.bGain.In =
        &x2cModel.blocks.bFrequency.Out;

    /* Block LD2_on                                                                                                   */

    /* Block LD3_on                                                                                                   */

    /* Block LEDoff                                                                                                   */

    /* Block LEDon                                                                                                    */

    /* Block AutoSwitch                                                                                               */
    x2cModel.blocks.sOscillator.bAutoSwitch.In1 =
        &x2cModel.blocks.sOscillator.bNegation.Out;
    x2cModel.blocks.sOscillator.bAutoSwitch.Switch =
        &x2cModel.blocks.sOscillator.bIntegrator.Out;
    x2cModel.blocks.sOscillator.bAutoSwitch.In3 =
        &x2cModel.blocks.sOscillator.bConstant.Out;

    /* Block Constant                                                                                                 */

    /* Block Enable                                                                                                   */

    /* Block Init                                                                                                     */

    /* Block Integrator                                                                                               */
    x2cModel.blocks.sOscillator.bIntegrator.In =
        &x2cModel.blocks.sOscillator.bLoopBreaker.Out;
    x2cModel.blocks.sOscillator.bIntegrator.I0 =
        &x2cModel.blocks.sOscillator.bInit.Out;
    x2cModel.blocks.sOscillator.bIntegrator.Enable =
        &x2cModel.blocks.sOscillator.bEnable.Out;

    /* Block LoopBreaker                                                                                              */
    x2cModel.blocks.sOscillator.bLoopBreaker.In =
        &x2cModel.blocks.sOscillator.bAutoSwitch.Out;

    /* Block Negation                                                                                                 */
    x2cModel.blocks.sOscillator.bNegation.In =
        &x2cModel.blocks.sOscillator.bConstant.Out;

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

    /* Block SinGen                                                                                                   */
    x2cModel.blocks.bSinGen.A =
        &x2cModel.blocks.bAmplitude.Out;
    x2cModel.blocks.bSinGen.f =
        &x2cModel.blocks.bAutoSwitch1.Out;

    /******************************************************************************************************************/
    /**                                                 Link Outports                                                **/
    /******************************************************************************************************************/
    x2cModel.outports.bLD1 =
        &x2cModel.blocks.bAutoSwitch.Out;
    x2cModel.outports.bLD2 =
        &x2cModel.blocks.bLD2_on.Out;
    x2cModel.outports.bLD3 =
        &x2cModel.blocks.bLD3_on.Out;
    x2cModel.outports.bOsc_out =
        &x2cModel.blocks.sOscillator.bAutoSwitch.Out;

    /******************************************************************************************************************/
    /**                                           Run Block Init Functions                                           **/
    /******************************************************************************************************************/
    Constant_FiP16_Init(&x2cModel.blocks.bAmplitude);
    AutoSwitch_FiP16_Init(&x2cModel.blocks.bAutoSwitch);
    AutoSwitch_FiP16_Init(&x2cModel.blocks.bAutoSwitch1);
    Constant_FiP16_Init(&x2cModel.blocks.bFrequency);
    Gain_FiP16_Init(&x2cModel.blocks.bGain);
    Constant_Bool_Init(&x2cModel.blocks.bLD2_on);
    Constant_Bool_Init(&x2cModel.blocks.bLD3_on);
    Constant_FiP16_Init(&x2cModel.blocks.bLED_off);
    Constant_FiP16_Init(&x2cModel.blocks.bLED_on);
    AutoSwitch_FiP16_Init(&x2cModel.blocks.sOscillator.bAutoSwitch);
    Constant_FiP16_Init(&x2cModel.blocks.sOscillator.bConstant);
    Constant_Bool_Init(&x2cModel.blocks.sOscillator.bEnable);
    Constant_FiP16_Init(&x2cModel.blocks.sOscillator.bInit);
    I_FiP16_Init(&x2cModel.blocks.sOscillator.bIntegrator);
    LoopBreaker_FiP16_Init(&x2cModel.blocks.sOscillator.bLoopBreaker);
    Negation_FiP16_Init(&x2cModel.blocks.sOscillator.bNegation);
    Sin3Gen_FiP16_Init(&x2cModel.blocks.bSin3Gen);
    SinGen_FiP16_Init(&x2cModel.blocks.bSinGen);
    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_FiP16_Update(&x2cModel.blocks.sOscillator.bLoopBreaker);
    Gain_FiP16_Update(&x2cModel.blocks.bGain);
    AutoSwitch_FiP16_Update(&x2cModel.blocks.bAutoSwitch1);
    Negation_FiP16_Update(&x2cModel.blocks.sOscillator.bNegation);
    SinGen_FiP16_Update(&x2cModel.blocks.bSinGen);
    AutoSwitch_FiP16_Update(&x2cModel.blocks.bAutoSwitch);
    Sin3Gen_FiP16_Update(&x2cModel.blocks.bSin3Gen);
    I_FiP16_Update(&x2cModel.blocks.sOscillator.bIntegrator);
    AutoSwitch_FiP16_Update(&x2cModel.blocks.sOscillator.bAutoSwitch);
    Scope_Main_Update(&x2cScope);
}

