I made Digital Camera using Arducam & WIZwiki-W7500

Dependencies:   Arduino Arducam_OV5642 SDFileSystem Arducam_UTFT_SPI WIZnetInterface_Ricky mbed

Revision:
0:e01f64037748
Child:
1:3a14a4c84db2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Oct 29 06:27:02 2015 +0000
@@ -0,0 +1,244 @@
+#include "mbed.h"
+
+#include "UTFT_SPI.h"
+#include "OV5642.h"
+#include "OV5642_regs.h"
+#include "SDFileSystem.h"
+#include "Arduino.h"
+
+void setup();
+void loop();
+void GrabImage(char* str);
+void dispBitmap();
+void Playback();
+void dispBitmap(FILE* fname);
+/* 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];
+    }   
+}
+
+#define BMPIMAGEOFFSET 66
+static FILE *outFile;
+static FILE *inFile;
+
+const char bmp_header[BMPIMAGEOFFSET] =
+{
+      0x42, 0x4D, 0x36, 0x58, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x28, 0x00, 
+      0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x01, 0x00, 0x10, 0x00, 0x03, 0x00, 
+      0x00, 0x00, 0x00, 0x58, 0x02, 0x00, 0xC4, 0x0E, 0x00, 0x00, 0xC4, 0x0E, 0x00, 0x00, 0x00, 0x00, 
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x00, 0x00, 0xE0, 0x07, 0x00, 0x00, 0x1F, 0x00,
+      0x00, 0x00
+};
+
+ArduCAM myCAM(D11, D12, D13, D10, D14, D15);
+//ArduLCD myGLCD(D11, D12, D13, D10);
+SDFileSystem SD(PB_3, PB_2, PB_1, PB_0, "sd");
+Serial pc(USBTX, USBRX);
+bool isShowFlag = true;
+
+int main()
+{
+    //*(volatile uint32_t *)(0x41001014) = 0x0060100; //clock 48MHz
+    
+    setup();
+    
+    while(1)
+    {
+        loop();
+    }
+}
+
+void setup()
+{
+  uint8_t vid,pid;
+  uint8_t temp; 
+  
+  pc.baud(115200);
+  pc.printf("ArduCAM Start!\r\n"); 
+
+  //Check if the ArduCAM SPI bus is OK
+  myCAM.write_reg(ARDUCHIP_TEST1, 0x55);
+  temp = myCAM.read_reg(ARDUCHIP_TEST1);
+  if(temp != 0x55)
+  {
+    pc.printf("SPI interface Error!\r\n");
+    while(1);
+  }
+  
+  //Change MCU mode
+  myCAM.set_mode(MCU2LCD_MODE);
+  
+  //Initialize the LCD Module
+  //myGLCD.InitLCD();
+  
+  //Check if the camera module type is OV5642
+  myCAM.rdSensorReg16_8(OV5642_CHIPID_HIGH, &vid);
+  myCAM.rdSensorReg16_8(OV5642_CHIPID_LOW, &pid);
+  if((vid != 0x56) || (pid != 0x42))
+    pc.printf("Can't find OV5642 module!\r\n");
+  else
+    pc.printf("OV5642 detected\r\n");
+    
+  myCAM.set_format(BMP);
+  myCAM.InitCAM();
+}
+  
+void loop()
+{
+  char str[8];
+  unsigned long previous_time = 0;
+  static int k = 0;
+  myCAM.set_mode(CAM2LCD_MODE);             //Switch to CAM
+  
+  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
+    }
+    else if(myCAM.get_bit(ARDUCHIP_TRIG,SHUTTER_MASK))
+    {
+       previous_time = millis();
+       while(myCAM.get_bit(ARDUCHIP_TRIG,SHUTTER_MASK))
+       {
+         if((millis() - previous_time) > 1500)
+         {
+           Playback();
+         }
+       }
+       if((millis() - previous_time) < 1500)
+       {
+         k = k + 1;
+         itoa(k, str);
+         strcat(str,".bmp");                //Generate file name
+         myCAM.set_mode(MCU2LCD_MODE);      //Switch to MCU, freeze the screen 
+         GrabImage(str);
+       }
+    }
+  }
+}
+
+
+void GrabImage(char* str)
+{
+  char VH,VL;
+  uint8_t buf[256];
+  static int k = 0;  
+  int i,j = 0;
+  outFile = fopen("preview.jpg", "w");
+  if (!outFile) 
+  {
+    pc.printf("Open File Error\r\n");
+    return;
+  }
+    
+  //Switch to FIFO Mode
+  myCAM.write_reg(ARDUCHIP_TIM, MODE_MASK);
+  //Flush the FIFO 
+  myCAM.flush_fifo();        
+  //Start capture
+  myCAM.start_capture();
+  pc.printf("Start Capture\r\n");   
+
+  //Polling the capture done flag
+  while(!myCAM.get_bit(ARDUCHIP_TRIG,CAP_DONE_MASK));
+  pc.printf("Capture Done!\r\n");  
+
+  k = 0;
+  //Write the BMP header
+  for( i = 0; i < BMPIMAGEOFFSET; i++)
+  {
+    char ch = pgm_read_byte(&bmp_header[i]);
+    buf[k++] = ch;
+  }
+  fwrite(buf,256,1,outFile);
+  //Read the first dummy byte
+  //myCAM.read_fifo();
+  
+  k = 0;
+  //Read 320x240x2 byte from FIFO
+  //Save as RGB565 bmp format
+  for(i = 0; i < 240; i++)
+    for(j = 0; j < 320; j++)
+  {
+      VH = myCAM.read_fifo();
+      VL = myCAM.read_fifo();
+      buf[k++] = VL;
+      buf[k++] = VH;
+      //Write image data to bufer if not full
+      if(k >= 256)
+      {
+        //Write 256 bytes image data to file from buffer
+        fwrite(buf,256,1,outFile);
+        k = 0;
+      }
+  }
+  //Close the file  
+  fclose(outFile); 
+
+  //Clear the capture done flag 
+  myCAM.clear_fifo_flag();
+  
+  //Switch to LCD Mode
+  myCAM.write_reg(ARDUCHIP_TIM, 0);
+  return;
+}
+
+void Playback()
+{  
+  char str[8];
+  int k = 0;
+  myCAM.set_mode(MCU2LCD_MODE);         //Switch to MCU
+  //myGLCD.InitLCD(PORTRAIT);
+  
+  while(1)
+  {
+      k = k + 1;
+      itoa(k, str); 
+      strcat(str,".bmp");
+      inFile = fopen("preview","r");
+      if (! inFile) 
+        return;
+      //myGLCD.clrScr();
+      //myGLCD.resetXY();
+      dispBitmap(inFile);
+      fclose(inFile);
+      wait_ms(2000);
+  }
+}
+
+//Only support RGB565 bmp format
+void dispBitmap(FILE* fname)
+{
+    uint8_t buf[256];
+    char VH,VL;
+    int i,j = 0;
+    for(i = 0 ;i < BMPIMAGEOFFSET; i++)
+        fread(buf,256,1,fname);
+    for(i = 0; i < 320; i++)
+    for(j = 0; j < 240; j++)
+    {
+        VL = fread(buf,256,1,fname);
+        //Serial.write(VL);
+        VH = fread(buf,256,1,fname);
+        //Serial.write(VH);
+        //myGLCD.LCD_Write_DATA(VH,VL);
+    }
+    //myGLCD.clrXY();
+}