int main(int argc, char *argv[])
{
    uint8_t buf[BUF_SIZE];
    int i = 0;
    uint8_t temp, temp_last;
  
    if (argc == 1) {
        printf("Usage: %s [-s <resolution>] | [-c <filename]", argv[0]);
        printf(" -s <resolution> Set resolution, valid resolutions are:\n");      
        printf("                   320x240\n");
        printf("                   640x480\n");
        printf("                   800x600\n");
        //printf("                   1280x720\n");
        printf("                   1920x1080\n");
        printf("                   2048x1563\n");
        printf("                   2592x1944\n");
        printf(" -c <filename>   Capture image\n");
        exit(EXIT_SUCCESS);
    }
    
    if (strcmp(argv[1], "-c") == 0 && argc == 4) {
        setup();
        arducam_set_format(fmtJPEG);
        arducam_init();
        
        // Change to JPEG capture mode and initialize the OV2640 module   
        if (strcmp(argv[3], "320x240") == 0) arducam_OV5642_set_jpeg_size(OV5642_320x240);
        else if (strcmp(argv[3], "640x480") == 0) arducam_OV5642_set_jpeg_size(OV5642_640x480);
        //else if (strcmp(argv[2], "1280x720") == 0) arducam_OV5642_set_jpeg_size(OV5642_1280x720);
        else if (strcmp(argv[3], "1920x1080") == 0) arducam_OV5642_set_jpeg_size(OV5642_1920x1080);
        else if (strcmp(argv[3], "2048x1536") == 0) arducam_OV5642_set_jpeg_size(OV5642_2048x1536);
        else if (strcmp(argv[3], "2592x1944") == 0) arducam_OV5642_set_jpeg_size(OV5642_2592x1944);
      
        else {
            printf("Unknown resolution %s\n", argv[3]);
            exit(EXIT_FAILURE);
        }
        sleep(1); // Let auto exposure do it's thing after changing image settings
        printf("Changed resolution1 to %s\n", argv[3]);
        
        delay(1000);
        
        arducam_write_reg(ARDUCHIP_TIM, VSYNC_LEVEL_MASK,CAM1_CS);		//VSYNC is active HIGH   
			  
        // Flush the FIFO
        arducam_flush_fifo(CAM1_CS);    
        // Clear the capture done flag
        arducam_clear_fifo_flag(CAM1_CS);
        // Start capture
        printf("Start capture\n");  
        arducam_start_capture(CAM1_CS);
        
       while (!(arducam_read_reg(ARDUCHIP_TRIG,CAM1_CS) & CAP_DONE_MASK)) ;
        
        printf(" CAM1 Capture Done\n");
                 
        // Open the new file
        FILE *fp1 = fopen(argv[2], "w+");
        
        if (!fp1) {
            printf("Error: could not open %s\n", argv[2]);
            exit(EXIT_FAILURE);
        }
          
        printf("Reading FIFO\n");
        
        i = 0;
        temp = arducam_read_fifo(CAM1_CS);
        // 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(CAM1_CS);
            // Write image data to buffer if not full
            if(i < BUF_SIZE) {
                buf[i++] = temp;
            } else {
                // Write BUF_SIZE uint8_ts image data to file
                fwrite(buf, BUF_SIZE, 1, fp1);
                i = 0;
                buf[i++] = temp;
            }
        }
        // Write the remain uint8_ts in the buffer
        if(i > 0) {
            fwrite(buf, i, 1, fp1);
        }
        // Close the file
        fclose(fp1);
        // Clear the capture done flag
        arducam_clear_fifo_flag(CAM1_CS);
    
    } else {
        printf("Error: unknown or missing argument.\n");
        exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
}
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);
}