/*******************************************************************************
 * @file	mcp4725_adc.c
 * @author	CandyPops Co.
 * @version	V1.0.0
 * @date	2022-09-05
 * @brief   
 ******************************************************************************/

#include "sdk_common.h"
			
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "nrf.h"
#include "boards.h"
#include "app_error.h"
#include "nrf_drv_saadc.h"
#include "ble_nus.h"
#include "mcp4725_adc.h"
#include "main.h"
#include "debug_print.h"
#define MCP4725_REF_VOLTAGE_IN_MILLIVOLTS   		600.0f                                     /**< Reference voltage (in milli volts) used by ADC while doing conversion. */
#define MCP4725_PRE_SCALING_COMPENSATION    		6.0f                                       /**< The ADC is configured to use VDD with 1/3 prescaling as input. And hence the result of conversion is to be multiplied by 3 to get the actual value of the battery voltage.*/
#define MCP4725_ADC_RES_10BITS             			1024.0f                                    /**< Maximum digital value for 10-bit ADC conversion. */

/**@brief Macro to convert the result of ADC conversion in millivolts.
 *
 * @param[in]  ADC_VALUE   ADC result.
 *
 * @retval     Result converted to millivolts.
 */
#define MCP4725_VOUT_IN_MILLI_VOLTS(ADC_VALUE)\
				((((ADC_VALUE) * MCP4725_REF_VOLTAGE_IN_MILLIVOLTS) / MCP4725_ADC_RES_10BITS) * MCP4725_PRE_SCALING_COMPENSATION)

#define ADC_SAMPLES_IN_BUFFER 		1

static nrf_saadc_value_t mcp4725_adc_buf[2][ADC_SAMPLES_IN_BUFFER];
float mcp4725_voltage_in_milli_volts = 0;

//extern char		ble_tx_buffer[BLE_NUS_MAX_DATA_LEN];

extern which_cmd_t cmd_type_t;
extern uint8_t ble_bin_buffer[BLE_NUS_MAX_DATA_LEN]	;

/**@brief Function for handling the ADC interrupt.
 *
 * @details  This function will fetch the conversion result from the ADC, convert the value into
 *           percentage and send it to peer.
 */
void mcp4725_voltage_handler(nrf_drv_saadc_evt_t const * p_event)	/* ADC_GAIN reading */
{
	float	Vref = 3.3f; /* It same as Vdd */
	float	dac_value = 0.0f;
	uint16_t dac_value_16=0;
    if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
    {
        nrf_saadc_value_t adc_result;

        nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, ADC_SAMPLES_IN_BUFFER);
        adc_result = p_event->data.done.p_buffer[0];
		
		nrf_drv_saadc_uninit();
		nrf_drv_saadc_channel_uninit(0);

		mcp4725_voltage_in_milli_volts = MCP4725_VOUT_IN_MILLI_VOLTS(adc_result);
#if FEATURE_DETAIL_VALUE_AGC
		DBG_PRINTF("AGC read Vol: %f(mV)\r\n", mcp4725_voltage_in_milli_volts);
#endif
		/* For MCP4725, Dn = (Vout/Vref) x 4096, Vref = Vdd */
		dac_value = (((mcp4725_voltage_in_milli_volts/Vref) * 4096.0f)/1000.0f);	/* Unit is Volt */
		if(cmd_type_t == CMD_UART) {
			DBG_PRINTF("Te%d\r\n\r\n",(uint16_t)(dac_value + 0.5f));
		} else if(cmd_type_t == CMD_BLE) {	
			dac_value_16 = (uint16_t)(dac_value + 0.5f);
					single_format_data(ble_bin_buffer, "rse:",  	dac_value_16);
			
				
						
						
						binary_tx_handler(ble_bin_buffer,3);
//			sprintf(ble_tx_buffer, "Te%d\r\n",(uint16_t)(dac_value + 0.5f));
//			data_tx_handler(ble_tx_buffer);
		}
    }
}


void mcp4725_adc_init(void)
{
	ret_code_t err_code = nrf_drv_saadc_init(NULL, mcp4725_voltage_handler);
	APP_ERROR_CHECK(err_code);

	nrf_saadc_channel_config_t config =
		NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN6);	/* FSA5157P6X Voltage Output Measurement */

	err_code = nrf_drv_saadc_channel_init(0, &config);
	APP_ERROR_CHECK(err_code);

	err_code = nrf_drv_saadc_buffer_convert(mcp4725_adc_buf[0], ADC_SAMPLES_IN_BUFFER);
	APP_ERROR_CHECK(err_code);
	err_code = nrf_drv_saadc_buffer_convert(mcp4725_adc_buf[1], ADC_SAMPLES_IN_BUFFER);
	APP_ERROR_CHECK(err_code);

  err_code = nrf_drv_saadc_sample();
  APP_ERROR_CHECK(err_code);	
}


void mcp4725_voltage_level_meas(void)
{
	mcp4725_adc_init();
}

