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

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: 嵌入式

2011-05-03 20:05:09

 本文的copyright归yuweixian4230@163.com 所有,使用GPL发布,可以自由拷贝,转载。但转载请保持文档的完整性,注明原作者及原链接,严禁用于任何商业用途。
作者:yuweixian4230@163.com
博客:
yuweixian4230.blog.chinaunix.net 



  简单实现了 画点、划线、画矩形的程序编写,没有时间去编写其他的了,以后具体做到的这方面的事,在详细写吧。。
   
     没有写 颜色操作。

代码上传; framebuff-myself-press.rar  
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <linux/fb.h>
  5. #include <sys/mman.h>

  6. typedef struct fbdev{
  7.     int fdfd; //open "dev/fb0"
  8.     struct fb_var_screeninfo vinfo;
  9.     struct fb_fix_screeninfo finfo;
  10.     long int screensize;
  11.     char *map_fb;
  12.     
  13. }FBDEV;

  14. void init_dev(FBDEV *dev)
  15. {
  16.     FBDEV *fr_dev=dev;

  17.     fr_dev->fdfd=open("/dev/fb0",O_RDWR);
  18.     printf("the framebuffer device was opended successfully.\n");

  19.     ioctl(fr_dev->fdfd,FBIOGET_FSCREENINFO,&(fr_dev->finfo)); //获取 固定参数

  20.     ioctl(fr_dev->fdfd,FBIOGET_VSCREENINFO,&(fr_dev->vinfo)); //获取可变参数

  21.     fr_dev->screensize=fr_dev->vinfo.xres*fr_dev->vinfo.yres*fr_dev->vinfo.bits_per_pixel/8;

  22.     fr_dev->map_fb=(char *)mmap(NULL,fr_dev->screensize,PROT_READ|PROT_WRITE,MAP_SHARED,fr_dev->fdfd,0);

  23.     printf("init_dev successfully.\n");
  24. }

  25. void draw_dot(FBDEV *dev,int x,int y) //(x.y) 是坐标
  26. {
  27.     FBDEV *fr_dev=dev;
  28.     int *xx=&x;
  29.     int *yy=&y;    

  30.     long int location=0;
  31.     location=location=(*xx+fr_dev->vinfo.xoffset)*(fr_dev->vinfo.bits_per_pixel/8)+
  32.                  (*yy+fr_dev->vinfo.yoffset)*fr_dev->finfo.line_length;
  33.     int b=10;
  34.     int g=10;
  35.     int r=10;
  36.     unsigned short int t=r<<11|g<<5|b;
  37.     *((unsigned short int *)(fr_dev->map_fb+location))=t;
  38. }


  39. void draw_line(FBDEV *dev,int x1,int y1,int x2,int y2)
  40. {
  41.     FBDEV *fr_dev=dev;
  42.     int *xx1=&x1;
  43.     int *yy1=&y1;
  44.     int *xx2=&x2;
  45.     int *yy2=&y2;

  46.     int i=0;
  47.     int j=0;
  48.     int tekxx=*xx2-*xx1;
  49.     int tekyy=*yy2-*yy1;

  50.     //if((*xx2>=*xx1)&&(*yy2>=*yy1))
  51.     if(*xx2>=*xx1)
  52.     {    
  53.         for(i=*xx1;i<=*xx2;i++)
  54.         {
  55.             j=(i-*xx1)*tekyy/tekxx+*yy1;
  56.             draw_dot(fr_dev,i,j);
  57.         }
  58.     }
  59.     else
  60.     {
  61.         //if(*xx2<*xx1)
  62.         for(i=*xx2;i<*xx1;i++)
  63.         {
  64.             j=(i-*xx2)*tekyy/tekxx+*yy2;
  65.             draw_dot(fr_dev,i,j);
  66.         }
  67.     }


  68. }

  69. void draw_rect(FBDEV *dev,int x1,int y1,int x2,int y2)
  70. {
  71.     FBDEV *fr_dev=dev;
  72.     int *xx1=&x1;
  73.     int *yy1=&y1;
  74.     int *xx2=&x2;
  75.     int *yy2=&y2;
  76.     int i=0,j=0;
  77.    
  78.     for(j=*yy1;j<*yy2;j++) //注意 这里要 xx1 < xx2
  79.         for(i=*xx1;i<*xx2;i++)
  80.         {

  81.             draw_dot(fr_dev,i,j);            
  82.         
  83.         }
  84.     
  85. }
  86. int main()
  87. {
  88.     FBDEV     fr_dev;
  89.     fr_dev.fdfd=-1;
  90.     init_dev(&fr_dev);
  91.   
  92.     draw_line(&fr_dev,0,0,100,100);
  93.     draw_line(&fr_dev,200,200,110,110);
  94.     draw_line(&fr_dev,10,200,150,100);
  95.     draw_line(&fr_dev,300,10,160,90);
  96.     draw_rect(&fr_dev,300,200,320,240);
  97.         
  98.     printf("bye the framebuffer\n");
  99.     munmap(fr_dev.map_fb,fr_dev.screensize);
  100.     close(fr_dev.fdfd);
  101.     
  102.     return 0;    
  103. }
画了四条线,和一个矩形





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

54wanjia2016-10-14 17:33:09

博主,你这篇文章写的真是及时雨,我正在做这一块,想问一下,你这个能在linux桌面系统上跑吗,要做那些配置,谢谢了