fb.c 代码

帧缓冲区操作示例

FB 列子

#include <stdio.h>
#include <errno.h>
#include <string.h>

#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>

#include "common.h"

#define FB_DEVICE "/dev/fb0"

int init_fb(fb_info *fb_inf)
{
	int fd;

	if ((fd = open(FB_DEVICE, O_RDWR)) < 0){
		fprintf(stderr, "Open %s failed:%s\n", FB_DEVICE, strerror(errno));
		return -1;
	}

	struct fb_var_screeninfo fb_var;
	
	if (ioctl(fd, FBIOGET_VSCREENINFO, &fb_var) < 0){
		fprintf(stderr, "fb ioctl failed:%s\n", strerror(errno));
		return -1;
	}
	
	fb_inf->w = fb_var.xres;
	fb_inf->h = fb_var.yres;
	fb_inf->bpp = fb_var.bits_per_pixel;
#if 1
	printf("width:%d\thign:%d\tbpp:%d\n",
		  fb_inf->w, fb_inf->h, fb_inf->bpp);
#endif	
	fb_inf->fbmem = mmap(0, fb_inf->w * fb_inf->h * fb_inf->bpp /8,
					 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (fb_inf->fbmem == MAP_FAILED){
		fprintf(stderr, "fb mmap failed:%s\n", strerror(errno));
		return -1;
	}
	
	close(fd);
	
	return 0;
}

int fb_pixel(fb_info fb_inf, int x, int y, u32_t color)
{
	u8_t *pos = (u8_t*)fb_inf.fbmem + (fb_inf.w * y + x) * fb_inf.bpp/8;

	switch (fb_inf.bpp){
	case 32:				  /* 32 bits */
		*(pos + 3) = color >> 24;
	case 24:				  /* 24 bits */
		*(pos + 2) = color >> 16;
	case 16:	  		       /* 16 bits */
		*(pos + 1) = color >> 8;
	case 8:			      /* 8 bits */
		*pos = color;
		return 0;
	default:
		return -1;
	}

	return -1;

}

/* painting horizontal */
int fb_pixel_row(fb_info fb_inf, int x, int y, int len, u32_t color)
{
	int i;
#if 1
	for(i = x; i < len; i++){
		fb_pixel(fb_inf,x+i, y, color);
	}
#else
	/* FIXME: not */
//	memset(fb_inf.fbmem + (y * fb_inf.w + x) * fb_inf.bpp/8, color, len * 
#endif

	return 0;
}

int fb_test(void)
{

	printf("Framebuffer Test\n");
	
	fb_info fb_inf;
	if(init_fb(&fb_inf) < 0)
		return -1;
	printf("Framebuffer  Test fb_pixel\n");
	
	/* point test */
//	point_t p, p2; p.x = 640;	p.y = 400;
	fb_pixel(fb_inf, 640, 400, 0xFFFFFFFF);

	/* line test*/
//	p.x = 0; p.y = 200;
//	p2.x = 1280, p2.y = 200;
	fb_pixel_row(fb_inf, 0, 200, 1280, 0xFF0000);
	

	munmap(fb_inf.fbmem, fb_inf.w * fb_inf.h * fb_inf.bpp / 8);
	return 0;
}


### 关于 `fb.h` 库的文档和使用 在现有的引用中并未提及有关 `fb.h` 的具体信息。然而,基于广泛的知识体系可以推测,`fb.h` 可能是一个特定领域内的头文件,通常用于嵌入式开发、硬件驱动或者某些专用框架中。 #### 头文件的作用 头文件(如 `.h` 文件)通常是 C 或 C++ 编程语言中的接口定义文件,其中可能包含了函数声明、宏定义以及结构体或其他数据类型的定义[^3]。对于 `fb.h` 而言,其功能取决于具体的实现环境: 1. **帧缓冲区支持** 在 Linux 嵌入式系统中,`fb.h` 很可能是与帧缓冲设备(Frame Buffer, FB)相关的头文件。它提供了访问图形显示硬件的功能,允许开发者通过标准 API 控制屏幕输出[^4]。 2. **第三方库或自定义模块** 如果不是标准的帧缓冲头文件,则 `fb.h` 可能属于某个特定的第三方库或项目的一部分。这种情况下,建议查阅该库的相关文档或源码仓库以获取更多信息。 #### 使用方法 假设 `fb.h` 是指代 Linux 中的标准帧缓冲头文件 `/usr/include/linux/fb.h`,以下是基本的使用方式: ```c #include <linux/fb.h> #include <fcntl.h> #include <sys/mman.h> int main() { int fbfd = open("/dev/fb0", O_RDWR); // 打开帧缓冲设备 if (fbfd == -1) { perror("无法打开帧缓冲设备"); return 1; } struct fb_var_screeninfo vinfo; // 获取可变屏幕信息 ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo); printf("分辨率: %dx%d\n", vinfo.xres, vinfo.yres); close(fbfd); return 0; } ``` 上述代码展示了如何通过帧缓冲设备读取显示器的基本参数[^5]。 #### 文档查找途径 如果目标是寻找更详细的官方文档: - 访问 Linux 内核文档页面:https://www.kernel.org/doc/html/latest/gpu/index.html[^6] - 查阅相关书籍,例如《Linux Device Drivers》第三版[^7] --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值