嵌入式一 Feamebuffer编程

本文介绍了如何在Linux系统中使用Framebuffer驱动程序控制LCD,包括设置时序、信号极性,以及通过ioctl获取屏幕信息和mmap映射Framebuffer,展示了如何使用`lcd_put_pixel`函数在屏幕上显示一片红色区域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值