1. 程式人生 > >圖片播放器(二):framebuffer基本操作程式碼

圖片播放器(二):framebuffer基本操作程式碼

display###############

    fb.c                                         //fb檔案
    Makefile

include###############
    fb.h


start.c                                                //最開始的檔案

Makefile                                         //主Makefile
Makefile.build

 

fb.c

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

#include <fb.h>

// 全域性變數
unsigned int *pfb = NULL;
int           fb_fd = -1;


int fb_open(void)
{
	int ret = -1;
	
	struct fb_fix_screeninfo finfo = {{0},};
	struct fb_var_screeninfo vinfo = {{0},};
	
	// 第1步:開啟裝置
	fb_fd = open(FBDEVICE, O_RDWR);
	if (fb_fd < 0)
	{
		perror("open");
		return -1;
	}
	printf("open %s success.\n", FBDEVICE);
	
	// 第2步:獲取裝置的硬體資訊
	ret = ioctl(fb_fd, FBIOGET_FSCREENINFO, &finfo);
	if (ret < 0)
	{
		perror("ioctl");
		return -1;
	}
	printf("smem_start = 0x%lx, smem_len = %u.\n", finfo.smem_start, finfo.smem_len);
	
	ret = ioctl(fb_fd, FBIOGET_VSCREENINFO, &vinfo);
	if (ret < 0)
	{
		perror("ioctl");
		return -1;
	}
	printf("xres = %u, yres = %u.\n", vinfo.xres, vinfo.yres);
	printf("xres_virtual = %u, yres_virtual = %u.\n", vinfo.xres_virtual, vinfo.yres_virtual);
	printf("bpp = %u.\n", vinfo.bits_per_pixel);

	
	// 第3步:進行mmap
	unsigned long len = vinfo.xres_virtual * vinfo.yres_virtual * vinfo.bits_per_pixel / 8;
	printf("len = %ld\n", len);
	pfb = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fb_fd, 0);
	if (NULL == pfb)
	{
		perror("mmap");
		return -1;
	}
	printf("pfb = %p.\n", pfb);
	


	return 0;
}

void fb_close(void)
{
	close(fb_fd);
}


void fb_draw_back(unsigned int width, unsigned int height, unsigned int color)
{
	unsigned int x, y;
	
	for (y=0; y<height; y++)
	{
		for (x=0; x<width; x++)
		{
			*(pfb + y * WIDTH + x) = color;
		}
	}
}

void fb_draw_line(unsigned int color)
{
	unsigned int x, y;
	
	for (x=50; x<600; x++)
	{
		*(pfb + 200 * WIDTH + x) = color;
	}
}