嵌入式Linux下完成LCD屏文字显示(帧缓冲框架)

帧缓冲框架是Linux下专门为显示类设备设计的接口,目的是将硬件和软件层分离开,方便应用层的编程,也方便应用层程序移植。帧缓冲框架向驱动层和应用层分别提供了一套标准接口,驱动层按照框架编写驱动,应用层按照框架编写应用程序。帧缓冲在/dev目录下生成的标准节点是fb,比如:/dev/fb0,/dev/fb1等等。

这篇文章就介绍在应用层 如何利用帧缓冲框架接口封装LCD屏的画点函数,获取LCD屏的硬件信息,完成对LCD屏编程,实现文字、数字显示。当期的文字采用点阵方式取模来完成显示,比较简单,与单片机上的LCD编程思路一样,可以更方便快速学习帧缓冲编程。后续正常开发中一般采用矢量字库完成字体显示,大小调整方便,字体更换方法,在前面文章有介绍过嵌入式Linux如何交叉编译freetype库。

嵌入式Linux下完成LCD屏文字显示(帧缓冲框架)

编程思路

下面是帧缓冲框架图:

嵌入式Linux下完成LCD屏文字显示(帧缓冲框架)
嵌入式Linux下完成LCD屏文字显示(帧缓冲框架)
帧缓冲设备是标准的字符设备,通过open函数打开设备,再通过ioctl接口获取LCD屏的一些硬件参数信息,在利用mmap函数映射LCD屏的地址到应用层。映射的这个地址就相当于是LCD屏的显存地址,对这个地址里写入数据就可以在LCD屏硬件上实时显示出来。

2. 完整示例代码

2.1 中文显示

下面这份代码是单个汉字取模进行显示的,代码较少,整体比较简单。

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fb.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <string.h>
struct fb_var_screeninfo var; //可变参数
struct fb_fix_screeninfo fix; //固定参数
unsigned char *fb_mem=NULL; //LCD屏的首地址
extern unsigned char font[];
/*
函数功能: 画点
*/
void Show_Pixel(int x,int y,int color)
{
unsigned int *lcd=(unsigned int *)(fb_mem+y*var.xres*var.bits_per_pixel/8+x*var.bits_per_pixel/8);
*lcd=color; //颜色赋值
}
/*
函数功能: 显示中文
说明:取模的字体是按照横向取模进行取点阵码。
取模的字体宽度是8的倍数。
*/
void ShowFont(int x,int y,int size,unsigned char *data)
{
int i,j,x0=x;
unsigned char tmp;
for(i=0;i<size/8*size;i++)
{
tmp=data[i];
for(j=0;j<8;j++)
{
if(tmp&0x80)Show_Pixel(x0,y,0xFF0033);
//else 画背景色
x0++;
tmp<<=1;
}
if(x0-x==size)
{
y++;
x0=x;
}
}
}
int main(int argc,char **argv)
{
int fd=open("/dev/fb0",O_RDWR);
if(fd<0)
{
perror("设备文件打开失败");
return 0;
}
/*1. 获取LCD屏的可变形参*/
ioctl(fd,FBIOGET_VSCREENINFO,&var);
printf("分辨率:%d*%d\n",var.xres,var.yres);
printf("像素点位数:%d\n",var.bits_per_pixel);
/*2. 获取LCD屏的固定形参*/
ioctl(fd,FBIOGET_FSCREENINFO,&fix);
printf("映射的长度:%d\n",fix.smem_len);
printf("一行的字节数:%d\n",fix.line_length);
/*3. 映射LCD缓冲区地址到进程空间*/
fb_mem=mmap(NULL,fix.smem_len,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if(fb_mem==NULL)
{
perror("空间映射失败!\n");
return 0;
}
/*4. 控制显示屏*/
memset(fb_mem,0xFFFFFF,fix.smem_len); //将屏幕清屏为白色
ShowFont(100+0*56,100,56,font);
ShowFont(100+1*56,100,56,font+7*56*1);
ShowFont(100+2*56,100,56,font+7*56*2);
munmap(fb_mem,fix.smem_len);
close(fd);
return 0;
}
unsigned char font[]=
{ 
/*--文字:嵌--*/
/*--幼圆42;此字体下对应的点阵为:宽x高=56x56 --*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,
0x00,0x00,0x00,0x03,0x80,0x00,0x7C,0x00,0x03,0x80,0x07,0xC0,0x00,0x7C,0x00,0x03,
0xC0,0x07,0xC0,0x00,0x7C,0x00,0x03,0x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0x0F,0xE0,0x00,0x01,0xE0,0x0F,0x00,0x0F,0xF0,0x00,0x01,
0xE0,0x0F,0x00,0x1E,0xF0,0x00,0x01,0xE0,0x0F,0x00,0x1E,0xF8,0x00,0x01,0xE0,0x0F,
0x00,0x3E,0x78,0x00,0x01,0xE0,0x0F,0x00,0x7C,0x7C,0x00,0x01,0xE0,0x0F,0x00,0x78,
0x3E,0x00,0x01,0xE0,0x0F,0x00,0xF8,0x1E,0x00,0x01,0xE0,0x0F,0x01,0xF0,0x1F,0x00,
0x01,0xE0,0x0F,0x03,0xE0,0x0F,0x80,0x01,0xE0,0x0F,0x07,0xE0,0x07,0xC0,0x01,0xE0,
0x0F,0x0F,0xC0,0x03,0xE0,0x01,0xE0,0x0F,0x1F,0x80,0x01,0xF0,0x01,0xFF,0xFF,0x3F,
0x00,0x01,0xF8,0x00,0xFF,0xFF,0x7E,0x00,0x00,0xFC,0x00,0x7F,0xFE,0x78,0x00,0x00,
0x3C,0x00,0x00,0x00,0x70,0x00,0x00,0x18,
/*--文字:入--*/
/*--幼圆42;此字体下对应的点阵为:宽x高=56x56 --*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xC0,0x00,0x00,
0x00,0x00,0x00,0x0F,0xE0,0x00,0x00,0x00,0x00,0x00,0x07,0xF0,0x00,0x00,0x00,0x00,
0x00,0x01,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,
0x7C,0x00,0x00,0x00,0x00,0x00,0x00,0x3E,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x00,
0x00,0x00,0x00,0x00,0x00,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,
0x00,0x00,0x00,0x3F,0x80,0x00,0x00,0x00,0x00,0x00,0x3F,0x80,0x00,0x00,0x00,0x00,
0x00,0x7F,0xC0,0x00,0x00,0x00,0x00,0x00,0x7F,0xC0,0x00,0x00,0x00,0x00,0x00,0x7B,
0xC0,0x00,0x00,0x00,0x00,0x00,0xFB,0xE0,0x00,0x00,0x00,0x00,0x00,0xF1,0xE0,0x00,
0x00,0x00,0x00,0x00,0xF1,0xE0,0x00,0x00,0x00,0x00,0x01,0xF1,0xF0,0x00,0x00,0x00,
0x00,0x01,0xE0,0xF0,0x00,0x00,0x00,0x00,0x03,0xE0,0xF8,0x00,0x00,0x00,0x00,0x03,
0xC0,0xF8,0x00,0x00,0x00,0x00,0x07,0xC0,0x7C,0x00,0x00,0x00,0x00,0x07,0x80,0x7C,
0x00,0x00,0x00,0x00,0x0F,0x80,0x3C,0x00,0x00,0x00,0x00,0x0F,0x00,0x3E,0x00,0x00,
0x00,0x00,0x1F,0x00,0x1E,0x00,0x00,0x00,0x00,0x1E,0x00,0x1F,0x00,0x00,0x00,0x00,
0x3E,0x00,0x0F,0x00,0x00,0x00,0x00,0x7C,0x00,0x0F,0x80,0x00,0x00,0x00,0x7C,0x00,
0x07,0xC0,0x00,0x00,0x00,0xF8,0x00,0x07,0xC0,0x00,0x00,0x01,0xF0,0x00,0x03,0xE0,
0x00,0x00,0x01,0xE0,0x00,0x01,0xE0,0x00,0x00,0x03,0xE0,0x00,0x01,0xF0,0x00,0x00,
0x07,0xC0,0x00,0x00,0xF8,0x00,0x00,0x0F,0x80,0x00,0x00,0xF8,0x00,0x00,0x1F,0x00,
0x00,0x00,0x7C,0x00,0x00,0x1F,0x00,0x00,0x00,0x3E,0x00,0x00,0x3E,0x00,0x00,0x00,
0x3E,0x00,0x00,0x7C,0x00,0x00,0x00,0x1F,0x00,0x00,0xF8,0x00,0x00,0x00,0x0F,0x80,
0x01,0xF0,0x00,0x00,0x00,0x07,0xC0,0x03,0xE0,0x00,0x00,0x00,0x03,0xE0,0x07,0xC0,
0x00,0x00,0x00,0x03,0xF0,0x0F,0x80,0x00,0x00,0x00,0x01,0xF8,0x1F,0x00,0x00,0x00,
0x00,0x00,0xFC,0x1E,0x00,0x00,0x00,0x00,0x00,0x7C,0x1C,0x00,0x00,0x00,0x00,0x00,
0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*--文字:式--*/
/*--幼圆42;此字体下对应的点阵为:宽x高=56x56 --*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x06,0x00,0x00,0x00,0x00,0x00,0x00,0xEF,0x80,0x00,0x00,0x00,0x00,0x01,0xEF,0xE0,
0x00,0x00,0x00,0x00,0x01,0xEF,0xF0,0x00,0x00,0x00,0x00,0x01,0xE3,0xFC,0x00,0x00,
0x00,0x00,0x01,0xE0,0xFF,0x00,0x00,0x00,0x00,0x01,0xF0,0x7F,0x80,0x00,0x00,0x00,
0x01,0xF0,0x1F,0xC0,0x00,0x00,0x00,0x00,0xF0,0x07,0xC0,0x00,0x00,0x00,0x00,0xF0,
0x03,0xC0,0x0F,0xFF,0xFF,0xFF,0xFF,0xFF,0xF0,0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,0xF8,
0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,0xF8,0x0F,0xFF,0xFF,0xFF,0xFF,0xFF,0xF0,0x00,0x00,
0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,
0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x78,0x00,
0x00,0x00,0x00,0x00,0x00,0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x78,0x00,0x00,0x00,
0x00,0x00,0x00,0x7C,0x00,0x00,0x0F,0xFF,0xFF,0xFE,0x3C,0x00,0x00,0x0F,0xFF,0xFF,
0xFE,0x3C,0x00,0x00,0x0F,0xFF,0xFF,0xFE,0x3C,0x00,0x00,0x00,0x00,0xF0,0x00,0x3C,
0x00,0x00,0x00,0x00,0xF0,0x00,0x3E,0x00,0x00,0x00,0x00,0xF0,0x00,0x3E,0x00,0x00,
0x00,0x00,0xF0,0x00,0x1E,0x00,0x00,0x00,0x00,0xF0,0x00,0x1E,0x00,0x00,0x00,0x00,
0xF0,0x00,0x1E,0x00,0x00,0x00,0x00,0xF0,0x00,0x1F,0x00,0x00,0x00,0x00,0xF0,0x00,
0x0F,0x00,0x00,0x00,0x00,0xF0,0x00,0x0F,0x00,0x00,0x00,0x00,0xF0,0x00,0x0F,0x80,
0x00,0x00,0x00,0xF0,0x00,0x0F,0x80,0x18,0x00,0x00,0xF0,0x00,0x07,0x80,0x3C,0x00,
0x00,0xF0,0x00,0x07,0x80,0x3C,0x00,0x00,0xF0,0x00,0x07,0xC0,0x3C,0x00,0x00,0xF0,
0x00,0x03,0xC0,0x3C,0x00,0x00,0xF0,0x03,0xC3,0xE0,0x3C,0x00,0x00,0xF0,0x1F,0xC1,
0xE0,0x3C,0x00,0x00,0xF1,0xFF,0xC1,0xF0,0x3C,0x00,0x00,0xFF,0xFF,0x01,0xF0,0x3C,
0x00,0x01,0xFF,0xF8,0x00,0xF8,0x3C,0x00,0x0F,0xFF,0x80,0x00,0xF8,0x7C,0x01,0xFF,
0xFC,0x00,0x00,0x7C,0x7C,0x1F,0xFF,0xC0,0x00,0x00,0x3F,0x78,0x3F,0xFC,0x00,0x00,
0x00,0x1F,0xF8,0x3F,0xC0,0x00,0x00,0x00,0x1F,0xF0,0x00,0x00,0x00,0x00,0x00,0x07,
0xF0,0x00,0x00,0x00,0x00,0x00,0x01,0xE0
};

2.2 调用字库显示

单个汉字取模太麻烦,可以使用点阵字库取模软件,然后程序里打开字库,读取字模进行绘制,下面的示例代码就是采用字库读取字模完成汉字显示。

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fb.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
struct fb_var_screeninfo var; //可变参数
struct fb_fix_screeninfo fix; //固定参数
unsigned char *fb_mem=NULL; //LCD屏的首地址
unsigned char *fb_GBK=NULL; //GBK字库的首地址
extern unsigned char font[];
extern const unsigned char ASCII_16_32[95][64];
/*
函数功能: 画点
*/
void Show_Pixel(int x,int y,int color)
{
unsigned int *lcd=(unsigned int *)(fb_mem+y*var.xres*var.bits_per_pixel/8+x*var.bits_per_pixel/8);
*lcd=color; //颜色赋值
}
/*
函数功能: 显示中文
说明:取模的字体是按照横向取模进行取点阵码。
取模的字体宽度是8的倍数。
*/
void ShowFont(int x,int y,int size,unsigned char *font)
{
unsigned char L,H;
unsigned int Addr;
unsigned int font_size=size*size/8; //得到该字节的总字节大小
unsigned char FontData[128];//最大可以存放32*32尺寸字体点阵码
unsigned char *GBK_p; //存放汉字点阵码首地址
/*计算该汉字在字库里的地址偏移量*/
L=*(font+1); //低字节
H=*font; //高字节
if(L<0x7f)L=L-0x40;
else L=L-0x41;
H=H-0x81;
Addr=(190*H+L)*font_size; //得到当前汉字在字库里的偏移量
GBK_p=(unsigned char *)(Addr+fb_GBK); //得到汉字点阵码在字库里首地址
int i,j,x0=x;
unsigned char tmp;
for(i=0;i<size/8*size;i++)
{
tmp=GBK_p[i];
for(j=0;j<8;j++)
{
if(tmp&0x80)Show_Pixel(x0,y,0xFF0033);
//else 画背景色
x0++;
tmp<<=1;
}
if(x0-x==size)
{
y++;
x0=x;
}
}
}
/*
函数功能:显示一个ASCII码。“空格开始 到~ 结束”
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
说明: ASCII码取模 宽度和高度必须是8的倍数,传入的size(高)
*/
void ShowASCII(int x,int y,int size,unsigned char c_data)
{
int i,j,x0=x;
unsigned char data;
int cnt;
if(c_data<' ' ||c_data > '~')return; 
cnt=c_data-' ';
for(i=0;i<size/2/8*size;i++)
{
switch(size)
{
case 16:
break;
case 32:
data=ASCII_16_32[cnt][i];
break;
}
for(j=0;j<8;j++)
{
if(data&0x80)Show_Pixel(x0,y,0xFF0033);
x0++;
data<<=1;
}
if(x0-x==size/2)
{
x0=x;
y++;
}
}
}
int main(int argc,char **argv)
{
if(argc!=2)
{
printf("./app <GBK字库文件>\n");
return 0;
}
int fd=open("/dev/fb0",O_RDWR);
if(fd<0)
{
perror("设备文件打开失败");
return 0;
}
/*1. 获取LCD屏的可变形参*/
ioctl(fd,FBIOGET_VSCREENINFO,&var);
printf("分辨率:%d*%d\n",var.xres,var.yres);
printf("像素点位数:%d\n",var.bits_per_pixel);
/*2. 获取LCD屏的固定形参*/
ioctl(fd,FBIOGET_FSCREENINFO,&fix);
printf("映射的长度:%d\n",fix.smem_len);
printf("一行的字节数:%d\n",fix.line_length);
/*3. 映射LCD缓冲区地址到进程空间*/
fb_mem=mmap(NULL,fix.smem_len,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if(fb_mem==NULL)
{
perror("空间映射失败!\n");
return 0;
}
/*4. 映射GBK字库文件到进程空间*/
int gbk_fb;
struct stat gbk_buf;
gbk_fb=open(argv[1],2);
if(gbk_fb<0)
{
perror("GBK字库文件打开失败!");
return 0;
}
stat(argv[1],&gbk_buf);
printf("GBK字库文件大小=%d\n",gbk_buf.st_size);
fb_GBK=mmap(NULL,gbk_buf.st_size,PROT_READ,MAP_SHARED,gbk_fb,0);
if(fb_GBK==NULL)
{
perror("空间映射失败!\n");
return 0;
}
/*4. 控制显示屏*/
memset(fb_mem,0xFFFFFF,fix.smem_len); //将屏幕清屏为白色
ShowFont(100+0*32,100,32,"嵌");
ShowFont(100+1*32,100,32,"入");
ShowFont(100+2*32,100,32,"式");
ShowFont(100+3*32,100,32,"开");
ShowFont(100+4*32,100,32,"发");
ShowASCII(100+0*16,200,32,'A');
ShowASCII(100+1*16,200,32,'C');
ShowASCII(100+2*16,200,32,'B');
munmap(fb_mem,fix.smem_len);
close(fd);
return 0;
}