/**
 * @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 "grundfos.h"
#include "od_entries.h"
#include "nms_can.h"
#include "analogMeasurement.h"
#include "hal_system.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 PU_CANOPEN_SLAVE_LINE						0u
#define PU_CANOPEN_MASTER_LINE						1u


#define PU_PUMP_SPEED_CHANGE_INTERVAL               900000uLL
#define PU_PUMP_MAX_SPEED                           10u
#define PU_PUMP_MIN_SPEED                           0u

#define PU_PMP_ENABLE								1u
#define PU_PMP_DISABLE								0u
/******************************************************************************
 * 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;

GrundfosMain_st grundfosPMP_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(float32 rawQ_f32);
static void ProcessBoardPressureSensorDataOUT(float32 rawT_f32);
static void ProcessBoardPumpSpeedDataOUT(uint32 pmpSpeed_u32);
static void ProcessBoardGrundfosPumpHandler(void);
/******************************************************************************
 * Extern Function Declarations
 ******************************************************************************/
void ProcessBoardInit(void)
{
	HalSystemInit();
	AnalogMeasurementInit();
	motorCmd_gu8 = 0u;

	/* Initializing the structures (Pressure sensor, Flow
	 * meter, and Grundfos) with their respective ADC channel */
	pressureSensorPS1_gst.channel_u32 	= PS1_ADC_CHANNEL;
	pressureSensorPS2_gst.channel_u32 	= PS2_ADC_CHANNEL;
	pressureSensorPS3_gst.channel_u32 	= PS3_ADC_CHANNEL;
	pressureSensorPS4_gst.channel_u32 	= PS4_ADC_CHANNEL;

	grundfosPMP_gst.channel_u32 		= PMP_ADC_CHANNEL;

	flowmeterFM1_gst.channel_u32 		= FM1_ADC_CHANNEL;
	flowmeterFM2_gst.channel_u32 		= FM2_ADC_CHANNEL;
	flowmeterFM3_gst.channel_u32 		= FM3_ADC_CHANNEL;
	flowmeterFM4_gst.channel_u32 		= FM4_ADC_CHANNEL;

	FlowmeterInit(&flowmeterFM1_gst);
	FlowmeterInit(&flowmeterFM2_gst);
	FlowmeterInit(&flowmeterFM3_gst);
	FlowmeterInit(&flowmeterFM4_gst);
}


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

	AnalogMesaurementRun();
	/* DEBUG code to be replaced. Logic to be changed based on state machine
	 * implementation. */
	if(motorCmd_u8 == 1u)
	{
		ProcessBoardTriggerMvPosCtrl(11, posData_u8);		/* DEBUG */
	}
	else
	{
		ProcessBoardTriggerMvCartCtrl(id_u8, flowRate_u8, pressure_u8);
	}

	/* Flowmeter data IN */
	FlowmeterGetFlow(&flowmeterFM1_gst);
	FlowmeterGetFlow(&flowmeterFM2_gst);
	FlowmeterGetFlow(&flowmeterFM3_gst);
	FlowmeterGetFlow(&flowmeterFM4_gst);

	/* Pressure sensor data IN */
	PressureSensorGetVal(&pressureSensorPS1_gst);
	PressureSensorGetVal(&pressureSensorPS2_gst);
	PressureSensorGetVal(&pressureSensorPS3_gst);
	PressureSensorGetVal(&pressureSensorPS4_gst);

	/* Flowmeter data OUT */
	ProcessBoardFlowmeterDataOUT(flowmeterFM1_gst.rawQ_f32);
	ProcessBoardFlowmeterDataOUT(flowmeterFM2_gst.rawQ_f32);
	ProcessBoardFlowmeterDataOUT(flowmeterFM3_gst.rawQ_f32);
	ProcessBoardFlowmeterDataOUT(flowmeterFM4_gst.rawQ_f32);

	/* Pressure sensor data OUT */
	ProcessBoardPressureSensorDataOUT(pressureSensorPS1_gst.rawT_f32);
	ProcessBoardPressureSensorDataOUT(pressureSensorPS2_gst.rawT_f32);
	ProcessBoardPressureSensorDataOUT(pressureSensorPS3_gst.rawT_f32);
	ProcessBoardPressureSensorDataOUT(pressureSensorPS4_gst.rawT_f32);

	ProcessBoardGrundfosPumpHandler();

	ProcessBoardReadCondDataIN();
	ProcessBoardWriteCondDataOUT();
}


/******************************************************************************
 * Private function definitions
 ******************************************************************************/
static void ProcessBoardGrundfosPumpHandler(void)
{
    static uint32 pmpSpeed_u32   = 0uL;
    static uint8 speed_u8        = 0u;
    static uint64 startTime_u64  = 0uLL;
    uint64 currentTimeMs_u64;

    HalSystemGetRunTimeMs(&currentTimeMs_u64);

    if (startTime_u64 == 0uLL)
    {
        HalSystemGetRunTimeMs(&startTime_u64);
    }

    GrundfosPmpEnable(PU_PMP_ENABLE);

    if ((currentTimeMs_u64 - startTime_u64) >= PU_PUMP_SPEED_CHANGE_INTERVAL)
    {
        if (speed_u8 < PU_PUMP_MAX_SPEED)
        {
            speed_u8 += 2u;
        }
        else
        {
            speed_u8 = PU_PUMP_MIN_SPEED;
        }

        GrundfosPmpSetSpeed(speed_u8);

        startTime_u64 = currentTimeMs_u64;

        /* Grundfos Pump feedback speed OUT */
        pmpSpeed_u32 = GrundfosPmpFeedbackSpeed(PMP_ADC_CHANNEL);

        /* To be verified */
        if (pmpSpeed_u32 > PU_PUMP_MAX_SPEED)
        {
        	GrundfosPmpEnable(PU_PMP_DISABLE);
        }
    }
}


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)
{
	NmsCanGetObj_u32(OD_ENTRY_PU_COND_DATA_IN_INDEX, OD_ENTRY_PU_COND1_DATA_IN_SUB_INDEX, &condBoard1_gst.conductivityData_u32);
	NmsCanGetObj_u32(OD_ENTRY_PU_COND_DATA_IN_INDEX, OD_ENTRY_PU_TEMP1_DATA_IN_SUB_INDEX, &condBoard1_gst.temperature_u32);

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

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


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

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

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


static void ProcessBoardFlowmeterDataOUT(float32 rawQ_f32)
{
	NmsCanPutObj_f32(OD_ENTRY_PU_FLOWMETER_DATA_OUT_INDEX, OD_ENTRY_PU_FLOWMETER1_DATA_OUT_SUB_INDEX, rawQ_f32);
	NmsCanPutObj_f32(OD_ENTRY_PU_FLOWMETER_DATA_OUT_INDEX, OD_ENTRY_PU_FLOWMETER1_DATA_OUT_SUB_INDEX, rawQ_f32);
	NmsCanPutObj_f32(OD_ENTRY_PU_FLOWMETER_DATA_OUT_INDEX, OD_ENTRY_PU_FLOWMETER1_DATA_OUT_SUB_INDEX, rawQ_f32);
	NmsCanPutObj_f32(OD_ENTRY_PU_FLOWMETER_DATA_OUT_INDEX, OD_ENTRY_PU_FLOWMETER1_DATA_OUT_SUB_INDEX, rawQ_f32);
}


static void ProcessBoardPressureSensorDataOUT(float32 rawT_f32)
{
	NmsCanPutObj_f32(OD_ENTRY_PU_PRESSURE_DATA_OUT_INDEX, OD_ENTRY_PU_PRESSURE1_DATA_OUT_SUB_INDEX, rawT_f32);
	NmsCanPutObj_f32(OD_ENTRY_PU_PRESSURE_DATA_OUT_INDEX, OD_ENTRY_PU_PRESSURE1_DATA_OUT_SUB_INDEX, rawT_f32);
	NmsCanPutObj_f32(OD_ENTRY_PU_PRESSURE_DATA_OUT_INDEX, OD_ENTRY_PU_PRESSURE1_DATA_OUT_SUB_INDEX, rawT_f32);
	NmsCanPutObj_f32(OD_ENTRY_PU_PRESSURE_DATA_OUT_INDEX, OD_ENTRY_PU_PRESSURE1_DATA_OUT_SUB_INDEX, rawT_f32);
}


static void ProcessBoardPumpSpeedDataOUT(uint32 pmpSpeed_u32)
{
	NmsCanPutObj_u32(OD_ENTRY_PU_GRUNDFOS_PUMP_CONTROL_INDEX, OD_ENTRY_PU_GRUNDFOS_PUMP_FEEDBACK_SUB_INDEX, pmpSpeed_u32);
}


static void ProcessBoardReadMvDataIN(void)
{
//	NmsCanGetObj_u8(uint8 canLine_u8, uint16 index_u16, uint8 subIndex_u8, uint8 *pObj_pu8);
}
