#include "common.h" 
#include <pthread.h>

YSP_IEC_DATA ysp_glb_data;

OUTPUT_TYPE_DEF my_output_data;
INPUT_TYPE_DEF  my_input_data;
SAMPLE_INPUT_TYPE my_sample_data;

YSP_PARA        my_para_data;
unsigned int    last_sample_time;
ANALY_RESULT    ana_result;

pthread_mutex_t  my_output_mutex;
pthread_mutex_t  my_input_mutex;
static int mutex_inited=0;

float last_co2_ppm=0;
int co2_is_ok=0;

int comunication_flg=0;

PID_ATune HeatAutoTuner;
AUTO_TUNE_RECORD HeatTuneRecord;

int ctrl_fd=-1;  //���ư崮�� 
int sample_fd=-1;//�ɼ��崮�� 


void mutex_init(void)
{
 if(!mutex_inited)
  {
   if(pthread_mutex_init(&my_output_mutex,NULL)!=0)
   {
     printf("init output mutex failed\r\n");
	 return;	
   }  
   if(pthread_mutex_init(&my_input_mutex,NULL)!=0)
   {
   	 printf("init input mutex failed\r\n");
   	 return;
   }        
   mutex_inited++;
  }
}

void lock_input_data(void)
{
 pthread_mutex_lock(&my_input_mutex);	
}

void unlock_input_data(void)
{
 pthread_mutex_unlock(&my_input_mutex);	
}

void lock_output_data(void)
{
 pthread_mutex_lock(&my_output_mutex);	
}

void unlock_output_data(void)
{
 pthread_mutex_unlock(&my_output_mutex);	
}

unsigned char cal_sum(unsigned char *buf,unsigned int len)
{
 unsigned char sum=0;
 unsigned int  i;
 for(i=0;i<len;i++)
	{
	 sum+=(*buf++);
	}
 return sum;
}


int chk_sum(unsigned char *buf,unsigned int len,unsigned char sum)
{
 unsigned char tmp=0;
 tmp=cal_sum(buf,len);
 if(sum==tmp)
	 return 0;
 return 1;
}