Framebuffer应用程序编程实例
编程流程:
(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)
#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)返回映射的首地址。
实例:
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <stdlib.h>
int main()
{
int fbfd = 0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
long int screensize = 0;
char *fbp = 0;
int x = 0, y = 0;
long int location = 0;
int sav=0;
/* open device*/
fbfd = open("/dev/fb0", O_RDWR);
if (!fbfd) {
printf("Error: cannot open framebuffer device.\n");
exit(1);
}
printf("The framebuffer device was opened successfully.\n");
/* Get fixed screen information */
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
printf("Error reading fixed information.\n");
exit(2);
}
/* Get variable screen information */
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
printf("Error reading variable information.\n");
exit(3);
}
/* show these information*/
printf("vinfo.xres=%d\n",vinfo.xres);
printf("vinfo.yres=%d\n",vinfo.yres);
printf("vinfo.bits_per_bits=%d\n",vinfo.bits_per_pixel);
printf("vinfo.xoffset=%d\n",vinfo.xoffset);
printf("vinfo.yoffset=%d\n",vinfo.yoffset);
printf("finfo.line_length=%d\n",finfo.line_length);
/* Figure out the size of the screen in bytes */
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
/* Map the device to memory */
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"); exit(4);
}
printf("The framebuffer device was mapped to memory successfully.\n");
memset(fbp,0,screensize);
/* Where we are going to put the pixel */
for(x=0;x<vinfo.xres;x )
for(y=0;y<vinfo.yres;y )
{
location = (x vinfo.xoffset) * (vinfo.bits_per_pixel/8)
(y vinfo.yoffset) * finfo.line_length;
*(fbp location) = 0xff; /* blue */
*(fbp location 1) = 0x00;
}
munmap(fbp, screensize); /* release the memory */
close(fbfd);
return 0;
}