显示设备文件接口

 

例:  将/dev/fb0文件映射到内存空间(mmap), 显示相应内容

   先通过ioctl()取得framebuffer对应的设备文件/dev/fb0相关的信息, 包括显示区域的长度/宽度/每像素占用的位长等, 并以此计算映射到内存空间时的对应内存空间大小.

   向/dev/f0b映射到的内存空间写入数据, 即可在屏幕上显示相应内容.

myfb.c

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <sys/mman.h>

#include <linux/fb.h>

#define ERROR(flag,msg)            \
if(flag)                        \
{                                \
    printf("%d: ",__LINE__);    \
    fflush(stdout);                \
    perror(msg);                \
    exit(errno);                \
}

int main(int argc, char *argv[])
{
    int fd;
    int i;
    
    struct fb_var_screeninfo var;

    fd = open("/dev/fb0", O_RDWR);
    ERROR(fd == -1, "open()");

    int ret = ioctl(fd, FBIOGET_VSCREENINFO, &var);
    ERROR(ret == -1, "ioctl()");

    printf("bits_per_pixel = %d\n", var.bits_per_pixel);
    printf("x = %d\n", var.xres);
    printf("y = %d\n", var.yres);

    int screensize = var.xres * var.yres * var.bits_per_pixel / 8;
    char *fbp = mmap(NULL, screensize,PROT_READ | PROT_WRITE,MAP_SHARED, fd, 0);

    int x,y;

    for(y = var.yres/3; y < var.yres * 2/3; y++)
    {
        for(x = var.xres/3; x < var.xres * 2/3; x++)
        {
            int location = (y * var.xres + x) * var.bits_per_pixel/8;
            int r = 31 - (y - 200) / 16;
            int g = (x - 400) / 6;
            int b = 10;
            unsigned short t = r << 11 | g << 5 | b;
            *(unsigned short*)(fbp + location) = t;
        }
    }
    munmap(fbp,screensize);
    sleep(2);

    return 0;
}

编译链接后, 在tty1终端运行, 输出结果如下:

 

posted @ 2016-01-30 23:17  zhanglong71  阅读(200)  评论(0编辑  收藏  举报