Chinaunix首页 | 论坛 | 博客
  • 博客访问: 329969
  • 博文数量: 45
  • 博客积分: 669
  • 博客等级: 上士
  • 技术积分: 675
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-27 17:59
文章分类
文章存档

2015年(5)

2014年(6)

2013年(4)

2012年(30)

分类: LINUX

2012-09-08 07:04:51

    本设计基于linux framebuffer,首先用ioctl获取屏幕的结构参数,将设备fb0地址映射进入内存,然后利用获得的屏幕的分辨率,颜色深度等信息,编写画点函数draw_point(x,y,color),利用随机函数在指定范围内生成红,绿,蓝的三色点。可形成星空般彩色的效果。
 
draw_point.c:
 

#include
#include
#include
#include
#include
#include
#include
#include
#include

#include


unsigned char *fbp;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;

void drawpoint(unsigned int x,unsigned int y,unsigned int *color)
{
    unsigned char *fbp0;
    fbp0=fbp;
    fbp0+=y*vinfo.xres*vinfo.bits_per_pixel/8+x*vinfo.bits_per_pixel/8;
    memcpy(fbp0,color,vinfo.bits_per_pixel/8);
}


int mrand(int x,int y)      //
产生从xy之间的随机数
{
     int i;
     time_t t;
     srand((unsigned) time(&t));
     return (rand()%(y-x+1)+x);   
}



int main()
{
    int fd;
    unsigned int i=0;
    int bgcolor;  
    long int screensize;
    bgcolor=0;
    int color;
    //color=0x0000ff00;
    int redcolor=0x00ff0000;
    int greencolor=0x0000ff00;
    int bluecolor=0x000000ff;
      

 //   int color[3]={0xff000000,0x00ff0000,0x0000ff00};

 
    if((fd=open("/dev/fb0",O_RDWR))<0)
    {
        perror("fail to open");
        return 0;      
    }
     if (ioctl(fd,FBIOGET_FSCREENINFO, &finfo))
     {
        perror("fail to get");
        return 0;
     }
    if (ioctl(fd,FBIOGET_VSCREENINFO, &vinfo))
     {
        perror("fail to get");
        return 0;
     }
    screensize=vinfo.xres*vinfo.yres*vinfo.bits_per_pixel/8;
    fbp=(unsigned char *)mmap(0,screensize,PROT_READ | PROT_WRITE,MAP_SHARED,fd, 0);
    for(i=0;i          memcpy(fbp+i,&bgcolor,vinfo.bits_per_pixel/8);
    usleep(10000);
   
   
       
    int xrand=0;
    int yrand=0;
    int crand=0;      

//    drawpoint(100,100,&redcolor);
//    drawpoint(200,200,&greencolor);
//    drawpoint(300,300,&bluecolor);

    while(1)
    {
    xrand=mrand(0,800);
    yrand=mrand(0,480);
    crand=mrand(0,2);   

    printf("xrand=%d\n",xrand);
    printf("yrand=%d\n",yrand);
    printf("crand=%d\n",crand);

    switch(crand)
    {
    case 0: //red
        color=0x00ff0000;
        break;
    case 1://green
        color=0x0000ff00;
        break;
    default://blue
        color=0x000000ff;
        break;
    }  
    usleep(10000);
    drawpoint(xrand,yrand,&color);
    //drawpoint(xrand+1,yrand,&color);
    //drawpoint(xrand,yrand+1,&color);
    //drawpoint(xrand+1,yrand+1,&color);   

    }
         
    munmap(fbp,screensize);
    close(fd);
    return 0;
}

   
 

阅读(1464) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~