⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lcd4.c

📁 在Linux下调用系统函数
💻 C
字号:
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
//#define row 2//纵坐标放大倍数
//#define col 2 //横坐标放大倍数
#define row 1
#define col 1

struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
long screensize=0;
char *fbp = 0;
  
//===========================================================画点函数
int DrawPixel ( int x,int y,char color,char flag )
{ 
 //long location=x*2+y*1280;
 long location=(x+vinfo.xoffset)*(vinfo.bits_per_pixel/8)+(y+vinfo.yoffset)*finfo.line_length;
 if(flag)
 	{
 		switch(color)
 		{
 		case 'g':
 		     { *(fbp + location) = 0xe0; 
          *(fbp + location + 1) = 0x07; /* 绿色的色深*/ 
        break;}
    case 'r':  
          {*(fbp + location) = 0x0; /* 红色的色深*/ 
          *(fbp + location+1) = 0xf8; 
        break;}
    case 'b':     
        {*(fbp + location) = 0x1f; /* 蓝色的色深*/ 
        *(fbp + location+1) = 0x0;
        break;}
    }     
   }
   else
   	{
   		*(fbp + location) = 0x0; /* 红色的色深*/ 
       *(fbp + location+1) = 0x0;
      }
 }
 //===============================================================显示一个汉字
 DispChar(int x,int y,const char *str ,char flag )
{
	int i,n,j,k,m;
  FILE *fp;
  char buffer[32];
  unsigned char qh,wh;
  unsigned long offset;

  if((fp=fopen("./hzk16","rb"))==NULL)
  {
  printf("error,please try again\n");
  return 0;
  }
while(*str)
{
qh=*(str)-0xa0;
wh=*(str+1)-0xa0;
offset=(94*(qh-1)+(wh-1))*32L;//计算该汉字在字库中偏移量
fseek(fp,offset,SEEK_SET);
fread(buffer,1,32,fp); //取出汉字32字节的点阵字模存入buffer中(一个汉字)
for(i=0;i<16;i++)
for(n=0;n<row;n++)
for( j=0;j<2;j++)
for( k=0;k<8;k++)
for(m=0;m<row;m++)
if((buffer[i*2+j]>>(7-k))&0x1)
DrawPixel(x+8*j*col+k*col+m,y+i*row+n,'r',1);
//将32位字节的点阵按位在屏幕上画出来
str+=2;//因为一个汉字内码占用两个字节,所以s必须加2
//x+=35;
x+=30;
}
return 1;
}


//=============================================================主函数
int main () {
int fp=0;
int x,y;
fp = open ("/dev/fb0",O_RDWR);//打开液晶的缓存
if (fp < 0){
printf("Error : Can not open framebuffer device\n");
exit(1);
}

if (ioctl(fp,FBIOGET_FSCREENINFO,&finfo)){
printf("Error reading fixed information\n");
exit(2);
}

if (ioctl(fp,FBIOGET_VSCREENINFO,&vinfo)){
printf("Error reading variable information\n");
exit(3);
}

screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
printf("%d,%d", vinfo.xres,vinfo.yres);
/*这就是把fp所指的文件中从开始到screensize大小的内容给映射出来,得到一个指向这块空间的指针*/
fbp =(char *) mmap (0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fp,0);
  if ((int) fbp == -1)
    {
     printf ("Error: failed to map framebuffer device to memory.\n");
      exit (4);
    }
memset(fbp , 0 ,screensize);//清屏
//写几个字
//for(x=150;x<360;x++)
for(x=80;x<160;x++)
 for(y=72;y<108;y++)
  //for(y=150;y<;x++)
   DrawPixel(x,y,'b',1);
//DispChar(150,75,"直",1);
/*
DispChar(150,75,"直接进入系统",1);
DispChar(150,125,"使用网线升级",1);
DispChar(150,175,"使用优盘升级",1);
*/
DispChar(100,75,"电脑",1);
DispChar(400,75,"显示器",1);  
DispChar(600,75,"回收站",1);
DispChar(100,275,"浏览器",1);
DispChar(400,275,"宽带",1);
DispChar(600,275,"信箱",1);
   munmap (fbp, screensize); /*解除映射*/
  close (fp); /*关闭文件*/
  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -