/*
 * wt_sablona.c
 *
 * Code generation for model "wt_sablona".
 *
 * Model version              : 1.45
 * Simulink Coder version : 8.3 (R2012b) 20-Jul-2012
 * C source code generated on : Wed May 13 16:14:33 2015
 *
 * Target selection: rtwin.tlc
 * Note: GRT includes extra infrastructure and instrumentation for prototyping
 * Embedded hardware selection: Generic->32-bit x86 compatible
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */
#include "wt_sablona.h"
#include "wt_sablona_private.h"
#include "wt_sablona_dt.h"

/* options for Real-Time Windows Target board 0 */
static double RTWinBoardOptions0[] = {
  1.0,
};

/* list of Real-Time Windows Target timers */
const int RTWinTimerCount = 1;
const double RTWinTimers[2] = {
  0.0001, 0.0,
};

/* list of Real-Time Windows Target boards */
const int RTWinBoardCount = 1;
RTWINBOARD RTWinBoards[1] = {
  { "Humusoft/MF624", 4294967295U, 1, RTWinBoardOptions0 },
};

/* Block signals (auto storage) */
BlockIO_wt_sablona wt_sablona_B;

/* Block states (auto storage) */
D_Work_wt_sablona wt_sablona_DWork;

/* Real-time model */
RT_MODEL_wt_sablona wt_sablona_M_;
RT_MODEL_wt_sablona *const wt_sablona_M = &wt_sablona_M_;

/* Model output function */
void wt_sablona_output(void)
{
  /* local block i/o variables */
  real_T rtb_EncoderInput;

  /* ManualSwitch: '<Root>/Manual Switch' incorporates:
   *  Constant: '<Root>/Constant1'
   *  Constant: '<Root>/Constant2'
   */
  if (wt_sablona_P.ManualSwitch_CurrentSetting == 1) {
    rtb_EncoderInput = wt_sablona_P.Constant1_Value;
  } else {
    rtb_EncoderInput = wt_sablona_P.Constant2_Value;
  }

  /* End of ManualSwitch: '<Root>/Manual Switch' */

  /* Gain: '<S1>/Slider Gain' incorporates:
   *  Constant: '<Root>/Constant3'
   */
  wt_sablona_B.SliderGain = wt_sablona_P.SliderGain_Gain *
    wt_sablona_P.Constant3_Value;

  /* S-Function Block: <Root>/Enable */
  {
    RTBIO_DriverIO(0, DIGITALOUTPUT, IOWRITE, 1, &wt_sablona_P.Enable_Channels,
                   &wt_sablona_P.Constant_Value, &wt_sablona_P.Enable_BitMode);
  }

  /* S-Function Block: <Root>/DIR */
  {
    RTBIO_DriverIO(0, DIGITALOUTPUT, IOWRITE, 1, &wt_sablona_P.DIR_Channels,
                   &rtb_EncoderInput, &wt_sablona_P.DIR_BitMode);
  }

  /* S-Function Block: <Root>/Frequency Output */
  {
    {
      RTBIO_DriverIO(0, FREQUENCYOUTPUT, IOWRITE, 1,
                     &wt_sablona_P.FrequencyOutput_Channels,
                     &wt_sablona_B.SliderGain, &wt_sablona_P.Constant4_Value);
    }
  }

  /* S-Function Block: <Root>/Encoder Input */
  {
    ENCODERINPARM parm;
    parm.quad = (QUADMODE) 0;
    parm.index = (INDEXPULSE) 2;
    parm.infilter = wt_sablona_P.EncoderInput_InputFilter;
    RTBIO_DriverIO(0, ENCODERINPUT, IOREAD, 1,
                   &wt_sablona_P.EncoderInput_Channels, &rtb_EncoderInput, &parm);
  }

  /* Gain: '<Root>/Gain' */
  wt_sablona_B.Gain = wt_sablona_P.Gain_Gain * rtb_EncoderInput;

  /* Gain: '<Root>/Gain1' */
  wt_sablona_B.Gain1 = wt_sablona_P.Gain1_Gain * wt_sablona_B.Gain;
}

/* Model update function */
void wt_sablona_update(void)
{
  /* Update absolute time for base rate */
  /* The "clockTick0" counts the number of times the code of this task has
   * been executed. The absolute time is the multiplication of "clockTick0"
   * and "Timing.stepSize0". Size of "clockTick0" ensures timer will not
   * overflow during the application lifespan selected.
   * Timer of this task consists of two 32 bit unsigned integers.
   * The two integers represent the low bits Timing.clockTick0 and the high bits
   * Timing.clockTickH0. When the low bit overflows to 0, the high bits increment.
   */
  if (!(++wt_sablona_M->Timing.clockTick0)) {
    ++wt_sablona_M->Timing.clockTickH0;
  }

  wt_sablona_M->Timing.t[0] = wt_sablona_M->Timing.clockTick0 *
    wt_sablona_M->Timing.stepSize0 + wt_sablona_M->Timing.clockTickH0 *
    wt_sablona_M->Timing.stepSize0 * 4294967296.0;
}

/* Model initialize function */
void wt_sablona_initialize(void)
{
  /* S-Function Block: <Root>/Enable */

  /* no initial value should be set */

  /* S-Function Block: <Root>/DIR */

  /* no initial value should be set */

  /* S-Function Block: <Root>/Frequency Output */
  {
    {
      RTBIO_DriverIO(0, FREQUENCYOUTPUT, IOWRITE, 1,
                     &wt_sablona_P.FrequencyOutput_Channels,
                     &wt_sablona_P.FrequencyOutput_Frequency,
                     &wt_sablona_P.FrequencyOutput_Duty);
    }
  }

  /* S-Function Block: <Root>/Encoder Input */
  {
    ENCODERINPARM parm;
    parm.quad = (QUADMODE) 0;
    parm.index = (INDEXPULSE) 2;
    parm.infilter = wt_sablona_P.EncoderInput_InputFilter;
    RTBIO_DriverIO(0, ENCODERINPUT, IORESET, 1,
                   &wt_sablona_P.EncoderInput_Channels, NULL, &parm);
  }
}

/* Model terminate function */
void wt_sablona_terminate(void)
{
  /* S-Function Block: <Root>/Enable */
  {
    RTBIO_DriverIO(0, DIGITALOUTPUT, IOWRITE, 1, &wt_sablona_P.Enable_Channels,
                   &wt_sablona_P.Enable_FinalValue, &wt_sablona_P.Enable_BitMode);
  }

  /* S-Function Block: <Root>/DIR */

  /* no final value should be set */

  /* S-Function Block: <Root>/Frequency Output */
  {
    {
      RTBIO_DriverIO(0, FREQUENCYOUTPUT, IOWRITE, 1,
                     &wt_sablona_P.FrequencyOutput_Channels,
                     &wt_sablona_P.FrequencyOutput_FrequencyFinalValue,
                     &wt_sablona_P.FrequencyOutput_DutyFinalValue);
    }
  }
}

/*========================================================================*
 * Start of Classic call interface                                        *
 *========================================================================*/
void MdlOutputs(int_T tid)
{
  wt_sablona_output();

  /* tid is required for a uniform function interface.
   * Argument tid is not used in the function. */
  UNUSED_PARAMETER(tid);
}

void MdlUpdate(int_T tid)
{
  wt_sablona_update();

  /* tid is required for a uniform function interface.
   * Argument tid is not used in the function. */
  UNUSED_PARAMETER(tid);
}

void MdlInitializeSizes(void)
{
}

void MdlInitializeSampleTimes(void)
{
}

void MdlInitialize(void)
{
}

void MdlStart(void)
{
  wt_sablona_initialize();
}

void MdlTerminate(void)
{
  wt_sablona_terminate();
}

RT_MODEL_wt_sablona *wt_sablona(void)
{
  /* Registration code */

  /* initialize non-finites */
  rt_InitInfAndNaN(sizeof(real_T));

  /* non-finite (run-time) assignments */
  wt_sablona_P.EncoderInput_InputFilter = rtInf;

  /* initialize real-time model */
  (void) memset((void *)wt_sablona_M, 0,
                sizeof(RT_MODEL_wt_sablona));

  /* Initialize timing info */
  {
    int_T *mdlTsMap = wt_sablona_M->Timing.sampleTimeTaskIDArray;
    mdlTsMap[0] = 0;
    wt_sablona_M->Timing.sampleTimeTaskIDPtr = (&mdlTsMap[0]);
    wt_sablona_M->Timing.sampleTimes = (&wt_sablona_M->Timing.sampleTimesArray[0]);
    wt_sablona_M->Timing.offsetTimes = (&wt_sablona_M->Timing.offsetTimesArray[0]);

    /* task periods */
    wt_sablona_M->Timing.sampleTimes[0] = (0.0001);

    /* task offsets */
    wt_sablona_M->Timing.offsetTimes[0] = (0.0);
  }

  rtmSetTPtr(wt_sablona_M, &wt_sablona_M->Timing.tArray[0]);

  {
    int_T *mdlSampleHits = wt_sablona_M->Timing.sampleHitArray;
    mdlSampleHits[0] = 1;
    wt_sablona_M->Timing.sampleHits = (&mdlSampleHits[0]);
  }

  rtmSetTFinal(wt_sablona_M, 40.0);
  wt_sablona_M->Timing.stepSize0 = 0.0001;

  /* External mode info */
  wt_sablona_M->Sizes.checksums[0] = (2315612762U);
  wt_sablona_M->Sizes.checksums[1] = (4157257973U);
  wt_sablona_M->Sizes.checksums[2] = (1908928266U);
  wt_sablona_M->Sizes.checksums[3] = (2668233982U);

  {
    static const sysRanDType rtAlwaysEnabled = SUBSYS_RAN_BC_ENABLE;
    static RTWExtModeInfo rt_ExtModeInfo;
    static const sysRanDType *systemRan[1];
    wt_sablona_M->extModeInfo = (&rt_ExtModeInfo);
    rteiSetSubSystemActiveVectorAddresses(&rt_ExtModeInfo, systemRan);
    systemRan[0] = &rtAlwaysEnabled;
    rteiSetModelMappingInfoPtr(wt_sablona_M->extModeInfo,
      &wt_sablona_M->SpecialInfo.mappingInfo);
    rteiSetChecksumsPtr(wt_sablona_M->extModeInfo, wt_sablona_M->Sizes.checksums);
    rteiSetTPtr(wt_sablona_M->extModeInfo, rtmGetTPtr(wt_sablona_M));
  }

  wt_sablona_M->solverInfoPtr = (&wt_sablona_M->solverInfo);
  wt_sablona_M->Timing.stepSize = (0.0001);
  rtsiSetFixedStepSize(&wt_sablona_M->solverInfo, 0.0001);
  rtsiSetSolverMode(&wt_sablona_M->solverInfo, SOLVER_MODE_SINGLETASKING);

  /* block I/O */
  wt_sablona_M->ModelData.blockIO = ((void *) &wt_sablona_B);
  (void) memset(((void *) &wt_sablona_B), 0,
                sizeof(BlockIO_wt_sablona));

  /* parameters */
  wt_sablona_M->ModelData.defaultParam = ((real_T *)&wt_sablona_P);

  /* states (dwork) */
  wt_sablona_M->Work.dwork = ((void *) &wt_sablona_DWork);
  (void) memset((void *)&wt_sablona_DWork, 0,
                sizeof(D_Work_wt_sablona));

  /* data type transition information */
  {
    static DataTypeTransInfo dtInfo;
    (void) memset((char_T *) &dtInfo, 0,
                  sizeof(dtInfo));
    wt_sablona_M->SpecialInfo.mappingInfo = (&dtInfo);
    dtInfo.numDataTypes = 14;
    dtInfo.dataTypeSizes = &rtDataTypeSizes[0];
    dtInfo.dataTypeNames = &rtDataTypeNames[0];

    /* Block I/O transition table */
    dtInfo.B = &rtBTransTable;

    /* Parameters transition table */
    dtInfo.P = &rtPTransTable;
  }

  /* Initialize Sizes */
  wt_sablona_M->Sizes.numContStates = (0);/* Number of continuous states */
  wt_sablona_M->Sizes.numY = (0);      /* Number of model outputs */
  wt_sablona_M->Sizes.numU = (0);      /* Number of model inputs */
  wt_sablona_M->Sizes.sysDirFeedThru = (0);/* The model is not direct feedthrough */
  wt_sablona_M->Sizes.numSampTimes = (1);/* Number of sample times */
  wt_sablona_M->Sizes.numBlocks = (18);/* Number of blocks */
  wt_sablona_M->Sizes.numBlockIO = (3);/* Number of block outputs */
  wt_sablona_M->Sizes.numBlockPrms = (29);/* Sum of parameter "widths" */
  return wt_sablona_M;
}

/*========================================================================*
 * End of Classic call interface                                          *
 *========================================================================*/
