Framebuffer编程

         应用程序需要知道Framebuffer设备的相关参数,必须通过ioctl()系统调用来完成。
在头文件中定义了所有的ioctl命令字,不过,最常用的ioctl命令字是下面这两个:FBIOGET_FSCREENINFO和FBIOGET_VSCREENINFO。
前者返回与Framebuffer有关的固定的信息,比如图形硬件上实际的帧缓存空间的大小、能否硬件加速等信息。
而后者返回的是与Framebuffer有关的可变信息。可变的信息就是指Framebuffer的长度、宽度以及颜色深度等信息。

控制framebuffer设备的一般步骤如下:
1) 打开设备,映射framebuffer
2)依照硬件要求,准备好数据
3)把数据复制到framebuffer

打开设备,映射framebuffe
 
    static void*fbbuf;  
 
    intopenfb(char *devname)  
 
   
 
            int fd;  
 
            fd = open(devname, O_RDWR);  
 
            if (ioctl(fd, FBIOGET_VSCREENINFO, &fbvar)< 0)  
 
                    return -1;  
 
            bpp = fbvar.bits_per_pixel;  
 
            screen_size = fbvar.xres * fbvar.yres * bpp / 8; 
 
            fbbuf = mmap(0, screen_size, PROT_READ | PROT_WRITE, MAP_SHARED,fd, 0);  
 
            return fd;  
 
       

数据准备,假设lcd控制器被初始化为565,16bit格式的
 
    staticinline int  make_pixel(unsigned int a, unsignedint r, unsigned int g, unsigned int b)  
 
   
 
            return (unsignedint)(((r>>3)<<11)|((g>>2)<<5|(b>>3))); 
 
    }

把想要显示的数据复制到framebuffer
 
    static voidfill_pixel(unsigned int pixel, int x0, int y0, int w, int h) 
 
   
 
            int i, j;  
 
            unsigned short *pbuf = (unsigned short *)fbbuf; 
 
            for (i = y0; i < h; i ++) {  
 
                    for (j = x0; j < w; j ++) {  
 
                            pbuf[i * screen_width + j] = pixel;  
 
                    }  
 
            }  
 
    }
以上就是Linux Framebuffer编程的过程。

#include<asm/types.h> 
               
#include<fcntl.h> 
                       
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <linux/fb.h>
#include <sys/time.h>
static void *fbbuf;
unsigned int bpp;
unsigned int screen_size;
struct fb_var_screeninfo fbvar;
struct fb_fix_screeninfo finfo;
int openfb(char *devname)
{
 
    intfd;
 
    fd =open(devname, O_RDWR);
 
    if(ioctl(fd, FBIOGET_VSCREENINFO, &fbvar)< 0)
 
            return -1;
 
    printf("theclock is %d\n", fbvar.pixclock);
 
    bpp =fbvar.bits_per_pixel;
 
    screen_size= fbvar.xres * fbvar.yres * bpp / 8;
 
    fbbuf =mmap(0, screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,0);
 
    returnfd;
}
static inline int 
make_pixel(unsigned int a,unsigned int r, unsigned int g, unsigned int b)
{
 
    return(unsignedint)(((r>>3)<<11)|((g>>2)<<5|(b>>3)));
}
static void fill_pixel(unsigned int pixel, int x0, int y0, int w,int h)
{
 
    int i,j;
 
    unsignedshort *pbuf = (unsigned short *)fbbuf;
 
    for (i = y0;i < h; i ++) {
 
            for (j = x0; j < w; j ++) {
 
                    pbuf[i * 240 + j] = pixel;
 
            }
 
    }
}
int main () {
 
    unsigned intr, g, b;
 
    intfp=0;
 
    unsigned intcount;

 
    r = 0;
 
    g = 0;
 
    b = 0;
 
    fp = openfb("/dev/fb0");

 
    if (fp< 0){
 
            printf("Error : Can not open framebuffer device\n");
 
            exit(1);
 
    }

 
    while(1){
 
            for (count = 0; count < 100; count++) {
 
                    r = 0xff;
 
                    fill_pixel(make_pixel(0, r, g, b), 0, 0, 240, 320);
 
            }
 
          for (count = 0; count < 100; count++) {
 
                    r = 0x00;
 
                    fill_pixel(make_pixel(0, r, g, b), 0, 0, 240, 320);
 
          }
 
    }
 
    return0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值