/*******************************************************************************
 TEST medi50  Dec 23
 ******************************************************************************/
#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 "nrfx_gpiote.h"
#include "app_timer.h"
#include "nrf_drv_timer.h"
#include "nrf_delay.h"
#include "nrf_drv_ppi.h"
#include "ada2200_spi.h"
#include "ble_nus.h"
#include "fstorage.h"
#include "measurements.h"
#include "meas_pd_voltage_half.h"
#include "mcp4725_i2c.h"
#include "ad5272_i2c.h"
#include "main_timer.h"
#include "battery_saadc.h"
#include "main.h"
#include "power_control.h"
#define HALF_REF_VOLTAGE_IN_MILLIVOLTS   	600.0f                                     /**< Reference voltage (in milli volts) used by ADC while doing conversion. */
#define HALF_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 HALF_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 HALF_VOUT_IN_MILLI_VOLTS(ADC_VALUE)\
				(((((ADC_VALUE) * HALF_REF_VOLTAGE_IN_MILLIVOLTS) / HALF_ADC_RES_10BITS) * HALF_PRE_SCALING_COMPENSATION)*2)


uint8_t half_samples_in_buffer = 8;


uint8_t LED_list_half[6];
uint8_t PD_list_half[6];

uint8_t led_half_list_a[6]	= {1,2,3,4,5,6};
uint8_t pd_half_list_a[6]	= {5,6,7,8,9,10};

uint8_t led_half_list_b[6]	= {7,8,9,10,11,12};
uint8_t pd_half_list_b[6]	= {6,5,4,3,2,1};

uint8_t led_half_list_c[6]	= {13,14,15,16,17,18};
uint8_t pd_half_list_c[6]	= {15,16,17,18,19,20};

uint8_t led_half_list_d[6]	= {19,20,21,22,23,24};
uint8_t pd_half_list_d[6]	= {16,15,14,13,12,11};

bool prestatus;
static int8_t 	pd_no 		= -1;
static int8_t 	led_no 		= -1;
static int8_t	buf_no		= 0;
static uint8_t order = 0;
double	half_cycle_buff[36][HALF_CYCLE_CNT] = 
				{
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //0
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //1
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //2
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //3
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //4
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //5
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //6
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //7
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //29
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //30
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //31
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //32
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //33
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //34
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //35
				};	
double	half_cycle_send_buff[HALF_LED_NO][HALF_PD_NO] = 
				{
					{0, 0, 0, 0, 0, 0},
					{0, 0, 0, 0, 0, 0},
					{0, 0, 0, 0, 0, 0},
					{0, 0, 0, 0, 0, 0},
					{0, 0, 0, 0, 0, 0},
					{0, 0, 0, 0, 0, 0}
				};


#define SAMPLES_IN_BUFFER	4095+32
static nrf_saadc_value_t pd_half_adc_buf[2][SAMPLES_IN_BUFFER];

bool pd_adc_half_a_start	= false;
bool pd_adc_half_b_start	= false;
bool pd_adc_half_c_start	= false;
bool pd_adc_half_d_start	= false;
bool pd_adc_half_end		= false;


char half_tx_buffer_0[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_1[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_2[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_3[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_4[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_5[BLE_NUS_MAX_DATA_LEN];

char half_tx_buffer_6[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_7[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_8[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_9[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_10[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_11[BLE_NUS_MAX_DATA_LEN];

char half_tx_buffer_12[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_13[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_14[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_15[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_16[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_17[BLE_NUS_MAX_DATA_LEN];

char half_tx_buffer_18[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_19[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_20[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_21[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_22[BLE_NUS_MAX_DATA_LEN];
char half_tx_buffer_23[BLE_NUS_MAX_DATA_LEN];

extern which_cmd_t cmd_type_t;


APP_TIMER_DEF(m_half_send_loop_timer_id);
#if FEATURE_DELAY
#define HALF_SEND_LOOP_INTERVAL 500
#else
#define HALF_SEND_LOOP_INTERVAL 100
#endif

extern volatile bool ble_connection_st;
#if FEATURE_DEBUG_REPEAT_TEST 
extern uint32_t rpt_cnt;
#endif

#if FEATURE_PRINTF
APP_TIMER_DEF(m_half_loop_timer_id);
#define HALF_LOOP_INTERVAL              1

void half_timer_loop(void * p_context)
{
	UNUSED_PARAMETER(p_context);

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

void half_timer_start(void)
{
	APP_ERROR_CHECK(app_timer_start(m_half_loop_timer_id, APP_TIMER_TICKS(HALF_LOOP_INTERVAL), NULL));	
}

void half_timer_stop(void)
{
	APP_ERROR_CHECK(app_timer_stop(m_half_loop_timer_id));	
}

void half_timer_init(void)
{
    APP_ERROR_CHECK(app_timer_create(&m_half_loop_timer_id, APP_TIMER_MODE_REPEATED, half_timer_loop));	
}
#else
static nrf_ppi_channel_t	m_ppi_channel;
#endif


#if !FEATURE_PRINTF
void half_ppi_init(void)
{
    ret_code_t err_code;

    err_code = nrf_drv_ppi_init();
    APP_ERROR_CHECK(err_code);

    uint32_t gpiote_event_addr = nrf_drv_gpiote_in_event_addr_get(ADA2200_SYNCO_PIN);	
    uint32_t saadc_sample_task_addr   = nrf_drv_saadc_sample_task_get();

    /* setup ppi channel so that timer compare event is triggering sample task in SAADC */
    err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_ppi_channel_assign(m_ppi_channel,
                                          gpiote_event_addr,
                                          saadc_sample_task_addr);
    APP_ERROR_CHECK(err_code);
}


void half_ppi_uninit(void)
{
    ret_code_t err_code;

	err_code = nrf_drv_ppi_uninit();
    APP_ERROR_CHECK(err_code);
}


void half_sampling_event_enable(void)
{
    ret_code_t err_code = nrf_drv_ppi_channel_enable(m_ppi_channel);
    APP_ERROR_CHECK(err_code);
}


void half_sampling_event_disable(void)
{
	ret_code_t err_code = nrf_drv_ppi_channel_disable(m_ppi_channel);
	APP_ERROR_CHECK(err_code);
}
#endif


/**@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.
 */
static void half_voltage_handler(nrf_drv_saadc_evt_t const * p_event)	/* PD Voltage reading */
{
	ret_code_t err_code;
	double sum = 0.0f;

	uint32_t half_clk_delay = m_config.pd_delay_us/16;
	if (prestatus==true)
	{ half_clk_delay =10;}
		if(ble_connection_st == 0) {
								half_adc_end();
								half_send_timer_stop();
								printf("Half ADC STOP 1\r\n");
						}
		
			else{			
    if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
    {
		err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, half_samples_in_buffer + half_clk_delay);
		APP_ERROR_CHECK(err_code);
		
		if(led_no == -1) {
			led_no = 0;
			pd_no = 0;

			led_on(LED_list_half[led_no]);
			led_pd_matching_value_set(LED_list_half[led_no], PD_list_half[pd_no]); /* MCP4725 DAC setting and PD on */

		} else {
#if FEATURE_FOR_SCOPE    
			nrf_gpio_pin_set(ADC_CLK_18);
			nrf_gpio_pin_clear(ADC_CLK_18);
#endif
			for(uint16_t i = half_clk_delay; i < half_clk_delay + half_samples_in_buffer; i++){
				half_cycle_buff[buf_no][i-half_clk_delay] = (HALF_VOUT_IN_MILLI_VOLTS(p_event->data.done.p_buffer[i])) * -1;
			}
#if FEATURE_PRINTF
			printf("-----------------Read ADC // led_no = %d(%d), pd_no = %d(%d), buf_no = %d\r\n\r\n", led_no, LED_list_half[led_no], pd_no,  PD_list_half[pd_no], buf_no);
#endif

			buf_no++;

			if(pd_no < HALF_PD_NO - 1) {
				pd_no++;
				led_pd_matching_value_set(LED_list_half[led_no], PD_list_half[pd_no]); /* MCP4725 DAC setting and PD on */
			
			}else if(pd_no >= HALF_PD_NO - 1) {
				pd_no = 0;

#if FEATURE_PRINTF
				printf("\r\n\r\n");
#endif

				if(led_no < HALF_LED_NO - 1) {
					led_no++;
					led_on(LED_list_half[led_no]);
					led_pd_matching_value_set(LED_list_half[led_no], PD_list_half[pd_no]); /* MCP4725 DAC setting and PD on */

				} else if(led_no >= HALF_LED_NO - 1) {
					pd_no = -1;
					led_no = -1;
#if FEATURE_PRINTF
					printf("\r\nEnded\r\n");
#endif
					half_adc_end();

#if FEATURE_DETAIL_VALUE_HALF
					sum = 0;
					printf("\r\nHalf_============half_cycle_buff =\r\n");
					for(uint16_t i = 0; i < 36; i++){
						for(uint16_t j = 0; j < half_samples_in_buffer; j++){
							printf("%lf\r\n", half_cycle_buff[i][j]);
						}
						for(uint16_t k = 0; k < half_samples_in_buffer; k++){
							sum += half_cycle_buff[i][k];
						}
						printf("\t%lf\r\n", sum);
						sum = 0;
					}
					printf("\r\n");
#endif

					///////////////////////////////////////////////////////////////////////////////////////////
					uint8_t k =0;
					sum = 0;
					for(uint16_t i = 0; i < 36; i++){
						for(uint16_t j = 0; j < half_samples_in_buffer; j++){
							sum += half_cycle_buff[i][j];
						}
						
						half_cycle_send_buff[i/6][k++] = sum;
						sum = 0;

						if(k >= 6) k = 0;
					}
					///////////////////////////////////////////////////////////////////////////////////////////

					/* Send data */
					/*********************/
					if(cmd_type_t == CMD_UART && prestatus==false) {
						printf("Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[0], half_cycle_send_buff[0][0], half_cycle_send_buff[0][1], half_cycle_send_buff[0][2], half_cycle_send_buff[0][3], half_cycle_send_buff[0][4], half_cycle_send_buff[0][5]);
						printf("Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[1], half_cycle_send_buff[1][0], half_cycle_send_buff[1][1], half_cycle_send_buff[1][2], half_cycle_send_buff[1][3], half_cycle_send_buff[1][4], half_cycle_send_buff[1][5]);
						printf("Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[2], half_cycle_send_buff[2][0], half_cycle_send_buff[2][1], half_cycle_send_buff[2][2], half_cycle_send_buff[2][3], half_cycle_send_buff[2][4], half_cycle_send_buff[2][5]);
						printf("Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[3], half_cycle_send_buff[3][0], half_cycle_send_buff[3][1], half_cycle_send_buff[3][2], half_cycle_send_buff[3][3], half_cycle_send_buff[3][4], half_cycle_send_buff[3][5]);
						printf("Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[4], half_cycle_send_buff[4][0], half_cycle_send_buff[4][1], half_cycle_send_buff[4][2], half_cycle_send_buff[4][3], half_cycle_send_buff[4][4], half_cycle_send_buff[4][5]);
						printf("Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[5], half_cycle_send_buff[5][0], half_cycle_send_buff[5][1], half_cycle_send_buff[5][2], half_cycle_send_buff[5][3], half_cycle_send_buff[5][4], half_cycle_send_buff[5][5]);
						printf("\r\n");
					} else if(cmd_type_t == CMD_BLE && prestatus==false) {
						if(pd_adc_half_a_start == true) {
							sprintf(half_tx_buffer_0, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[0], half_cycle_send_buff[0][0], half_cycle_send_buff[0][1], half_cycle_send_buff[0][2], half_cycle_send_buff[0][3], half_cycle_send_buff[0][4], half_cycle_send_buff[0][5]);
							sprintf(half_tx_buffer_1, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[1], half_cycle_send_buff[1][0], half_cycle_send_buff[1][1], half_cycle_send_buff[1][2], half_cycle_send_buff[1][3], half_cycle_send_buff[1][4], half_cycle_send_buff[1][5]);
							sprintf(half_tx_buffer_2, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[2], half_cycle_send_buff[2][0], half_cycle_send_buff[2][1], half_cycle_send_buff[2][2], half_cycle_send_buff[2][3], half_cycle_send_buff[2][4], half_cycle_send_buff[2][5]);
							sprintf(half_tx_buffer_3, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[3], half_cycle_send_buff[3][0], half_cycle_send_buff[3][1], half_cycle_send_buff[3][2], half_cycle_send_buff[3][3], half_cycle_send_buff[3][4], half_cycle_send_buff[3][5]);
							sprintf(half_tx_buffer_4, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[4], half_cycle_send_buff[4][0], half_cycle_send_buff[4][1], half_cycle_send_buff[4][2], half_cycle_send_buff[4][3], half_cycle_send_buff[4][4], half_cycle_send_buff[4][5]);
							sprintf(half_tx_buffer_5, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[5], half_cycle_send_buff[5][0], half_cycle_send_buff[5][1], half_cycle_send_buff[5][2], half_cycle_send_buff[5][3], half_cycle_send_buff[5][4], half_cycle_send_buff[5][5]);
						}else if(pd_adc_half_b_start == true) {
							sprintf(half_tx_buffer_6, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[0], half_cycle_send_buff[0][0], half_cycle_send_buff[0][1], half_cycle_send_buff[0][2], half_cycle_send_buff[0][3], half_cycle_send_buff[0][4], half_cycle_send_buff[0][5]);
							sprintf(half_tx_buffer_7, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[1], half_cycle_send_buff[1][0], half_cycle_send_buff[1][1], half_cycle_send_buff[1][2], half_cycle_send_buff[1][3], half_cycle_send_buff[1][4], half_cycle_send_buff[1][5]);
							sprintf(half_tx_buffer_8, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[2], half_cycle_send_buff[2][0], half_cycle_send_buff[2][1], half_cycle_send_buff[2][2], half_cycle_send_buff[2][3], half_cycle_send_buff[2][4], half_cycle_send_buff[2][5]);
							sprintf(half_tx_buffer_9, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[3], half_cycle_send_buff[3][0], half_cycle_send_buff[3][1], half_cycle_send_buff[3][2], half_cycle_send_buff[3][3], half_cycle_send_buff[3][4], half_cycle_send_buff[3][5]);
							sprintf(half_tx_buffer_10, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[4], half_cycle_send_buff[4][0], half_cycle_send_buff[4][1], half_cycle_send_buff[4][2], half_cycle_send_buff[4][3], half_cycle_send_buff[4][4], half_cycle_send_buff[4][5]);
							sprintf(half_tx_buffer_11, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[5], half_cycle_send_buff[5][0], half_cycle_send_buff[5][1], half_cycle_send_buff[5][2], half_cycle_send_buff[5][3], half_cycle_send_buff[5][4], half_cycle_send_buff[5][5]);
						}else if(pd_adc_half_c_start == true) {
							sprintf(half_tx_buffer_12, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[0], half_cycle_send_buff[0][0], half_cycle_send_buff[0][1], half_cycle_send_buff[0][2], half_cycle_send_buff[0][3], half_cycle_send_buff[0][4], half_cycle_send_buff[0][5]);
							sprintf(half_tx_buffer_13, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[1], half_cycle_send_buff[1][0], half_cycle_send_buff[1][1], half_cycle_send_buff[1][2], half_cycle_send_buff[1][3], half_cycle_send_buff[1][4], half_cycle_send_buff[1][5]);
							sprintf(half_tx_buffer_14, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[2], half_cycle_send_buff[2][0], half_cycle_send_buff[2][1], half_cycle_send_buff[2][2], half_cycle_send_buff[2][3], half_cycle_send_buff[2][4], half_cycle_send_buff[2][5]);
							sprintf(half_tx_buffer_15, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[3], half_cycle_send_buff[3][0], half_cycle_send_buff[3][1], half_cycle_send_buff[3][2], half_cycle_send_buff[3][3], half_cycle_send_buff[3][4], half_cycle_send_buff[3][5]);
							sprintf(half_tx_buffer_16, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[4], half_cycle_send_buff[4][0], half_cycle_send_buff[4][1], half_cycle_send_buff[4][2], half_cycle_send_buff[4][3], half_cycle_send_buff[4][4], half_cycle_send_buff[4][5]);
							sprintf(half_tx_buffer_17, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[5], half_cycle_send_buff[5][0], half_cycle_send_buff[5][1], half_cycle_send_buff[5][2], half_cycle_send_buff[5][3], half_cycle_send_buff[5][4], half_cycle_send_buff[5][5]);
						}else if(pd_adc_half_d_start == true) {
							sprintf(half_tx_buffer_18, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[0], half_cycle_send_buff[0][0], half_cycle_send_buff[0][1], half_cycle_send_buff[0][2], half_cycle_send_buff[0][3], half_cycle_send_buff[0][4], half_cycle_send_buff[0][5]);
							sprintf(half_tx_buffer_19, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[1], half_cycle_send_buff[1][0], half_cycle_send_buff[1][1], half_cycle_send_buff[1][2], half_cycle_send_buff[1][3], half_cycle_send_buff[1][4], half_cycle_send_buff[1][5]);
							sprintf(half_tx_buffer_20, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[2], half_cycle_send_buff[2][0], half_cycle_send_buff[2][1], half_cycle_send_buff[2][2], half_cycle_send_buff[2][3], half_cycle_send_buff[2][4], half_cycle_send_buff[2][5]);
							sprintf(half_tx_buffer_21, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[3], half_cycle_send_buff[3][0], half_cycle_send_buff[3][1], half_cycle_send_buff[3][2], half_cycle_send_buff[3][3], half_cycle_send_buff[3][4], half_cycle_send_buff[3][5]);
							sprintf(half_tx_buffer_22, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[4], half_cycle_send_buff[4][0], half_cycle_send_buff[4][1], half_cycle_send_buff[4][2], half_cycle_send_buff[4][3], half_cycle_send_buff[4][4], half_cycle_send_buff[4][5]);
							sprintf(half_tx_buffer_23, "Ti%d,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf,\t%lf\r\n", LED_list_half[5], half_cycle_send_buff[5][0], half_cycle_send_buff[5][1], half_cycle_send_buff[5][2], half_cycle_send_buff[5][3], half_cycle_send_buff[5][4], half_cycle_send_buff[5][5]);
						}
					}
					/*********************/

					buf_no = 0;

					if(ble_connection_st == 0) {
							half_adc_end();
							half_send_timer_stop();
							printf("Half ADC STOP 1\r\n");
						}
					else if(pd_adc_half_a_start == true) {		// B mode
						pd_adc_half_a_start = false;
						pd_adc_half_b_start = true;
					
						half_adc_start();
					}else if(pd_adc_half_b_start == true) {	// C mode
						pd_adc_half_b_start = false;
						pd_adc_half_c_start = true;
						
						half_adc_start();
					}else if(pd_adc_half_c_start == true) {	// D mode
						pd_adc_half_c_start = false;
						pd_adc_half_d_start = true;
						
						half_adc_start();
					}else if(pd_adc_half_d_start == true) { // Completed
						pd_adc_half_d_start = false;

						if(cmd_type_t == CMD_UART && prestatus==false) {
							half_send_timer_stop();
							
						}else if(cmd_type_t == CMD_BLE && prestatus==false) {
							half_send_timer_start();
							
						}

						if(ble_connection_st == BLE_CONNECTED_ST) {
							battery_timer_start();
						}

						led_off(99);
						pd_off(99);
					}		
				}
			}
		}
   }
  }
}

void half_send_loop(void * p_context)	/* For x ms */
{
	UNUSED_PARAMETER(p_context);
	half_send_timer_stop();

	

	switch(order) {
		case 0:
			data_tx_handler(half_tx_buffer_0);
			break;

		case 1:
			data_tx_handler(half_tx_buffer_1);
			break;
				
		case 2:
			data_tx_handler(half_tx_buffer_2);
			break;

		case 3:
			data_tx_handler(half_tx_buffer_3);
			break;

		case 4:
			data_tx_handler(half_tx_buffer_4);
			break;

		case 5:
			data_tx_handler(half_tx_buffer_5);
			break;
			
		case 6:
			data_tx_handler(half_tx_buffer_6);
			break;

		case 7:
			data_tx_handler(half_tx_buffer_7);
			break;
				
		case 8:
			data_tx_handler(half_tx_buffer_8);
			break;

		case 9:
			data_tx_handler(half_tx_buffer_9);
			break;

		case 10:
			data_tx_handler(half_tx_buffer_10);
			break;

		case 11:
			data_tx_handler(half_tx_buffer_11);
			break;
			
		case 12:
			data_tx_handler(half_tx_buffer_12);
			break;

		case 13:
			data_tx_handler(half_tx_buffer_13);
			break;
				
		case 14:
			data_tx_handler(half_tx_buffer_14);
			break;

		case 15:
			data_tx_handler(half_tx_buffer_15);
			break;

		case 16:
			data_tx_handler(half_tx_buffer_16);
			break;

		case 17:
			data_tx_handler(half_tx_buffer_17);
			break;
			
		case 18:
			data_tx_handler(half_tx_buffer_18);
			break;

		case 19:
			data_tx_handler(half_tx_buffer_19);
			break;
				
		case 20:
			data_tx_handler(half_tx_buffer_20);
			break;

		case 21:
			data_tx_handler(half_tx_buffer_21);
			break;

		case 22:
			data_tx_handler(half_tx_buffer_22);
			break;

		case 23:
			data_tx_handler(half_tx_buffer_23);

			order = 0;
#if FEATURE_PRINTF
			printf("half_send Completed\r\n");
#endif


			return;

		default:
			break;
	}
	order++;
						if(ble_connection_st == 0) {
							half_send_timer_stop();
							order = 0;
							printf("Half ADC STOP 2\r\n");}
							else{
								half_send_timer_start();	
							}
}


void half_adc_start(void)
{
	pd_no		= -1;
	led_no		= -1;
	buf_no		= 0;
  
	
	if(pd_adc_half_a_start == true) {
#if FEATURE_PRINTF
		printf("\r\n===== Half_A start ====================\r\n");
		printf("LED : ");
#endif
		for(uint8_t i =0; i < HALF_LED_NO; i++) {
			LED_list_half[i] = led_half_list_a[i];
#if FEATURE_PRINTF
			printf("%d ", LED_list_half[i]);
#endif
		}
#if FEATURE_PRINTF
		printf("\r\n");
		printf("PD  : ");
#endif
		for(uint8_t i =0; i < HALF_PD_NO; i++) {
			PD_list_half[i] = pd_half_list_a[i];
#if FEATURE_PRINTF
			printf("%d ", PD_list_half[i]);
#endif
		}
#if FEATURE_PRINTF
		printf("\r\n\r\n");
#endif		
	}

	if(pd_adc_half_b_start == true) {
#if FEATURE_PRINTF
		printf("\r\n===== Half_B start ====================\r\n");
		printf("LED : ");
#endif
		for(uint8_t i =0; i < HALF_LED_NO; i++) {
			LED_list_half[i] = led_half_list_b[i];
#if FEATURE_PRINTF
			printf("%d ", LED_list_half[i]);
#endif
		}
		
#if FEATURE_PRINTF
		printf("\r\n");
		printf("PD  : ");
#endif

		for(uint8_t i =0; i < HALF_PD_NO; i++) {
			PD_list_half[i] = pd_half_list_b[i];
#if FEATURE_PRINTF
			printf("%d ", PD_list_half[i]);
#endif
		}
#if FEATURE_PRINTF
		printf("\r\n\r\n");
#endif		
	}

	if(pd_adc_half_c_start == true) {
#if FEATURE_PRINTF
		printf("\r\n===== Half_C start ====================\r\n");
		printf("LED : ");
#endif
		for(uint8_t i =0; i < HALF_LED_NO; i++) {
			LED_list_half[i] = led_half_list_c[i];
#if FEATURE_PRINTF
			printf("%d ", LED_list_half[i]);
#endif
		}
		
#if FEATURE_PRINTF
		printf("\r\n");
		printf("PD  : ");
#endif

		for(uint8_t i =0; i < HALF_PD_NO; i++) {
			PD_list_half[i] = pd_half_list_c[i];
#if FEATURE_PRINTF
			printf("%d ", PD_list_half[i]);
#endif
		}
#if FEATURE_PRINTF
		printf("\r\n\r\n");
#endif		
	}

	if(pd_adc_half_d_start == true) {
#if FEATURE_PRINTF
		printf("\r\n===== Half_D start ====================\r\n");
		printf("LED : ");
#endif
		for(uint8_t i =0; i < HALF_LED_NO; i++) {
			LED_list_half[i] = led_half_list_d[i];
#if FEATURE_PRINTF
			printf("%d ", LED_list_half[i]);
#endif
		}
		
#if FEATURE_PRINTF
		printf("\r\n");
		printf("PD  : ");
#endif

		for(uint8_t i =0; i < HALF_PD_NO; i++) {
			PD_list_half[i] = pd_half_list_d[i];
#if FEATURE_PRINTF
			printf("%d ", PD_list_half[i]);
#endif
		}
#if FEATURE_PRINTF
		printf("\r\n\r\n");
#endif		
	}

	for(uint16_t i = 0; i < 72; i++) {
		for(uint16_t j = 0; j < HALF_CYCLE_CNT; j++) {
			half_cycle_buff[i][j] = 0.0f;
		}
	}
	for(uint8_t i = 0; i < HALF_LED_NO; i++) {
		for(uint8_t j = 0; j < HALF_PD_NO; j++) {
			half_cycle_send_buff[i][j] = 0.0f;
		}
	}

	half_adc_start2();
 order =0;
}


void half_adc_start2(void) 
{
	half_adc_init();
#if FEATURE_PRINTF
	half_timer_init();
	half_timer_start();
#else
	half_irq_init();
	half_ppi_init();
	half_sampling_event_enable();	
	//ada2200_start();
#endif
}


void half_adc_end(void) 
{
#if FEATURE_PRINTF
	printf("half_adc_end\r\n");
	half_timer_stop();
#endif

#if !FEATURE_PRINTF
	half_sampling_event_disable();
	half_irq_uninit();
	half_ppi_uninit();
#endif
	half_adc_uninit();
	//ada2200_stop();
	nrf_delay_ms(5);
	if(prestatus == true){
		printf("1st\r\n");
	if(device_activated() == 0)
	{printf("2nd\r\n");
	}
}
}


void half_adc_init(void)
{
#if FEATURE_PRINTF
	printf("half_adc_init\r\n");
#endif

	static nrfx_saadc_config_t default_config;
	default_config.resolution			= (nrf_saadc_resolution_t)NRFX_SAADC_CONFIG_RESOLUTION; /* Resolution is 10bits */
	default_config.oversample			= (nrf_saadc_oversample_t)NRFX_SAADC_CONFIG_OVERSAMPLE; /* Over Sampling Disabled */
	default_config.interrupt_priority	= NRFX_SAADC_CONFIG_IRQ_PRIORITY;						/* Interrupt Priority is 0(Highest) */
	default_config.low_power_mode		= NRFX_SAADC_CONFIG_LP_MODE;							/* Low Power Mode is Disabled */

	static nrf_saadc_channel_config_t config;
	config.resistor_p	= NRF_SAADC_RESISTOR_DISABLED;
	config.resistor_n	= NRF_SAADC_RESISTOR_DISABLED;
	config.gain 		= NRF_SAADC_GAIN1_6;
	config.reference	= NRF_SAADC_REFERENCE_INTERNAL;
	config.acq_time 	= NRF_SAADC_ACQTIME_3US;
	config.mode 		= NRF_SAADC_MODE_DIFFERENTIAL;
	config.burst		= NRF_SAADC_BURST_DISABLED;
	config.pin_p		= (nrf_saadc_input_t)(NRF_SAADC_INPUT_AIN0);
	config.pin_n		= (nrf_saadc_input_t)(NRF_SAADC_INPUT_AIN1);
	
	ret_code_t err_code = nrf_drv_saadc_init(&default_config, half_voltage_handler);
	APP_ERROR_CHECK(err_code);

	err_code = nrf_drv_saadc_channel_init(0, &config);
	APP_ERROR_CHECK(err_code);
	if (prestatus==true)
	{	
	err_code = nrf_drv_saadc_buffer_convert(pd_half_adc_buf[0], half_samples_in_buffer + 10);
	}
	else	
	{	
	err_code = nrf_drv_saadc_buffer_convert(pd_half_adc_buf[0], half_samples_in_buffer + m_config.pd_delay_us/16);
	}	
	APP_ERROR_CHECK(err_code);
}



void half_adc_uninit(void)
{
#if FEATURE_PRINTF
	printf("pd_half_adc_uninit\r\n");
#endif

	nrf_drv_saadc_uninit();
	nrf_drv_saadc_channel_uninit(0);
}


#if !FEATURE_PRINTF
void half_irq_init(void)
{
    ret_code_t err_code;

	/* Initialize int pin */
	if (!nrfx_gpiote_is_init())
	{
	  err_code = nrfx_gpiote_init();
	  APP_ERROR_CHECK(err_code);
	}

    nrfx_gpiote_in_config_t in_config = NRFX_GPIOTE_CONFIG_IN_SENSE_LOTOHI(true);
    in_config.pull = NRF_GPIO_PIN_PULLDOWN;

    err_code = nrfx_gpiote_in_init(ADA2200_SYNCO_PIN, &in_config, NULL);
    APP_ERROR_CHECK(err_code);

    nrfx_gpiote_in_event_enable(ADA2200_SYNCO_PIN, true);
}


void half_irq_uninit(void)
{
	nrfx_gpiote_in_event_disable(ADA2200_SYNCO_PIN);
	nrfx_gpiote_in_uninit(ADA2200_SYNCO_PIN);
}
#endif


void half_send_timer_start(void)
{
	APP_ERROR_CHECK(app_timer_start(m_half_send_loop_timer_id, APP_TIMER_TICKS(HALF_SEND_LOOP_INTERVAL), NULL));
}


void half_send_timer_stop(void)
{
	APP_ERROR_CHECK(app_timer_stop(m_half_send_loop_timer_id));

}


void half_send_timer_init(void)
{
    APP_ERROR_CHECK(app_timer_create(&m_half_send_loop_timer_id, APP_TIMER_MODE_SINGLE_SHOT, half_send_loop));
}

