Feamebuffer
Feamebuffer的概念
在 Linux 系统中通过 Framebuffer 驱动程序来控制 LCD。 Frame 是帧的意思, buffer 是缓冲的意思,这意味着 Framebuffer 就是一块内存,里面保存着一帧图像。
简单介绍 LCD 的操作原理: 驱动程序设置好 LCD 控制器: 根据 LCD 的参数设置 LCD 控制器的时序、信号极性; 根据 LCD
分辨率、 BPP 分配 Framebuffer。 APP 使用 ioctl 获得 LCD 分辨率、 BPP APP 通过 mmap 映射
Framebuffer,在 Framebuffer 中写入数据
BPP三种格式 如上图所示
我们可以很轻松的技术 具体某个像素点的值。
显示一片红色区域
首选我们先认识一个函数
有了这个函数,我们就可以拿到屏幕的信息。存放在var中
下面附上代码
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/fb.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
static int fd_fb;
static struct fb_var_screeninfo var; /* Current var */
static int screen_size;
static unsigned char *fb_base;
static unsigned int line_width;
static unsigned int pixel_width;
void lcd_put_pixel(int x, int y, unsigned int color)
{
unsigned char *pen_8 = fb_base+y*line_width+x*pixel_width;//注意 此处应该用char*指针
//
unsigned short *pen_16;
//16位给16bpp使用
unsigned int *pen_32;
//32位给32bpp使用
unsigned int red, green, blue;
pen_16 = (unsigned short *)pen_8;
pen_32 = (unsigned int *)pen_8;
switch (var.bits_per_pixel)
{
case 8:
{
*pen_8 = color;
break;
}
case 16:
{
/* 565 */
red = (color >> 16) & 0xff;
green = (color >> 8) & 0xff;
blue = (color >> 0) & 0xff;
color = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3);
*pen_16 = color;
break;
}
case 32:
{
*pen_32 = color;
break;
}
default:
{
printf("can't surport %dbpp\n", var.bits_per_pixel);
break;
}
}
}
int main(int argc, char **argv)
{
int i;
fd_fb = open("/dev/fb0", O_RDWR);// 打开嵌入式开发板的只读方式
if (fd_fb < 0)
{
printf("can't open /dev/fb0\n");
return -1;
}
if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var))//读取成功返回1
{
printf("can't get var\n");
return -1;
}
line_width = var.xres * var.bits_per_pixel / 8;//一行像素点的数量
pixel_width = var.bits_per_pixel / 8;// 像素点的宽度字节
screen_size = var.xres * var.yres * var.bits_per_pixel / 8;//屏幕一共有多少子节像素
fb_base = (unsigned char *)mmap(NULL , screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);
if (fb_base == (unsigned char *)-1)
{
printf("can't mmap\n");
return -1;
}
/* 清屏: 全部设为白色 */
memset(fb_base, 0xff, screen_size);
/* 随便设置出100个为红色 */
for (i = 0; i < 100; i++)
lcd_put_pixel(var.xres/2+i, var.yres/2, 0xFF0000);
munmap(fb_base , screen_size);
close(fd_fb);
return 0;
}