• Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
HomeJPEG CameraA faster camera code for Jpeg serial camera
Previous Next

A faster camera code for Jpeg serial camera

Posted by: Alvin Jin , March 6, 2014

[vc_row][vc_column width=”1/1″][vc_tour][vc_tab title=”Analysis” tab_id=”1394080826-1-5″][vc_column_text]Last night when we debugging serial camera, found  that sometimes it took a long time for collecting one frame. Because, after reading a piece of data need wait for a while, and the time is not fixed. If it’s to short, maybe can not run on the other platform, if  it’s to long, will take a long time for photographing. Now we will update the code to improve the speed.

QQ图片20140306112547

The main modification is the send command function, the original command did not verify the camera received the correct instruction or not which cause collection error. We added verification in code, and do not need add the extension waiting function for  every piece of data, greatly reducing the image acquisition time.

[/vc_column_text][/vc_tab][vc_tab title=”Updated Code” tab_id=”1394080826-2-52″][vc_column_text]

// LinkSprite.com
// Note:
// 1. SD must be formated to FAT16
// 2. As the buffer of softserial has 64 bytes, so the code read 32 bytes each time

// 3. Please add the libaray to the lib path

//  * SD card attached to SPI bus as follows:
//** MOSI - pin 11
// ** MISO - pin 12
// ** CLK - pin 13
// ** CS - pin 4

#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>

#define ZERO ((uint8_t)0x00)

File  myFile;
byte ImageBuffer[32];
char PrintBuffer[4];
SoftwareSerial mySerial(5,6);          // Set Arduino pin 4 and 5 as softserial

void SendResetCmd();
void SetBaudRateCmd();
void SetImageSizeCmd();
void SendTakePhotoCmd();
void StopTakePhotoCmd();
void SendReadDataCmd(unsigned short AL);

void setup()
{

  Serial.begin(115200);
  while (!Serial)
  {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  mySerial.begin(38400);

  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(10, OUTPUT);

  if (!SD.begin(4)) 
  {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
}

void loop()
{
  char  count;
  boolean EndFlag;
  char tmpChar;
  unsigned short AL;

  AL = 0;
  EndFlag = 0;
  SendResetCmd();
  myFile = SD.open("pic.jpg", FILE_WRITE); 
  SendTakePhotoCmd();
  while(!EndFlag)
  {  
     count = 0;
     SendReadDataCmd(AL); 
     while(count < 32)
     {
       if(mySerial.available())
       {
         ImageBuffer[count ++ ] = mySerial.read();
         if((count >=2) && (ImageBuffer[count -2 ] == 0xFF)&& (ImageBuffer[count -1 ] == 0xD9))
         {
           EndFlag = 1;
           break;
         }
       }
     }
     AL += 32;
     for(tmpChar = 0;tmpChar <count; tmpChar ++)
     {
       myFile.write(ImageBuffer[tmpChar]);
       sprintf(PrintBuffer,"%02X ",ImageBuffer[tmpChar]);
       Serial.print(PrintBuffer);
     }
     Serial.println(); 

  }
  Serial.println(); 
  myFile.close();
  Serial.print("Finished writing data to file");
  while(1);
}

void SendResetCmd()
{
  char tmp;
  char count;
  char buf[4];

BEGIN: 
  count = 0;
  Serial.print("begin to send rst cmd\n");
  while(mySerial.available()>0)
  {
    buf[count]=mySerial.read();
  } 
  mySerial.write(0x56);
  mySerial.write(ZERO);
  mySerial.write(0x26);
  mySerial.write(ZERO);
  while(count < 4)
  {
    if(mySerial.available())
    {
      buf[count] = mySerial.read();
      count++;
    }
  }
  if(buf[0] != 0x76)goto BEGIN;
  if(buf[1] != 0x00)goto BEGIN;
  if(buf[2] != 0x26)goto BEGIN;
  if(buf[3] != 0x00)goto BEGIN; 
  delay(500);
}

void SetImageSizeCmd()
{
  char tmp;
  char count;
  char buf[5];

BEGIN: 
  count = 0;
  Serial.print("begin to send Set Image Size cmd\n");
  while(mySerial.available()>0)
  {
    buf[count]=mySerial.read();
  } 
  mySerial.write(0x56);
  mySerial.write(ZERO);
  mySerial.write(0x31);
  mySerial.write(0x05);
  mySerial.write(0x04);
  mySerial.write(0x01);
  mySerial.write(ZERO);
  mySerial.write(0x19);
  mySerial.write(0x11);
  while(count < 5)
  {
    if(mySerial.available())
    {
      buf[count] = mySerial.read();
      count++;
    }
  }
  if(buf[0] != 0x76)goto BEGIN;
  if(buf[1] != 0x00)goto BEGIN;
  if(buf[2] != 0x31)goto BEGIN;
  if(buf[3] != 0x00)goto BEGIN; 
  if(buf[4] != 0x00)goto BEGIN; 
}

void SetBaudRateCmd()
{
  char tmp;
  char count;
  char buf[5];

BEGIN: 
  count = 0;
  Serial.print("begin to send Set Baud Rate cmd\n");
  while(mySerial.available()>0)
  {
    buf[count]=mySerial.read();
  } 
  mySerial.write(0x56);
  mySerial.write(ZERO);
  mySerial.write(0x24);
  mySerial.write(0x03);
  mySerial.write(0x01);
  mySerial.write(0x2A);
  mySerial.write(0xC8);
  while(count < 5)
  {
    if(mySerial.available())
    {
      buf[count] = mySerial.read();
      count++;
    }
  }
  if(buf[0] != 0x76)goto BEGIN;
  if(buf[1] != 0x00)goto BEGIN;
  if(buf[2] != 0x24)goto BEGIN;
  if(buf[3] != 0x00)goto BEGIN; 
  if(buf[4] != 0x00)goto BEGIN; 
}

void SendTakePhotoCmd()
{
  char tmp;
  char count;
  char buf[5];

BEGIN: 
  count = 0;
  Serial.print("begin to send Take Photo cmd\n");
  while(mySerial.available()>0)
  {
    buf[count]=mySerial.read();
  } 
  mySerial.write(0x56);
  mySerial.write(ZERO);
  mySerial.write(0x36);
  mySerial.write(0x01);
  mySerial.write(ZERO);
  while(count < 5)
  {
    if(mySerial.available())
    {
      buf[count] = mySerial.read();
      count++;
    }
  }
  if(buf[0] != 0x76)goto BEGIN;
  if(buf[1] != 0x00)goto BEGIN;
  if(buf[2] != 0x36)goto BEGIN;
  if(buf[3] != 0x00)goto BEGIN; 
  if(buf[4] != 0x00)goto BEGIN; 
}

void StopTakePhotoCmd()
{
  char tmp;
  char count;
  char buf[5];

BEGIN: 
  count = 0;
  Serial.print("begin to send Stop Take Photo cmd\n");
  while(mySerial.available()>0)
  {
    buf[count]=mySerial.read();
  } 
  mySerial.write(0x56);
  mySerial.write(ZERO);
  mySerial.write(0x36);
  mySerial.write(0x01);
  mySerial.write(0x03);
  while(count < 5)
  {
    if(mySerial.available())
    {
      buf[count] = mySerial.read();
      count++;
    }
  }
  if(buf[0] != 0x76)goto BEGIN;
  if(buf[1] != 0x00)goto BEGIN;
  if(buf[2] != 0x36)goto BEGIN;
  if(buf[3] != 0x00)goto BEGIN; 
  if(buf[4] != 0x00)goto BEGIN; 
}

void SendReadDataCmd(unsigned short AL)
{
  char tmp;
  char count;
  char buf[5];

BEGIN: 
  count = 0;
  while(mySerial.available()>0)
  {
    buf[count]=mySerial.read();
  } 
  mySerial.write(0x56);
  mySerial.write(ZERO);
  mySerial.write(0x32);
  mySerial.write(0x0c);
  mySerial.write(ZERO);
  mySerial.write(0x0a);
  mySerial.write(ZERO);
  mySerial.write(ZERO);
  mySerial.write(AL>>8);
  mySerial.write(AL&0xff);
  mySerial.write(ZERO);
  mySerial.write(ZERO);
  mySerial.write(ZERO);
  mySerial.write(0x20);
  mySerial.write(ZERO);
  mySerial.write(0x0a);
  while(count < 5)
  {
    if(mySerial.available())
    {
      buf[count] = mySerial.read();
      count++;
    }
  }
  if(buf[0] != 0x76)goto BEGIN;
  if(buf[1] != 0x00)goto BEGIN;
  if(buf[2] != 0x32)goto BEGIN;
  if(buf[3] != 0x00)goto BEGIN; 
  if(buf[4] != 0x00)goto BEGIN; 
}

[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]

Tags: JPEG Camera

Share!
Tweet

Alvin Jin

About the author

Leave a Reply Cancel reply

You must be logged in to post a comment.

Category

  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors