⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 docapture3.c.bak

📁 嵌入式linuxMX21开发板数据采集程序
💻 BAK
字号:
/*
 * MX21 OV9640 Application
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
  by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Copyright (C) 2004 Motorola Semiconductors Hong Kong.
 *
 */

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

#include "csi.h"
#include "csilib.h"
#include "ov9640.h"
#include "ov9640lib.h"
#include "camera.h"
static unsigned char  *test_buf;
char tmpbuf[104 * 80 * 2];

typedef struct
{
	int width;
	int height;
	int format;
	int bytes_per_pixel;
} IMG_SPEC;


//local function
static void rgb_rotate(unsigned int BufIn, unsigned int BufOut, int imgH, int imgV, int dir, int out_stride);
void ov9640_get_config(OV9640_CFG * _cfg, FILE * _fp);
void draw(U8 * fb, U8 * display_buf,  int screen_height,int screen_width, int screen_bytes_per_pixel);


//16-bit rotation
//1=clockwise, -1=anticlockwise, 0=no rot
static void rgb_rotate(unsigned int BufIn, unsigned int BufOut, int imgH, int imgV, int dir, int out_stride)
{
	int v, h, k;
	unsigned short * _dataIn = (unsigned short *)BufIn;
	unsigned short * _dataOut = (unsigned short *)BufOut;

	switch(dir)
	{
		case 1:
		{
			k = 0;
			for(h = 0; h < imgH; h ++)
			{
				for(v = imgV - 1; v >= 0; v --)
				{
					_dataOut[k] = _dataIn[v * imgH + h];
					k ++;
				}
			}
			break;
		}
		case -1:
		{
			k = 0;
			for(h = imgH - 1; h >= 0; h --)
			{
				for(v = 0; v < imgV; v ++)
				{
					_dataOut[k] = _dataIn[v * imgH + h];
					k ++;
				}
				_dataOut += (out_stride - imgV);
			}
			break;
		}
		case 0:
		{
		//provide the effect of line stride
			for(v = 0; v < imgV; v ++)
			{
				memcpy(_dataOut, _dataIn, imgH * 2);
				_dataIn += imgH;
				_dataOut += out_stride;
			}
			break;
		}
	}
	
	return;
}


/*
//read sensor control file
//modify according to control file
void ov9640_get_config(OV9640_CFG * _cfg, FILE * _fp)
{
	char buf[200];

	memset(_cfg, 0, sizeof(OV9640_CFG));

	if(_fp)
	{
		fgets(buf, sizeof(buf), _fp);
		if(!strncmp(buf, "ov9640", 6))
		{
			printf("ov9640: reading control file\n");
			
			fgets(buf, sizeof(buf), _fp);
			_cfg->color_format = strtoul(buf, NULL, 0);
			
			fgets(buf, sizeof(buf), _fp);
			_cfg->vert_mirror = strtoul(buf, NULL, 0);
			
			fgets(buf, sizeof(buf), _fp);
			_cfg->hori_mirror = strtoul(buf, NULL, 0);
			
			fgets(buf, sizeof(buf), _fp);
			_cfg->out_width = strtoul(buf, NULL, 0);
			
			fgets(buf, sizeof(buf), _fp);
			_cfg->out_height = strtoul(buf, NULL, 0);
		
			fgets(buf, sizeof(buf), _fp);
			_cfg->test_pattern_enable = strtoul(buf, NULL, 0);
			
			fgets(buf, sizeof(buf), _fp);
			_cfg->gamma = strtoul(buf, NULL, 0);
		
			fgets(buf, sizeof(buf), _fp);
			_cfg->sharpening = strtoul(buf, NULL, 0);
		
			fgets(buf, sizeof(buf), _fp);
			_cfg->colorsat = strtoul(buf, NULL, 0);
		
			fgets(buf, sizeof(buf), _fp);
			_cfg->awb_left = strtoul(buf, NULL, 0);
		
			fgets(buf, sizeof(buf), _fp);
			_cfg->awb_right = strtoul(buf, NULL, 0);
		
			fgets(buf, sizeof(buf), _fp);
			_cfg->awb_top = strtoul(buf, NULL, 0);
		
			fgets(buf, sizeof(buf), _fp);
			_cfg->awb_bottom = strtoul(buf, NULL, 0);
		
			fgets(buf, sizeof(buf), _fp);
			_cfg->fps = strtoul(buf, NULL, 0);

			fgets(buf, sizeof(buf), _fp);
			_cfg->afc_freq = strtoul(buf, NULL, 0);

			fgets(buf, sizeof(buf), _fp);
			_cfg->capture_width = strtoul(buf, NULL, 0);

			fgets(buf, sizeof(buf), _fp);
			_cfg->capture_height = strtoul(buf, NULL, 0);
		}
	}

	printf("\nOV9640 CONFIGURATION\n\n");
	
	printf("color_format = %d\n", _cfg->color_format);
	printf("vert_mirror = %d\n", _cfg->vert_mirror);
	printf("hori_mirror = %d\n", _cfg->hori_mirror);
	printf("out_width = %d\n", _cfg->out_width);
	printf("out_height = %d\n", _cfg->out_height);
	printf("test_pattern_enable = %d\n", _cfg->test_pattern_enable);
	printf("gain_green1 = %f\n", (double)_cfg->gain_green1 / SCALEFAC);
	printf("gain_blue = %f\n", (double)_cfg->gain_blue / SCALEFAC);
	printf("gain_red = %f\n", (double)_cfg->gain_red / SCALEFAC);
	printf("gain_green2 = %f\n", (double)_cfg->gain_green2 / SCALEFAC);
	printf("gain_global = %f\n", (double)_cfg->gain_global / SCALEFAC);
	printf("gamma = %f\n", (double)_cfg->gamma / SCALEFAC);
	printf("sharpening = %d%%\n", _cfg->sharpening);
	printf("colorsat = %d%%\n", _cfg->colorsat);
	printf("awb_left = %d\n", _cfg->awb_left);
	printf("awb_right = %d\n", _cfg->awb_right);
	printf("awb_top = %d\n", _cfg->awb_top);
	printf("awb_bottom = %d\n", _cfg->awb_bottom);
	printf("fps = %d\n", _cfg->fps);
	printf("afc_freq = %d\n", _cfg->afc_freq);
	printf("capture_width = %d\n", _cfg->capture_width);
	printf("capture_height = %d\n", _cfg->capture_height);
	printf("\n");
	
	return;
}
*/ 
/* update guojunjie jinya
void draw(U8 * fb, U8 * display_buf,  int screen_height,int screen_width, float screen_bytes_per_pixel)
{
                int i,j;
                for(i=0;i<320;i++)
	       for(j=0;j<240;j++)
		*((U16 *)test_buf+240*i+j)=*((U16 *)display_buf+320*j+i);
	
	memcpy(fb, test_buf, screen_height * screen_width * screen_bytes_per_pixel);
}



*/

//camera & capture main func
//handles polling, dma & prp
int main(int argc, char *argv[])
{
#define LCD_WIDTH	240
#define ROTATION	-1

#define READ_MODE_PRP	0
#define READ_MODE_POLL	1
	
	IMG_SPEC img_spec;
	OV9640_CFG ov9640_cfg;
	CSI_CFG csi_cfg;
	FILE * _fp_ov9640_cfg;
	char * _file_name_sensor;
	int read_mode;
	
	int fb;
	struct fb_var_screeninfo vinfo;
	int screensize;
	unsigned int *_tbuf, *_fbuf;

	int fr = 0;

	if(argc != 3)
	{
		printf( "Camera application for OV9640 version 0.1\n"
			"usage: doCapture2 [read mode] [sensor control file]\n"
			"  - read mode          : p=prp, o=poll\n"
			"  - sensor control file: contains sensor config\n");

		return 0;
	}
	else
	{
		if(strcmp(argv[1], "p") == 0)
			read_mode = READ_MODE_PRP;
		else if(strcmp(argv[1], "o") == 0)
			read_mode = READ_MODE_POLL;

		_file_name_sensor = argv[2];
	}
printf("%s: %d\n",__FUNCTION__,__LINE__);
//csi config
	csi_open();
printf("%s: %d\n",__FUNCTION__,__LINE__);
	switch(read_mode)
	{
		case READ_MODE_PRP:	
			csi_select_config(TIMING_IMAGIC_RGB_PRP, &csi_cfg);
			break;
		case READ_MODE_POLL:
			csi_select_config(TIMING_IMAGIC_RGB_POLL, &csi_cfg);
			break;
	}
printf("%s: %d\n",__FUNCTION__,__LINE__);
	csi_config(&csi_cfg);
printf("%s: %d\n",__FUNCTION__,__LINE__);
	csi_dump_config();
printf("%s: %d\n",__FUNCTION__,__LINE__);

//sensor config
	ov9640_open();
printf("%s: %d\n",__FUNCTION__,__LINE__);
	ov9640_i2c_test();
printf("%s: %d\n",__FUNCTION__,__LINE__);
	ov9640_select_config(OV9640_RGB_QVGA, &ov9640_cfg);
printf("%s: %d\n",__FUNCTION__,__LINE__);
	_fp_ov9640_cfg = fopen(_file_name_sensor, "r");
printf("%s: %d\n",__FUNCTION__,__LINE__);
	ov9640_get_config(&ov9640_cfg, _fp_ov9640_cfg);
printf("%s: %d\n",__FUNCTION__,__LINE__);
	fclose(_fp_ov9640_cfg);
printf("%s: %d\n",__FUNCTION__,__LINE__);
	ov9640_config(&ov9640_cfg);
printf("%s: %d\n",__FUNCTION__,__LINE__);
	
	img_spec.width = ov9640_cfg.out_width;
	img_spec.height = ov9640_cfg.out_height;
	img_spec.bytes_per_pixel = 2;
	
printf("%s: %d\n",__FUNCTION__,__LINE__);
printf("csi_cfg.prp_if_en=%d\n",csi_cfg.prp_if_en);
	if(!csi_cfg.prp_if_en)
	{
	//csi-prp link
	//nothing to do

                }
//	else if(csi_cfg.dma_en)
//	{
//	//support dma here
//	//most of time prp is used instead, dma is not useful and not implemented
//	}
	else
	{
	//polling
	//write data to frame buffer
//		if ((fb = open("/dev/fb0", O_RDWR)) < 0)
		if ((fb = open("/dev/fbdir/0", O_RDWR)) < 0)
		{
			printf("doCapture2: cannot open fb device!\n");
			exit(-1);
		}
		if( ioctl(fb,FBIOGET_VSCREENINFO,&vinfo) )
		{
			printf("Error reading variable information\n");
			exit(2);
		}
/*  update guojunjie
                               if ((fd_fb1 = open(FB_DEVICE1, O_RDWR)) <= 0) {
		                  
		                  printf("Frame buffer %s open error!\n", FB_DEVICE1);
		                  close(fd_fb1);
		                  csi_close();
		                  exit(-1);
	                 }
	                PDEBUG("Use %s as frame buffer device.\n", FB_DEVICE1);

	                if ((fb1 = (U8 *) mmap(0, SCREEN_IMAGE_SIZE1, PROT_READ |
			       PROT_WRITE, MAP_SHARED, fd_fb1, 0)) == 0) {
		                  
		                  printf("Error: failed to map framebuffer device %s.\n",FB_DEVICE1);
		                  close(fd_fb1);
		                  csi_close();
		                  exit(-1);
	                }
	                printf("Frame buffer 1 mapping succeed.\n");	
*/	
		
	                 
printf("%s: %d\n",__FUNCTION__,__LINE__);
		screensize = vinfo.xres*vinfo.yres*vinfo.bits_per_pixel / 8;
		_fbuf = (unsigned int *)mmap(NULL, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0);
		_tbuf = (unsigned int *)malloc(img_spec.width * img_spec.height * img_spec.bytes_per_pixel);
	
printf("%s: %d\n",__FUNCTION__,__LINE__);
		while(1)
		{
//			csi_read(_tbuf, img_spec.width * img_spec.height * img_spec.bytes_per_pixel);
//			rgb_rotate((unsigned int)_tbuf, (unsigned int)_fbuf, img_spec.width, img_spec.height, ROTATION, LCD_WIDTH);

printf("%s: %d\n",__FUNCTION__,__LINE__);
			csi_read(_fbuf, img_spec.width * img_spec.height * img_spec.bytes_per_pixel);
/*	guojunjie jinya		
			draw(fb1,_fbuf,img_spec.width,img_spec.height,img_spec.bytes_per_pixel);
*/
			perror("csi read");
			perror("csi cpture succeed");
			fr ++;
			if(!(fr % 100))
				printf("fr %d\n", fr);
		}
	
printf("%s: %d\n",__FUNCTION__,__LINE__);
		free(_tbuf);
	}
	
	ov9640_close();
	csi_close();

	printf("doCapture2 completed\n");
	
	return 0;
}





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -