Chinaunix首页 | 论坛 | 博客
  • 博客访问: 541171
  • 博文数量: 150
  • 博客积分: 5010
  • 博客等级: 大校
  • 技术积分: 1861
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-17 00:19
文章分类

全部博文(150)

文章存档

2011年(1)

2009年(14)

2008年(135)

我的朋友

分类: LINUX

2008-08-21 16:41:57

用到了select函数,并知道时间参数要在它超时返回零时再重新设定。framebuffer,还有读文件的触摸型信息。画线的算法,我写的不好,用的别人的,是Glib_Line()函数


头文件:

#include <unistd.h>

#include <sys/types.h>

#define INPUT_DEVICE_NAME     "/dev/input/event0"

#define TOUCH_POINT_NAME "/data/misc/pointer"



#define ABS_X            0x00

#define ABS_Y            0x01

#define TOUCH_TYPE 0x03



struct input_event {

    struct timeval time;

    unsigned short int type;

     unsigned short int code;

    unsigned int value;

};



Touch_panel.c

#include <stdio.h>

#include <string.h>

#include <sys/times.h>

#include <sys/select.h>

#include <stdlib.h>

#include <fcntl.h>

#include <sys/time.h>
#include <sys/unistd.h>

#include <sys/ioctl.h>
#include <sys/mman.h>
#include <asm/page.h>
#include <linux/fb.h>

#define LCD_TFT
#ifdef LCD_TFT
#define RED_COLOR 0xf800
#define GREEN_COLOR 0x07e0
#define BLUE_COLOR 0x001f
#define BLACK_COLOR 0x0000
#define WHITE_COLOR 0xffff

#define RED_COLOR_24    0x01ff0000
#define GREEN_COLOR_24    0x0100ff00
#define BLUE_COLOR_24    0x010000ff
#define BLACK_COLOR_24    0x01000000
#define WHITE_COLOR_24    0x01ffff88
      
#else
#define RED_COLOR 0xE0
#define GREEN_COLOR 0x1C
#define BLUE_COLOR 0x03
#endif

#define Max_color_index 30 /* this parameter determins the total seconds of color changing:
                            time = Max_color_index * 2 */

unsigned short int Color_Array_16[5] ={RED_COLOR, GREEN_COLOR,BLUE_COLOR,WHITE_COLOR,BLACK_COLOR };
unsigned int Color_Array_24[5] ={RED_COLOR_24, GREEN_COLOR_24,BLUE_COLOR_24,WHITE_COLOR_24,BLACK_COLOR_24 };

char *Color_String[] = { "Red Screen","Green Screen" ,"Blue Screen","White Screen","Black Screen"};

void Draw_Point( struct fb_var_screeninfo vinfo,char *fbf,int x , int y );

void Draw_Line( struct fb_var_screeninfo vinfo,char *fbp ,int x , int y, int u ,int v );
void Glib_Line( struct fb_var_screeninfo vinfo,char *fbp, int x,int y,int u,int v);

/*==================================================================================================

                                     LOCAL CONSTANTS

==================================================================================================*/




/*==================================================================================================

                          LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)

==================================================================================================*/




/*==================================================================================================

                                      FUNCTION DEFINITIONS

==================================================================================================*/




/*==================================================================================================

FUNCTION: TC_TouchPointHandler()



DESCRIPTION: This handler is responsible for handling read touch pannel x y value.



ARGUMENTS PASSED:

  TC_TCMD_T* tcmd - Pointer to TCMD data structure

                     

RETURN VALUE:

  void



IMPORTANT NOTES:

  None

==================================================================================================*/


int main( int argc , char *argv[])

{

      

        int length=0;

        int fd ,fd2;

        int len;

        struct input_event data;

        fd_set fds;

        char x_flag=0,y_flag=0,ret,rsp_length=0;

        unsigned short int XY[4]={0};

        char rsp_buff[128],*st1,*st2,*st3,*st4,*st5,*st6;

        double a,b,c,d,e,f,convert_value_x,convert_value_y;

           

        unsigned short int XY_CUR[4]={0};

        int loop1_counter =200;

        int counter=0;
        struct timeval time_wait;
        time_wait.tv_sec=1;
        time_wait.tv_usec=250;

        /* parse parameter */

        // length = tcmd->cmd_header.length;


          

        //parameter with the framebuffer

        int fbfd = 0;
        struct fb_var_screeninfo vinfo;
        struct fb_fix_screeninfo finfo;
        long int screensize = 0;
        char *fbp = 0;

         //printf ("TC_Touchpoint.c receive Opcode 0x%04x was found, length 0x%04x\n", tcmd->cmd_header.opcode,length);


        
        // Open the file for reading and writing

        fbfd = open("/dev/graphics/fb0", O_RDWR);
        if (fbfd< 0) {
        printf("Error: cannot open framebuffer device.\n");
        exit(1);
        }
        printf("the file descripter is %d \n" ,fbfd);
 
        printf("The framebuffer device was opened successfully.\n");

        // Get fixed screen information

   
        if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
          printf("Error reading fixed information.\n");
          exit(2);
        }
   
   
        // Get variable screen information

       if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
          printf("Error reading variable information.\n");
          exit(3);
       }

       // Figure out the size of the screen in bytes

       if( vinfo.bits_per_pixel == 16)
     screensize = vinfo.xres * vinfo.yres * 2;
       else if( vinfo.bits_per_pixel == 24 || vinfo.bits_per_pixel == 18 ||
        vinfo.bits_per_pixel == 19 || vinfo.bits_per_pixel == 25 || vinfo.bits_per_pixel == 32 )
     screensize = vinfo.xres * vinfo.yres * 4;

        printf("%dx%d, %dbpp, screensize = %d\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel, screensize );

       // Map the device to memory

       fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,fbfd, 0);
       if ((int)fbp == -1) {
           printf("Error: failed to map framebuffer device to memory.\n");
           exit(4);
       }
       printf("The framebuffer device was mapped to memory successfully.\n");
       memset( fbp, 66, screensize);
       //open event file


       fd = open(INPUT_DEVICE_NAME, O_RDONLY);    //O_RDWR);


       printf(" open device %s ret %d\n", INPUT_DEVICE_NAME, fd);

       if(fd < 0)

       {

        printf("Error opening the file \n");

        //TC_sendResponse(tcmd,TC_CMD_RES_GEN_FAIL,TC_RSP_FLAG_FAIL,0, NULL);


        return;

       }
       //open convert parameter file

          

       fd2 = open(TOUCH_POINT_NAME, O_RDONLY);    //O_RDWR);


        printf(" open device %s ret %d\n", TOUCH_POINT_NAME, fd2);

       if(fd2 < 0)

       {

        printf("Error opening the file \n");

        //TC_sendResponse(tcmd,TC_CMD_RES_GEN_FAIL,TC_RSP_FLAG_FAIL,0, NULL);


        return;

       }

       memset(rsp_buff, 0, 128);

       rsp_length = read(fd2, rsp_buff, 128);

       if(rsp_length== 0)

       {

          printf("failed to get response \n");

          //TC_sendResponse(tcmd,TC_CMD_RES_GEN_FAIL,TC_RSP_FLAG_FAIL,0, NULL);


          return;

       }

       close(fd2);

       a = strtod(rsp_buff , &st1 );

       b = strtod(st1 , &st2 );

       c = strtod(st2 , &st3 );

       d = strtod(st3 , &st4 );

       e = strtod(st4 , &st5 );

       f = strtod(st5 , &st6 );

       printf(" strtod a=%f , b=%f , c=%f, d=%f, e=%f, f=%f \n", a,b,c,d,e,f );


while(loop1_counter--)

{

         while(1)

              {

            FD_ZERO(&fds);

            FD_SET(fd,&fds);

            ret = select (fd+1, &fds, NULL, NULL, &time_wait);
                if( ret == 0)
                   {
                     printf("-----------------------------------------------time outs%d\n",loop1_counter);
                     XY_CUR[2]=XY_CUR[3]=0;
                     time_wait.tv_sec=1;
                     time_wait.tv_usec=590;
                   };

            if(ret > 0)

                   {

                if(fd >= 0 && FD_ISSET (fd, &fds))

                         {

                    FD_CLR (fd, &fds);

                    ret = read(fd, &data, sizeof(data));

                    if(ret >0)

                                {

                     //printf("type : %d, code : %d, value : %d\n",data.type,data.code,data.value);


                                  if (data.type==TOUCH_TYPE)

                                 {

                     if(data.code == ABS_X)

                                   {

                     // printf("read X: %d\n", data.value);


                                    XY[0]=data.value;

                                    x_flag=1;

                     }

                     else if(data.code == ABS_Y)

                                   {

                     // printf("read Y: %d\n", data.value);


                                     XY[1]=data.value;

                                      y_flag=1;

                     }

                     else

                                   {

                     }

                                    

                     }
                               }

                }

            }

           

            if ((x_flag) && (y_flag))

            { //printf("get position %d \n", counter++ );

                 if(counter==63578){break; }

                 lseek( fd , 0 , SEEK_SET );

                 //printf( "reading position return to the file beginning\n" );


                 x_flag=y_flag=0;
                 convert_value_x = a*XY[0]+b*XY[1]+c;
                 convert_value_y = d*XY[0]+e*XY[1]+f;

                 if((convert_value_x>0&&convert_value_y>0)&&((XY_CUR[0] != XY[0])||(XY_CUR[1] != XY[1]) ))

                  {

                   XY_CUR[0] = XY[0]; XY_CUR[1] = XY[1];//assign the value


                   break;

                  }//when not be equal,we convert the value!


            };

         //XY_CUR[0] = XY[0]; XY_CUR[1] = XY[1];//assign the value


         //printf(" XY_CUR[0] = %d , XY_CUR[1] = %d \n" , XY_CUR[0], XY_CUR[1] );


        }//end while(1);


      

      printf("=====================================break :%d\n", loop1_counter);

            

     

       XY[2] = (int)(convert_value_x);

       XY[3] = (int)(convert_value_y);

       printf("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%==(X_value=%d Y_value=%d) --(X_convert=%d Y_convert =%d )\n", XY[0],XY[1],XY[2],XY[3]);

      //TC_sendResponse(tcmd,TC_CMD_RES_GEN_SUCC,TC_RSP_FLAG_NONE,8, XY);


     // Draw_Point(vinfo,fbp, XY[2] , XY[3] );

       //Draw_Line(vinfo,fbp, XY[2] , XY[3] ,XY_CUR[2], XY_CUR[3] );

        Glib_Line(vinfo,fbp, XY[2] , XY[3] ,XY_CUR[2], XY_CUR[3] );
      //printf("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%==XY_CUR[2] = %d , XY_CUR[3] = %d \n" , XY_CUR[2], XY_CUR[3] );


      XY_CUR[2] = XY[2]; XY_CUR[3] = XY[3];
        if(loop1_counter%90==0)
            {memset( fbp, 66, screensize);}

}//end while(2);

   //close framebuffer


   munmap(fbp, screensize);
   close(fbfd);
return 0;

            

}



void Draw_Point( struct fb_var_screeninfo vinfo,char *fbp ,int x , int y )
{
   
   int x_index=x;
   int y_index=y;
   if(vinfo.bits_per_pixel == 16) // 16bpp only

   {
      // memcpy(fbp+screensize,fbp,screensize);/* backup to second buffer */


       // 16bpp framebuffer test

       printf("16bpp framebuffer test\n");
       printf("two byte in fbp is a pixel of LCD, just set the value of fbp to put color to LCD\n");
       printf("byte format:\n");
       printf(" bit:| 15 14 13 12 11 | 10 9 8 7 6 5 | 4 3 2 1 0 |\n");
       printf(" | red | green | blue |\n");

        // full screen red/green/blue/white/black colors in turn

    
        if( x < vinfo.xres && y < vinfo.yres )
      
           *(((unsigned int *)fbp) + y * vinfo.xres + x) = Color_Array_16[2];

        
        //sleep(2);

                                                                                                              
      
   }else if(vinfo.bits_per_pixel == 24 || vinfo.bits_per_pixel == 18 ||
        vinfo.bits_per_pixel == 19 || vinfo.bits_per_pixel == 25 || vinfo.bits_per_pixel == 32)
   {
       printf("24bpp framebuffer test\n");
       printf("four byte in fbp is a pixel of LCD, just set the value of fbp to put color to LCD\n");
       printf("byte format:\n");
       printf(" bit:| 31 30 29 28 27 26 25| 24| 23 22 21 20 19 18 17 16 |\n");
       printf(" | No use | T | Red |\n");
       printf(" bit:| 15 14 13 12 11 10 9 8 | 7 6 5 4 3 2 1 0 |\n");
       printf(" | green | Blue |\n");
                                                                                                             
       // full screen red/green/blue/white/black colors in turn

       printf("seconds color \n");
     
     
          for( y_index=y; y_index < vinfo.yres; y_index++)
            { //printf("y_index*************************************************%d\n",y_index);

           for(x_index=x; x_index< vinfo.xres; x_index++)
           {
                  *(((unsigned int *)fbp) + y_index * vinfo.xres + x_index) = Color_Array_24[2];
                  // printf("x_index,y_index===========================================%d,%d\n",x_index,y_index);

           }
         }
       
                                                                                                
                                                                                           
   }else
   {
       printf("16bpp && 24bpp only!!!\n");
   }
}

void Draw_Line( struct fb_var_screeninfo vinfo,char *fbp ,int x , int y, int u ,int v )
{
    int x_index;
    int y_index;
    int k_m;//the line slope

    int temp;//for exchange value

   (x> vinfo.xres)?(x=vinfo.xres):(x=x);
   (u> vinfo.xres)?(u=vinfo.xres):(u=u);
   (y> vinfo.yres)?(y=vinfo.yres):(y=y);
   (v> vinfo.yres)?(v=vinfo.yres):(v=v);
    if(y==479&&x==320){ x=319;};
 if(vinfo.bits_per_pixel == 16) // 16bpp only

   {
      // memcpy(fbp+screensize,fbp,screensize);/* backup to second buffer */


       // 16bpp framebuffer test

       printf("16bpp framebuffer test\n");
       printf("two byte in fbp is a pixel of LCD, just set the value of fbp to put color to LCD\n");
       printf("byte format:\n");
       printf(" bit:| 15 14 13 12 11 | 10 9 8 7 6 5 | 4 3 2 1 0 |\n");
       printf(" | red | green | blue |\n");

        // full screen red/green/blue/white/black colors in turn

    
        if( x < vinfo.xres && y < vinfo.yres )
      
           *(((unsigned int *)fbp) + y * vinfo.xres + x) = Color_Array_16[2];

        
        //sleep(2);

                                                                                                              
      
   }else if(vinfo.bits_per_pixel == 24 || vinfo.bits_per_pixel == 18 ||
        vinfo.bits_per_pixel == 19 || vinfo.bits_per_pixel == 25 || vinfo.bits_per_pixel == 32)
   {
       printf("24bpp framebuffer test\n");
       printf("four byte in fbp is a pixel of LCD, just set the value of fbp to put color to LCD\n");
       printf("byte format:\n");
       printf(" bit:| 31 30 29 28 27 26 25| 24| 23 22 21 20 19 18 17 16 |\n");
       printf(" | No use | T | Red |\n");
       printf(" bit:| 15 14 13 12 11 10 9 8 | 7 6 5 4 3 2 1 0 |\n");
       printf(" | green | Blue |\n");
                                                                                                             
     
    
     printf( "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&( x=%d ,y= %d),( u=%d , v=%d )\n", x, y, u, v );
   //Draw the first point

   if( u==0 && v==0 )
   { u=x;
     v=y;
     *(((unsigned int *)fbp) + y * vinfo.xres + x) = Color_Array_24[2];
     return;
   }
   if( x>u && y>v )
   { k_m = (y-v)/(x-u);
      for( x_index=u; x_index<x; x_index++ )
         { printf( "x>u&&y>u &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&%d\n",x_index);
          *(((unsigned int *)fbp) + (k_m *(x_index-u)+v) * vinfo.xres + x_index) = Color_Array_24[0];
         }
   }
   else if( x> u && y<v )
    {
      k_m = (v-y)/(x-u);
      for( x_index=u; x_index<x; x_index++ )
      { printf( "x>u&&y,x_index);
          *(((unsigned int *)fbp) + (v-k_m * (x_index-u))* vinfo.xres + x_index) = Color_Array_24[2];
      }
    }
   else if( x<u && y>v )
    {
         k_m = (y-v)/(u-x);
         for( x_index=x; x_index<u; x_index++ )
          { printf( " xv ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^%d\n",x_index);
            *(((unsigned int *)fbp) + (y-k_m * (x_index-x))* vinfo.xres + x_index) = Color_Array_24[1];
          }
     
    }
    else if( x<u && y<v )
    {
         k_m = (v-y)/(u-x);
         for( x_index=x; x_index<u; x_index++ )
          { printf( " x,x_index);
            *(((unsigned int *)fbp) + (y+k_m * (x_index-x))* vinfo.xres + x_index) = Color_Array_24[2];
          }
      
    }
   else if( x==u && y!=v )
    { if( y<v )// exchange the value

        { temp=y ; y=v ; v=temp; }
        for( y_index=v; y_index<y; y_index++ )
          { printf( "x==u && y!=v $$$$$$$$$$$$$$$$$$$$$$$$$$$$ %d\n",x);
            *(((unsigned int *)fbp) + y_index* vinfo.xres + x) = Color_Array_24[2];
          }
       
    }
   else if( y==v && x!=u )
    { if(y==480||v==480){ y=479;v=479;}
        if( x<u )// exchange the value

        { temp=x ; x=u ; u=temp; }
       for( x_index=u; x_index<x ; x_index++ )
          { printf( " y==v && x!=u +++++++++++++++++++++++++++++%d\n",x_index);
            *(((unsigned int *)fbp) + y* vinfo.xres + x_index) = Color_Array_24[0];
          }
       
    }
   else
    {
       printf( "else &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&( x=%d ,y= %d),( u=%d , v=%d )\n", x, y,u,v) ;
      
    }
   }else
   {
       printf("16bpp && 24bpp only!!!\n");
   }


}




void Glib_Line( struct fb_var_screeninfo vinfo,char *fbp, int x,int y,int u,int v)
{
    int dx,dy,e;
         (x> vinfo.xres)?(x=vinfo.xres):(x=x);
         (u> vinfo.xres)?(u=vinfo.xres):(u=u);
         (y>=vinfo.yres)?(y=vinfo.yres-1):(y=y);
         (v>= vinfo.yres)?(v=vinfo.yres-1):(v=v);
          if(y==479&&x==320){ x=319;};
          if( u==0 && v==0 )
          { u=x;
           v=y;
           *(((unsigned int *)fbp) + y * vinfo.xres + x) = Color_Array_24[2];
           return;
          }
    dx=x-u;
    dy=y-v;
         printf( "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&( x=%d ,y= %d),( u=%d , v=%d )\n", x, y, u, v );
    if(dx>=0)
    {
        if(dy >= 0) // dy>=0

        {
            if(dx>=dy) // 1/8 octant

            {
                e=dy-dx/2;
                while(u<=x)
                {
                     *(((unsigned int *)fbp) + v* vinfo.xres + u) = Color_Array_24[2];
                    if(e>0){v+=1;e-=dx;}    
                    u+=1;
                    e+=dy;
                }
            }
            else // 2/8 octant

            {
                e=dx-dy/2;
                while(v<=y)
                {
                     *(((unsigned int *)fbp) + v* vinfo.xres + u) = Color_Array_24[2];
                    if(e>0){u+=1;e-=dy;}    
                    v+=1;
                    e+=dx;
                }
            }
        }
        else // dy<0

        {
            dy=-dy; // dy=abs(dy)


            if(dx>=dy) // 8/8 octant

            {
                e=dy-dx/2;
                while(u<=x)
                {
                     *(((unsigned int *)fbp) + v* vinfo.xres + u) = Color_Array_24[2];
                    if(e>0){v-=1;e-=dx;}    
                    u+=1;
                    e+=dy;
                }
            }
            else // 7/8 octant

            {
                e=dx-dy/2;
                while(v>=y)
                {
                     *(((unsigned int *)fbp) + v* vinfo.xres + u) = Color_Array_24[2];
                    if(e>0){u+=1;e-=dy;}    
                    v-=1;
                    e+=dx;
                }
            }
        }    
    }
    else //dx<0

    {
        dx=-dx; //dx=abs(dx)

        if(dy >= 0) // dy>=0

        {
            if(dx>=dy) // 4/8 octant

            {
                e=dy-dx/2;
                while(u>=x)
                {
                     *(((unsigned int *)fbp) + v* vinfo.xres + u) = Color_Array_24[2];
                    if(e>0){v+=1;e-=dx;}    
                    u-=1;
                    e+=dy;
                }
            }
            else // 3/8 octant

            {
                e=dx-dy/2;
                while(v<=y)
                {
                     *(((unsigned int *)fbp) + v* vinfo.xres + u) = Color_Array_24[2];
                    if(e>0){u-=1;e-=dy;}    
                    v+=1;
                    e+=dx;
                }
            }
        }
        else // dy<0

        {
            dy=-dy; // dy=abs(dy)


            if(dx>=dy) // 5/8 octant

            {
                e=dy-dx/2;
                while(u>=x)
                {
                     *(((unsigned int *)fbp) + v* vinfo.xres + u) = Color_Array_24[2];
                    if(e>0){v-=1;e-=dx;}    
                    u-=1;
                    e+=dy;
                }
            }
            else // 6/8 octant

            {
                e=dx-dy/2;
                while(v>=y)
                {
                     *(((unsigned int *)fbp) + v* vinfo.xres + u) = Color_Array_24[2];
                    if(e>0){u-=1;e-=dy;}    
                    v-=1;
                    e+=dx;
                }
            }
        }    
    }
}


#include <unistd.h>

#include <sys/types.h>

#define INPUT_DEVICE_NAME     "/dev/input/event0"

#define TOUCH_POINT_NAME "/data/misc/pointer"



#define ABS_X            0x00

#define ABS_Y            0x01

#define TOUCH_TYPE 0x03



struct input_event {

    struct timeval time;

    unsigned short int type;

     unsigned short int code;

    unsigned int value;

};



文件:touch_panel.rar
大小:27KB
下载:下载
阅读(1969) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-03-17 22:04:21

很长,其实更想看一看,在触摸屏上画线的思想,