Chinaunix首页 | 论坛 | 博客
  • 博客访问: 119385
  • 博文数量: 24
  • 博客积分: 616
  • 博客等级: 中士
  • 技术积分: 375
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-01 10:54
文章分类
文章存档

2012年(24)

我的朋友

分类: LINUX

2012-07-31 15:15:05

添加了滤除无效坐标点的程序。
 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
char buffer[50];
int len;
long int value = -1;
unsigned int  xp_temp[10]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
unsigned int  yp_temp[10]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
unsigned long xp_he=0;
unsigned long yp_he=0;
unsigned int  xp_max=0;
unsigned int  xp_min=1024;
unsigned int  yp_max=0;
unsigned int  yp_min=1024;
unsigned int  count=0;
unsigned int  count_feichu=0;
unsigned int  max_xp_select=0;
unsigned int  max_yp_select=0;
unsigned int  min_xp_select=0;
unsigned int  min_yp_select=0;
unsigned int xp_last=0;
unsigned int yp_last=0;
unsigned int count_error=0;
unsigned int xp_old=0;
unsigned int yp_old=0;
unsigned int xp_down_over=0;
unsigned int xp_up_over=0;
unsigned int yp_down_over=0;
unsigned int yp_up_over=0;
unsigned int xp=0;
unsigned int yp=0;
int main(void)
{
 unsigned i;
 fprintf(stderr, "press Ctrl-C to stop\n");
 int fd = open("/dev/touch_micro2440_drv", 0);
 if (fd < 0)
  {
  perror("open ADC device:");
  return 1;
 }
 count=0;
 for(;;)
 {
    int len = read(fd, buffer, sizeof buffer -1);
  if (len > 0) {
   buffer[len] = '\0';
   value = -1;
   sscanf(buffer, "%d", &value);//将字符串buffer按照整数的形式存在value中
    xp=(short int)value;
   yp=(short int)(value>>16);
   if(count==10)
   {
      count=0;
      for(i=0;i<10;i++)
      {
          if(xp_max<=xp_temp[i])
          {
              xp_max=xp_temp[i];
              max_xp_select=i; 
          }
      }
      for(i=0;i<10;i++)
      {
          if(xp_min>xp_temp[i])
          {
              xp_min=xp_temp[i];
              min_xp_select=i; 
          }
      }
      for(i=0;i<10;i++)
      {
          if(yp_max<=yp_temp[i])
          {
              yp_max=yp_temp[i];
              max_yp_select=i; 
          }
      }
      for(i=0;i<10;i++)
      {
          if(yp_min>yp_temp[i])
          {
              yp_min=yp_temp[i];
              min_yp_select=i; 
          }
      }
      xp_temp[max_xp_select]=0;xp_temp[min_xp_select]=0;xp_temp[max_yp_select]=0;xp_temp[min_yp_select]=0;
      yp_temp[max_xp_select]=0;yp_temp[min_xp_select]=0;yp_temp[max_yp_select]=0;yp_temp[min_yp_select]=0;
      for(i=0;i<10;i++)
      {
         xp_he=xp_he+xp_temp[i];
         yp_he=yp_he+yp_temp[i];
          if(xp_temp[i]==0)
           count_feichu++; 
      }
      xp_last=xp_he/(10-count_feichu);
      yp_last=yp_he/(10-count_feichu);
      count_feichu=0;
      xp_he=0;yp_he=0;xp_max=0;xp_min=1024;yp_max=0;yp_min=1024;max_xp_select=0;min_xp_select=0;max_yp_select=0;min_yp_select=0;
      printf("ADC Value x: %d\n", xp_last);
       printf("ADC Value y: %d\n", yp_last);
   }
   else
   {
          //得数上下浮动50
          xp_up_over=xp_old+50;//XP上限
          if(xp_up_over>1023)
           xp_up_over=1023;
          yp_up_over=yp_old+50;//YP上限
          if(yp_up_over>1023)
           yp_up_over=1023;
          if(xp_down_over<50)//XP下限
           xp_down_over=1;
          else
           xp_down_over=xp_down_over-50;
          if(yp_down_over<50)//YP下限
           yp_down_over=1;
          else
           yp_down_over=yp_down_over-50; 
          //计算所得数据是否超过上下线,超过上下线就抛,如果连续超过三次就认为有效
          if((xpxp_down_over)&&(ypyp_down_over))
          {
      xp_temp[count]=xp;
      yp_temp[count]=yp;
      if(xp_temp[count]==0)
       xp_temp[count]=1;
      if(yp_temp[count]==0)
       yp_temp[count]=1;
      count++;
      count_error=0;
    }
    else
    {
       count_error++;
       if(count_error==3)
       {
          count_error=0;
          xp_old=xp;
          yp_old=yp;
          xp_temp[count]=xp;
            yp_temp[count]=yp;
            if(xp_temp[count]==0)
           xp_temp[count]=1;
            if(yp_temp[count]==0)
           yp_temp[count]=1;
            count++;
       }
    }
   }
   
  } else {
   perror("read ADC device:");
   return 1;
  }
  usleep(1* 1000);
 }
 
 close(fd);
}
阅读(1404) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~