コード例 #1
0
int main()
{
    setup();
    unsigned long previous_time = 0;
    static int k = 0;
    uint8_t temp;
    struct timeval tv;
    struct timezone tz;
    write_reg(ARDUCHIP_MODE, 0x01);		 	//Switch to CAM

    while(1)
    {
        temp = read_reg(ARDUCHIP_TRIG);

        if(!(temp & VSYNC_MASK))				//New Frame is coming
        {
            write_reg(ARDUCHIP_MODE, 0x00);    	//Switch to MCU
            resetXY();
            write_reg(ARDUCHIP_MODE, 0x01);    	//Switch to CAM
            while(!(read_reg(ARDUCHIP_TRIG)&0x01)); 	//Wait for VSYNC is gone
        }
        else if(temp & SHUTTER_MASK)
        {
            gettimeofday (&tv , &tz);
            previous_time = tv.tv_sec;
            //printf("previous_time is %d.\n",previous_time);
            while(read_reg(ARDUCHIP_TRIG) & SHUTTER_MASK)
            {
                gettimeofday (&tv , &tz);
                //printf("put time is %d.\n",tv.tv_sec);
                if((tv.tv_sec - previous_time) >= 2)
                {
                    Playback();
                    delayms(1000);
                }
            }

            gettimeofday (&tv , &tz);
            if((tv.tv_sec - previous_time) < 2)
            {
                //printf(" get time is %d.\n",tv.tv_sec);
                memset(filePath,0,28);
                strcat(filePath,"/home/pi/");
                getnowtime();
                strcat(filePath,nowtime);
                strcat(filePath,".bmp");
                //Open the new file
                fp = fopen(filePath,"w+");
                if (fp == NULL)
                {
                    printf("open file failed\n");
                    return 0;
                }				//Generate file name
                write_reg(ARDUCHIP_MODE, 0x00);    	//Switch to MCU, freeze the screen
                GrabImage(filePath);
            }
        }
    }
}
コード例 #2
0
int  main(void)
{
	BOOL isShowFlag = TRUE;
	int nmemb = 1;

	setup();

	while(1)
	{
		uint8_t buf[256];
		static int i = 0;
		uint8_t temp,temp_last;
		uint8_t start_capture = 0;

		//Wait trigger from shutter buttom
		if(arducam_read_reg(ARDUCHIP_TRIG) & SHUTTER_MASK)
		{
			isShowFlag = FALSE;
			arducam_write_reg(ARDUCHIP_MODE, 0x00);
			arducam_set_format(fmtJPEG);
			arducam_init();
			arducam_write_reg(ARDUCHIP_TIM, VSYNC_LEVEL_MASK);		//VSYNC is active HIGH

			//Wait until buttom released
			while(arducam_read_reg(ARDUCHIP_TRIG) & SHUTTER_MASK);
			arducam_delay_ms(1000);
			start_capture = 1;
    	
		}
		else
		{
			if(isShowFlag )
			{
				temp = arducam_read_reg(ARDUCHIP_TRIG);
  
				if(!(temp & VSYNC_MASK))				 			//New Frame is coming
				{
					arducam_write_reg(ARDUCHIP_MODE, 0x00);    		//Switch to MCU
					resetXY();
					arducam_write_reg(ARDUCHIP_MODE, 0x01);    		//Switch to CAM
					while(!(arducam_read_reg(ARDUCHIP_TRIG)&0x01)); 	//Wait for VSYNC is gone
				}
			}
		}
		if(start_capture)
		{
			//Flush the FIFO
			arducam_flush_fifo();
			//Clear the capture done flag
			arducam_clear_fifo_flag();
			//Start capture
			arducam_start_capture();
			printf("Start Capture\n");
		}
  
		if(arducam_read_reg(ARDUCHIP_TRIG) & CAP_DONE_MASK)
		{

			printf("Capture Done!\n");
    
			//Construct a file name
	        char filePath[128];
	        time_t timep;
	        struct tm *p;
	        time(&timep);
	        p = localtime(&timep);
	        printf("Capture Done!\n");
	        snprintf(filePath, sizeof(filePath), "/home/pi/%04d%02d%02d%02d%02d%02d.bmp", 1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
			//Open the new file
			FILE *fp = fopen(filePath,"w+");
			if (fp == NULL)
			{
				printf("open file failed");
			  	exit(EXIT_FAILURE);
			}
			i = 0;
			temp = arducam_read_fifo();
			//Write first image data to buffer
			buf[i++] = temp;

			//Read JPEG data from FIFO
			while( (temp != 0xD9) | (temp_last != 0xFF) )
			{
				temp_last = temp;
				temp = arducam_read_fifo();
				//Write image data to buffer if not full
				if(i < 256)
					buf[i++] = temp;
				else
				{
					//Write 256 uint8_ts image data to file
					fwrite(buf,256,nmemb,fp);
					i = 0;
					buf[i++] = temp;
				}
			}
			//Write the remain uint8_ts in the buffer
			if(i > 0)
				fwrite(buf,i,nmemb,fp);

			//Close the file
			fclose(fp);

			//Clear the capture done flag
			arducam_clear_fifo_flag();
			//Clear the start capture flag
			start_capture = 0;
    
			arducam_set_format(fmtBMP);
			arducam_init();
			isShowFlag = TRUE;
		}
	}
  	exit(EXIT_SUCCESS);
}