Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1579244
  • 博文数量: 239
  • 博客积分: 1760
  • 博客等级: 上尉
  • 技术积分: 1595
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-08 23:53
文章分类

全部博文(239)

文章存档

2016年(1)

2015年(28)

2014年(53)

2013年(42)

2012年(50)

2011年(65)

分类: 嵌入式

2013-09-05 09:17:30

     本设计根据tslib中提供的ts_test程序修改而来,本设计中ts_test.c还依赖于tslib中的其他文件,若想直接编译,可置于tslib中的test文件夹中,替换到原来的文件。本设计根据tslib中提供的API取得X,Y坐标,根据得到的X,Y,打开fb0,映射进入内存(出于速度的考虑),进行划线,对线条进行了加粗处理,并随机生成颜色,最终形成灿烂的笔画。
 
ts_test.c

点击(此处)折叠或打开

  1. #include "config.h"

  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <signal.h>
  6. #include <sys/fcntl.h>
  7. #include <sys/ioctl.h>
  8. #include <sys/mman.h>
  9. #include <sys/time.h>
  10. #include<linux/fb.h>


  11. #include "tslib.h"
  12. #include "fbutils.h"

  13. unsigned char *fbp;
  14. struct fb_var_screeninfo vinfo;
  15. struct fb_fix_screeninfo finfo;


  16. void drawpoint(unsigned int x,unsigned int y,unsigned int *color)
  17. {
  18.     unsigned char *fbp0;
  19.     fbp0=fbp;
  20.     fbp0+=y*vinfo.xres*vinfo.bits_per_pixel/8+x*vinfo.bits_per_pixel/8;
  21.     memcpy(fbp0,color,vinfo.bits_per_pixel/8);
  22. }


  23. void drawline(unsigned int x0,unsigned int y0,unsigned int x1,unsigned int y1,unsigned int *color)
  24. {
  25.     unsigned int x=x0;
  26.     unsigned int y=y0;
  27.     int s0,s1,s2;
  28.     while(x!=x1||y!=y1)
  29.     {
  30.         drawpoint(x,y,color);//以下为加粗直线
  31.         drawpoint(x+1,y,color);
  32.         drawpoint(x,y+1,color);
  33.     drawpoint(x+1,y+1,color);
  34.     drawpoint(x+2,y,color);
  35.     drawpoint(x,y+2,color);
  36.     drawpoint(x+2,y+2,color);

  37.         if(x0>=x1&&y0>=y1) //求直线下一点的算法。
  38.         {
  39.             s0=abs((x-x0-1)*(y0-y1)-(y-y0)*(x0-x1));
  40.             s1=abs((x-x0)*(y0-y1)-(y-y0-1)*(x0-x1));
  41.             s2=abs((x-x0-1)*(y0-y1)-(y-y0-1)*(x0-x1));
  42.             s0<=s1?(s0<=s2?x=x-1:(x=x-1,y=y-1)):(s1<=s2?y=y-1:(x=x-1,y=y-1));
  43.         }
  44.         else if(x0>=x1&&y0<=y1)
  45.         {
  46.             s0=abs((x-x0-1)*(y0-y1)-(y-y0)*(x0-x1));
  47.             s1=abs((x-x0)*(y0-y1)-(y-y0+1)*(x0-x1));
  48.             s2=abs((x-x0-1)*(y0-y1)-(y-y0+1)*(x0-x1));
  49.             s0<=s1?(s0<=s2?x=x-1:(x=x-1,y=y+1)):(s1<=s2?y=y+1:(x=x-1,y=y+1));
  50.         }
  51.         else if(x0<=x1&&y0>=y1)
  52.         {
  53.             s0=abs((x-x0+1)*(y0-y1)-(y-y0)*(x0-x1));
  54.             s1=abs((x-x0)*(y0-y1)-(y-y0-1)*(x0-x1));
  55.             s2=abs((x-x0+1)*(y0-y1)-(y-y0-1)*(x0-x1));
  56.             s0<=s1?(s0<=s2?x=x+1:(x=x+1,y=y-1)):(s1<=s2?y=y-1:(x=x+1,y=y-1));
  57.         }
  58.         else if(x0<=x1&&y0<=y1)
  59.         {
  60.             s0=abs((x-x0+1)*(y0-y1)-(y-y0)*(x0-x1));
  61.             s1=abs((x-x0)*(y0-y1)-(y-y0+1)*(x0-x1));
  62.             s2=abs((x-x0+1)*(y0-y1)-(y-y0+1)*(x0-x1));
  63.             s0<=s1?(s0<=s2?x=x+1:(x=x+1,y=y+1)):(s1<=s2?y=y+1:(x=x+1,y=y+1));
  64.         }
  65.     }
  66.     drawpoint(x,y,color);
  67. }

  68. int mrand(int x,int y) //生成x到y之间的随机数。
  69. {
  70.      int i;
  71.      struct timeval tpstart;
  72.      gettimeofday(&tpstart,NULL);
  73.      srand(tpstart.tv_usec); //微秒级产生随机数种子
  74.      return (rand()%(y-x+1)+x);
  75. }



  76. static int palette [] =
  77. {
  78.     0x000000, 0xffe080, 0xffffff, 0xe0c0a0, 0x304050, 0x80b8c0
  79. };
  80. #define NR_COLORS (sizeof (palette) / sizeof (palette [0]))

  81. struct ts_button {
  82.     int x, y, w, h;
  83.     char *text;
  84.     int flags;
  85. #define BUTTON_ACTIVE 0x00000001
  86. };

  87. /* [inactive] border fill text [active] border fill text */
  88. static int button_palette [6] =
  89. {
  90.     1, 4, 2,
  91.     1, 5, 0
  92. };

  93. #define NR_BUTTONS 2
  94. static struct ts_button buttons [NR_BUTTONS];

  95. static void sig(int sig)
  96. {
  97.     close_framebuffer();
  98.     fflush(stderr);
  99.     printf("signal %d caught\n", sig);
  100.     fflush(stdout);
  101.     exit(1);
  102. }

  103. static void button_draw (struct ts_button *button)
  104. {
  105.     int s = (button->flags & BUTTON_ACTIVE) ? 3 : 0;
  106.     rect (button->x, button->y, button->x + button->w - 1,
  107.           button->y + button->h - 1, button_palette [s]);
  108.     fillrect (button->x + 1, button->y + 1,
  109.           button->x + button->w - 2,
  110.           button->y + button->h - 2, button_palette [s + 1]);
  111.     put_string_center (button->x + button->w / 2,
  112.                button->y + button->h / 2,
  113.                button->text, button_palette [s + 2]);
  114. }

  115. static int button_handle (struct ts_button *button, struct ts_sample *samp)
  116. {
  117.     int inside = (samp->x >= button->x) && (samp->y >= button->y) &&
  118.         (samp->x < button->x + button->w) &&
  119.         (samp->y < button->y + button->h);

  120.     if (samp->pressure > 0) {
  121.         if (inside) {
  122.             if (!(button->flags & BUTTON_ACTIVE)) {
  123.                 button->flags |= BUTTON_ACTIVE;
  124.                 button_draw (button);
  125.             }
  126.         } else if (button->flags & BUTTON_ACTIVE) {
  127.             button->flags &= ~BUTTON_ACTIVE;
  128.             button_draw (button);
  129.         }
  130.     } else if (button->flags & BUTTON_ACTIVE) {
  131.         button->flags &= ~BUTTON_ACTIVE;
  132.         button_draw (button);
  133.                 return 1;
  134.     }

  135.         return 0;
  136. }

  137. static void refresh_screen ()
  138. {
  139.     int i;

  140.     fillrect (0, 0, xres - 1, yres - 1, 0);
  141.     put_string_center (xres/2, yres/4, "Enjoy the writing! ", 1);
  142.     put_string_center (xres/2, yres/4+20,"Touch screen to move crosshair", 2);

  143.     for (i = 0; i < NR_BUTTONS; i++)
  144.         button_draw (&buttons [i]);
  145. }

  146. int main()
  147. {
  148.     int fd;
  149.     long int screensize;
  150.     int color=0x00ff0000;//榛樿?red
  151.     
  152.     if((fd=open("/dev/fb0",O_RDWR))<0)
  153.     {
  154.         perror("fail to open");
  155.         return 0;
  156.     }
  157.         if (ioctl(fd,FBIOGET_FSCREENINFO, &finfo))
  158.     {
  159.         perror("fail to get");
  160.         return 0;
  161.     }
  162.         if (ioctl(fd,FBIOGET_VSCREENINFO, &vinfo))
  163.          {
  164.         perror("fail to get");
  165.         return 0;
  166.          }
  167.         screensize=vinfo.xres*vinfo.yres*vinfo.bits_per_pixel/8;
  168.         fbp=(unsigned char *)mmap(0,screensize,PROT_READ | PROT_WRITE,MAP_SHARED,fd, 0);
  169.    
  170.     
  171.     struct tsdev *ts;
  172.     int x, y;
  173.     unsigned int i;
  174.     unsigned int mode = 0;

  175.     char *tsdevice=NULL;

  176.     signal(SIGSEGV, sig);
  177.     signal(SIGINT, sig);
  178.     signal(SIGTERM, sig);

  179.     if ((tsdevice = getenv("TSLIB_TSDEVICE")) == NULL) {
  180. #ifdef USE_INPUT_API
  181.         tsdevice = strdup ("/dev/input/event0");
  182. #else
  183.         tsdevice = strdup ("/dev/touchscreen/ucb1x00");
  184. #endif /* USE_INPUT_API */
  185.         }

  186.     ts = ts_open (tsdevice, 0);

  187.     if (!ts) {
  188.         perror (tsdevice);
  189.         exit(1);
  190.     }

  191.     if (ts_config(ts)) {
  192.         perror("ts_config");
  193.         exit(1);
  194.     }

  195.     if (open_framebuffer()) {
  196.         close_framebuffer();
  197.         exit(1);
  198.     }

  199.     x = xres/2;
  200.     y = yres/2;

  201.     for (i = 0; i < NR_COLORS; i++)
  202.         setcolor (i, palette [i]);

  203.     /* Initialize buttons */
  204.     memset (&buttons, 0, sizeof (buttons));
  205.     buttons [0].w = buttons [1].w = xres / 4;
  206.     buttons [0].h = buttons [1].h = 20;
  207.     buttons [0].x = xres / 4 - buttons [0].w / 2;
  208.     buttons [1].x = (3 * xres) / 4 - buttons [0].w / 2;
  209.     buttons [0].y = buttons [1].y = 10;
  210.     buttons [0].text = "Drag";
  211.     buttons [1].text = "Draw";

  212.     refresh_screen ();

  213.     while (1) {
  214.         struct ts_sample samp;
  215.         int ret;

  216.         /* Show the cross */
  217.         if ((mode & 15) != 1)
  218.             put_cross(x, y, 2 | XORMODE);

  219.         ret = ts_read(ts, &samp, 1); //璇诲叆

  220.         /* Hide it */
  221.         if ((mode & 15) != 1)
  222.             put_cross(x, y, 2 | XORMODE);

  223.         if (ret < 0) {
  224.             perror("ts_read");
  225.             close_framebuffer();
  226.             exit(1);
  227.         }

  228.         if (ret != 1)
  229.         {
  230.             continue;
  231.         }
  232.         for (i = 0; i < NR_BUTTONS; i++)
  233.             if (button_handle (&buttons [i], &samp))
  234.                 switch (i) {
  235.                 case 0:
  236.                     mode = 0;
  237.                     refresh_screen ();
  238.                     break;
  239.                 case 1:
  240.                     mode = 1;
  241.                     refresh_screen ();
  242.                     break;
  243.                 }

  244.         printf("%ld.%06ld: %6d %6d %6d\n", samp.tv.tv_sec, samp.tv.tv_usec,
  245.             samp.x, samp.y, samp.pressure);
  246.         
  247.          
  248.         
  249.         if (samp.pressure > 0)
  250.         {
  251.             
  252.             if (mode == 0x80000001)
  253.         
  254.     
  255.                 {
  256.                 color=mrand(0x00000000,0x00ffffff);//随机生成颜色,可能因屏幕不同而范围不同。
  257.                 drawline(x,y,samp.x,samp.y,&color);
  258.             }
  259.             x = samp.x;
  260.             y = samp.y;
  261.      
  262.             mode |= 0x80000000;
  263.         }
  264.         else
  265.         {
  266.      
  267.             mode &= ~0x80000000;
  268.         }
  269.     }
  270.     close_framebuffer();
  271. }

 

阅读(1338) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~