#include <JPEGCamera.h>
#include <SoftwareSerial.h>
//#include <SD.h>
//File picture;
//Create a character array to store the cameras response to commands
char response[42];
//Count is used to store the number of characters in the response string.
unsigned int count = 0;
//Size will be set to the size of the jpeg image.
int size = 0;
//This will keep track of the data address being read from the camera
int address = 0;
//eof is a flag for the sketch to determine when the end of a file is detected
//while reading the file data from the camera.
int eof=0;
JPEGCamera camera;
void setup()
{
//Setup serial port, as well as the SD card
Serial.begin(9600);
Serial.println("BEGUN");
//SD.begin(9);
//pinMode(10, OUTPUT);
//Open a new picture file on the SD card
// picture = SD.open("pic06", FILE_WRITE);
//Setup the Camera
camera.begin();
Serial.println("Camera finished intializing.");
//camera.resize(response, 1);
//camera.setBaud(response, 1);
Serial.println("Resetting Camera");
count = camera.reset(response);
Serial.println("Finished Resetting Camera");
Serial.println("Response: ");
hexdump(response, count);
delay(3000);
//take a picture
Serial.println("Taking picture");
count = camera.takePicture(response);
Serial.println("Finished taking picture.");
Serial.println("Response: ");
hexdump(response, count);
delay(2000);
Serial.println("");
//Get + print the size of the picture
Serial.println("Size");
count = camera.getSize(response, &size);
Serial.println("Response ");
hexdump(response, count);
Serial.println(size);
Serial.println("");
Serial.println("Data");
Serial.println("{");
while(address < size)
{
//int startTime = millis();//pull the data out that we requested earlier
count = camera.readData(response, address);
//int endTime = millis();
//Serial.print(count);
//Serial.print(" ");
//Serial.println(endTime - startTime);
//camera.wait();
for(int i = 0; i < count; i++)
{
//If not end of file
if((response[i] == (char)0xD9) && (response[i-1]==(char)0xFF)) eof = 1;
//picture.write(uint8_t(response[i]));
//Serial.print("0x");
Serial.print(uint8_t(response[i]), HEX);
//Serial.print(", ");
if(eof == 1) break;
}
Serial.println();
address += count;
//int end2 = millis();
//Serial.println(end2 - endTime);
if(eof == 1) break;
}
Serial.println("}");
//picture.close();
}
void loop()
{
}
void hexdump( char *binaryBuf, int len )
{
for (int i=0; i<len; i++)
{
if ( i != 0 && i % 8 == 0 )
{
Serial.println("");
}
Serial.print((unsigned char)(*(binaryBuf+i)), HEX);
Serial.print(" ");
}
Serial.println(""); // print final newline
}