/**
*    @file        SwcActuators.c
*    @version     1.0.0
*
*    @brief       Integration Framework - Actuators SW component.
*
*    @details     Application handler for all Outputs in the system.
*
*   (c) Copyright 2006-2016 Freescale Semiconductor, Inc. 
*       Copyright 2017-2018 NXP
*   All Rights Reserved.
*   This file contains sample code only. It is not part of the production code deliverables.
*
*   @addtogroup SWCACTUATORS
*   @{
*
*/
/*==================================================================================================
*   Project              : AUTOSAR 4.3 MCAL
*   Platform             : PA
*   SW Version           : 1.0.0
*   Build Version        : MPC5777CXG_MCAL_1_0_0_RTM_ASR_REL_4_3_REV_0001_20190318
==================================================================================================*/
/*==================================================================================================
==================================================================================================*/

#ifdef __cplusplus
extern "C"{
#endif

/*==================================================================================================
                                         INCLUDE FILES
 1) system and project includes
 2) needed interfaces from external units
 3) internal and external interfaces from this unit
==================================================================================================*/
#include "typedefs.h"
#include "SwcActuators.h"
#include "SwcRte_Types.h"
#include "SwcRte.h"
#include "SwcRte_Cfg.h"

/*==================================================================================================
                                       SOURCE FILE VERSION INFORMATION
====================================================================================================*/
#define SWCACTUATORS_VENDOR_ID_C                          43

#define SWCACTUATORS_SW_MAJOR_VERSION_C                   1
#define SWCACTUATORS_SW_MINOR_VERSION_C                   0
#define SWCACTUATORS_SW_PATCH_VERSION_C                   0
/*==================================================================================================
*                                     FILE VERSION CHECKS
===================================================================================================*/
/* Check if SwcActuators.c source file and SwcActuators.h header file are of the same vendor */
#if (SWCACTUATORS_VENDOR_ID_C != SWCACTUATORS_VENDOR_ID)
    #error "SwcActuators.c and SwcActuators.h have different vendor ids"
#endif


/* Check if SwcActuators.c source file and SwcActuators.h header file are of the same Software version */
#if ((SWCACTUATORS_SW_MAJOR_VERSION_C != SWCACTUATORS_SW_MAJOR_VERSION) || \
     (SWCACTUATORS_SW_MINOR_VERSION_C != SWCACTUATORS_SW_MINOR_VERSION) || \
     (SWCACTUATORS_SW_PATCH_VERSION_C != SWCACTUATORS_SW_PATCH_VERSION) \
    )
    #error "Software Version Numbers of SwcActuators.c and SwcActuators.h are different"
#endif

/* Check if SwcActuators.c source file and SwcRte_Types.h header file are of the same vendor */
#if (SWCACTUATORS_VENDOR_ID_C != SWCRTE_TYPES_VENDOR_ID)
    #error "SwcActuators.c and SwcRte_Types.h have different vendor ids"
#endif


/* Check if SwcActuators.c source file and SwcRte_Types.h header file are of the same Software version */
#if ((SWCACTUATORS_SW_MAJOR_VERSION_C != SWCRTE_TYPES_SW_MAJOR_VERSION) || \
     (SWCACTUATORS_SW_MINOR_VERSION_C != SWCRTE_TYPES_SW_MINOR_VERSION) || \
     (SWCACTUATORS_SW_PATCH_VERSION_C != SWCRTE_TYPES_SW_PATCH_VERSION) \
    )
    #error "Software Version Numbers of SwcActuators.c and SwcRte_Types.h are different"
#endif


/* Check if SwcActuators.c source file and  SwcRte.h header file are of the same vendor */
#if (SWCACTUATORS_VENDOR_ID_C != SWCRTE_VENDOR_ID)
    #error "SwcActuators.c and SwcRte.h have different vendor ids"
#endif


/* Check if SwcActuators.c source file and SwcRte.h header file are of the same Software version */
#if ((SWCACTUATORS_SW_MAJOR_VERSION_C != SWCRTE_SW_MAJOR_VERSION) || \
     (SWCACTUATORS_SW_MINOR_VERSION_C != SWCRTE_SW_MINOR_VERSION) || \
     (SWCACTUATORS_SW_PATCH_VERSION_C != SWCRTE_SW_PATCH_VERSION) \
    )
    #error "Software Version Numbers of SwcActuators.c and SwcRte.h are different"
#endif


/* Check if SwcActuators.c source file and SwcRte_Cfg.h header file are of the same vendor */
#if (SWCACTUATORS_VENDOR_ID_C != SWCRTE_CFG_VENDOR_ID)
    #error "SwcActuators.c and SwcRte_Cfg.h have different vendor ids"
#endif


/* Check if SwcActuators.c source file and SwcRte_Cfg.h header file are of the same Software version */
#if ((SWCACTUATORS_SW_MAJOR_VERSION_C != SWCRTE_CFG_SW_MAJOR_VERSION) || \
     (SWCACTUATORS_SW_MINOR_VERSION_C != SWCRTE_CFG_SW_MINOR_VERSION) || \
     (SWCACTUATORS_SW_PATCH_VERSION_C != SWCRTE_CFG_SW_PATCH_VERSION) \
    )
    #error "Software Version Numbers of SwcActuators.c and SwcRte_Cfg.h are different"
#endif


/*==================================================================================================
                                        LOCAL MACROS
==================================================================================================*/
/* Shift needed in order to encode motor type */
#define SWCACTUATORS_MOTOR_TYPE_SHIFT      24   
    
/* Shift needed in order to encode motor direction */
#define SWCACTUATORS_MOTOR_DIRECTION_SHIFT 16 
       
/*==================================================================================================
                                      FILE VERSION CHECKS
==================================================================================================*/


/*==================================================================================================
                          LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
==================================================================================================*/


/*==================================================================================================
                                       LOCAL CONSTANTS
==================================================================================================*/

/*==================================================================================================
                                       LOCAL VARIABLES
==================================================================================================*/

/*==================================================================================================
                                       GLOBAL CONSTANTS
==================================================================================================*/


/*==================================================================================================
                                       GLOBAL VARIABLES
==================================================================================================*/

 
/*==================================================================================================
                                   LOCAL FUNCTION PROTOTYPES
==================================================================================================*/
static const SwcRte_GlobalConfigType * SwcActuators_ConfigPtr;

/*==================================================================================================
                                       LOCAL FUNCTIONS
==================================================================================================*/

/*==================================================================================================
                                       GLOBAL FUNCTIONS
==================================================================================================*/

/*================================================================================================*/
/**
@brief      SwcActuators_Init - main processing function for SWC Actuators
@details    This function is called periodically by the OS or by the main() function.

@param[in]  ConfigPtr    Pointer to configuration data (this data is shared between all SWC components)

@return     Returns the value of success
@retval     E_OK or E_NOT_OK

@pre None
@post None
*/
/*================================================================================================*/
void SwcActuators_Init( const SwcRte_GlobalConfigType * ConfigPtr )
{    
    SwcActuators_ConfigPtr = ConfigPtr;      
}


/*================================================================================================*/
/**
@brief      SwcActuators_MainRunnable - main processing function for Sensors
@details    This function is called periodically by the OS or by the main() function.
            On each entry, the Sensors runnable will process only the instance that is given as parameter.

@return     Returns the value of success
@retval     E_OK or E_NOT_OK

@pre None
@post None
*/
/*================================================================================================*/
void SwcActuators_MainRunnable()
{
    SwcRte_AppChannelIdType AppChannelId;
    volatile SwcRte_OutputValueType OutputValue;
    volatile SwcRte_AppInstanceIdType RteAppInstance;
    
#if (SWCRTE_LIGTHING_APP_ENABLED == SWCRTE_STD_ON)    
    SwcRte_AppLightingActuatorValueType  LightingOutputValue;
#endif
#if (SWCRTE_MOTORCTRL_APP_ENABLED == SWCRTE_STD_ON)    
    SwcRte_AppMotorCtrlActuatorValueType  MotorCtrlOutputValue;
#endif    
    SwcRte_AppInstanceType   AppType;

    for(RteAppInstance = 0; RteAppInstance < SWCRTE_MAX_APP_CTRL_INSTANCES; RteAppInstance++)
    {
        /*get the sensor channel id base on type of channel  */
        AppType = SwcActuators_ConfigPtr[RteAppInstance].eInstanceType;            

        /*process values differently depending on their type*/
        switch (AppType)
        {
#if (SWCRTE_LIGTHING_APP_ENABLED == SWCRTE_STD_ON)    
            case SWCRTE_LIGHTING:
            {
                /*read output info associated to that current instance*/
                SwcRte_ReadLightingOutputRequest(RteAppInstance,  &LightingOutputValue);

                /*for all available inputs */
                for (AppChannelId = 0; AppChannelId < SwcActuators_ConfigPtr[RteAppInstance].u8NoOutputChannel; AppChannelId++)
                {
                    if((SWCRTE_CHANNEL_SHORT2GND == LightingOutputValue.aChannelState[AppChannelId]) ||
                      (SWCRTE_CHANNEL_OPEN_LOAD == LightingOutputValue.aChannelState[AppChannelId]))
                    {
                        OutputValue = 0U;
                    }
                    else
                    {
                        OutputValue = LightingOutputValue.aLightingOutputIntesity[AppChannelId];
                    }    
                    
                    SwcRte_WriteOutputs(RteAppInstance, AppChannelId,  OutputValue);
                }
            }
            break;
#endif
      
#if (SWCRTE_MOTORCTRL_APP_ENABLED == SWCRTE_STD_ON)    
            case SWCRTE_MOTORCTRL:
            {    
                 SwcRte_ReadMotorCtrlOutputRequest(RteAppInstance - SWCRTE_MOTORCTRL_INSTANCE_OFFSET,  &MotorCtrlOutputValue);
                 OutputValue = (SwcRte_OutputValueType) (SwcActuators_ConfigPtr[RteAppInstance].pMotorCtrlOutputInstance->u8MotorType) << SWCACTUATORS_MOTOR_TYPE_SHIFT;
                 OutputValue |= (SwcRte_OutputValueType) MotorCtrlOutputValue.eMotorCtrlOutputDirection << SWCACTUATORS_MOTOR_DIRECTION_SHIFT;
                 OutputValue |= MotorCtrlOutputValue.u32MotorCtrlOutputDuty;
                 SwcRte_WriteOutputs(RteAppInstance, 0,  OutputValue);
            }
            break;
#endif

            default:
               /*DO NOTHING*/
            break;                
        }
    }
}


/*================================================================================================*/
#ifdef __cplusplus
}
#endif
/** @} */
