编程流程:
(1) 打开设备 open("/dev/fb0",O_RDWR);
(2) 获取framebuffer设备信息.ioctl(int fb,FBIOGET_FSCREENINFO,&finfo);
ioctl函数是实现对设备的信息获取和设定,
第一个参数为文件描述符,第二个参数为具体设备的参数,对于framebuffer,参数在linux/fb.h中定义的。
#define FBIOGET_VSCREENINFO 0x4600 //获取设备无关的数据信息fb_var_screeninfo #define FBIOPUT_VSCREENINFO 0x4601 //设定设备无关的数据信息 #define FBIOGET_FSCREENINFO 0x4602 //获取设备无关的常值信息fb_fix_screeninfo #define FBIOGETCMAP 0x4604 //获取设备无关颜色表信息 #define FBIOPUTCMAP 0x4605 //设定设备无关颜色表信息 #define FBIOPAN_DISPLAY 0x4606 #define FBIO_CURSOR _IOWR('F', 0x08, struct fb_cursor)
第三个参数是存放信息的结构体或者缓冲区
(3) 内存映射 mmap函数。
头文件:sys/mman.h
常用用法:mmap(0,screensize,PROT_RD |PROT_WR,MAP_SHARED,int fb,0)返回映射的首地址。
实例:
1 #include <unistd.h> 2 #include <stdio.h> 3 #include <fcntl.h> 4 #include <linux/fb.h> 5 #include <sys/mman.h> 6 #include <stdlib.h> 7 int main() 8 { 9 int fbfd = 0; 10 struct fb_var_screeninfo vinfo; 11 struct fb_fix_screeninfo finfo; 12 long int screensize = 0; 13 char *fbp = 0; 14 int x = 0, y = 0; 15 long int location = 0; 16 int sav=0; 17 18 /* open device*/ 19 fbfd = open("/dev/fb0", O_RDWR); 20 if (!fbfd) { 21 printf("Error: cannot open framebuffer device. "); 22 exit(1); 23 } 24 printf("The framebuffer device was opened successfully. "); 25 26 /* Get fixed screen information */ 27 if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) { 28 printf("Error reading fixed information. "); 29 exit(2); 30 } 31 /* Get variable screen information */ 32 if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) { 33 printf("Error reading variable information. "); 34 exit(3); 35 } 36 /* show these information*/ 37 printf("vinfo.xres=%d ",vinfo.xres); 38 printf("vinfo.yres=%d ",vinfo.yres); 39 printf("vinfo.bits_per_bits=%d ",vinfo.bits_per_pixel); 40 printf("vinfo.xoffset=%d ",vinfo.xoffset); 41 printf("vinfo.yoffset=%d ",vinfo.yoffset); 42 printf("finfo.line_length=%d ",finfo.line_length); 43 44 /* Figure out the size of the screen in bytes */ 45 screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8; 46 47 /* Map the device to memory */ 48 fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, 49 fbfd, 0); 50 if ((int)fbp == -1) { printf("Error: failed to map framebuffer device to memory. "); exit(4); 51 } 52 printf("The framebuffer device was mapped to memory successfully. "); 53 memset(fbp,0,screensize); 54 /* Where we are going to put the pixel */ 55 56 for(x=0;x<vinfo.xres;x ) 57 for(y=0;y<vinfo.yres;y ) 58 { 59 location = (x vinfo.xoffset) * (vinfo.bits_per_pixel/8) 60 (y vinfo.yoffset) * finfo.line_length; 61 *(fbp location) = 0xff; /* blue */ 62 *(fbp location 1) = 0x00; 63 } 64 munmap(fbp, screensize); /* release the memory */ 65 close(fbfd); 66 return 0; 67 }