嵌入式linux文件操作lcd屏幕指定坐标显示一个点简单示例

嵌入式linux文件操作lcd简单示例

linux操作lcd的方式就是通过映射/dev/fb*,读写来操作。
操作lcd又以下几个步骤
1.打开/dev/fb设备文件

fbfd = open("/dev/fb0", O_RDWR);

2.用 ioctl 操作取得当前显示屏幕的参数,如屏幕分辨率,每个像素点的比特数。根据屏幕参数可计算屏幕缓冲区的大小。

 ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo);  
 ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo);
 screensize = vinfo.xres * vinfo.yres * 

3.将屏幕缓冲区映射到用户空间(mmap)。

fbp = mmap(0,screensize,PROT_READ | PROT_WRITE,MAP_SHARED, fbfd, 0);

4.映射后就可以直接读写屏幕缓冲区

int lcd_disp_point(int x,int y,unsigned int c)
{
    
    
	 *(fbd + y*vinfo.xres + x) = c;
	return 0;
}

lcd.c

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

static int fbfd = 0;
static long int screensize = 0;
static struct fb_var_screeninfo vinfo;
static struct fb_fix_screeninfo finfo;
static int *fbp = 0;

int lcd_init()
{
    
    
	/*打开设备文件*/
 	fbfd = open("/dev/fb0", O_RDWR);
	if(-1 == fbfd)
	{
    
    
		printf("[%s]:[%d] open fb file error\r\n", __FUNCTION__, __LINE__);
		return (-1);
	]
	/*取得屏幕相关参数*/
	 ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo);  
	 ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo);
	/*计算屏幕缓冲区大小*/
	screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
	/*映射屏幕缓冲区到用户地址空间*/
	fbp = mmap(0,screensize,PROT_READ | PROT_WRITE,MAP_SHARED, fbfd, 0);
	if(fbp == NULL)
    {
    
    
        printf("mmap framebuffer fail.\n");
        return -1;
    }

    if(vinfo.bits_per_pixel == 8)
    {
    
    
        printf("8bpp framebuffer test.\n");
    }
    else if(vinfo.bits_per_pixel == 16)
    {
    
    
        printf("16bpp framebuffer test.\n");
    }
    else if(vinfo.bits_per_pixel == 24)
    {
    
    
        printf("24bpp framebuffer test.\n");
    }
    else if(vinfo.bits_per_pixel == 32)
    {
    
    
        printf("32bpp framebuffer test.\n");
    }
	return 0;
}


int close_lcd()
{
    
    
	/*释放缓冲区,关闭设备*/
	munmap(fbp, screensize);
	close(fbfd);
	return 0;
}

int lcd_dis_point(int x,int y,unsigned int c)
{
    
    
	 *(fbp + y*vinfo.xres + x) = c;
	return 0;
}

#define lcd_xy_point(x,y,c) *(fbp + y*vinfo.xres + x)=c

int lcd_dis_pic(unsigned int *rgb,unsigned int w,unsigned int h)
{
    
    
	unsigned int i,j,dis_w,dis_h;
	if(w < vinfo.xres){
    
    dis_w = w}
	else{
    
    dis_w = vinfo.xres}
	if(h < vinfo.xres){
    
    dis_h = h}
	else{
    
    dis_h = vinfo.yres}
	for(i = 0; i < dis_h; i++)
	{
    
    
		for(j = 0; j < dis_w; j++)
		{
    
    
			*(fbd + i*vinfo.xres + j) = *rgb;
			rgb++;
		}
	}
	
	return 0;
}

int lcd_clear()
{
    
    
	int x,y;

	for(y = 0; y < vinfo.yres; y++)
	{
    
    
		for(x =0; x < vinfo.xres; x++)
		{
    
    
			lcd_xy_point(x,y,0x000000);
		}
	}

	return 0;
}

main.c

#include <stdio.h>
#include “lcd.h”

int main()
{
    
    
	lcd_init(); 
	lcd_clear();
	lcd_dis_point(100, 50, 0x00ffffff);
	while(1)
	{
    
    
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/u010835747/article/details/108543194