1. /* Arduino JPEGCamera Library
  2.  * Copyright 2010 SparkFun Electronic
  3.  * Written by Ryan Owens
  4. */
  5.  
  6. #include <avr/pgmspace.h>
  7. #include "JPEGCamera.h"
  8. #include "Arduino.h"
  9. #include "SoftwareSerial.h"
  10.  
  11. //Commands for the LinkSprite Serial JPEG Camera
  12. const char GET_SIZE[5] = {0x56, 0x00, 0x34, 0x01, 0x00};
  13. const char RESET_CAMERA[4] = {0x56, 0x00, 0x26, 0x00};
  14. const char TAKE_PICTURE[5] = {0x56, 0x00, 0x36, 0x01, 0x00};
  15. const char STOP_TAKING_PICS[5] = {0x56, 0x00, 0x36, 0x01, 0x03};
  16. char RESIZE[9] = {0x56, 0x00, 0x31, 0x05, 0x04, 0x01, 0x00, 0x19, 0x22};
  17. char SETBAUD[7] = {0x56, 0x00, 0x24, 0x03, 0x01, 0x2A, 0xF2};
  18. char READ_DATA[8] = {0x56, 0x00, 0x32, 0x0C, 0x00, 0x0A, 0x00, 0x00};
  19. char STORE[5];
  20.  
  21. //We read data from the camera in chunks, this is the chunk size
  22. const int read_size=32;
  23.  
  24. //Make sure the camera is plugged into pins 2 and 3 for Rx/Tx
  25. SoftwareSerial cameraPort(2,3);
  26.  
  27. JPEGCamera::JPEGCamera()
  28. {
  29. }
  30.  
  31. //Initialize the serial connection
  32. void JPEGCamera::begin(void)
  33. {
  34.     //Camera baud rate is 38400
  35.     cameraPort.begin(38400);
  36.     Serial.begin(9600);
  37. }
  38.  
  39. void JPEGCamera::wait()
  40. {
  41.     while(cameraPort.available() < 42);
  42. }
  43.  
  44. int JPEGCamera::resize(char * response, int size)
  45. {
  46.     //160x120
  47.     if(size == 1) {RESIZE[8] = 0x22;}
  48.     //320x240
  49.     if(size == 2) {RESIZE[8] = 0x11;}
  50.     //640x480
  51.     if(size == 3) {RESIZE[8] = 0x00;}
  52.     return sendCommand(RESIZE, response, 9);
  53.  
  54. }
  55.  
  56. int JPEGCamera::setBaud(char * response, int baud)
  57. {
  58.     //38400 BAUD
  59.     if(baud == 1) {SETBAUD[5] = 0x2A;
  60.                    SETBAUD[6] = 0xF2;
  61.                   }
  62.     //57600 BAUD
  63.     if(baud == 2) {SETBAUD[5] = 0x1C;
  64.                    SETBAUD[6] = 0x4C;
  65.                   }
  66.     //115200 BAUD
  67.     if(baud == 3) {SETBAUD[5] = 0x0D;
  68.                    SETBAUD[6] = 0xA6;
  69.                   }
  70.  
  71.     return sendCommand(SETBAUD, response, 7);
  72.  
  73. }
  74.  
  75. //Get the size of the image currently stored in the camera
  76. //pre: response is an empty string. size is a pointer to an integer
  77. //post: response string contains the entire camera response to the GET_SIZE command
  78. //      size is set to the size of the image
  79. //return: number of characters in the response string
  80. //usage: length = camera.getSize(response, &img_size);
  81. int JPEGCamera::getSize(char * response, int * size)
  82. {
  83.     int count=0;
  84.     //Send the GET_SIZE command string to the camera
  85.     count = sendCommand(GET_SIZE, response, 5);
  86.     //Read 4 characters from the camera and add them to the response string
  87.     for(int i=0; i<4; i++)
  88.     {
  89.         while(!cameraPort.available());
  90.         response[count+i]=cameraPort.read();
  91.     }
  92.     //Set the number of characters to return
  93.     count+=4;
  94.     //The size is in the last 2 characters of the response.
  95.     //Parse them and convert the characters to an integer
  96.     *size = response[count-2]*256;
  97.     *size += (int)response[count-1] & 0x00FF;
  98.     //Send the number of characters in the response back to the calling function
  99.     return count;
  100. }
  101.  
  102. //Reset the camera
  103. //pre: response is an empty string
  104. //post: response contains the cameras response to the reset command
  105. //returns: number of characters in the response
  106. //Usage: camera.reset(response);
  107. int JPEGCamera::reset(char * response)
  108. {
  109.     return sendCommand(RESET_CAMERA, response, 4);
  110. }
  111.  
  112. //Take a new picture
  113. //pre: response is an empty string
  114. //post: response contains the cameras response to the TAKE_PICTURE command
  115. //returns: number of characters in the response
  116. //Usage: camera.takePicture(response);
  117. int JPEGCamera::takePicture(char * response)
  118. {
  119.     return sendCommand(TAKE_PICTURE, response, 5);
  120. }
  121.  
  122. //Stop taking pictures
  123. //pre: response is an empty string
  124. //post: response contains the cameras response to the STOP_TAKING_PICS command
  125. //returns: number of characters in the response
  126. //Usage: camera.stopPictures(response);
  127. int JPEGCamera::stopPictures(char * response)
  128. {
  129.     return sendCommand(STOP_TAKING_PICS, response, 5);
  130. }
  131.  
  132. //Get the read_size bytes picture data from the camera
  133. //pre: response is an empty string, address is the address of data to grab
  134. //post: response contains the cameras response to the readData command, but the response header is parsed out.
  135. //returns: number of characters in the response
  136. //Usage: camera.readData(response, cur_address);
  137. //NOTE: This function must be called repeatedly until all of the data is read
  138. //See Sample program on how to get the entire image.
  139. int JPEGCamera::readData(char * response, int address)
  140. {
  141.     int count=0;
  142.  
  143.     //Flush out any data currently in the serial buffer
  144.     cameraPort.flush();
  145.  
  146.     //Send the command to get read_size bytes of data from the current address
  147.     for(int i=0; i<8; i++) cameraPort.print(READ_DATA[i]);
  148.     cameraPort.print(address>>8);
  149.     cameraPort.print(address);
  150.     cameraPort.print(0x00);
  151.     cameraPort.print(0x00);
  152.     cameraPort.print(read_size>>8);
  153.     cameraPort.print(read_size);
  154.     cameraPort.print(0x00);
  155.     cameraPort.print(0x0A);
  156.  
  157.     //Print the data to the serial port. Used for debugging.
  158.     /*
  159.     for(int i=0; i<8; i++)Serial.print(READ_DATA[i]);
  160.     Serial.print(address>>8, BYTE);
  161.     Serial.print(address, BYTE);
  162.     Serial.print(0x00, BYTE);
  163.     Serial.print(0x00, BYTE);
  164.     Serial.print(read_size>>8, BYTE);
  165.     Serial.print(read_size, BYTE);
  166.     Serial.print(0x00, BYTE);
  167.     Serial.print(0x0A, BYTE);
  168.     Serial.println();
  169.     */
  170.  
  171.     //Read the response header.
  172.     for(int i=0; i<5; i++){
  173.         while(!cameraPort.available());
  174.         Serial.print(cameraPort.read(), HEX);
  175.         Serial.print(" ");
  176.     }
  177.  
  178.     Serial.print(": ");
  179.  
  180.     //Now read the actual data and add it to the response string.
  181.     count=0;
  182.     while(count < read_size)
  183.     {
  184.         while(!cameraPort.available());
  185.         *response++=cameraPort.read();
  186.         count+=1;
  187.     }
  188.  
  189.     //Return the number of characters in the response.
  190.     return count;
  191. }
  192.  
  193. //Send a command to the camera
  194. //pre: cameraPort is a serial connection to the camera set to 3800 baud
  195. //     command is a string with the command to be sent to the camera
  196. //     response is an empty string
  197. //     length is the number of characters in the command
  198. //post: response contains the camera response to the command
  199. //return: the number of characters in the response string
  200. //usage: None (private function)
  201. int JPEGCamera::sendCommand(const char * command, char * response, int length)
  202. {
  203.     char c=0;
  204.     int count=0;
  205.     //Clear any data currently in the serial buffer
  206.     cameraPort.flush();
  207.     //Send each character in the command string to the camera through the camera serial port
  208.     for(int i=0; i<length; i++){
  209.         cameraPort.print(*command++);
  210.     }
  211.     //Get the response from the camera and add it to the response string.
  212.     for(int i=0; i<length; i++)
  213.     {
  214.         while(!cameraPort.available());
  215.         *response++=cameraPort.read();
  216.         count+=1;
  217.     }
  218.     //return the number of characters in the response string
  219.     return count;
  220. }