#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
void setpixel(void *fb,struct fb_var_screeninfo *pVarInfo, int x, int y, char r, char g, char b)
{
if(pVarInfo->bits_per_pixel==16) *((unsigned short *)((char *)fb + (y + x)*2))=(unsigned short )(r<<11|g<<5|b);
else if(pVarInfo->bits_per_pixel==24 || pVarInfo->bits_per_pixel== 32) *((unsigned long *)fb + y + x)=(unsigned long )(r<<16|g<<8|b);
else fprintf(stderr, " error bits_per_pixel is not surport !\n");
}
void DrawScreen(void *fb, struct fb_var_screeninfo *pVarInfo, int h)
{
int x, y, ytimesw;
for(y = 0; y < pVarInfo->yres; y++ )
{
ytimesw = y*pVarInfo->xres;
for( x = 0; x < pVarInfo->xres; x++ )
{
setpixel(fb,pVarInfo,x, ytimesw, (x*x)/256+3*y+h, (y*y)/256+x+h, h);
}
}
}
int main() {
int i =0;
struct fb_fix_screeninfo fixInfo;
struct fb_var_screeninfo varInfo;
int fbfd = -1;
void *buffer = (void *) -1;
/* open the buffer device */
fbfd = open("/dev/fb0", O_RDWR);
if (fbfd < 0) {
fprintf(stderr, "Error opening /dev/fb0\n");
exit(1);
}
/* Get the fixed screen info */
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &fixInfo)) {
fprintf(stderr, "error: ioctl(FBIOGET_FSCREENINFO) failed\n");
exit(1);
}
/* get the variable screen info */
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &varInfo)) {
fprintf(stderr, "error: ioctl(FBIOGET_VSCREENINFO) failed\n");
exit(1);
}
printf("xres=%d ; yres=%d ; bits_per_pixel=%d\n",varInfo.xres,varInfo.yres,varInfo.bits_per_pixel );
if (fixInfo.visual != FB_VISUAL_TRUECOLOR && fixInfo.visual != FB_VISUAL_DIRECTCOLOR) {
fprintf(stderr, "non-TRUE/DIRECT-COLOR visuals (0x%x) not supported by this demo.\n", fixInfo.visual);
exit(1);
}
/*
* fbdev says the frame buffer is at offset zero, and the mmio region
* is immediately after.
*/
/* mmap the buffer into our address space */
buffer = (void *) mmap(0, /* start */
fixInfo.smem_len, /* bytes */
PROT_READ | PROT_WRITE, /* prot */
MAP_SHARED, /* flags */
fbfd, /* fd */
0 /* offset */);
if (buffer == (void *) - 1) {
fprintf(stderr, "error: unable to mmap buffer\n");
exit(1);
}
fprintf(stderr, "open finish\n");
while (1) DrawScreen(buffer,&varInfo,i++);
munmap(buffer, fixInfo.smem_len);
close(fbfd);
fprintf(stderr, "close finish\n");
return 0;
}