新路程------测试framebuffer的小程序(1)显示红色方块

来源:互联网 发布:路由器ip绑定mac地址 编辑:程序博客网 时间:2024/06/07 03:54
#include <unistd.h>  
#include <stdio.h>  
#include <stdlib.h>  
#include <fcntl.h>  
#include <linux/fb.h>  
#include <sys/mman.h>  
  
int main ()   
{  
     int fp=0;  
     struct fb_var_screeninfo  vinfo;  
     struct fb_fix_screeninfo  finfo;  
     long screensize=0;  
     char *fbp = 0;  
     int x = 0, y = 0;  
     long location = 0;  
     fp = open ("/dev/fb0",O_RDWR);  
  
     if (fp < 0)  
     {  
          printf("Error : Can not open framebuffer device/n");  
          exit(1);  
     }  
  
     if (ioctl(fp,FBIOGET_FSCREENINFO,&finfo))  
     {  
          printf("Error reading fixed information/n");  
          exit(2);  
     }  
       
     if (ioctl(fp,FBIOGET_VSCREENINFO,&vinfo))  
     {  
          printf("Error reading variable information/n");  
          exit(3);  
     }  
  
      screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;  
     /*这就是把fp所指的文件中从开始到screensize大小的内容给映射出来,得到一个指向这块空间的指针*/  
     fbp =(char *) mmap (0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fp,0);  
     if ((int) fbp == -1)  
     {  
        printf ("Error: failed to map framebuffer device to memory./n");  
        exit (4);  
     }  
     /*这是你想画的点的位置坐标,(0,0)点在屏幕左上角*/  
     x = 100;  
     y = 100;  
      
  for(x=100;x<200;x++)
  {
  for(y=100;y<200;y++)
    {
      location = x * (vinfo.bits_per_pixel / 8) + y  *  finfo.line_length;
     *(fbp + location) = 0;  /* 蓝色的色深 */  /*直接赋值来改变屏幕上某点的颜色*/  
     *(fbp + location + 1) = 0; /* 绿色的色深*/    
     *(fbp + location + 2) = 200; /* 红色的色深*/    
     *(fbp + location + 3) = 0;  /* 是否透明*/ 
        }
     }  
  
     munmap (fbp, screensize); /*解除映射*/  
     close (fp);    /*关闭文件*/  
     return 0;  
  

}  

编译:arm-linux-gcc -o lisa testlvds.c

结果如下


方框内的红色就是

0 0
原创粉丝点击