linux下如何实现简单刷屏【转】

     今天为了测试我的LCD有没有坏点写了一个简单的刷屏程序,效果还不错,分析给大家。代码如下:

 #include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc,char *argv[])
{
 int fbfd = 0;
 struct fb_var_screeninfo vinfo;
 struct fb_fix_screeninfo finfo;
 long int screensize = 0;
 char *fbp = 0;
 char inbuf[240*320*2];
    int i = 0;
 /*open /dev/fb0 */
 fbfd = open("/dev/fb0",O_RDWR);
 if(fbfd<=0)
 {
  printf("Error:cannot open framebuffer devices./n");
  return -1;
 }
 printf("The framebuffer devices was opened successfully./n");
 /*get framebuffer infomation*/
 if(ioctl(fbfd,FBIOGET_FSCREENINFO,&finfo))
 {
  printf("Error: reanding fixed informatin./n");
  return -1;
 }
    if(ioctl(fbfd,FBIOGET_VSCREENINFO,&vinfo))
     {
   printf("Error: reanding var informatin./n");
   return -1;
  }
 
 /*set screensiez*/
 screensize = vinfo.xres*vinfo.yres*vinfo.bits_per_pixel/8;
    /*mmap*/
 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");
   return -4;
 }
    
  printf(" The framebuffer device was mapped to memory sucessfully./n");
  memcpy(inbuf,fbp,screensize);//保存刷屏前显存中的数据
     for(i = 0;i<0xff;i++)
  {
   memset(fbp,i,screensize);
   sleep(1);
  }
   memcpy(fbp,inbuf,screensize);//将保存的显存数据复原回去
   munmap(fbp,screensize);
  printf("The framebuffer device was munmaped successfully./n");
  close(fbfd);
  printf("The framebuffer device was closed successfully./n");
  return 0;

}

posted @ 2016-06-15 14:05  Sky&Zhang  阅读(1758)  评论(0编辑  收藏  举报