/*
 * 函数:信号量接收
 * 描述:接收信号量,LED反转
 * 接口参数:无
 * 返回:无
 * 备注:
 */
void Sem_Control_Led2(void* arg)
{
	ADI_GPIO_RESULT result;
	static uint8_t gpioMemory[ADI_GPIO_CALLBACK_MEM_SIZE];
	uint32_t gpioMaxCallbacks;
	uint32_t GPIO_Data;
	OS_ERR err;

	/* set GPIO output LED 3*/
	result = adi_gpio_SetDirection(
		    		    	    ADI_GPIO_PORT_G,
		    		    	    ADI_GPIO_PIN_12,
		    		    	    ADI_GPIO_DIRECTION_OUTPUT);
	printf("LED2 has be ready.\n");
	adi_gpio_Toggle(ADI_GPIO_PORT_G, ADI_GPIO_PIN_12);
	//CheckResult(result, "adi_gpio_SetDirection");
	while(1)
	{
		OSSemPend(&g_Sem_Uart_2,0u, OS_OPT_PEND_BLOCKING, NULL, &err);
		adi_gpio_Toggle(ADI_GPIO_PORT_G, ADI_GPIO_PIN_12);


	}

}
/*
 * GPIO event Callback function
 */
static void pinDIntCallback(ADI_GPIO_PIN_INTERRUPT ePinInt, uint32_t PinIntData,  void *pCBParam)
{
	OS_ERR err;
	switch(ePinInt)
	{
	case ADI_GPIO_PIN_INTERRUPT_2:
		/* push button 1 */
		if ((PinIntData & ADI_GPIO_PIN_14)&&(PinIntData & ADI_GPIO_PIN_2))//两个中断都产生之后再进行信号量发送
		{
			adi_gpio_Toggle(ADI_GPIO_PORT_E, ADI_GPIO_PIN_14);
			OSSemPost(&g_Sem_7793_Run, OS_OPT_POST_1, &err);
//			/* toggle LED 1 */
//			adi_gpio_Toggle(ADI_GPIO_PORT_E, ADI_GPIO_PIN_14);
//			printf("pinIntG11");
		}
		if (PinIntData & ADI_GPIO_PIN_2)
		{
//			/* toggle LED 1 */
			adi_gpio_Toggle(ADI_GPIO_PORT_E, ADI_GPIO_PIN_14);
//			printf("pinIntG11");
			OSSemPost(&g_Sem_7793_Run, OS_OPT_POST_1, &err);
		}

		break;
//	case ADI_GPIO_PIN_INTERRUPT_3:
//		/* push button 2 */
//		if (PinIntData & ADI_GPIO_PIN_1)
//		{
//			/* toggle LED 2 */
//			adi_gpio_Toggle(ADI_GPIO_PORT_G, ADI_GPIO_PIN_15);
//		}
//		break;
	default:
		break;
	}

}
/*GPT1 timer callback contains the sensor st8-mc*/
static void GPTimer1Callback(void *pCBParam, uint32_t Event, void *pArg)
{
    ADI_ADC_RESULT  adcResult = ADI_ADC_SUCCESS;
    ADI_ADC_BUFFER Buffer;
    adi_tmr_Enable(hDevice1,false); 
  switch(Event)
  {
      case ADI_TMR_EVENT_TIMEOUT:
        switch (curr_state) {
        
	
	/* st8 0 : CO sensor - heater ON for 980 ms*/	
        case 0 : 
          
            if (cnt_samples >= CNT_SAMPLES_AVG) 
             {
               curr_state = 10;//go to PM2.5 sensor - begin by turning on the fan [st8 10]
               cnt_samples = 0;
               adi_gpio_SetHigh(LED3);
               adi_gpio_SetHigh(LED4);
               
	       adi_gpio_SetLow(CO_HEATER);
               adi_gpio_SetLow(CO_SENSE);

               adi_gpio_SetHigh(PM25_FAN);
               adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_500MSEC);
               adi_tmr_Enable(hDevice1,true); 
               
             }
            else
            {
              adi_gpio_SetHigh(CO_HEATER);
              
	      adi_gpio_SetLow(LED3);
              adi_gpio_Toggle(DBG_ST8_PIN);

              /*Wait until 2 heater-cycles of 490 ms are done - i.e., wait for heater to be ON for 980 ms as per sensor spec.
	        (limitation of GPT1 clock frequency - cannot count 980 ms in one go)*/
	      if (cnt_co_heater_cycles == 1)
              {
                curr_state = 1;  		  
	      }
	      else
	      {            
                curr_state = 0;
	      }
              cnt_co_heater_cycles++;
              adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_490MSEC);
              adi_tmr_Enable(hDevice1,true); 
           }   
           break;
         
       /* st8 1 : CO sensor - sense circuit ON for 2.5 ms*/	
       case 1 :
               adi_gpio_SetHigh(CO_SENSE);
               adi_gpio_Toggle(DBG_ST8_PIN);
               
               curr_state = 2;
               adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_2p5MSEC);    
               adi_tmr_Enable(hDevice1,true); 
              
         break;
         
       /* st8 2 : CO sensor - trigger ADC sampling on channel-2 */	
       case 2 :
         
                     /* Populate the buffer structure */
             Buffer.nBuffSize = sizeof(ADC_DataBuffer);
             Buffer.nChannels = ADI_ADC_CHANNEL_3;                                           
             Buffer.nNumConversionPasses = ADC_NUM_SAMPLES;
             Buffer.pDataBuffer = ADC_DataBuffer;
    
             /* Submit the buffer to the driver */
             adcResult = adi_adc_SubmitBuffer (hDevice, &Buffer);
             adi_gpio_Toggle(DBG_ST8_PIN);
             adi_gpio_SetHigh(DBG_ADC_PIN);
             adi_gpio_SetLow(LED4);
                          
             curr_state = 3;
             adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_2p5MSEC);
             adi_tmr_Enable(hDevice1,true);                         
            
         break;         
       
       /* st8 2 : CO sensor - heater and sense circuit OFF - wait for 14ms before taking next CO measurement */	
        case 3 :
             adi_gpio_SetLow(CO_HEATER);
             adi_gpio_SetLow(CO_SENSE);
             
             adi_gpio_SetHigh(LED3);
             adi_gpio_Toggle(DBG_ST8_PIN);
             
             cnt_co_heater_cycles = 0;
             curr_state = 0;
             adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_14MSEC);
             adi_tmr_Enable(hDevice1,true); 
                      
         break; 

	/*st8 10: PM2.5 sensor FAN ON */	
        case 10 :
          /*Wait until 4 cycles of 500 ms are done - i.e., wait for fan to be ON for 2s [actually needs to be on for 10s as per sensor spec,
	   * but trying to save power by reducing fan ON time]*/
          if (cnt_fan_cycles == (NUM_FAN_500MS_CYCLES - 1))
          {
            curr_state = 4;  		  
	  }
	  else
	  {            
            curr_state = 10;
	  }
          cnt_fan_cycles++;
          adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_500MSEC);
          adi_tmr_Enable(hDevice1,true); 
          break;

	/*st8 4: PM2.5 sensor LED ON for 0.28ms */	
       case 4 : 
          
            if (cnt_samples >= CNT_SAMPLES_AVG) 
             {
               adi_tmr_Enable(hDevice1, false);
               adi_gpio_SetHigh(LED3);
               adi_gpio_SetHigh(LED4);
               adi_gpio_SetLow(PM25_LED);
               adi_gpio_SetLow(PM25_FAN);
               //cnt_samples = 0;                                                              //change
             }
            else
            {
              adi_gpio_SetHigh(PM25_LED);
              
	      adi_gpio_SetLow(LED3);
              adi_gpio_Toggle(DBG_ST8_PIN);

              curr_state      = 5;
              adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_0p28MSEC);
              adi_tmr_Enable(hDevice1,true); 
           }   
           break;         
	
	/*st8 5: PM2.5 sensor - trigger ADC sampling on channel-3 */	
        case 5 :
             adi_gpio_SetLow(LED4);
             /* Populate the buffer structure */
             Buffer.nBuffSize = sizeof(ADC_DataBuffer);
             Buffer.nChannels = ADI_ADC_CHANNEL_2;                                             
             Buffer.nNumConversionPasses = ADC_NUM_SAMPLES;
             Buffer.pDataBuffer = ADC_DataBuffer;
    
             /* Submit the buffer to the driver */
             adcResult = adi_adc_SubmitBuffer (hDevice, &Buffer);
             adi_gpio_Toggle(DBG_ST8_PIN);
             adi_gpio_SetHigh(DBG_ADC_PIN);
             
	     curr_state = 6;  
             adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_0p04MSEC); 
             adi_tmr_Enable(hDevice1,true); 
             
         break;   

	/*st8 6: PM2.5 sensor - LED OFF, wait for 9.68 ms before next PM2.5 measurement */	
        case 6 :
             adi_gpio_SetLow(PM25_LED);
             
             adi_gpio_SetHigh(LED3);
             adi_gpio_Toggle(DBG_ST8_PIN);

             curr_state = 4;  
             adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_9p68MSEC);
             //adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_50MSEC);
             adi_tmr_Enable(hDevice1,true);                       

         break;         
         
       default :
         break;
     }
  
        break;
      case ADI_TMR_EVENT_CAPTURED:
        break;
      default:
        break;
  }
  
}