lcd.rar

  • PUDN用户
    了解作者
  • C/C++
    开发工具
  • 7KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 1
    下载次数
  • 2014-08-26 18:26
    上传日期
使用C语言编写的LCD操作界面程序,带触屏功能所以请确认你的运行环境中是否有tslib库!
lcd.rar
  • lcd
  • MyLcd.h
    1.1KB
  • MyLcd.c
    23.2KB
  • app.c
    935B
  • Makefile
    280B
内容介绍
##include <unistd.h> #include <stdio.h> #include <fcntl.h> #include <linux/fb.h> #include <sys/mman.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include "tslib.h" #include <signal.h> #include <time.h> #include "MyLcd.h" #include "LcdPicture.h" #include "pic.h" #include "long.h" #include "BMP.h" #include "farsight.h" #include "flower.h" #include "Button.h" #include <math.h> #define LCD_WIDTH 480 //屏幕的宽 #define LCD_HEIGHT 272 //屏幕的高 int fbfd = 0; //帧缓存设备描述符 long int screensize = 0; //帧缓存空间大小 char *fbp = 0; //指向帧缓存首地址的指针 struct fb_var_screeninfo vinfo; //屏幕的可变参数 struct fb_fix_screeninfo finfo; //屏幕的固定参数 unsigned char time_buf[25]; //存储时间的数组 unsigned char buf[10]; //存储日期的数组 //lcd的绝对坐标 int lcd_x=0; int lcd_y=0; //================================================================================================= /** 函数名称:Framebuffer_Map() 功能说明:开帧缓存并映射到应用层共应用程序使用 参数说明:无参数 */ //================================================================================================= static void Framebuffer_Map() { // Open the file for reading and writing fbfd = open("/dev/fb0", O_RDWR); //以读写方式打开帧缓存设备 if (!fbfd) { printf("Error: cannot open framebuffer device.\n"); return; } printf("The framebuffer device was opened successfully.\n"); // Get fixed screen information if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) { //获取LCD屏的固定参数 printf("Error reading fixed information.\n"); return; } // Get variable screen information if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) { //获取LCD屏的可变参数:xres表示屏幕的一行有多少个点, //yres表示屏幕的一列有多少个点,bits_per_pixel表示每个像素的位数 printf("Error reading variable information.\n"); return; } printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel); // Figure out the size of the screen in bytes screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8; //计算屏幕大小以字节为单位以便开辟足够的帧缓存 // Map the device to memory fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, //利用mmap()函数将帧缓存映射到用户空间,成功返回帧缓存首地址失败返回-1 fbfd, 0); if ((int)fbp == -1) { //判断帧缓存是否开辟成功 printf("Error: failed to map framebuffer device to memory.\n"); return; } printf("The framebuffer device was mapped to memory successfully.\n"); } //================================================================================================= /** 函数名称:Framebuffer_Close() 功能说明:先解除映射内存,然后关闭文件描述符(直接关闭文件描述符不会自动解除映射)。 也可使用exit直接退出进程,但这里不能这样使用 参数说明:无参数 */ //================================================================================================= static void Framebuffer_Close() { munmap(fbp, screensize); //解除映射内存 printf("The framebuffer device was munmapped to memory successfully.\n"); close(fbfd); printf("The framebuffer device was closed successfully.\n"); } //================================================================================================= /** 函数名称:Clear_Screen() 功能说明:指定范围内清屏 参数说明:(x0,y0)表示从第x0行y0列开始清,(x_height,y_width)表示到第x_height行y_width列结束, color表示清为什么颜色 */ //================================================================================================= static void Clear_Screen(int x0, int x_height, int y0, int y_width, unsigned short color) { long location=0; int x=0; int y=0; for (x = x0; x<(x0+x_height) ; x++) { for (y = y0; y <(y0+ y_width); y++) { location = (y+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + (x+vinfo.yoffset) * finfo.line_length;//计算帧缓存的偏移量 *(unsigned short *)(fbp + location) = color; } } } //================================================================================================= /** 函数名称:Show_Char_16() 功能说明:8X16字符/字符串显示 参数说明:(x_start,y_start)表示从第x_start行y_start列开始显示字符,incode接受字符/字符串 color表示字体颜色 */ //================================================================================================= static void Show_Char_16(int x_start,int y_start, unsigned char *incode,unsigned short color) { int n; int i=0,j=0; unsigned char chinese_matrix[16]; unsigned long location = 0; int offset; FILE *fp; for(n=0;n<strlen(incode);n++) { if(LCD_WIDTH==x_start) x_start+=16; else if(1<=n) y_start+=8; offset=incode[n]*16+1; //寻找该字符在ASC16字符库的偏移位置 if((fp=fopen("/lib1/ASC16","r"))==NULL) return; //打开字符库文件 fseek(fp,offset,SEEK_SET); //文件指针偏移到要找的字符处 memset(chinese_matrix,0,sizeof(chinese_matrix)); fread(chinese_matrix,16,1,fp); //读取该字符的字模 fclose(fp); for(i=0;i<16;i++) for(j=0;j<8;j++) { location = (j+y_start+vinfo.xoffset) * (vinfo.bits_per_pixel/8)+(i+x_start+vinfo.yoffset) * finfo.line_length; *((unsigned short*)(fbp + location))=((chinese_matrix[i]>>(7-j%8))&0x01)*color; //将字模数据拷入针缓存中显示 } } } //================================================================================================= /** 函数名称:Show_Data() 功能说明:16x32数字显示 参数说明:(x_start,y_start)表示从第x_start行y_start列开始显示字符,data接受字符/字符串 color表示字体颜色 */ //================================================================================================= static void Show_Data(int x_start,int y_start,unsigned char *data,unsigned short color) { unsigned char *p="0123456789.+-"; int n=0,i=0,j=0,num=0,location=0; unsigned char *temp=p; for(n=0;n<strlen(data);n++) { while((*p!=data[n])&&(*p!='\0')) { num++; p++; } if(n>=1) y_start+=16; for(i=0;i<32;i++) for(j=0;j<16;j++) { location = (j+y_start+vinfo.xoffset) * (vinfo.bits_per_pixel/8)+(i+x_start+vinfo.yoffset) * finfo.line_length; *((unsigned short*)(fbp + location))=((Data[num][i*2+j/8]>>(7-j%8))&0x01)*color; //将字模数据拷入针缓存中显示 } p=temp; num=0; } } //================================================================================================= /** 函数名称:Show_Chinese() 功能说明:24x24/16x16汉字显示 参数说明:len表示字体大小,(x_start,y_start)表示从第x_start行y_start列开始显示汉字,s接受字符/字符串 color表示字体颜色 */ //================================================================================================= static void Show_Chinese(int len,int x_start,int y_start,unsigned char *s,unsigned short color) { int n=0; int i=0,j=0; unsigned long offset; unsigned long location = 0; FILE *fp; for(n=0;n<strlen(s)/2;n++) { if(len == 24) { if((fp=fopen("/lib1/HZK24","r"))==NULL) return; if(LCD_WIDTH==x_start) x_start+=24; else if(1<=n) y_start+=24; unsigned char chinese_matrix[24][3]; //一个汉字占两个字节:前
评论
    相关推荐