
/** @file sc_pressure sensor.c
 *
 * @defgroup user application
 * @{
 * @ingroup  sensor comm
 * @brief     file.
 *
 * This file contains the source code for a pressure sensor management
 *
 */
 
 #define PRE_SENSOR_CONFIG_LOG_ENABLED 1

#define NRF_LOG_MODULE_NAME PS
#if PRE_SENSOR_CONFIG_LOG_ENABLED
#define NRF_LOG_LEVEL       BLE_NUS_CONFIG_LOG_LEVEL
#define NRF_LOG_INFO_COLOR  BLE_NUS_CONFIG_INFO_COLOR
#define NRF_LOG_DEBUG_COLOR BLE_NUS_CONFIG_DEBUG_COLOR
#else // PRE_SENSOR_CONFIG_LOG_ENABLED
#define NRF_LOG_LEVEL       0
#endif // PRE_SENSOR_CONFIG_LOG_ENABLED
#include "nrf_log.h"
NRF_LOG_MODULE_REGISTER();

//-----------------------------------/
// Include files
//-----------------------------------/
#include <stdio.h>
#include "sc_pressure_sensor.h"
#include "sc_timer.h"
#include "common.h"
#include "custom_board.h"
#include "nrf_drv_twi.h"

//-----------------------------------/
// Macros
//-----------------------------------/
#define TWI_INSTANCE_ID         0
#define PRESS_SEN_ADDR          0x28

#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))

//-----------------------------------/
// Global Variables
//-----------------------------------/
/* TWI instance. */
static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
static bool is_sens_present = false;
static bool is_pres_reading_available = false;
static float press_data = 0;

// Transfer Function of SSCDANN150PG2A3
static const int outMin = 1638;						// 1638  counts (10% of 2^14 counts or 0x0666)
static const int outMax = 14745;					// 14745 counts (90% of 2^14 counts or 0x3999)
static const float pMin = 0;
static const float pMax = 150;

//-----------------------------------/
// Private functions
//-----------------------------------/
/**
 * @brief TWI initialization.
 */
static void twi_init (void)
{
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_config = {
       .scl                = PRE_SENSOR_CLK,
       .sda                = PRE_SENSOR_DATA,
       .frequency          = NRF_DRV_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

    err_code = nrf_drv_twi_init(&m_twi, &twi_config, NULL, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&m_twi);
}

/**
 * @brief Function calculates pressure from raw pressure data.
 */
static float getPressure(uint16_t rawP) 
{
    return ((float)(((constrain(rawP, outMin, outMax) - outMin) * (pMax - pMin) / (outMax - outMin)) + pMin));
}

static void mes_app_timer_cb(void)
{
	NRF_LOG_INFO("Pres Mes Trigg");
	press_data = get_ssc_data();
}

/**
 * @brief Function to fetch data from SSC.
 */
float get_ssc_data(void)
{
    ret_code_t err_code;
    
    uint8_t raw_values[4];
    err_code = nrf_drv_twi_rx(&m_twi, PRESS_SEN_ADDR, raw_values, 4);
    APP_ERROR_CHECK(err_code);
	
    SSC_DATA_STATUS statusBit = (SSC_DATA_STATUS) (raw_values[0] >> 6);
	
	NRF_LOG_INFO("Pres Status = 0x%02X", statusBit);
		
    float pressure = 0.00;
    uint16_t rawPressure;
    
    switch(statusBit)
    {
        case VALID_DATA:
            rawPressure = (((uint16_t) (raw_values[0] & 0x3f)) << 8) | raw_values[1];
            pressure = getPressure(rawPressure);
		    NRF_LOG_INFO("Pres = "NRF_LOG_FLOAT_MARKER, NRF_LOG_FLOAT(pressure));
	        set_pressure_data_available(true);
        break;
		
        case COMMAND_PROCESSING:
			
        break;
        
		case STALE_DATA:
			
        break;
        
		case DIAGNOSTIC_CONDITION:
			
        break;
        
		default:
			
        break;
    }
    return pressure;
}

//-----------------------------------/
// public functions
//-----------------------------------/
ret_code_t init_pressure_sensor(void)
{
	ret_code_t err_code;
    uint8_t sample_data;
	
	twi_init();
	
	err_code = nrf_drv_twi_rx(&m_twi, PRESS_SEN_ADDR, &sample_data, sizeof(sample_data));
	if (err_code == NRF_SUCCESS)
	{
		is_sens_present = true;
		NRF_LOG_INFO("Pressure sensor I2C address 0x%x.", PRESS_SEN_ADDR);
	}

    if (!is_sens_present)
    {
        NRF_LOG_INFO("No device was found.");
    }
	
	NRF_LOG_FLUSH();
	
	reg_acq_timer(mes_app_timer_cb);
	
	return err_code;
}

bool is_pressure_data_available(void)
{
	return is_pres_reading_available;
}

void set_pressure_data_available(bool flag)
{
	is_pres_reading_available = flag;
}

uint16_t get_pressure_data(void)
{
	uint32_t press = 0;
	press = press_data * 1000;
	NRF_LOG_INFO("Press Data = %d", press);
	return ((uint16_t) press);
}

/* End of file */
/**
 * @}
 */
