lcd framebuffer 编程

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h> 






#define cpu_get_pc() \
({ \
register long pg asm("r0"); \
__asm__("mov  %0, pc" \
: "=r" (pg) : : "cc"); \
(unsigned int *)(pg); \
})








int main() 
{     
    int fbfd = 0;     
    struct fb_var_screeninfo vinfo;     
    struct fb_fix_screeninfo finfo;     
    long int screensize = 0;     
    char *fbp = 0;     
    int x = 0, y = 0;     
    long int location = 0;      
    // Open the file for reading and writing     
    fbfd = open("/dev/fb0", O_RDWR);     
    if( fbfd == -1 ){         
   perror("Error: cannot open framebuffer device");         
   exit(1);     
}  
printf( "fbfd = %d.\n", fbfd ); 
printf( "cpu_get_pc() = %x.\n", cpu_get_pc() ); 
    printf("The framebuffer device was opened successfully.\n");    
    // Get fixed screen information     
    if( ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1 ){    
   perror("Error reading fixed information");   
   exit(2);    

printf( "&finfo = 0x%x.\n", &finfo ); 
    // Get variable screen information     
    if( ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1 ){        
perror("Error reading variable information");         
exit(3);     
}     
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, fbfd, 0);     
//do_mmap
    if( (int)fbp == -1 ){ 
         perror("Error: failed to map framebuffer device to memory");         
exit(4);     
}     
printf( "fbp = 0x%x.\n", fbp );
printf( "screensize = 0x%x.\n", screensize );
printf("The framebuffer device was mapped to memory successfully.\n");    


if( ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1 ){    
   perror("Error reading fixed information");   
   exit(2);    


x = 100; y = 100;       
// Where we are going to put the pixel     
// Figure out where in memory to put the pixel
printf( "vinfo.xoffset = %d.\n", vinfo.xoffset );  
printf( "vinfo.yoffset = %d.\n", vinfo.yoffset );  
printf( "vinfo.bits_per_pixel = %d.\n", vinfo.bits_per_pixel );
printf( "finfo.line_length = %d.\n", finfo.line_length );
for (y = 0; y < 40; y++)         
for (x = 0; x < 120; x++) {             
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +                        
(y+vinfo.yoffset) * finfo.line_length;             
if (vinfo.bits_per_pixel == 32) {                 
*(fbp + location) = 100;        // Some blue                 
*(fbp + location + 1) = 15+(x-100)/2;     // A little green               
*(fbp + location + 2) = 200-(y-100)/5;    // A lot of red           
*(fbp + location + 3) = 0;      // No transparency             
} else  { 
   //assume 16bpp            
   int b = 0;      
//int g = (x-100)/6;     
// A little green 
int g = 0;   
//int r = 31-(y-100)/16;    
// A lot of red    
int r = 10;
unsigned short int t = r<<11 | g << 5 | b;    
*((unsigned short int*)(fbp + location)) = t;   
//printf( "t = %x.\n", t );  
}         
}   
munmap(fbp, screensize);     
close(fbfd);     
return 0; 


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值