测试显示
dd if=/dev/zero of=/dev/fb0 bs=1024 count=768
framebuffer.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <errno.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
int flush_screen(char *buf,char *framebuf,int width,int height);
int main(int argc, char *argv[])
{
int fb_fd=0;
struct fb_fix_screeninfo fb_fixinfo;
struct fb_var_screeninfo fb_varinfo;
char *fb_addr;
unsigned long fb_size;
char *buffer;
int i=0;
if((fb_fd=open("/dev/fb0", O_RDWR))<0){
printf("open framebuffer device failed."n");
return -1;
}
printf("open framebuffer device ok"n");
if(ioctl(fb_fd, FBIOGET_FSCREENINFO, &fb_fixinfo) == -1)
{
printf("ioctl FBIOGET_FSCREENINFO failed."n");
return -1;
}
printf("ioctl FBIOGET_FSCREENINFO ok"n");
if(ioctl(fb_fd, FBIOGET_VSCREENINFO, &fb_varinfo) == -1)
{
printf("ioctl FBIOGET_VSCREENINFO failed."n");
return -1;
}
printf("ioctl FBIOGET_VSCREENINFO ok"n");
fb_size = fb_varinfo.yres * fb_fixinfo.line_length;
fb_addr = (char *)mmap(NULL, fb_size, PROT_READ|PROT_WRITE,MAP_SHARED, fb_fd, 0);
buffer=(char*)malloc(fb_varinfo.yres * fb_fixinfo.line_length);
for(i=0;i<=0xff;i+=0x0f){
memset(buffer,i,fb_varinfo.yres * fb_fixinfo.line_length);
flush_screen(buffer,fb_addr,fb_varinfo.xres,fb_varinfo.yres);
sleep(1);
}
if (!fb_addr)
exit(1);
free(buffer);
return 0;
}
int flush_screen(char *buf,char *framebuf,int width,int height)
{
char *t_data = buf;
char *t_fb_addr = framebuf;
int bytew= width<<1;
while(--height >= 0)
{
memcpy(t_fb_addr,t_data,bytew);
t_fb_addr += width;
t_data += width;
}
}
frame_logo.c
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
typedef struct
{
char cfType[2];
long cfSize;
long cfReserved;
long cfoffBits;
}__attribute__((packed)) BITMAPFILEHEADER;
typedef struct
{
char ciSize[4];
long ciWidth;
long ciHeight;
char ciPlanes[2];
int ciBitCount;
char ciCompress[4];
char ciSizeImage[4];
char ciXPelsPerMeter[4];
char ciYPelsPerMeter[4];
char ciClrUsed[4];
char ciClrImportant[4];
}__attribute__((packed)) BITMAPINFOHEADER;
typedef struct
{
unsigned short blue;
unsigned short green;
unsigned short red;
unsigned short reserved;
}__attribute__((packed)) PIXEL;
BITMAPFILEHEADER FileHead;
BITMAPINFOHEADER InfoHead;
static char *fbp = 0;
static int xres = 0;
static int yres = 0;
static int bits_per_pixel = 0;
int show_bmp();
int main ( int argc, char *argv[] )
{
int fbfd = 0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
long int screensize = 0;
struct fb_bitfield red;
struct fb_bitfield green;
struct fb_bitfield blue;
fbfd = open("/dev/fb0", O_RDWR);
if (!fbfd)
{
printf("Error: cannot open framebuffer device.\n");
exit(1);
}
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo))
{
printf("Error:reading fixed information.\n");
exit(2);
}
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo))
{
printf("Error: reading variable information.\n");
exit(3);
}
printf("R:%d,G:%d,B:%d \n", vinfo.red, vinfo.green, vinfo.blue );
printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel );
xres = vinfo.xres;
yres = vinfo.yres;
bits_per_pixel = vinfo.bits_per_pixel;
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
printf("screensize=%d byte\n",screensize);
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
if ((int)fbp == -1)
{
printf("Error: failed to map framebuffer device to memory.\n");
exit(4);
}
printf("sizeof file header=%d\n", sizeof(BITMAPFILEHEADER));
printf("into show_bmp function\n");
show_bmp();
munmap(fbp, screensize);
close(fbfd);
return 0;
}
int show_bmp()
{
FILE *fp;
int rc;
int line_x, line_y;
long int location = 0, BytesPerLine = 0;
char tmp[1024*10];
fp = fopen( "./data/waitting.bmp", "rb" );
if (fp == NULL)
{
return( -1 );
}
rc = fread( &FileHead, sizeof(BITMAPFILEHEADER),1, fp );
if ( rc != 1)
{
printf("read header error!\n");
fclose( fp );
return( -2 );
}
if (memcmp(FileHead.cfType, "BM", 2) != 0)
{
printf("it's not a BMP file\n");
fclose( fp );
return( -3 );
}
rc = fread( (char *)&InfoHead, sizeof(BITMAPINFOHEADER),1, fp );
if ( rc != 1)
{
printf("read infoheader error!\n");
fclose( fp );
return( -4 );
}
fseek(fp, FileHead.cfoffBits, SEEK_SET);
BytesPerLine = (InfoHead.ciWidth * InfoHead.ciBitCount + 31) / 32 * 4;
line_x = line_y = 0;
while(!feof(fp))
{
PIXEL pix;
unsigned short int tmp;
rc = fread( (char *)&pix, 1, sizeof(PIXEL), fp);
if (rc != sizeof(PIXEL))
break;
location = line_x * bits_per_pixel / 8 + (InfoHead.ciHeight - line_y - 1) * xres * bits_per_pixel / 8;
*(fbp + location + 0)=pix.blue;
*(fbp + location + 1)=pix.green;
*(fbp + location + 2)=pix.red;
*(fbp + location + 3)=pix.reserved;
line_x++;
if (line_x == InfoHead.ciWidth )
{
line_x = 0;
line_y++;
if(line_y == InfoHead.ciHeight)
break;
}
}
fclose( fp );
return( 0 );
}