/*******************************************************************************
 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_buff.h"
#include "mcp4725_i2c.h"
#include "ad5272_i2c.h"
#include "main_timer.h"
#include "battery_saadc.h"
#include "main.h"
#include "app_raw_main.h"

//#define CUSTOM_REF_VOLTAGE_IN_MILLIVOLTS   	600.0f                                     /**< Reference voltage (in milli volts) used by ADC while doing conversion. */
//#define CUSTOM_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 CUSTOM_ADC_RES_10BITS             	1024.0f                                    /**< Maximum digital value for 10-bit ADC conversion. */
//#define st_c_max																20
///**@brief Macro to convert the result of ADC conversion in millivolts.
// *
// * @param[in]  ADC_VALUE   ADC result.
// *
// * @retval     Result converted to millivolts.
// */
//#define CUSTOM_VOUT_IN_MILLI_VOLTS(ADC_VALUE)\
//				(((((ADC_VALUE) * CUSTOM_REF_VOLTAGE_IN_MILLIVOLTS) / CUSTOM_ADC_RES_10BITS) * CUSTOM_PRE_SCALING_COMPENSATION)*2)

uint16_t b_t_cnt=5;
uint8_t buff_samples_in_buffer = 8;

//uint32_t	s_cnt;
uint8_t LED_list_buff[buff_LED_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, 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};
//uint8_t PD_list_custom[4];

//uint8_t led_custom_list_a[5]	= {2,3,4,5,6};
//uint8_t pd_custom_list_a[4]	= {5,7,8,10};

//uint8_t led_custom_list_b[5]	= {20,21,22,23,24};
//uint8_t pd_custom_list_b[4]	= {16,14,13,11};

//int32_t c_max;
extern volatile bool processing;
extern volatile bool data_tx_in_progress;
extern volatile bool ble_connection_st;


uint32_t buff_cnt;
static int8_t 	pd_no 		= -1;
static int8_t 	led_no 		= -1;
static int8_t	  buf_no		= 0;
static int8_t	  order		= 0;
uint16_t bsel_led_index0 =0;
uint16_t bsel_led_index1 =1;
uint16_t bsel_led_index2 =2;
uint16_t bsel_led_index3 =3;


static int32_t	t_ms		= 0;
int16_t	buff_cycle_buff[buff_LED_NO][buff_CYCLE_CNT] = 
				{
					{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}, //2
					{0, 0, 0, 0, 0, 0, 0, 0}, //3
					{0, 0, 0, 0, 0, 0, 0, 0}, //4
					{0, 0, 0, 0, 0, 0, 0, 0}, //5
					{0, 0, 0, 0, 0, 0, 0, 0}, //6
					{0, 0, 0, 0, 0, 0, 0, 0}, //7
					{0, 0, 0, 0, 0, 0, 0, 0}, //8
					{0, 0, 0, 0, 0, 0, 0, 0}, //9
										{0, 0, 0, 0, 0, 0, 0, 0}, //10
					{0, 0, 0, 0, 0, 0, 0, 0}, //1
					{0, 0, 0, 0, 0, 0, 0, 0}, //2
					{0, 0, 0, 0, 0, 0, 0, 0}, //3
					{0, 0, 0, 0, 0, 0, 0, 0}, //4
					{0, 0, 0, 0, 0, 0, 0, 0}, //5
					{0, 0, 0, 0, 0, 0, 0, 0}, //6
					{0, 0, 0, 0, 0, 0, 0, 0}, //7
					{0, 0, 0, 0, 0, 0, 0, 0}, //8
					{0, 0, 0, 0, 0, 0, 0, 0}, //9
										{0, 0, 0, 0, 0, 0, 0, 0}, //20
					{0, 0, 0, 0, 0, 0, 0, 0}, //1
					{0, 0, 0, 0, 0, 0, 0, 0}, //2
					{0, 0, 0, 0, 0, 0, 0, 0}, //3
					{0, 0, 0, 0, 0, 0, 0, 0}, //4
					{0, 0, 0, 0, 0, 0, 0, 0}, //5
					{0, 0, 0, 0, 0, 0, 0, 0}, //6
					{0, 0, 0, 0, 0, 0, 0, 0}, //7
					{0, 0, 0, 0, 0, 0, 0, 0}, //8
					{0, 0, 0, 0, 0, 0, 0, 0}, //9
										{0, 0, 0, 0, 0, 0, 0, 0}, //30
					{0, 0, 0, 0, 0, 0, 0, 0}, //1
					{0, 0, 0, 0, 0, 0, 0, 0}, //2
					{0, 0, 0, 0, 0, 0, 0, 0}, //3
					{0, 0, 0, 0, 0, 0, 0, 0}, //4
					{0, 0, 0, 0, 0, 0, 0, 0}, //5
					{0, 0, 0, 0, 0, 0, 0, 0}, //6
					{0, 0, 0, 0, 0, 0, 0, 0}, //7
					{0, 0, 0, 0, 0, 0, 0, 0}, //8
					{0, 0, 0, 0, 0, 0, 0, 0}, //9
										{0, 0, 0, 0, 0, 0, 0, 0}, //40
					{0, 0, 0, 0, 0, 0, 0, 0}, //1
					{0, 0, 0, 0, 0, 0, 0, 0}, //2
					{0, 0, 0, 0, 0, 0, 0, 0}, //3
					{0, 0, 0, 0, 0, 0, 0, 0}, //4
					{0, 0, 0, 0, 0, 0, 0, 0}, //5
					{0, 0, 0, 0, 0, 0, 0, 0}, //6
					{0, 0, 0, 0, 0, 0, 0, 0}, //7
					{0, 0, 0, 0, 0, 0, 0, 0}, //8
					{0, 0, 0, 0, 0, 0, 0, 0}, //9
										{0, 0, 0, 0, 0, 0, 0, 0}, //50
					{0, 0, 0, 0, 0, 0, 0, 0}, //1
					{0, 0, 0, 0, 0, 0, 0, 0}, //2
					{0, 0, 0, 0, 0, 0, 0, 0}, //3
					{0, 0, 0, 0, 0, 0, 0, 0}, //4
					{0, 0, 0, 0, 0, 0, 0, 0}, //5
					{0, 0, 0, 0, 0, 0, 0, 0}, //6
					{0, 0, 0, 0, 0, 0, 0, 0}, //7
					{0, 0, 0, 0, 0, 0, 0, 0}, //8
					{0, 0, 0, 0, 0, 0, 0, 0}, //9
										{0, 0, 0, 0, 0, 0, 0, 0}, //60
					{0, 0, 0, 0, 0, 0, 0, 0}, //1
					{0, 0, 0, 0, 0, 0, 0, 0}, //2
					{0, 0, 0, 0, 0, 0, 0, 0}, //3
					{0, 0, 0, 0, 0, 0, 0, 0}, //4
					{0, 0, 0, 0, 0, 0, 0, 0}, //5
					{0, 0, 0, 0, 0, 0, 0, 0}, //6
					{0, 0, 0, 0, 0, 0, 0, 0}, //7
					{0, 0, 0, 0, 0, 0, 0, 0}, //8
					{0, 0, 0, 0, 0, 0, 0, 0}, //9
										{0, 0, 0, 0, 0, 0, 0, 0}, //70
					{0, 0, 0, 0, 0, 0, 0, 0}, //1
					{0, 0, 0, 0, 0, 0, 0, 0}, //2
					{0, 0, 0, 0, 0, 0, 0, 0}, //3
					{0, 0, 0, 0, 0, 0, 0, 0}, //4
					{0, 0, 0, 0, 0, 0, 0, 0}, //5
					{0, 0, 0, 0, 0, 0, 0, 0}, //6
					{0, 0, 0, 0, 0, 0, 0, 0}, //7
					{0, 0, 0, 0, 0, 0, 0, 0}, //8
					{0, 0, 0, 0, 0, 0, 0, 0}, //9
										{0, 0, 0, 0, 0, 0, 0, 0}, //80
					{0, 0, 0, 0, 0, 0, 0, 0}, //1
					{0, 0, 0, 0, 0, 0, 0, 0}, //2
					{0, 0, 0, 0, 0, 0, 0, 0}, //3
					{0, 0, 0, 0, 0, 0, 0, 0}, //4
					{0, 0, 0, 0, 0, 0, 0, 0}, //5
					{0, 0, 0, 0, 0, 0, 0, 0}, //6
					{0, 0, 0, 0, 0, 0, 0, 0}, //7
					{0, 0, 0, 0, 0, 0, 0, 0}, //8
					{0, 0, 0, 0, 0, 0, 0, 0}, //9
										{0, 0, 0, 0, 0, 0, 0, 0}, //90
					{0, 0, 0, 0, 0, 0, 0, 0}, //1
					{0, 0, 0, 0, 0, 0, 0, 0}, //2
					{0, 0, 0, 0, 0, 0, 0, 0}, //3
					{0, 0, 0, 0, 0, 0, 0, 0}, //4
					{0, 0, 0, 0, 0, 0, 0, 0}, //5
					{0, 0, 0, 0, 0, 0, 0, 0}, //6
					{0, 0, 0, 0, 0, 0, 0, 0}, //7
					{0, 0, 0, 0, 0, 0, 0, 0}, //8
					{0, 0, 0, 0, 0, 0, 0, 0}, //9
				};	
int16_t	buff_cycle_send_buff[buff_LED_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, 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};
//int16_t	buff_cycle_send_buff1[buff_LED_NO] = {0, };
//int16_t	buff_cycle_send_buff2[buff_LED_NO] = {0, };
//int16_t	buff_cycle_send_buff3[buff_LED_NO] = {0, };
//int16_t	buff_cycle_send_buff4[buff_LED_NO] = {0, };
//int16_t	buff_cycle_send_buff5[buff_LED_NO] = {0, };
//int16_t	buff_cycle_send_buff6[buff_LED_NO] = {0, };
//int16_t	buff_cycle_send_buff7[buff_LED_NO] = {0, };
//int16_t	buff_cycle_send_buff8[buff_LED_NO] = {0, };
//int16_t	buff_cycle_send_buff9[buff_LED_NO] = {0, };
uint16_t	bi_buff_cycle_send_buff[60][buff_LED_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, 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},
	};
uint16_t	single_bi_buff_cycle_send_buff[buff_LED_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, 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};
#define SAMPLES_IN_BUFFER	4095+32
static nrf_saadc_value_t pd_buff_adc_buf[2][SAMPLES_IN_BUFFER];

//bool pd_adc_custom_a_start	= false;
//bool pd_adc_custom_b_start	= false;
//bool pd_adc_custom_c_start	= false;
//bool pd_adc_custom_d_start	= false;
//bool pd_adc_custom_end		= false;
//bool pd_adc_custom_start = false;
bool pd_adc_buff_start = false;
bool pd_adc_buff_running = false;
bool buff_testing = false;
extern uint16_t m_pd_delay_us;
//
//bool custom_add_data;
extern bool ble_got_new_data;
extern bool motion_raw_data_enabled;
//extern char	ble_tx_buffer[BLE_NUS_MAX_DATA_LEN];
uint8_t buff_bin_buffer[BLE_NUS_MAX_DATA_LEN];					
//add imu






extern which_cmd_t cmd_type_t;

APP_TIMER_DEF(m_buff_check_loop_timer_id);
APP_TIMER_DEF(m_buff_send_loop_timer_id);
#if FEATURE_DELAY
#define CUSTOM_SEND_LOOP_INTERVAL 500
#else
#define BUFF_SEND_LOOP_INTERVAL 100
#endif
#define BUFF_CHECK_LOOP_INTERVAL 1




static nrf_ppi_channel_t	m_ppi_channel;



#if !FEATURE_PRINTF
void buff_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 buff_ppi_uninit(void)
{
    ret_code_t err_code;

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


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


void buff_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 buff_voltage_handler(nrf_drv_saadc_evt_t const * p_event)	/* PD Voltage reading */
{
	ret_code_t err_code;
	int16_t sum = 0;

	uint32_t buff_clk_delay = m_pd_delay_us/16;
		if(ble_connection_st == 0) {
								//imm_adc_end();
								//custom_send_timer_stop();
								printf("Custom 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, buff_samples_in_buffer + buff_clk_delay);
		APP_ERROR_CHECK(err_code);
		
		if(led_no == -1) {
			led_no = 0;
			pd_no = 0;

			led_on(LED_list_buff[led_no]);
			led_pd_matching_value_set(LED_list_buff[led_no]); /* MCP4725 DAC setting and PD on */

		} else {

			for(uint16_t i = buff_clk_delay; i < buff_clk_delay + buff_samples_in_buffer; i++){
				buff_cycle_buff[buf_no][i-buff_clk_delay] = p_event->data.done.p_buffer[i];
			}
#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_custom[led_no], pd_no,  PD_list_custom[pd_no], buf_no);
#endif

			buf_no++;

			 if(pd_no >=  - 1) {
				//pd_no = 0;



				if(led_no < buff_LED_NO - 1) {
					led_no++;
					led_on(LED_list_buff[led_no]);
					led_pd_matching_value_set(LED_list_buff[led_no]); /* MCP4725 DAC setting and PD on */

				} else if(led_no >= buff_LED_NO - 1) {
					pd_no = -1;
					led_no = -1;
#if FEATURE_PRINTF
					printf("\r\nEnded\r\n");
#endif
					//imm_adc_end();
						//imm_adc_end_final();

					///////////////////////////////////////////////////////////////////////////////////////////
					uint8_t k =0;
					sum = 0;
				for(uint16_t i = 0; i < buff_LED_NO; i++){
						for(uint16_t j = 0; j < buff_samples_in_buffer; j++){
							sum += buff_cycle_buff[i][j];
						}
						
						buff_cycle_send_buff[k++] = sum;
						sum = 0;
					}
						
				
					

					buf_no = 0;

					if(ble_connection_st == 0) {
					
					printf("Custom ADC STOP 1");
					pd_adc_buff_start=false;
					pd_adc_buff_running=false;
					printf ("FINISH SEND\r\n");
					processing = false;
					buff_testing = false;
					buff_adc_end_final();
						}
					else {
						
						
						if(cmd_type_t == CMD_UART) {
							//custom_send_timer_stop();
							
						}else if(cmd_type_t == CMD_BLE) {
							//	printf("%d ms \r\n",t_ms);
								//printf("value 4: %d,%d,%d,%d  \r\n",imm_cycle_send_buff[0] ,imm_cycle_send_buff[1] ,imm_cycle_send_buff[2] ,imm_cycle_send_buff[3] );
							 for(uint16_t i = 0; i < buff_LED_NO; i++){
								 printf("%d,",buff_cycle_send_buff[i]);
								 bi_buff_cycle_send_buff[buff_cnt][i]=(uint16_t)(buff_cycle_send_buff[i]);
								 //single_bi_buff_cycle_send_buff[i]=(uint16_t)(buff_cycle_send_buff[i]);
								 
							 }
							 printf("%d ms \r\n",t_ms);
//							format_data(buff_bin_buffer, "rjb:",  bi_buff_cycle_send_buff, 200);
//						
//						
//						binary_tx_handler(buff_bin_buffer,102);
						}

//						if(ble_connection_st == 1) {
//							battery_timer_start();
//						}

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

void buff_check_loop(void * p_context)	
{
	UNUSED_PARAMETER(p_context);
	buff_check_timer_stop();
	if ( buff_testing == false)
	{
		 printf("%d ms \r\n",t_ms);
		//	sprintf(custom_tx_buffer, "Measure Time :%d ms \r\n",t_ms);
	}
	else
	{
		t_ms++;
		buff_check_timer_start();
	}
}

void buff_send_loop(void * p_context)	/* For x ms */
{
	UNUSED_PARAMETER(p_context);
	buff_send_timer_stop();
	
	char resp[4];
		
		if(ble_connection_st == BLE_DISCONNECTED_ST) {
							//	order=0;
							//	full_send_timer_stop();
			buff_adc_end_final();
								printf("BUFF ADC STOP 2\r\n");
						processing = false;
						}
		
	else if(order<4){
			sprintf(resp,"r%02d:", order);
			format_data(buff_bin_buffer, resp,  bi_buff_cycle_send_buff[order],100);
			binary_tx_handler(buff_bin_buffer,102);
			order++;
			buff_send_timer_start();
	}
	else 
	{
		printf("ddddd%d ms \r\n",t_ms);
		processing = false;
		buff_adc_end_final();
		//battery_timer_start();
	}

}	
	
//	switch(order) {
//		case 0:
//			 sprintf(resp,"r%02X:", order);
//			format_data(buff_bin_buffer, resp,  bi_buff_cycle_send_buff[0],100);
//			binary_tx_handler(buff_bin_buffer,102);
//			break;

//		case 1:
//			format_data(buff_bin_buffer, "rb1:",  bi_buff_cycle_send_buff[1],100);
//			binary_tx_handler(buff_bin_buffer,102);
//		
//			break;
//		case 2:
//		format_data(buff_bin_buffer, "rb2:",  bi_buff_cycle_send_buff[2],100);
//			binary_tx_handler(buff_bin_buffer,102);
//		
//			break;

//		case 3:
//		format_data(buff_bin_buffer, "rb3:",  bi_buff_cycle_send_buff[3],100);
//			binary_tx_handler(buff_bin_buffer,102);
//			break;


//			case 4:
//				format_data(buff_bin_buffer, "rb4:",  bi_buff_cycle_send_buff[4],100);
//			binary_tx_handler(buff_bin_buffer,102);
//				break;
//					
//			case 5:
//		format_data(buff_bin_buffer, "rb5:",  bi_buff_cycle_send_buff[5],100);
//			binary_tx_handler(buff_bin_buffer,102);
//				break;
//			
//			
//			case 6:
//			format_data(buff_bin_buffer, "rb6:",  bi_buff_cycle_send_buff[6],100);
//			binary_tx_handler(buff_bin_buffer,102);
//			break;

//		case 7:
//			format_data(buff_bin_buffer, "rb7:",  bi_buff_cycle_send_buff[7],100);
//			binary_tx_handler(buff_bin_buffer,102);
//		
//			break;
//		case 8:
//		format_data(buff_bin_buffer, "rb8:",  bi_buff_cycle_send_buff[8],100);
//			binary_tx_handler(buff_bin_buffer,102);
//		
//			break;

//		case 9:
//		format_data(buff_bin_buffer, "rb9:",  bi_buff_cycle_send_buff[9],100);
//			binary_tx_handler(buff_bin_buffer,102);
//			break;


//	
//			
//			
//			
//			
//				case 10:
//					printf("Measure Time : %d ms \r\n",t_ms);
//		//sprintf(full_tx_buffer, "Measure Time :%d ms \r\n",t_ms);
//		//	data_tx_handler(full_tx_buffer);
//					order = 0;
//					return;
//#if FEATURE_PRINTF
//			printf("full_send Completed\r\n");
//#endif



//		default:
//			
//			break;
//	}



void buff_adc_start_init(void)
{
	if (ble_got_new_data ==true)
		{
			pd_adc_buff_start=false;
			pd_adc_buff_running=false;
					printf ("FINISH SEND\r\n");
				led_off(99);
						pd_off(99);
			processing = false;
			 buff_testing = false;
			buff_adc_end_final();
		}
	
	else if (pd_adc_buff_running==true){
			
		if(buff_cnt>=b_t_cnt){
						led_off(99);
						pd_off(99);
		pd_adc_buff_start=false;
			pd_adc_buff_running=false;
					printf ("FIFNISH SEND\r\n");
			
			 buff_testing = false;
			
			order =0;
			buff_send_timer_start();
		//buff_adc_start();
	}
		else{
			buff_adc_start();
			buff_cnt++;
		}
	}
	else if (pd_adc_buff_start==true)
	{ 
//	    custom_testing = false;
//			motion_raw_data_enabled = true;
//			custom_add_data = true;
//			icm42670_main();
			for(uint8_t i =0; i < 25; i++) {
			LED_list_buff[i*4] = bsel_led_index0;
			LED_list_buff[i*4+1] = bsel_led_index1;
			LED_list_buff[i*4+2] = bsel_led_index2;
		  LED_list_buff[i*4+3] = bsel_led_index3;
//			data_tx_handler(ble_tx_buffer);
			}
			
			for(uint8_t i =0; i < 60; i++) {
				for(uint8_t j =0; j < 100; j++) {
			bi_buff_cycle_send_buff[i][j]=0;
				}
			}
		  t_ms=0; 
			buff_cnt=0;
			pd_adc_buff_start=false;
			pd_adc_buff_running=true;
		  buff_testing = true;
//			custom_check_timer_start();	
		//	c_cnt=0;
//			pd_adc_custom_start=false;
//			pd_adc_custom_a_start=true;
		
			buff_adc_start2();
			buff_check_timer_start();
		//	c_cnt++;
	}
		
	
	
	
	else
		{
		
	}
}



void buff_adc_start(void)
{
	pd_no		= -1;
	led_no		= -1;
	buf_no		= 0;



	for(uint16_t i = 0; i < buff_LED_NO; i++) {
		for(uint16_t j = 0; j < buff_CYCLE_CNT; j++) {
			buff_cycle_buff[i][j] = 0;
		}
	}

	
	

}


void buff_adc_start2(void) 
{
	buff_adc_start();
	buff_adc_init();
	buff_irq_init();
	buff_ppi_init();
	buff_sampling_event_enable();	

}


void buff_adc_end(void) 
{

	printf("adc_end\r\n");

	buff_sampling_event_disable();

}

void buff_adc_end_final(void) 
{

	printf("adc_end_for_good\r\n");

	buff_sampling_event_disable();
	buff_irq_uninit();
	buff_ppi_uninit();
	buff_adc_uninit();
	

}






void buff_adc_init(void)
{
#if FEATURE_PRINTF
	printf("custom_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, buff_voltage_handler);
	APP_ERROR_CHECK(err_code);

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

	err_code = nrf_drv_saadc_buffer_convert(pd_buff_adc_buf[0], buff_samples_in_buffer + m_pd_delay_us/16);
	APP_ERROR_CHECK(err_code);
}



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

	nrf_drv_saadc_uninit();
	nrf_drv_saadc_channel_uninit(0);
}


#if !FEATURE_PRINTF
void buff_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 buff_irq_uninit(void)
{
	nrfx_gpiote_in_event_disable(ADA2200_SYNCO_PIN);
	nrfx_gpiote_in_uninit(ADA2200_SYNCO_PIN);
}
#endif


void buff_send_timer_start(void)
{
	APP_ERROR_CHECK(app_timer_start(m_buff_send_loop_timer_id, APP_TIMER_TICKS(BUFF_SEND_LOOP_INTERVAL), NULL));
}


void buff_send_timer_stop(void)
{
	APP_ERROR_CHECK(app_timer_stop(m_buff_send_loop_timer_id));

}


void buff_send_timer_init(void)
{
    APP_ERROR_CHECK(app_timer_create(&m_buff_send_loop_timer_id, APP_TIMER_MODE_SINGLE_SHOT, buff_send_loop));
}


void buff_check_timer_start(void)
{
	APP_ERROR_CHECK(app_timer_start(m_buff_check_loop_timer_id, APP_TIMER_TICKS(BUFF_CHECK_LOOP_INTERVAL), NULL));
}

void buff_check_timer_stop(void)
{
	APP_ERROR_CHECK(app_timer_stop(m_buff_check_loop_timer_id));

}
void buff_check_timer_init(void)
{
    APP_ERROR_CHECK(app_timer_create(&m_buff_check_loop_timer_id, APP_TIMER_MODE_SINGLE_SHOT, buff_check_loop));
}
