Linux下,Framebuffer設備文件名通常是/dev/fb0,1,2等。控制framebuffer設備的一般步驟如下:
1) 打開設備,映射framebuffer
2)依照硬件要求,准備好數據
3)把數據復制到framebuffer
例子程序如下:
1)打開設備,映射framebuffer
static void *fbbuf;
int openfb(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;
}
2)數據准備,假設lcd控制器被初始化為565,16bit格式的
static inline int make_pixel(unsigned int a, unsigned int r, unsigned int g, unsigned int b)
{
return (unsigned int)(((r>>3)<<11)|((g>>2)<<5|(b>>3)));
}
3) 把想要顯示的數據復制到framebuffer,假設把framebuffer填充成一種顏色
static void fill_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;
}
}
}
下面程序把lcd屏幕填充成藍色
fill_pixel(make_pixel(0, 0, 0,0xff), 0, 0, screen_width, screen_height);