/**
 * @file	   processBoard.c
 *
 * @copyright  Nehemis SARL reserves all rights even in the event of industrial
 *             property rights. We reserve all rights of disposal such as
 *             copying and passing on to third parties.
 *
 * @brief	   This file provides the firmware for running the Production unit board.
 *
 */

/******************************************************************************
 * Include Header Files
 ******************************************************************************/
#include "processBoard.h"
#include "flowmeter.h"
#include "pressureSensor.h"
#include "od_entries.h"
#include "co_odaccess.h"
/******************************************************************************
 * Type  declarations
 ******************************************************************************/

/******************************************************************************
 * Macro Constant Declarations
 ******************************************************************************/
#define  PROCESS_BOARD_VALVE_POS_INDEX				0x6002
#define  PROCESS_BOARD_VALVE_FLOW_INDEX				0x6006
#define  PROCESS_BOARD_VALVE_PRESSURE_INDEX			0x6001

#define  PROCESS_BOARD_VALVE_SUB_INDEX				0x0

#define  PROCESS_BOARD_FLOWMETER_ADC_MODULE			&hadc1
#define  PROCESS_BOARD_PRESSURE_SENSOR_ADC_MODULE	&hadc1
/******************************************************************************
 * Global variables
 ******************************************************************************/
static volatile uint8 motorCmd_gu8;
static volatile float32 flow_gf32 		= 0.0f;
static volatile float32 pressure_gf32 	= 0.0f;

FlowmeterMain_st flowmeterFM1_gst;
FlowmeterMain_st flowmeterFM2_gst;
FlowmeterMain_st flowmeterFM3_gst;
FlowmeterMain_st flowmeterFM4_gst;

PressureSensorMain_st pressureSensorPS1_gst;
PressureSensorMain_st pressureSensorPS2_gst;
PressureSensorMain_st pressureSensorPS3_gst;
PressureSensorMain_st pressureSensorPS4_gst;

ProcessBoardCondDataSt condBoard1_gst;
ProcessBoardCondDataSt condBoard2_gst;
ProcessBoardCondDataSt condBoard3_gst;

ProcessBoardMvDataSt mvBoard1_gst;
ProcessBoardMvDataSt mvBoard2_gst;
ProcessBoardMvDataSt mvBoard3_gst;
ProcessBoardMvDataSt mvBoard4_gst;
ProcessBoardMvDataSt mvBoard5_gst;
ProcessBoardMvDataSt mvBoard6_gst;
ProcessBoardMvDataSt mvBoard7_gst;
ProcessBoardMvDataSt mvBoard8_gst;

/******************************************************************************
 * Private Function Declarations
 ******************************************************************************/
static void ProcessBoardTriggerMvPosCtrl(uint8 motorId_u8, uint8 posData_u8);
static void ProcessBoardTriggerMvCartCtrl(uint8 motorId_u8, uint8 flowRate_u8, uint8 pressure_u8);
static void ProcessBoardReadCondDataIN(void);
static void ProcessBoardWriteCondDataOUT(void);
static void ProcessBoardReadMvDataIN(void);
static void ProcessBoardFlowmeterDataOUT(FlowmeterMain_st flowmeter_st);
static void ProcessBoardPressureSensorDataOUT(PressureSensorMain_st pressureSensor_st);

/******************************************************************************
 * Extern Function Declarations
 ******************************************************************************/
void ProcessBoardInit(void)
{
	motorCmd_gu8 = 0u;

	FlowmeterInit(&flowmeterFM1_gst, PROCESS_BOARD_FLOWMETER_ADC_MODULE, FLOWMETER_FM1_ADC_CHANNEL);
	PressureSensorInit(&pressureSensorPS1_gst, PROCESS_BOARD_PRESSURE_SENSOR_ADC_MODULE, PRESSURE_SENSOR_PS1_ADC_CHANNEL);

	FlowmeterInit(&flowmeterFM2_gst, PROCESS_BOARD_FLOWMETER_ADC_MODULE, FLOWMETER_FM2_ADC_CHANNEL);
	PressureSensorInit(&pressureSensorPS2_gst, PROCESS_BOARD_PRESSURE_SENSOR_ADC_MODULE, PRESSURE_SENSOR_PS2_ADC_CHANNEL);

	FlowmeterInit(&flowmeterFM3_gst, PROCESS_BOARD_FLOWMETER_ADC_MODULE, FLOWMETER_FM3_ADC_CHANNEL);
	PressureSensorInit(&pressureSensorPS3_gst, PROCESS_BOARD_PRESSURE_SENSOR_ADC_MODULE, PRESSURE_SENSOR_PS3_ADC_CHANNEL);

	FlowmeterInit(&flowmeterFM4_gst, PROCESS_BOARD_FLOWMETER_ADC_MODULE, FLOWMETER_FM4_ADC_CHANNEL);
	PressureSensorInit(&pressureSensorPS4_gst, PROCESS_BOARD_PRESSURE_SENSOR_ADC_MODULE, PRESSURE_SENSOR_PS4_ADC_CHANNEL);

}


void ProcessBoardRun(void)
{
	uint8 id_u8;
	uint8 motorCmd_u8;
	uint8 posData_u8;
	uint8 flowRate_u8;
	uint8 pressure_u8;

	coOdGetObj_u8(OD_ENTRY_PU_MOTOR_CTRL_INDEX, OD_ENTRY_PU_MOTOR_CTRL_ID_SUB_INDEX, &id_u8);
	coOdGetObj_u8(OD_ENTRY_PU_MOTOR_CTRL_INDEX, OD_ENTRY_PU_MOTOR_CTRL_CMD_SUB_INDEX, &motorCmd_u8);
	coOdGetObj_u8(OD_ENTRY_PU_MOTOR_CTRL_INDEX, OD_ENTRY_PU_MOTOR_CTRL_POS_SUB_INDEX, &posData_u8);
	coOdGetObj_u8(OD_ENTRY_PU_MOTOR_CTRL_INDEX, OD_ENTRY_PU_MOTOR_CTRL_PRESSURE_SUB_INDEX, &flowRate_u8);
	coOdGetObj_u8(OD_ENTRY_PU_MOTOR_CTRL_INDEX, OD_ENTRY_PU_MOTOR_CTRL_FLOW_SUB_INDEX, &pressure_u8);

	if(motorCmd_u8 == 1u)
	{
		ProcessBoardTriggerMvPosCtrl(11, posData_u8);
	}
	else
	{
		ProcessBoardTriggerMvCartCtrl(id_u8, flowRate_u8, pressure_u8);
	}

	FlowmeterGetFlow(&flowmeterFM4_gst);
	PressureSensorGetVal(&pressureSensorPS1_gst);
	ProcessBoardFlowmeterDataOUT(flowmeterFM4_gst);
	ProcessBoardPressureSensorDataOUT(pressureSensorPS1_gst);
	ProcessBoardReadCondDataIN();
	ProcessBoardWriteCondDataOUT();
}


/******************************************************************************
 * Private function definitions
 ******************************************************************************/
static void ProcessBoardTriggerMvPosCtrl(uint8 motorId_u8, uint8 posData_u8)
{

}


static void ProcessBoardTriggerMvCartCtrl(uint8 motorId_u8, uint8 flowRate_u8, uint8 pressure_u8)
{

}


static void ProcessBoardReadCondDataIN(void)
{
	coOdGetObj_u32(OD_ENTRY_PU_COND_DATA_IN_INDEX, OD_ENTRY_PU_COND1_DATA_IN_SUB_INDEX, &condBoard1_gst.conductivityData_u32);
	coOdGetObj_u32(OD_ENTRY_PU_COND_DATA_IN_INDEX, OD_ENTRY_PU_TEMP1_DATA_IN_SUB_INDEX, &condBoard1_gst.temperature_u32);

	coOdGetObj_u32(OD_ENTRY_PU_COND_DATA_IN_INDEX, OD_ENTRY_PU_COND1_DATA_IN_SUB_INDEX, &condBoard2_gst.conductivityData_u32);
	coOdGetObj_u32(OD_ENTRY_PU_COND_DATA_IN_INDEX, OD_ENTRY_PU_TEMP2_DATA_IN_SUB_INDEX, &condBoard2_gst.temperature_u32);

	coOdGetObj_u32(OD_ENTRY_PU_COND_DATA_IN_INDEX, OD_ENTRY_PU_COND1_DATA_IN_SUB_INDEX, &condBoard3_gst.conductivityData_u32);
	coOdGetObj_u32(OD_ENTRY_PU_COND_DATA_IN_INDEX, OD_ENTRY_PU_TEMP3_DATA_IN_SUB_INDEX, &condBoard3_gst.temperature_u32);
}


static void ProcessBoardWriteCondDataOUT(void)
{
	coOdPutObj_u32(OD_ENTRY_PU_COND_DATA_OUT_INDEX, OD_ENTRY_PU_COND1_DATA_IN_SUB_INDEX, condBoard1_gst.conductivityData_u32);
	coOdPutObj_u32(OD_ENTRY_PU_COND_DATA_OUT_INDEX, OD_ENTRY_PU_TEMP1_DATA_IN_SUB_INDEX, condBoard1_gst.temperature_u32);

	coOdPutObj_u32(OD_ENTRY_PU_COND_DATA_OUT_INDEX, OD_ENTRY_PU_COND2_DATA_IN_SUB_INDEX, condBoard2_gst.conductivityData_u32);
	coOdPutObj_u32(OD_ENTRY_PU_COND_DATA_OUT_INDEX, OD_ENTRY_PU_TEMP2_DATA_IN_SUB_INDEX, condBoard2_gst.temperature_u32);

	coOdPutObj_u32(OD_ENTRY_PU_COND_DATA_OUT_INDEX, OD_ENTRY_PU_COND3_DATA_IN_SUB_INDEX, condBoard3_gst.conductivityData_u32);
	coOdPutObj_u32(OD_ENTRY_PU_COND_DATA_OUT_INDEX, OD_ENTRY_PU_TEMP3_DATA_IN_SUB_INDEX, condBoard3_gst.temperature_u32);
}


static void ProcessBoardFlowmeterDataOUT(FlowmeterMain_st flowmeter_st)
{
	coOdPutObj_r32(OD_ENTRY_PU_FLOWMETER_DATA_OUT_INDEX, OD_ENTRY_PU_FLOWMETER1_DATA_OUT_SUB_INDEX, flowmeterFM1_gst.rawQ_f32);
	coOdPutObj_r32(OD_ENTRY_PU_FLOWMETER_DATA_OUT_INDEX, OD_ENTRY_PU_FLOWMETER1_DATA_OUT_SUB_INDEX, flowmeterFM2_gst.rawQ_f32);
	coOdPutObj_r32(OD_ENTRY_PU_FLOWMETER_DATA_OUT_INDEX, OD_ENTRY_PU_FLOWMETER1_DATA_OUT_SUB_INDEX, flowmeterFM3_gst.rawQ_f32);
	coOdPutObj_r32(OD_ENTRY_PU_FLOWMETER_DATA_OUT_INDEX, OD_ENTRY_PU_FLOWMETER1_DATA_OUT_SUB_INDEX, flowmeterFM4_gst.rawQ_f32);
}


static void ProcessBoardPressureSensorDataOUT(PressureSensorMain_st pressureSensor_st)
{
	coOdPutObj_r32(OD_ENTRY_PU_PRESSURE_DATA_OUT_INDEX, OD_ENTRY_PU_PRESSURE1_DATA_OUT_SUB_INDEX, pressureSensorPS1_gst.rawT_f32);
	coOdPutObj_r32(OD_ENTRY_PU_PRESSURE_DATA_OUT_INDEX, OD_ENTRY_PU_PRESSURE1_DATA_OUT_SUB_INDEX, pressureSensorPS2_gst.rawT_f32);
	coOdPutObj_r32(OD_ENTRY_PU_PRESSURE_DATA_OUT_INDEX, OD_ENTRY_PU_PRESSURE1_DATA_OUT_SUB_INDEX, pressureSensorPS3_gst.rawT_f32);
	coOdPutObj_r32(OD_ENTRY_PU_PRESSURE_DATA_OUT_INDEX, OD_ENTRY_PU_PRESSURE1_DATA_OUT_SUB_INDEX, pressureSensorPS4_gst.rawT_f32);
}


static void ProcessBoardReadMvDataIN(void)
{
	/* To be implemented */
}
