/**
 * @file	   grundfos.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
 *
 */

/******************************************************************************
 * Include Header Files
 ******************************************************************************/
#include "grundfos.h"
#include "analogMeasurement.h"
#include "hal_gpio.h"
#include "hal_i2c.h"
/******************************************************************************
 * Type  declarations
 ******************************************************************************/

/******************************************************************************
 * Macro Constant Declarations
 ******************************************************************************/
#define GRUNDFOS_PMP_INTERNAL_DAC_ADDR	(0x61 << 1)
#define GRUNFOS_GAIN					0.32f
/******************************************************************************
 * Private function Declarations
 ******************************************************************************/
static float32 GrundfosPmpReadVoltage(uint8 channel_u8);

/******************************************************************************
 * Extern Function Definitions
 ******************************************************************************/
void GrundfosPmpInit(GrundfosMain_st *grundfos_pst)
{
	grundfos_pst->rawQ_f32 = 0.0f;
}


void GrundfosPmpEnable(uint8 state_u8)
{
	if(state_u8 == 0u)
	{
		HalGpioWrite(MAP_HAL_PMP_ENABLE, HAL_GPIO_STATE_HIGH); /* Pulling the line high stops the pump */
	}
	else
	{
		HalGpioWrite(MAP_HAL_PMP_ENABLE, HAL_GPIO_STATE_LOW);
	}
}


void GrundfosPmpFeedback(GrundfosMain_st *grundfos_pst)
{
	if (grundfos_pst == NULL)
	{
		/* Error */
	}
	float32 feedbackVoltage_f32 = GrundfosPmpReadVoltage(grundfos_pst->channel_u32);
	grundfos_pst->rawQ_f32 = feedbackVoltage_f32 * 3600.0f;
}


bool GrundfosPmpSetSpeed(float32 setSpeed_f32)
{
    uint8 data_au8[2];
    static volatile uint16 dacVal_u16 = 0u;

    /* Limit the input range to 0V - 10V */
    if (setSpeed_f32 < 0.0f)
    {
        setSpeed_f32 = 0.0f;
    }
    else if (setSpeed_f32 > 10.0f)
    {
        setSpeed_f32 = 10.0f;
    }

    /* Convert pump speed (0-10V) to DAC value (0-5V output) */
    dacVal_u16 = (uint16)((setSpeed_f32 * 4095.0f) / 10.0f);

    /* Format the 12-bit DAC value into the correct byte format */
    data_au8[0] = (uint8)(dacVal_u16 >> 8);        /* Upper 8 bits */
    data_au8[1] = (uint8)(dacVal_u16 & 0xFF);      /* Lower 8 bits */

    return HalI2CMasterTransmit(MAP_HAL_GRUNDFOS_PMP_I2C_HANDLE,
                                GRUNDFOS_PMP_INTERNAL_DAC_ADDR,
                                data_au8, 2u) == NMS_ERR_NONE;
}


/******************************************************************************
 * Private function Definitions
 ******************************************************************************/
/**
 * @brief     Function to read ADC voltage data which is directly coming from the pump.
 *
 * @return	  ADC output voltage based on sensor reading.
 *
 */
static float32 GrundfosPmpReadVoltage(uint8 channel_u8)
{
	/* Convert ADC value to voltage (assuming 12-bit resolution and 3.3V reference) */
	float32 voltage_f32 = 0.0f;
	uint32 adcVal_u32 = 0uL;
	AnalogMeasurementReadData(channel_u8, &adcVal_u32);
	voltage_f32 = (float32)(adcVal_u32 * (ANALOG_MEAS_ADC_REF_VOLTAGE / ANALOG_MEAS_ADC_RESOLUTION)) * GRUNFOS_GAIN;

	return voltage_f32;
}

