Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4463042
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: 嵌入式

2011-05-03 09:03:25

http://blog.sina.com.cn/s/blog_4d32d0b40100gjrp.html

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <stdint.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <sys/ioctl.h>
  9. #include <sys/mman.h>
  10. #include <asm/page.h>
  11. #include <linux/fb.h>

  12. #define TRUE 1
  13. #define FALSE 0

  14. #define MIN(x,y) ((x)>(y)?(y):(x))
  15. #define MAX(x,y) ((x)>(y)?(x):(y))

  16. typedef struct fbdev{
  17.         int fb;
  18.         unsigned long fb_mem_offset;
  19.         unsigned long fb_mem;
  20.         struct fb_fix_screeninfo fb_fix;
  21.         struct fb_var_screeninfo fb_var;
  22.         char dev[20];
  23. } FBDEV, *PFBDEV;

  24. typedef struct point{
  25.         int x;
  26.         int y;
  27.         int z;
  28. } POINT, *PPOINT;


  29. void draw_dot (PFBDEV pFbdev, POINT p, uint8_t r, uint8_t g, uint8_t b);
  30. void draw_line(PFBDEV pFbdev, POINT p1, POINT p2, uint8_t r, uint8_t g, uint8_t b);
  31. void draw_polygon(PFBDEV pFbdev, uint32_t num, PPOINT array, uint8_t r, uint8_t g, uint8_t b);
  32. void draw_triangle(PFBDEV pFbdev, POINT p1, POINT p2, POINT p3, uint8_t r, uint8_t g, uint8_t b);
  33. void draw_circle(PFBDEV pFbdev, POINT center, uint32_t radius, uint8_t r, uint8_t g, uint8_t b);
  34. void draw_parabola_x(PFBDEV pFbdev, POINT center, int a, uint8_t r, uint8_t g, uint8_t b);
  35. void draw_parabola_y(PFBDEV pFbdev, POINT center, int a, uint8_t r, uint8_t g, uint8_t b);

  36. int fb_open(PFBDEV pFbdev)
  37. {
  38.         pFbdev->fb = open(pFbdev->dev, O_RDWR);
  39.         if(pFbdev->fb < 0)
  40.         {
  41.                 printf("Error opening %s: %m. Check kernel config\n", pFbdev->dev);
  42.                 return FALSE;
  43.         }
  44.         if (-1 == ioctl(pFbdev->fb,FBIOGET_VSCREENINFO,&(pFbdev->fb_var)))
  45.         {
  46.                 printf("ioctl FBIOGET_VSCREENINFO\n");
  47.                 return FALSE;
  48.         }
  49.         if (-1 == ioctl(pFbdev->fb,FBIOGET_FSCREENINFO,&(pFbdev->fb_fix)))
  50.         {
  51.                 printf("ioctl FBIOGET_FSCREENINFO\n");
  52.                 return FALSE;
  53.         }

  54.         pFbdev->fb_mem_offset = (unsigned long)(pFbdev->fb_fix.smem_start) & (~PAGE_MASK);
  55.         pFbdev->fb_mem = (unsigned long int)mmap(NULL, pFbdev->fb_fix.smem_len +
  56.                         pFbdev->fb_mem_offset,
  57.                         PROT_READ | PROT_WRITE, MAP_SHARED, pFbdev->fb, 0);
  58.         if (-1L == (long) pFbdev->fb_mem)
  59.         {
  60.                 printf("mmap error! mem:%ld offset:%ld\n", pFbdev->fb_mem,
  61.                                 pFbdev->fb_mem_offset);
  62.                 return FALSE;
  63.         }

  64.         return TRUE;
  65. }

  66. void fb_close(PFBDEV pFbdev)
  67. {
  68.         close(pFbdev->fb);
  69.         pFbdev->fb=-1;
  70. }

  71. void fb_memcpy(void *addr, void *color, size_t len)
  72. {
  73.         memcpy(addr, color, len);
  74. }

  75. void draw_dot (PFBDEV pFbdev, POINT p, uint8_t r, uint8_t g, uint8_t b)
  76. {
  77.         uint32_t offset;
  78.         uint8_t color[4];
  79.         color[0] = b;
  80.         color[1] = g;
  81.         color[2] = r;
  82.         color[3] = 0x0;
  83.         offset = p.y*pFbdev->fb_fix.line_length+4*p.x;
  84.         fb_memcpy((void*)(pFbdev->fb_mem + pFbdev->fb_mem_offset + offset), color, 4);
  85. }

  86. void draw_line(PFBDEV pFbdev, POINT p1, POINT p2, uint8_t r, uint8_t g, uint8_t b)
  87. {
  88.         POINT p;
  89.         int x,y;
  90.         for( x=MIN(p1.x,p2.x); x<=MAX(p1.x,p2.x); x++ )
  91.         {
  92.                 y = (p2.y-p1.y)*(x-p1.x)/(p2.x-p1.x) + p1.y;
  93.                 p.x = x;
  94.                 p.y = y;
  95.                 draw_dot(pFbdev, p, r, g, b);
  96.         }
  97. }

  98. void draw_triangle(PFBDEV pFbdev, POINT p1, POINT p2, POINT p3, uint8_t r, uint8_t g, uint8_t b)
  99. {
  100.         POINT p[3];
  101.         p[0] = p1;
  102.         p[1] = p2;
  103.         p[2] = p3;
  104.         draw_polygon(pFbdev, 3, p, r, g, b);
  105. }

  106. void draw_polygon(PFBDEV pFbdev, uint32_t num, PPOINT array, uint8_t r, uint8_t g, uint8_t b)
  107. {
  108.         int i;
  109.         for(i=0; i<num; i++)
  110.         {
  111.                 draw_line(pFbdev, array[i], array[(i+1)%num], r, g, b);
  112.         }
  113. }

  114. void draw_circle(PFBDEV pFbdev, POINT center, uint32_t radius, uint8_t r, uint8_t g, uint8_t b)
  115. {
  116.         POINT p;
  117.         int x,y,tmp;
  118.         for(x=center.x-radius; x<=center.x+radius; x++)
  119.         {
  120.                 p.x = x;
  121.                 tmp = sqrt(radius*radius -(x-center.x)*(x-center.x));
  122.                 p.y = center.y + tmp;
  123.                 draw_dot(pFbdev, p, r, g, b);
  124.                 p.y = center.y - tmp;
  125.                 draw_dot(pFbdev, p, r, g, b);
  126.         }
  127.         for(y=center.y-radius; y<=center.y+radius; y++)
  128.         {
  129.                 p.y = y;
  130.                 tmp = sqrt(radius*radius - (y-center.y)*(y-center.y));
  131.                 p.x = center.x + tmp;
  132.                 draw_dot(pFbdev, p, r, g, b);
  133.                 p.x = center.x - tmp;
  134.                 draw_dot(pFbdev, p, r, g, b);
  135.         }
  136. }

  137. void draw_parabola_x(PFBDEV pFbdev, POINT center, int a, uint8_t r, uint8_t g, uint8_t b)
  138. {
  139.         int x;
  140.         POINT p;
  141.         for(x=center.x-100; x<center.x+100; x++)
  142.         {
  143.                 p.x = x;
  144.                 p.y = (x-center.x)*(x-center.x)/a + center.y;
  145.                 draw_dot(pFbdev, p, r, g, b);
  146.         }
  147. }

  148. void draw_parabola_y(PFBDEV pFbdev, POINT center, int a, uint8_t r, uint8_t g, uint8_t b)
  149. {
  150.         int y;
  151.         POINT p;
  152.         for(y=center.y-100; y<center.y+100; y++)
  153.         {
  154.                 p.y = y;
  155.                 p.x = (y-center.y)*(y-center.y)/a + center.x;
  156.                 draw_dot(pFbdev, p, r, g, b);
  157.         }
  158. }

  159. int main(void)
  160. {
  161.         FBDEV fb;
  162.         memset(&fb, 0, sizeof(FBDEV));
  163.         strcpy(fb.dev, "/dev/fb0");
  164.         if(fb_open(&fb)==FALSE)
  165.         {
  166.                 printf("open frame buffer error\n");
  167.                 return 0;
  168.         }

  169.         POINT p1, p2, p3;

  170.         p1.x = 300;
  171.         p1.y = 450;
  172.         p2.x = 500;
  173.         p2.y = 300;
  174.         draw_line( &fb, p1, p2, 0x0, 0xff, 0x0 );

  175.         p1.x = 200;
  176.         p1.y = 200;
  177.         p2.x = 300;
  178.         p2.y = 400;
  179.         p3.x = 500;
  180.         p3.y = 250;
  181.         draw_triangle(&fb, p1, p2, p3, 0x0, 0xff, 0x0);

  182.         POINT center = { 600, 500, 0 };
  183.         int radius = 100;
  184.         draw_circle(&fb, center, radius, 0x0, 0xff, 0x0);

  185.         center.x = 700;
  186.         center.y = 250;
  187.         int a = -100;
  188.         draw_parabola_x(&fb, center, a, 0x0, 0xff, 0x0);
  189.         draw_parabola_x(&fb, center, -a, 0x0, 0xff, 0x0);
  190.         draw_parabola_y(&fb, center, a, 0x0, 0xff, 0x0);
  191.         draw_parabola_y(&fb, center, -a, 0x0, 0xff, 0x0);

  192.         fb_close(&fb);
  193.         return 0;
  194. }

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