#include "LimitStatorVoltage.h"
#include "PDSFC.h"

SFIO_BlockExecutionResult LimitStatorVoltage(const void *pInBuff, void *pOutBuff, void *pVariables, SFIO_BlockCommand eCmd)
{
    /* input signals */
    typedef struct
    {
        short             u_s[2];
        short             U_max;
    } InStruct;

    /* output signals */
    typedef struct
    {
        long			  t1,t2;
        short             u_slim[2];
    } OutStruct;

    /* Uncomment the following structure and place your block-related 
       persistent variables there. Don't forget to change SIZEOF_StateStruct 
       below from 0 to sizeof(StateStruct). */ 
    /*typedef struct
    {
        short    x;
    }StateStruct;
    */

    /* structure lengths and sizes */
    enum StructSizes
    {
        LENGTHOF_InStruct  = 6,        /* real communicated length of input data items */
        LENGTHOF_OutStruct = 12,        /* real communicated length of output data items */
        SIZEOF_StateStruct = 0         /* size to be allocated, use sizeof(StateStruct) */
    };

    /* local variables */
    SFIO_BlockExecutionResult eResult;
    SFIO_BlockRegistration    *pReg;
    InStruct    *pIn = (InStruct*) pInBuff;
    OutStruct   *pOut = (OutStruct*) pOutBuff;
    //StateStruct *pState = (StateStruct *)pVariables;

    /* default result code */
    eResult = SFIO_EXEC_OK;

    switch(eCmd)
    {
        /* provide block registration data */
        case SFIO_CMD_REGISTER:
            pReg = (SFIO_BlockRegistration*) pOutBuff;
            pReg->wInputsCount = LENGTHOF_InStruct;          /* input data length in bytes */
            pReg->wOutputsCount = LENGTHOF_OutStruct;        /* output data length in bytes */
            pReg->wVariablesCount = SIZEOF_StateStruct;      /* state variables data size */
            //strcpy(pReg->pDescriptor,"LimitStatorVoltage");        /* block descriptor, customize if neccessary */
            break;

        /* state variables initialization */
        case SFIO_CMD_INIT:
            //pState->x = 0;
            break;

        /* compute function */
        case SFIO_CMD_EXECUTE:
            /* this macro gives an opportunity to break into this application
               when the "break" button is pressed in the Matlab/Simulink window */
            SFIO_Breakpoint();

            /**** place your computational code here ****/
			
			u_S.alpha = pIn->u_s[0];
			u_S.beta = pIn->u_s[1];
			
			SFIO_TimerStart();
			limit_stator_voltage(&u_S, pIn->U_max);		//call the C function 'limit_stator_voltage' with input parameters and save return value to the output parameter
			SFIO_TimerStop(&pOut->t1);
			
			SFIO_TimerStart();
			a_limit_stator_voltage(&u_S, pIn->U_max);	//call the assembler function 'a_limit_stator_voltage' with input parameter and save return value to the output parameter
			SFIO_TimerStop(&pOut->t2);
			
			pOut->u_slim[0] = u_S.alpha;
			pOut->u_slim[1] = u_S.beta;

					
            /**** end of your code ****/

            break;

        /* unknown command requested */
        default:
            eResult = SFIO_EXEC_UNKNOWN_COMMAND;
    }

    return(eResult);
}