/****************************************************************************************************
* created by sunpeng 2017 all rights reserved!
****************************************************************************************************/
#include "include.h"
#include "ADC.h"
#include "delay.h"
#include "wireless.h"
#include "uart_api.h"
#include "power.h"

#include "nrf.h"
#if ARCH_MODE == NRF52832_Slave_Mode 
#include "nrf_drv_saadc.h"
#include "nrf_drv_ppi.h"
#include "nrf_drv_timer.h"
#include "beep.h"

#elif ARCH_MODE == NRF51822_Peripheral_Mode
#include "nrf_drv_adc.h"

#elif ARCH_MODE == NRF51802_Peripheral_Mode
#include "app_util_platform.h"
#include "nrf_adc.h"
#include "nrf_soc.h"
//#include "ledGam.h"
//#include "rRecordGam.h"
#include "beep.h"

#endif

#define ADC1_DR_Address    ((u32)0x4001244C)

#if ARCH_MODE == CC254x_Slave_Mode
/**************************************************************************************/
//电阻编号存储偏移量 对应位置 颜色块类型		块类型【1=角块，2=边块】
int TypeNumPort[sAllColorMax] = {	
					1,2,1,2,1,2,1,2,
					1,2,1,2,1,2,1,2,
					1,2,1,2,1,2,1,2,
					1,2,1,2,1,2,1,2,
					1,2,1,2,1,2,1,2,
					1,2,1,2,1,2,1,2	
					};	


//	电阻全局编号 相同位置的 电阻值颜色首字母代号，用于颜色机器常数设定
char sAllSeqColorTab[sAllColorMax] = {	
					'G','R','Y','O','W','B','G','B',
					'G','R','Y','O','W','B','G','B',
					'G','R','Y','O','W','B','G','B',
					'G','R','Y','O','W','B','G','B',
					'G','R','Y','O','W','B','G','B',
					'G','R','Y','O','W','B','G','B',
					};

// 存储位置从48到54+5 转换	(每个面9个色块为一组，每组之间用“|”分割)				
char sAllSeqOffsetTab[sAllColorMax] = {  
					0,1,2,3, 5,6,7,8, 
					10,11,12,13, 15,16,17,18,
					20,21,22,23, 25,26,27,28,
					30,31,32,33, 35,36,37,38,
					40,41,42,43, 45,46,47,48,
					50,51,52,53, 55,56,57,58						
					};
#endif
					
//根据电阻编号存储偏移量 存储对应颜色首字母代号
//手动设置出厂机器常数，保证面顺序1-6，块顺序1-9 存储					
char sAllColorBuf[sAllColorFullMax+1] = {'|'}; 

#if ARCH_MODE == ESP8266_Station_Mode
/************************************************************************************/
u16 ADC_Value[ADC_CHANNEL_MAX]={0x0};	  //AD传感器值
u16 angle_tab[ADC_CHANNEL_MAX]={0x0};     //AD传感器转换后角度值
#endif
static uint16_t battery_voltage_percent = 101;		//电池电压百分比

//转换电压到电量百分比
static void battery_voltage_transfer(u16 battery_voltage, u16 *voltage_percent);
//根据电池电量，做出相应应用处理
static void battery_applications(u16 voltage_percent);

#if ARCH_MODE == NRF51802_Peripheral_Mode


//设置ADC通道配对的数字IO输出高电平
static s8 set_adc_select_channel_paire_io(ADC_CHANNEL_NUM_ENUM channel);
//设置ADC通道配对的数字IO输出低电平
static s8 clear_adc_select_channel_paire_io(ADC_CHANNEL_NUM_ENUM channel);

static void adc_select_channel(ADC_CHANNEL_NUM_ENUM channel);

#endif

#if ARCH_MODE == NRF52832_Slave_Mode 
/**************************************************************************************/
#define 	SAMPLES_IN_BUFFER		1
volatile uint8_t state = 1;

static const nrf_drv_timer_t TIMER_SAADC = NRF_DRV_TIMER_INSTANCE(4);
static nrf_saadc_value_t     m_buffer_pool[2][SAMPLES_IN_BUFFER];
static nrf_ppi_channel_t     m_ppi_channel;
static uint32_t              m_adc_evt_counter;

void timer_handler(nrf_timer_event_t event_type, void * p_context)
{

}


void saadc_sampling_event_init(void)
{
    ret_code_t err_code;

    err_code = nrf_drv_ppi_init();
    APP_ERROR_CHECK(err_code);

    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32;
    err_code = nrf_drv_timer_init(&TIMER_SAADC, &timer_cfg, timer_handler);
    APP_ERROR_CHECK(err_code);

    /* setup TIMER_SAADC for compare event every 400ms */
    uint32_t ticks = nrf_drv_timer_ms_to_ticks(&TIMER_SAADC, 2777);
    nrf_drv_timer_extended_compare(&TIMER_SAADC,
                                   NRF_TIMER_CC_CHANNEL4,
                                   ticks,
                                   NRF_TIMER_SHORT_COMPARE4_CLEAR_MASK,
                                   false);
    nrf_drv_timer_enable(&TIMER_SAADC);

    uint32_t timer_compare_event_addr = nrf_drv_timer_compare_event_address_get(&TIMER_SAADC,
                                                                                NRF_TIMER_CC_CHANNEL4);
    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,
                                          timer_compare_event_addr,
                                          saadc_sample_task_addr);
    APP_ERROR_CHECK(err_code);
}

/**********************************************************************************************
 * 描  述 : uninit_saadc_timer函数
 * 参  数 : 无
 * 返回值 : 无
 ***********************************************************************************************/ 
void uninit_saadc_timer(void)
{
	nrf_drv_timer_uninit(&TIMER_SAADC);
	NRF_SAADC->INTENCLR = (SAADC_INTENCLR_END_Clear << SAADC_INTENCLR_END_Pos);
    NVIC_ClearPendingIRQ(SAADC_IRQn);
	
}

/**********************************************************************************************
 * 描  述 : enable_saadc_timer函数
 * 参  数 : 无
 * 返回值 : 无
 ***********************************************************************************************/ 
void enable_saadc_timer(void)
{
    //启动定时器
    nrf_drv_timer_enable(&TIMER_SAADC);
}

/**********************************************************************************************
 * 描  述 : disable_saadc_timer函数
 * 参  数 : 无
 * 返回值 : 无
 ***********************************************************************************************/ 
void disable_saadc_timer(void)
{
    //禁用定时器
    nrf_drv_timer_disable(&TIMER_SAADC);
}

void saadc_sampling_event_enable(void)
{
    ret_code_t err_code = nrf_drv_ppi_channel_enable(m_ppi_channel);

    APP_ERROR_CHECK(err_code);
}

uint16_t bat_vol = 0;
void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
    if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
    {
        ret_code_t err_code;

        err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);

		//ADC0  测量电池电压 
		bat_vol = p_event->data.done.p_buffer[0];// / 1000;
		
		battery_voltage_transfer(bat_vol, &battery_voltage_percent);		//转换电压到电量百分比
		
		battery_applications(battery_voltage_percent);			//电量处理
		
        m_adc_evt_counter++;
    }
}


void saadc_init(void)
{
    ret_code_t err_code;
    nrf_saadc_channel_config_t channel_config =
        NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_VDD);

    err_code = nrf_drv_saadc_init(NULL, saadc_callback);
    APP_ERROR_CHECK(err_code);

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

    err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAMPLES_IN_BUFFER);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAMPLES_IN_BUFFER);
    APP_ERROR_CHECK(err_code);

}
#elif ARCH_MODE == NRF51822_Peripheral_Mode


#endif
/**************************************************************************************/

/*******************************************************************************
	函数名：ADC1_Configuration
	输  入:
	输  出:
	功能说明：配置ADC1
********************************************************************************/
void ADC1_Configuration(void)
{
#if ARCH_MODE == NRF52832_Slave_Mode
    saadc_init();
    saadc_sampling_event_init();
    saadc_sampling_event_enable();
#elif ARCH_MODE == NRF51822_Peripheral_Mode || ARCH_MODE == NRF51802_Peripheral_Mode
    adc_config();

#endif	
}

//手动触发电源电压测量
void trig_adc_sample_for_power_measure(void)
#if ARCH_MODE == NRF51822_Peripheral_Mode 
{
	nrf_drv_adc_sample();
}
#elif ARCH_MODE == NRF51802_Peripheral_Mode
{
	switch_to_next_adc_channel();
	nrf_adc_start();
}
#else
{}	
#endif

#if ARCH_MODE == NRF51802_Peripheral_Mode
void cycle_upload_all_adc_channel_values(void)
{
	
	sprintf((char *)debug_str, "ADC All:%d|%d|%d|%d|%d|%d|%d",
						gaacv.side[0].adcVal,
						gaacv.side[1].adcVal,
						gaacv.side[2].adcVal,
						gaacv.side[3].adcVal,
						gaacv.side[4].adcVal,
						gaacv.side[5].adcVal,
						gaacv.side[6].adcVal
			);
	print_wifi(debug_str);
}
	
#endif
	
//获取电池电压百分比
s8 GetBatteryVoltagePercent(u8 *percent)
{
	if(NULL == percent)
	{
		return false;
	}
	*percent = battery_voltage_percent;
	return true;
}

//转换电压到电量百分比
void battery_voltage_transfer(u16 battery_voltage, u16 *voltage_percent)
{
#if ARCH_MODE == NRF51802_Peripheral_Mode
	u16 battery_max = 80;		//120
	u16 battery_min = 4;		//5
#else
	u16 battery_max = 980;
	u16 battery_min = 840;
#endif
	if(battery_voltage >= battery_max)			//电量上限
	{
		*voltage_percent = 100;
	}
	else if(battery_voltage < battery_min)		//电量下限
	{
		*voltage_percent = 0;
	}
	else
	{
		*voltage_percent = ((battery_voltage-battery_min)*100)/(battery_max - battery_min);
	}
}

//根据电池电量，做出相应应用处理
void battery_applications(u16 voltage_percent)
{
	//检测到电量低于5%，自动关机
	if(voltage_percent < 15)
	{
#if HW_VOICE_TYPE == HW_VOICE_TYPE_BEEP
		//ring_notice(MUSIC_NOTICE_TYPE_game_over, 1);
#endif
		enterSystemOff();	//关机
	}
	
#if ARCH_MODE == NRF52832_Slave_Mode
	//电量下降后，主动调低扬声器声音以节省电量
	if(voltage_percent < 80)
	{
#if HW_VOICE_TYPE == HW_VOICE_TYPE_SPEAKER
		setSpeakerVoiceLower.flag = true;
#endif
	}
#endif
}

/**
  * @brief  ADC_Value_Convert_String
  * @param  None
  * @retval None
  */
void ADC_Value_Convert_String(u16 adc1_value,u8 *str)
{
    //u8 ADC_Value_Str[] = "0000";
    u8 Num_Tab[] = "0123456789";
    u16 tmp = 0;
    
    tmp = adc1_value/1000;
    *str = Num_Tab[tmp];
    tmp = adc1_value%1000/100;
    *(str+1) = Num_Tab[tmp];
    tmp = adc1_value%1000%100/10;
    *(str+2) = Num_Tab[tmp];
    tmp = adc1_value%1000%100%10;
    *(str+3) = Num_Tab[tmp];

}

//所有通道转动电阻检测一圈最大ADC数值，用来等比例计算角度
u16 AllChanRotResMaxTab[ADC_CHAN_SIGN_MAX] = {1800,1800,1800,1800,1800,1800};
/**
  * @brief  ADC_RotationRes_Convert_to_AngleNum
  * @param  axis: 对应轴编号
  * @param  adc1_value: 对应ADC值
  * @retval [0，359] 电阻电位器，串联10k分压电阻，本身9k左右
  */
int ADC_RotationRes_Convert_to_AngleNum(u8 axis, u16 adc1_value)
{
	int num = 0;
	u16 ChanRotResMax = 0;
	if(axis >= ADC_CHAN_SIGN_MAX)
	{
		return 0;
	}
	
	if(adc1_value >= AllChanRotResMaxTab[axis])
	{
		AllChanRotResMaxTab[axis] = adc1_value + 1;
		//num = 0;
	}
	
	ChanRotResMax = AllChanRotResMaxTab[axis];
	num = (350*adc1_value)/ChanRotResMax;

	return num;
}

//所有转动通道角度上一次实际上传的角度 
u16 AllChanRotAnglePreTab[ADC_CHAN_SIGN_MAX] = {0};
/**
  * @brief  RotationAngle_Other_Process 处理角度超范围、变动精度限制等
  * @param  axis: 对应轴编号
  * @param  angle: 对应轴角度值 【0,359】
  * @retval [0，359] 
  */
u16 RotationAngle_Other_Process(u8 axis, u16 angle)
{	
	u16 angle_rtn = angle;
	if(axis >= ADC_CHAN_SIGN_MAX)
	{
		return angle_rtn;
	}
	
	if(angle_rtn > 348)
	{
		angle_rtn = 0;
	}
	
	if(3 < minus_abs_u16_u16(angle_rtn, AllChanRotAnglePreTab[axis]))
	{
		AllChanRotAnglePreTab[axis] = angle_rtn;
	}
	else
	{
		angle_rtn = AllChanRotAnglePreTab[axis];
	}
	
	return angle_rtn;
}

/**
  * @brief  ADC_Value_Convert_to_SeqNum
  * @param  adc1_value: 对应ADC值
  * @retval [0，359] 步进=5  表示所在旋转角度
  */
int ADC_Value_Convert_to_AngleNum(u16 adc1_value)
{
    u16 adc =0 ;
	int num = 0;
    adc = adc1_value;
//20k
	if((     adc >  0	    )&&(adc < 51  )) num = 5  ;
	else if((adc >= 51	    )&&(adc < 101  )) num = 10 ;
	else if((adc >= 101	    )&&(adc < 152  )) num = 15 ;
	else if((adc >= 152	    )&&(adc < 202  )) num = 20 ;
	else if((adc >= 202	    )&&(adc < 253  )) num = 25 ;
	else if((adc >= 253	    )&&(adc < 303  )) num = 30 ;
	else if((adc >= 303	    )&&(adc < 354  )) num = 35 ;
	else if((adc >= 354	    )&&(adc < 405  )) num = 40 ;
	else if((adc >= 405	    )&&(adc < 455  )) num = 45 ;
	else if((adc >= 455	    )&&(adc < 506  )) num = 50 ;
	else if((adc >= 506	    )&&(adc < 556  )) num = 55 ;
	else if((adc >= 556	    )&&(adc < 607  )) num = 60 ;
	else if((adc >= 607	    )&&(adc < 657  )) num = 65 ;
	else if((adc >= 657	    )&&(adc < 708  )) num = 70 ;
	else if((adc >= 708	    )&&(adc < 759  )) num = 75 ;
	else if((adc >= 759	    )&&(adc < 809  )) num = 80 ;
	else if((adc >= 809	    )&&(adc < 860  )) num = 85 ;
	else if((adc >= 860	    )&&(adc < 910  )) num = 90 ;
	else if((adc >= 910	    )&&(adc < 961  )) num = 95 ;
	else if((adc >= 961	    )&&(adc < 1011  )) num = 100;
	else if((adc >= 1011    )&&(adc < 1062  )) num = 105;
	else if((adc >= 1062    )&&(adc < 1112  )) num = 110;
	else if((adc >= 1112    )&&(adc < 1163  )) num = 115;
	else if((adc >= 1163    )&&(adc < 1214  )) num = 120;
	else if((adc >= 1214    )&&(adc < 1264  )) num = 125;
	else if((adc >= 1264    )&&(adc < 1315  )) num = 130;
	else if((adc >= 1315    )&&(adc < 1365  )) num = 135;
	else if((adc >= 1365    )&&(adc < 1416  )) num = 140;
	else if((adc >= 1416    )&&(adc < 1466  )) num = 145;
	else if((adc >= 1466    )&&(adc < 1517  )) num = 150;
	else if((adc >= 1517    )&&(adc < 1568  )) num = 155;
	else if((adc >= 1568    )&&(adc < 1618  )) num = 160;
	else if((adc >= 1618    )&&(adc < 1669  )) num = 165;
	else if((adc >= 1669    )&&(adc < 1719  )) num = 170;
	else if((adc >= 1719    )&&(adc < 1770  )) num = 175;
	else if((adc >= 1770    )&&(adc < 1820  )) num = 180;
	else if((adc >= 1820    )&&(adc < 1871  )) num = 185;
	else if((adc >= 1871    )&&(adc < 1922  )) num = 190;
	else if((adc >= 1922    )&&(adc < 1972  )) num = 195;
	else if((adc >= 1972    )&&(adc < 2023  )) num = 200;
	else if((adc >= 2023    )&&(adc < 2073  )) num = 205;
	else if((adc >= 2073    )&&(adc < 2124  )) num = 210;
	else if((adc >= 2124    )&&(adc < 2174  )) num = 215;
	else if((adc >= 2174    )&&(adc < 2225  )) num = 220;
	else if((adc >= 2225    )&&(adc < 2276  )) num = 225;
	else if((adc >= 2276    )&&(adc < 2326  )) num = 230;
	else if((adc >= 2326    )&&(adc < 2377  )) num = 235;
	else if((adc >= 2377    )&&(adc < 2427  )) num = 240;
	else if((adc >= 2427    )&&(adc < 2478  )) num = 245;
	else if((adc >= 2478    )&&(adc < 2528  )) num = 250;
	else if((adc >= 2528    )&&(adc < 2579  )) num = 255;
	else if((adc >= 2579    )&&(adc < 2630  )) num = 260;
	else if((adc >= 2630    )&&(adc < 2680  )) num = 265;
	else if((adc >= 2680    )&&(adc < 2731  )) num = 270;
	else if((adc >= 2731    )&&(adc < 2781  )) num = 275;
	else if((adc >= 2781    )&&(adc < 2832  )) num = 280;
	else if((adc >= 2832    )&&(adc < 2882  )) num = 285;
	else if((adc >= 2882    )&&(adc < 2933  )) num = 290;
	else if((adc >= 2933    )&&(adc < 2984  )) num = 295;
	else if((adc >= 2984    )&&(adc < 3034  )) num = 300;
	else if((adc >= 3034    )&&(adc < 3085  )) num = 305;  //simulate below
	else if((adc >= 3085    )&&(adc < 3135  )) num = 310;
	else if((adc >= 3135    )&&(adc < 3186  )) num = 315;
	else if((adc >= 3186    )&&(adc < 3236  )) num = 320;
	else if((adc >= 3236    )&&(adc < 3287  )) num = 325;
	else if((adc >= 3287    )&&(adc < 3337  )) num = 330;
	else if((adc >= 3337    )&&(adc < 3388  )) num = 335;
	else if((adc >= 3388    )&&(adc < 3439  )) num = 340;
	else if((adc >= 3439    )&&(adc < 3489  )) num = 345;
	else if((adc >= 3489    )&&(adc < 3540  )) num = 350;
	else if((adc >= 3540    )&&(adc < 3590  )) num = 355;
	else if((adc >= 3590    )&&(adc < 3641  )) num = 360;
	else num = 360;
	return num;
}

#if ARCH_MODE == ESP8266_Station_Mode
//#define ADC_DEBUG
/**
  * @brief  rotation_detect.
  * @param  None
  * @retval None
  */
void rotation_detect(void)
{
	//int angle=0;
#ifdef ALL_SIDE_CHIP
	
#else
	//------------------------------------------------------------
	int Chan = 0;
	
#ifdef ADC_DEBUG  //调试模式下，直接发送获取的电压值，不带esp8266发送格式
	char ADC_Value_Str[] = "     ";
	for(Chan=0;Chan<ADC_CHANNEL_MAX;Chan++)//send 32bytes ADC values
	{
		ADC_Value_Convert_String(ADC_Value[Chan], ADC_Value_Str);
		printf("%s",ADC_Value_Str);
		//Sta_Send(sta_id,5,ADC_Value_Str);
		//RfSend(41,ADC_Value_Str,getStrLen(ADC_Value_Str));
	}
	printf("\n");  //TEST
	delay_ms(500); //test 
	//Sta_Send(sta_id,2,"\r\n");
	
#else
	char RotationAngle_Value_Str[128] = {0x0};
	for(Chan=0;Chan<ADC_CHANNEL_MAX;Chan++)//send 32bytes ADC values  ADC_CHANNEL_MAX
	{
		angle_tab[Chan] = ADC_Value_Convert_to_AngleNum(ADC_Value[Chan]);
		//NumValue_Convert_String_4Bit(angle, RotationAngle_Value_Str);
	}
	if(true == rotation_upload_en_Flag)
	{
		sprintf(RotationAngle_Value_Str,
				"%c|%d|%c|%d|%c|%d|%c|%d|%c|%d|%c|%d",  
							ADC_CHAN_SIGN[0],angle_tab[0],
							ADC_CHAN_SIGN[1],angle_tab[1],
							ADC_CHAN_SIGN[2],angle_tab[2],
							ADC_CHAN_SIGN[3],angle_tab[3],
							ADC_CHAN_SIGN[4],angle_tab[4],
							ADC_CHAN_SIGN[5],angle_tab[5]
					);
		//RfSend(41,RotationAngle_Value_Str,getStrLen(RotationAngle_Value_Str));
		RfSend(41,RotationAngle_Value_Str,getStrLen(RotationAngle_Value_Str));
		//print_wifi(RotationAngle_Value_Str);
	}
	
#endif
	
#endif            

}
#endif

#if ARCH_MODE == CC254x_Slave_Mode
/*************************************************************
	根据数据来自哪个端口和数据排序序号，获取电阻编号存储偏移量及所属块类型编号
*************************************************************/
int get_Res_Seq_Color(	u8 comport, 	//消息来自哪个端口  【1,2】
						u8 num,     	//数据排序序号      【0，SeqNumPort1Max】或【0，SeqNumPort2Max】
						u8 *SeqOffset, 	//获取的电阻编号存储偏移量
						u8 *TypeNum 	//获取的电阻编号对应块类型 【1=角块 2=边块】
						)
{
	int PortOffset = 0;
	//-------电阻编号存储偏移量通过计算得出---------
	if(1 == comport)
	{	
		PortOffset = SeqNumPort1Offset + num;
	}
	else if(2 == comport)
	{
		PortOffset = SeqNumPort2Offset + num;
	}
	else
	{
		return false;
	}

	*SeqOffset = PortOffset; 
	//-------根据 电阻编号存储偏移量  获取对应颜色块类型----------
	*TypeNum = TypeNumPort[PortOffset];
	return true;
}
#endif

/**
  * @brief  ADC_Value_Convert_to_SeqNum
  * @param  type		:（块类型）1=角块 2=棱块  adc1_value: 对应ADC值
  * @param  adc1_value	: ADC采样原始值
  * @retval 			：ADC值对应的编号 【1,48】
  */
int ADC_Value_Convert_to_SeqNum(int type, u16 adc1_value)
{
    u16 adc =0 ;
    int num = 0;
    adc = adc1_value;
    //u8 *str2 = "0   ";

    if(1 == type)
    {
        if((     adc >= 14    )&&(adc < 128 )) num = 1 ;
        else if((adc >= 128    )&&(adc < 298 )) num = 2 ;
        else if((adc >= 298    )&&(adc < 468 )) num = 3 ;
        else if((adc >= 468    )&&(adc < 638 )) num = 4 ;
        else if((adc >= 638    )&&(adc < 808 )) num = 5 ;
        else if((adc >= 808    )&&(adc < 978 )) num = 6 ;
        else if((adc >= 978    )&&(adc < 1148)) num = 7 ;
        else if((adc >= 1148   )&&(adc < 1318)) num = 8 ;
        else if((adc >= 1318   )&&(adc < 1488)) num = 9 ;
        else if((adc >= 1488   )&&(adc < 1658)) num = 10;
        else if((adc >= 1658   )&&(adc < 1828)) num = 11;
        else if((adc >= 1828   )&&(adc < 1998)) num = 12;
        else if((adc >= 1998    )&&(adc < 2168)) num = 13;
        else if((adc >= 2168    )&&(adc < 2338)) num = 14;
        else if((adc >= 2338    )&&(adc < 2508)) num = 15;
        else if((adc >= 2508    )&&(adc < 2678)) num = 16;
        else if((adc >= 2678    )&&(adc < 2848)) num = 17;
        else if((adc >= 2848    )&&(adc < 3018)) num = 18;
        else if((adc >= 3018    )&&(adc < 3188)) num = 19;
        else if((adc >= 3188    )&&(adc < 3358)) num = 20;
        else if((adc >= 3358    )&&(adc < 3528)) num = 21;
        else if((adc >= 3528    )&&(adc < 3698)) num = 22;
        else if((adc >= 3698    )&&(adc < 3868)) num = 23;
        else if((adc >= 3868    )&&(adc < 4038)) num = 24;
        else num = 0;
    }
    else
    {
        if((     adc >= 14    )&&(adc < 128 )) num = 25;
        else if((adc >= 128    )&&(adc < 298 )) num = 26;
        else if((adc >= 298    )&&(adc < 468 )) num = 27;
        else if((adc >= 468    )&&(adc < 638 )) num = 28;
        else if((adc >= 638    )&&(adc < 808 )) num = 29;
        else if((adc >= 808    )&&(adc < 978 )) num = 30;
        else if((adc >= 978    )&&(adc < 1148)) num = 31;
        else if((adc >= 1148    )&&(adc < 1318)) num = 32;
        else if((adc >= 1318    )&&(adc < 1488)) num = 33;
        else if((adc >= 1488    )&&(adc < 1658)) num = 34;
        else if((adc >= 1658    )&&(adc < 1828)) num = 35;
        else if((adc >= 1828    )&&(adc < 1998)) num = 36;
        else if((adc >= 1998    )&&(adc < 2168)) num = 37;
        else if((adc >= 2168    )&&(adc < 2338)) num = 38;
        else if((adc >= 2338    )&&(adc < 2508)) num = 39;
        else if((adc >= 2508    )&&(adc < 2678)) num = 40;
        else if((adc >= 2678    )&&(adc < 2848)) num = 41;
        else if((adc >= 2848    )&&(adc < 3018)) num = 42;
        else if((adc >= 3018    )&&(adc < 3188)) num = 43;
        else if((adc >= 3188    )&&(adc < 3358)) num = 44;
        else if((adc >= 3358    )&&(adc < 3528)) num = 45;
        else if((adc >= 3528    )&&(adc < 3698)) num = 46;
        else if((adc >= 3698    )&&(adc < 3868)) num = 47;
        else if((adc >= 3868    )&&(adc < 4038)) num = 48;
        else num = 0;
    
    }
    

    //------------------------------
    return num;
}


/*********************************************end of file*********************************************/
