Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1153330
  • 博文数量: 222
  • 博客积分: 5262
  • 博客等级: 大校
  • 技术积分: 3028
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-22 19:10
文章分类

全部博文(222)

文章存档

2012年(2)

2011年(192)

2010年(28)

分类: LINUX

2010-12-28 15:05:13

framebuff操作程序


 

/*
 * Usage:
 *
 */

#include <unistd.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "s1d13521ioctl.h"
#include <linux/fb.h>
#define WF_MODE_INIT 0
#define WF_MODE_DU 1
#define WF_MODE_GC16 2
#define WF_MODE_GC4 3
int main(int argc,char *argv[])
{
 int fbdev, i, j;
 static unsigned short *vram;
 unsigned short *pdata;
 unsigned short *pdata1;
 unsigned char clr;
 int choose = 0;
 int fd;
 int WIDTH = 600;
 int HEIGHT = 800;
 int PIC_WIDTH = 600;
 int PIC_HEIGHT= 800;
 unsigned char *data;
 struct stat sb;
 int xindex;
 int yindex;
 int retVal;
 int update_mode = 0;
 unsigned short mode;
 unsigned short cmd[10];
 struct fb_var_screeninfo info;

 choose = atoi(argv[1]);
 update_mode = atoi(argv[2]);
 printf("choose = %d, update_mode = %d\n", choose, update_mode);
 
 /*read a grayscale file from external*/
 fd = open(argv[3], O_RDONLY);
 
 if (fd < 0){
  printf("Failed to open:%s\n", argv[3]);
  exit(-1);
 }
 if (fstat(fd, &sb) == -1) {
  printf("stat test_fb error!\n");
  return -1;
     }
 
 printf("file size = %d!\n", sb.st_size);
 data = (unsigned char *)malloc(sb.st_size+1);
 if (data == NULL) {
  printf("Failed to malloc\n");
  exit(-1);
 }
 retVal = read(fd, data, sb.st_size);
     if (retVal < sb.st_size) {
         printf("read32read32: retVal < N, fd = %d ,retVal = %d\n", fd, retVal);
         return -1;
     }
 fbdev = open("/dev/graphics/fb0", O_RDWR);
 if ( fbdev < 0)
 {
  printf("Failed to open:fb");
  exit(-1);
 }
 if (ioctl(fbdev, FBIOGET_VSCREENINFO, &info) == -1) {
  printf("FBIOGET_VSCREENINFO error\n");
  exit(-1);
 }
 printf("yres = %d\n", info.yres);
 printf("xres = %d\n", info.xres);
 printf("yres_virtual = %d\n", info.yres_virtual);
 printf("xres_virtual = %d\n", info.xres_virtual);
 /* RGB16 */
 vram = (unsigned short *)mmap(NULL, info.xres_virtual * info.yres_virtual * 2,
   PROT_READ | PROT_WRITE, MAP_SHARED, fbdev,0);
 if(vram == MAP_FAILED){
  printf("mmap OVERLAY2 error!\n");
  goto out;
 }

 /*Clear the framebuffer, nor the screen*/
 for(i = 0; i < info.yres_virtual ; i++) {
  for(j = 0; j < info.xres_virtual; j++) {
   vram[i*info.xres_virtual + j] = 0xFFFF;
  }
 }
 pdata = (unsigned short *)data;
 xindex = 0;
 yindex = 0;
 
 for(i = 0; i < info.yres ; i++) {
  xindex = 0;
  for(j = 0; j < info.xres; j++) {
   vram[i*info.xres +j] = pdata[i*info.xres + j];
   xindex++;
   if(xindex >= PIC_WIDTH)
    break;
  }
  yindex++;
  if (yindex >= PIC_HEIGHT)
   break;
 }
 info.yoffset = 0;
 info.activate = FB_ACTIVATE_VBL;
 ioctl(fbdev,FBIOPUT_VSCREENINFO, &info);/*control the first frame buffer*/

 bzero(cmd, sizeof(cmd));
 
 switch (choose) {
   case 1:
    cmd[0] = WF_MODE_DU;
    break;
    
   case 2:
    cmd[0] = WF_MODE_GC16;
    break;
    
   case 3:
    cmd[0] = WF_MODE_GC4;
    break;
    
   default:
     printf("%s:input mode error! choose = %d\n", __func__, choose);
  }
 cmd[1] = 0;
 cmd[2] = 0;
 
 switch (update_mode) {
  case 1: //UPD_FULL

   ioctl(fbdev,S1D13521_UPD_FULL, cmd);
   break;
  case 2://UPD_PART

   ioctl(fbdev,S1D13521_UPD_PART, cmd);
   break;
  case 3://UPD_FULL_AREA

   cmd[3] = 20; /*x*/
   cmd[4] = 20; /*y*/
   cmd[5] = 200;/*width*/
   cmd[6] = 200;/*height*/
   ioctl(fbdev,S1D13521_UPD_FULL_AREA, cmd);
   break;
   
  case 4:
   cmd[3] = 100;/*x*/
   cmd[4] = 300;/*y*/
   cmd[5] = 400;/*width*/
   cmd[6] = 100;/*height*/
   ioctl(fbdev,S1D13521_UPD_PART_AREA, cmd);
   break;
   
  case 5:
   for (i = 0; i < 600; i++) {
    cmd[3] = i + 50;
    cmd[4] = 300;
    cmd[5] = 10;
    cmd[6] = 10;
    usleep(1000 * 10);
    ioctl(fbdev,S1D13521_UPD_PART_AREA, cmd);
   }
   
   break;
   
  default:
   
   for(i = 0; i < info.yres_virtual ; i++) {
    for(j = 0; j < info.xres_virtual; j++) {
     vram[i*info.xres_virtual + j] = 0xFFFF;
    }
   }
   cmd[0] = WF_MODE_GC16;
   cmd[1] = 0;
   cmd[2] = 0;
   ioctl(fbdev,S1D13521_UPD_FULL, cmd);
   printf("%s: input update_mode error! clear screen !\n", __func__);
 }
void showbar(char *cmd)
{
 int i = 0;
 for (i = 0; i < 600; i++) {
  cmd[3] = i + 10;
  cmd[4] = 300;
  cmd[5] = 10;
  cmd[6] = 10;
  usleep(1000 * 10);
  
 }
}
out:
 munmap(vram, info.xres_virtual * info.yres_virtual * 2);
 close(fbdev);
 close(fd);
 free(data);
 printf("end\n");
}

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