/*******************************************************************************
 * @file	tmp235_q1.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 "tmp235_q1.h"
#include "main.h"
#include "meas_pd_48.h"
#include <cmd_parse.h>
#include "main_timer.h"
#include "debug_print.h"
#define TMP235_REF_VOLTAGE_IN_MILLIVOLTS   		600.0f                                     /**< Reference voltage (in milli volts) used by ADC while doing conversion. */
#define TMP235_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 TMP235_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 TMP235_VOUT_IN_MILLI_VOLTS(ADC_VALUE)\
        ((((ADC_VALUE) * TMP235_REF_VOLTAGE_IN_MILLIVOLTS) / TMP235_ADC_RES_10BITS) * TMP235_PRE_SCALING_COMPENSATION)

static nrf_saadc_value_t adc_buf;
extern char		ble_tx_buffer[BLE_NUS_MAX_DATA_LEN];
extern uint8_t ble_bin_buffer[BLE_NUS_MAX_DATA_LEN]	;
extern which_cmd_t cmd_type_t;
extern bool info4;   //cmd_parse
extern bool go_temp;   //cmd_parse
extern volatile uint16_t info_temp; //48_C
extern bool	motion_raw_data_enabled; 

/**@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 tmp235_voltage_handler(nrf_drv_saadc_evt_t const * p_event)	/* TMP325 Vout reading */
{

	float led_temp;
	uint16_t led_temp_16;
    if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
    {
        nrf_saadc_value_t adc_result;
		float	tmp235_voltage_in_milli_volts = 0;

        adc_result = p_event->data.done.p_buffer[0];

		nrf_drv_saadc_uninit();
		nrf_drv_saadc_channel_uninit(0);

        tmp235_voltage_in_milli_volts = TMP235_VOUT_IN_MILLI_VOLTS(adc_result);

		if(tmp235_voltage_in_milli_volts <= 1500) {
			led_temp = (tmp235_voltage_in_milli_volts - 500.0f) / 10.0f + 0.0f;
		}else if(tmp235_voltage_in_milli_volts <= 1750) {
			led_temp = (tmp235_voltage_in_milli_volts - 1500.0f) / 10.1f + 100.0f;
		}else if(tmp235_voltage_in_milli_volts <= 2000) {
			led_temp = (tmp235_voltage_in_milli_volts - 1752.5f) / 10.6f + 125.0f;
		}else {
			DBG_PRINTF("ERR!!! Temprature is over 150c\r\n");
		}
		if (info4 == true){
			
			info_temp =(uint16_t)(led_temp*100);
			
		}
		else if(cmd_type_t == CMD_UART) {
			DBG_PRINTF("To%.2f\r\n\r\n",led_temp);
		} else if(cmd_type_t == CMD_BLE) {	
			   led_temp_16 = (uint16_t)(led_temp*100);
				single_format_data(ble_bin_buffer, "rso:",  led_temp_16);
						
				binary_tx_handler(ble_bin_buffer,3);
			
//			sprintf(ble_tx_buffer, "To%.2f\r\n",led_temp);
//			data_tx_handler(ble_tx_buffer);
		}
    }
		
				
		if (info4 == true){
		
		go_temp = false;

			
		motion_raw_data_enabled = true;
		main_timer_start();
	}
}


void tmp235_init(void)
{
	ret_code_t err_code = nrf_drv_saadc_init(NULL, tmp235_voltage_handler);
	APP_ERROR_CHECK(err_code);

	nrf_saadc_channel_config_t config =
		NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN3);	/* TMP235_Q1 Voltage Output Measurement */
	err_code = nrf_drv_saadc_channel_init(0, &config);
	APP_ERROR_CHECK(err_code);

	err_code = nrf_drv_saadc_buffer_convert(&adc_buf, 1);
	APP_ERROR_CHECK(err_code);

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


/* Ta = (Vout – Voffs ) / Tc + Tinfl */
void tmp235_voltage_level_meas(void)
{
	tmp235_init();
	//tmp235_uninit();
			
}

