嵌入式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++)
		{
			*(fbp + 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;
}

lcd.h

#ifndef __LCD_H
#define __LCD_H

int lcd_init();
int lcd_clear();
int close_lcd();
int lcd_dis_point(int x,int y,unsigned int c);

#endif

main.c

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

int main()
{
	lcd_init(); 
	lcd_clear();
	lcd_dis_point(100, 50, 0x00ffffff);
	while(1)
	{
	}
	return 0;
}
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要让LCD显示几个色块,可以使用一些图形库来完成,比如Framebuffer、DirectFB等。 以下是在嵌入式linux中使用Framebuffer显示几个色块的步骤: 1. 打开/dev/fb0设备文件,获取FB设备的属性信息。 ``` int fd_fb = open("/dev/fb0", O_RDWR); if (fd_fb < 0) { perror("open /dev/fb0 failed"); return -1; } struct fb_var_screeninfo fb_varinfo; struct fb_fix_screeninfo fb_fixinfo; if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &fb_varinfo) < 0) { perror("ioctl FBIOGET_VSCREENINFO failed"); return -1; } if (ioctl(fd_fb, FBIOGET_FSCREENINFO, &fb_fixinfo) < 0) { perror("ioctl FBIOGET_FSCREENINFO failed"); return -1; } ``` 2. 通过ioctl()函数以及mmap()函数,获取到显存和每个像素的信息,然后设置颜色并绘制矩形。 ``` unsigned int *fb_mem = mmap(NULL, fb_fixinfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0); unsigned int color[4] = {0x00ff0000, 0x0000ff00, 0x000000ff, 0x00ffffff}; int i, j, k = 0; for (i = 0; i < fb_varinfo.yres / 2; i++) { for (j = 0; j < fb_varinfo.xres; j++) { fb_mem[i * fb_varinfo.xres + j] = color[k % 4]; } k++; } for (i = fb_varinfo.yres / 2; i < fb_varinfo.yres; i++) { for (j = 0; j < fb_varinfo.xres; j++) { fb_mem[i * fb_varinfo.xres + j] = color[(k + 2) % 4]; } k++; } ``` 3. 最后,关闭设备文件和映射区域。 ``` munmap(fb_mem, fb_fixinfo.smem_len); close(fd_fb); ``` 上述的代码会在屏幕显示4个色块(红、绿、蓝、白),分别占据屏幕的上下两半部分。如果想要显示其他颜色的块或者调整块的位置和大小,可以修改代码中的颜色数组和循环次数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值