test

Dependencies:   mbed Prelude_OV5642 Arducam_UTFT_SPI Arducam_OV5642 Arduino WIZnetInterface_Ricky

Revision:
3:23d439c8526b
Parent:
2:3a12c5e8b030
Child:
4:63042b865304
diff -r 3a12c5e8b030 -r 23d439c8526b main.cpp
--- a/main.cpp	Thu Oct 29 07:57:09 2015 +0000
+++ b/main.cpp	Thu Oct 29 10:09:41 2015 +0000
@@ -2,18 +2,24 @@
 #include "UTFT_SPI.h"
 #include "OV5642.h"
 #include "OV5642_regs.h"
+#include "SDFileSystem.h"
 #include "Arduino.h"
 
 void setup();
 void loop();
+void itoa( unsigned long long int value, char *str);
 
 ArduCAM myCAM(D11, D12, D13, D10, D14, D15);
 ArduLCD myGLCD(D11, D12, D13, D10);
+SDFileSystem sd(D11, D12, D13, D9, "sd");
 Serial pc(USBTX, USBRX);
+bool isShowFlag = true;
+char fname[32];
+char fnamecnt=0;
 
 int main()
 {
-    *(volatile uint32_t *)(0x41001014) = 0x0060100; //clock 48MHz
+    *(volatile uint32_t *)(0x41001014) = 0x0060200; //clock 48MHz
     
     setup();
     
@@ -64,22 +70,128 @@
   else
     pc.printf("OV5642 detected\r\n");
     
+  //Change to BMP capture mode and initialize the OV5642 module     
+  myCAM.set_format(BMP);
+  
   myCAM.InitCAM();
 }
-  
+
 void loop()
 {
-  myCAM.set_mode(CAM2LCD_MODE);             //Switch to CAM
+  FILE *outFile;
+  uint8_t buf[256];
+  static int i = 0;
+  uint8_t temp,temp_last;
+  uint8_t start_capture = 0;
+
+  //Wait trigger from shutter buttom   
+  if(myCAM.get_bit(ARDUCHIP_TRIG , SHUTTER_MASK))   
+  {
+    isShowFlag = false;
+    myCAM.set_mode(MCU2LCD_MODE);
+    myCAM.set_format(JPEG);
+    myCAM.InitCAM();
+    myCAM.OV5642_set_JPEG_size();
+    myCAM.write_reg(ARDUCHIP_TIM, VSYNC_LEVEL_MASK);        //VSYNC is active HIGH
+
+    //Wait until buttom released
+    while(myCAM.get_bit(ARDUCHIP_TRIG, SHUTTER_MASK));
+    wait_ms(1000);
+    start_capture = 1;  
+  }
+  else
+  {
+    if(isShowFlag )
+    {
   
-  while(1)
+      if(!myCAM.get_bit(ARDUCHIP_TRIG,VSYNC_MASK))                          //New Frame is coming
+      {
+         myCAM.set_mode(MCU2LCD_MODE);          //Switch to MCU
+         myGLCD.resetXY();
+         myCAM.set_mode(CAM2LCD_MODE);          //Switch to CAM
+         while(!myCAM.get_bit(ARDUCHIP_TRIG,VSYNC_MASK));   //Wait for VSYNC is gone
+      }
+    }
+  }
+  if(start_capture)
+  {
+    //Flush the FIFO 
+    myCAM.flush_fifo(); 
+    //Clear the capture done flag 
+    myCAM.clear_fifo_flag();         
+    //Start capture
+    myCAM.start_capture();
+    pc.printf("Start Capture\r\n");     
+  }
+  
+  if(myCAM.get_bit(ARDUCHIP_TRIG ,CAP_DONE_MASK))
   {
 
-    if(!myCAM.get_bit(ARDUCHIP_TRIG,VSYNC_MASK))                //New Frame is coming
+    pc.printf("Capture Done!\r\n");
+    //Construct a file name
+    snprintf(fname, sizeof(fname), "/sd/jpss%04d.jpg", fnamecnt);
+    fnamecnt++;
+    //Open the new file  
+    outFile = fopen(fname,"w");
+    if (! outFile) 
+    { 
+      pc.printf("open file failed\r\n");
+      return;
+    }
+    //Read first dummy byte
+    //myCAM.read_fifo();
+    
+    i = 0;
+    temp = myCAM.read_fifo();
+    //Write first image data to buffer
+    buf[i++] = temp;
+
+    //Read JPEG data from FIFO
+    while( (temp != 0xD9) | (temp_last != 0xFF) )
     {
-       myCAM.set_mode(MCU2LCD_MODE);        //Switch to MCU
-       myGLCD.resetXY();
-       myCAM.set_mode(CAM2LCD_MODE);        //Switch to CAM
-       while(!myCAM.get_bit(ARDUCHIP_TRIG,VSYNC_MASK));     //Wait for VSYNC is gone
+      temp_last = temp;
+      temp = myCAM.read_fifo();
+      //Write image data to buffer if not full
+      if(i < 256)
+        buf[i++] = temp;
+      else
+      {
+        //Write 256 bytes image data to file
+        fwrite(buf,256,1,outFile);
+        i = 0;
+        buf[i++] = temp;
+      }
     }
+    //Write the remain bytes in the buffer
+    if(i > 0)
+      fwrite(buf,i,1,outFile);
+
+    //Close the file 
+    fclose(outFile);  
+    //Clear the capture done flag 
+    myCAM.clear_fifo_flag();
+    //Clear the start capture flag
+    start_capture = 0;
+    
+    myCAM.set_format(BMP);
+    myCAM.InitCAM();
+    isShowFlag = true;  
   }
 }
+
+/* itoa:  convert n to characters in s */
+void itoa( unsigned long long int value, char *str)
+{   
+   int i,j;
+   char temp[30];
+   for(i=0; value > 0; i++){    
+       str[i] = value%10+'0';
+       value=value/10;
+    }
+    for(j=0;i>=0;j++,i--){
+        temp[j]=str[i-1];
+    }
+    for(i=0;i<j;i++){
+        str[i]=temp[i];
+    }   
+}