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

 

 

實驗現象:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章