I made Digital Camera using Arducam & WIZwiki-W7500

Dependencies:   Arduino Arducam_OV5642 SDFileSystem Arducam_UTFT_SPI WIZnetInterface_Ricky mbed

main.cpp

Committer:
justinkim
Date:
2015-10-29
Revision:
3:23d439c8526b
Parent:
2:3a12c5e8b030
Child:
4:63042b865304

File content as of revision 3:23d439c8526b:

#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 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) = 0x0060200; //clock 48MHz
    
    setup();
    
    while(1)
    {
        loop();
    }
}

void setup()
{
  uint8_t vid,pid;
  uint8_t temp; 
  
  pc.baud(115200);
  pc.printf("ArduCAM Start!\r\n"); 
  
  uint8_t temp1,temp2;
  myCAM.write_reg(ARDUCHIP_TEST1, 0x55);         //Write to test1 register by 0x55
  myCAM.write_reg(ARDUCHIP_TEST2, 0xAA);         //Write to test1 register by 0xaa
  wait_ms(1000);
  temp1 = myCAM.read_reg(ARDUCHIP_TEST1);                //Read from test1 register 
  temp2 = myCAM.read_reg(ARDUCHIP_TEST2);                //Read from test1 register
  pc.printf("temp1 : %d\r\n",temp1);
  pc.printf("temp2 : %d\r\n",temp2);
  wait_ms(1000);
  
  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");
    
  //Change to BMP capture mode and initialize the OV5642 module     
  myCAM.set_format(BMP);
  
  myCAM.InitCAM();
}

void loop()
{
  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 )
    {
  
      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))
  {

    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) )
    {
      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];
    }   
}