ok6410LCD测试程序

1.在屏幕上分别显示红绿蓝三个长方形:

 

  1. #ifndef LCD_APP_H  
  2. #define LCD_APP_H  
  3.   
  4. /*自定义结构体用来在用户空间里管理帧缓冲的信息*/  
  5. typedef struct fd_dev  
  6. {  
  7.     int fd;//帧缓冲设备文件描述符  
  8.     void *pfb;//指向帧缓冲映射到用户空间的首地址  
  9.     int xres,yres,siz;//一帧图像的宽度、高度和大小  
  10.     int bpp;//每个像素的位数  
  11. }fb_dev_t;  
  12.   
  13.   
  14.   
  15. /*实现缓冲设备的打开和关闭操作的接口*/  
  16. int fb_open(fb_dev_t *fbd,char *fbn)  
  17. {  
  18.     struct fb_var_screeninfo vinfo;  
  19.       
  20.       
  21.       
  22.     if(-1 == (fbd->fd=open(fbn,O_RDWR))){  
  23.         printf("Error:cannot open framebuffer device.\n");  
  24.         _exit(EXIT_FAILURE);  
  25.     }  
  26.       
  27.       
  28.     /*获取LCD的可变参数*/  
  29.     ioctl(fbd->fd,FBIOGET_VSCREENINFO,&vinfo);  
  30.     //将可变参数的中的相关数据保存到自定的结构体fbd中  
  31.     fbd->xres = vinfo.xres;  
  32.     fbd->yres = vinfo.yres;  
  33.     fbd->bpp = vinfo.bits_per_pixel;  
  34.       
  35.     fbd->siz = fbd->xres * fbd->yres *fbd->bpp/8;  
  36.       
  37.       
  38.     printf("%dx%d,%dbpp,screensize = %d\n",fbd->xres,fbd->yres,  
  39.     fbd->bpp,fbd->siz);  
  40.       
  41.       
  42.       
  43.     /*将帧缓冲映射到内存*/  
  44.     fbd->pfb = mmap(0,fbd->siz,PROT_READ|PROT_WRITE,  
  45.     MAP_SHARED,fbd->fd,0);  
  46.       
  47.       
  48.     if((int)fbd->pfb == -1){  
  49.         printf("Error:failed to map framebuffer device to memory.\n");  
  50.         _exit(EXIT_FAILURE);  
  51.     }  
  52.     return 0;  
  53. }  
  54.   
  55.   
  56.   
  57. int fb_close(fb_dev_t *fbd)  
  58. {  
  59.     munmap(fbd->pfb,fbd->siz);//解除映射  
  60.     close(fbd->fd);//关闭设备文件  
  61.       
  62. }  
  63.   
  64.   
  65.   
  66. /*填充制定的巨型区域*/  
  67. int fb_drawrect(fb_dev_t *fbd,int x0,int y0,int w,int h,int color)  
  68. {  
  69.     int x,y;  
  70.     for(y=y0;y<y0+h;y++)  
  71.     {  
  72.         for(x = x0;x<x0+w;x++)  
  73.             *((short*)(fbd->pfb)+y*fbd->xres+x)=color;  
  74.     }  
  75.       
  76.     return 0;  
  77. }   
  78.   
  79. #endif  


测试程序lcd_app.c

  1. #include <unistd.h>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <fcntl.h>  
  5. #include <linux/fb.h>  
  6. #include <sys/mman.h>  
  7. #include "lcd_app.h"  
  8. #include "bmplib.h"  
  9.   
  10.   
  11.   
  12.   
  13.   
  14. #define FB_DEV_NAME "/dev/fb0"  
  15.   
  16.   
  17. #define RED_COLOR565 0x0F100  
  18. #define GREEN_COLOR565 0x007E0  
  19. #define BLUE_COLOR565  0x0001F  
  20.   
  21.   
  22.   
  23.   
  24. int main(int argc,char *argv[])  
  25. {  
  26.     fb_dev_t *fbd;  
  27.       
  28.       
  29.     fbd = (fb_dev_t*)malloc(sizeof(fb_dev_t));  
  30.       
  31.       
  32.     fb_open(fbd,FB_DEV_NAME);  
  33.       
  34.       
  35.       
  36.       
  37.       
  38.     if(fbd->bpp == 16){  
  39.         printf("Red/Green/Blue Screen\n");  
  40.         fb_drawrect(fbd,0,0,fbd->xres,fbd->yres/3,RED_COLOR565);  
  41.         fb_drawrect(fbd,0,fbd->yres/3,fbd->xres,fbd->yres*2/3,GREEN_COLOR565);  
  42.         fb_drawrect(fbd,0,fbd->yres*2/3,fbd->xres,fbd->yres,BLUE_COLOR565);  
  43.     }else  
  44.         printf("16 bits only!\n");  
  45.       
  46.          
  47.     //fb_drawbmp(fbd,0,0,argv[1]);  
  48.     fb_close(fbd);  
  49.     return 0;  
  50. }  


 

 

 

 

 

 

2.在lcd上显示位图bmplib.h

  1. #ifndef BMPLIB_H  
  2. #define BMPLIB_H  
  3.   
  4.   
  5.   
  6. typedef struct  
  7. {  
  8.     /*位图头文件*/  
  9.     char type[2];//文件类型,必须为"BM"(0x4D42)  
  10.     char siz[4];//文件的大小  
  11.     char reserved[4];//保留,必须为0  
  12.     char off[4];//位图阵列相对于文件头的偏移字节  
  13. }bmp_file_header_t;  
  14.   
  15.   
  16.   
  17. typedef struct{  
  18.     /*位图信息头*/  
  19.     char siz[4];  
  20.     char width[4];//位图的宽度  
  21.     char height[4];//位图的高度  
  22.     char planes[2];//目标设备的位平面数,必须设置为1  
  23.     char bitcount[2];//每个像素的位数,1,4,8,或24  
  24.     char compress[3];//位图阵列的压缩方法,0=不压缩  
  25.     char img_siz[4];//图像大小(字节)  
  26.     char xpel[4];//目标设备水平每米像素的个数  
  27.     char ypel[4];//目标像素垂直每米像素的个数  
  28.     char clr_used[4];//位图实际使用的颜色表的颜色数  
  29.     char clr_important[4];//重要颜色索引的个数  
  30.       
  31. }bmp_info_header_t;  
  32.   
  33.   
  34.   
  35.   
  36. /*bitmap格式的图像文件会带有54字节的信息头,其中包含了图像和文件的基本信息, 
  37. 紧接在文件头之后的就是实际的图像数据*/  
  38. typedef struct{  
  39.     char blue;  
  40.     char green;  
  41.     char red;  
  42.     char reserved;  
  43. }rgb_32_t;  
  44.   
  45.   
  46.   
  47.   
  48. /*对位图进行操作*/  
  49. typedef struct{  
  50.     rgb_32_t *curp;//指向当前像素点  
  51.     int width,height,bitcount,siz;//图形长宽、大小等信息  
  52.     int fd;//图像文件描述符  
  53.     void *data;//图像有效数据  
  54. }bmp_t;  
  55.   
  56.   
  57. //字符形到整形的转换  
  58. int char_to_int(char *ch)  
  59. {  
  60.     return *((int*)ch);  
  61. }  
  62.   
  63.   
  64.   
  65. /*打开位图操作*/  
  66. int bmp_open(bmp_t *bmp,char *bmpn)  
  67. {  
  68.     bmp_file_header_t fhr;//位图文件头  
  69.     bmp_info_header_t ihr;//位图信息头  
  70.       
  71.       
  72.       
  73.     if(-1 == (bmp->fd=open(bmpn,O_RDONLY))){  
  74.         printf("Error:cannot open framebuffer device.\n");  
  75.         _exit(EXIT_FAILURE);  
  76.     }  
  77.       
  78.       
  79.     read(bmp->fd,&fhr,sizeof(bmp_file_header_t));//读取文件头  
  80.     read(bmp->fd,&ihr,sizeof(bmp_info_header_t));//读取信息头  
  81.       
  82.       
  83.     bmp->width = char_to_int(ihr.width);  
  84.     bmp->height = char_to_int(ihr.height);  
  85.     bmp->bitcount = char_to_int(ihr.bitcount);//像素位数  
  86.     bmp->siz = (bmp->width *bmp->height*bmp->bitcount)/8;  
  87.       
  88.       
  89.       
  90.     printf("bmp->width = %d\n",bmp->width);  
  91.     printf("bmp->height = %d\n",bmp->height);  
  92.     printf("bmp->bitcount = %d\n",bmp->bitcount);  
  93.     printf("bmp->siz = %d\n",bmp->siz);  
  94.       
  95.       
  96.     bmp->data = malloc(bmp->siz);//为位图数据分配存储空间  
  97.     read(bmp->fd,bmp->data,bmp->siz);//读取数据  
  98.     bmp->curp = (rgb_32_t *)bmp->data;//获取当前像素点  
  99.       
  100.       
  101.     return 0;  
  102.       
  103. }  
  104.   
  105.   
  106. int bmp_close(bmp_t *bmp)  
  107. {  
  108.     close(bmp->fd);  
  109.     free(bmp->data);  
  110.     return 1;  
  111. }  
  112.   
  113.   
  114.   
  115.   
  116.   
  117. /* 
  118.     因为开发板的帧缓冲区在驱动中被设置为16位数据表示一个像素点,因此需要对24或 
  119.     32位的位图进行转换,使用下面的函数 
  120. */  
  121. static inline short transfer_to_16bit(char r,char g,char b)  
  122. {  
  123.     return ((r>>3)<<11)|((g>>2)<<5)|(b>>3);  
  124. }  
  125.   
  126.   
  127.   
  128. static inline short bmp_get_pixel_16bit(bmp_t *bmp)  
  129. {  
  130.     //将当前位图转化为16位位图信息  
  131.     return transfer_to_16bit(bmp->curp->red,bmp->curp->green,bmp->curp->blue);  
  132. }  
  133.   
  134.   
  135.   
  136. //移动到下一像素点  
  137. static inline void bmp_next_pixel(bmp_t *bmp)  
  138. {  
  139.     if(24==bmp->bitcount)//如果是24位位图  
  140.         bmp->curp = (rgb_32_t*)((int)bmp->curp+3);  
  141.     else if(32 == bmp->bitcount)//如果是32位位图  
  142.         bmp->curp = (rgb_32_t*)((int)bmp->curp+4);  
  143. }  
  144.   
  145.   
  146.   
  147.   
  148.   
  149.   
  150.   
  151. /*绘制位图*/  
  152. int fb_drawbmp(fb_dev_t *fbd,int x0,int y0,char *bmpn)  
  153. {  
  154.     int x,y,x1,y1;  
  155.     bmp_t bmp;  
  156.       
  157.       
  158.     bmp_open(&bmp,bmpn);  
  159.     x1 = x0+bmp.width;  
  160.     y1 = y0+bmp.height;  
  161.       
  162.       
  163.       
  164.     for(y = y1;y>y0;y--){  
  165.         for(x = x0;x<x1;x++){  
  166.           
  167.             //如果超出LCD屏幕坐标范围  
  168.             if(x>fbd->xres||y>fbd->yres){  
  169.                 bmp_next_pixel(&bmp);//移动到下一个像素点  
  170.                 continue;  
  171.             }  
  172.             *((short*)(fbd->pfb)+y*fbd->xres+x) = bmp_get_pixel_16bit(&bmp);  
  173.             bmp_next_pixel(&bmp);//移动到下一像素点  
  174.         }  
  175.     }  
  176.     bmp_close(&bmp);  
  177.     return 0;  
  178. }  
  179.   
  180.   
  181.   
  182.   
  183.   
  184.   
  185.   
  186.   
  187. #endif  


测试程序:

lcd_bmp.c

  1. #include <unistd.h>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <fcntl.h>  
  5. #include <linux/fb.h>  
  6. #include <sys/mman.h>  
  7. #include "lcd_app.h"  
  8. #include "bmplib.h"  
  9.   
  10.   
  11.   
  12.   
  13.   
  14. #define FB_DEV_NAME "/dev/fb0"  
  15.   
  16.   
  17. #define RED_COLOR565 0x0F100  
  18. #define GREEN_COLOR565 0x007E0  
  19. #define BLUE_COLOR565  0x0001F  
  20.   
  21.   
  22.   
  23.   
  24. int main(int argc,char *argv[])  
  25. {  
  26.     fb_dev_t *fbd;  
  27.       
  28.       
  29.     fbd = (fb_dev_t*)malloc(sizeof(fb_dev_t));  
  30.       
  31.       
  32.     fb_open(fbd,FB_DEV_NAME);  
  33.       
  34.       
  35.       
  36.       
  37.     /* 
  38.     if(fbd->bpp == 16){ 
  39.         printf("Red/Green/Blue Screen\n"); 
  40.         fb_drawrect(fbd,0,0,fbd->xres,fbd->yres/3,RED_COLOR565); 
  41.         fb_drawrect(fbd,0,fbd->yres/3,fbd->xres,fbd->yres*2/3,GREEN_COLOR565); 
  42.         fb_drawrect(fbd,0,fbd->yres*2/3,fbd->xres,fbd->yres,BLUE_COLOR565); 
  43.     }else 
  44.         printf("16 bits only!\n"); 
  45.     */  
  46.          
  47.     fb_drawbmp(fbd,0,0,argv[1]);  
  48.     fb_close(fbd);  
  49.     return 0;  
  50. }  


位图程序测试方法,例如这里有一张名为clock.bmp的图片

编译上述文件后生成lcd_bmp

运行如下命令:

./lcd_bmp clock.bmp

 

 

实验现象:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值