Vishal Rai / JPEGCamera

Fork of JPEGCamera by Hiroshi Yamaguchi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers JPEGCamera.cpp Source File

JPEGCamera.cpp

00001 /* Arduino JPEGCamera Library
00002  * Copyright 2010 SparkFun Electronic
00003  * Written by Ryan Owens
00004  * Modified by arms22
00005  * Ported to mbed by yamaguch
00006  */
00007 
00008 #include "JPEGCamera.h"
00009 
00010 #define min(x, y) ((x) < (y)) ? (x) : (y)
00011 
00012 const int RESPONSE_TIMEOUT = 500;
00013 const int DATA_TIMEOUT = 1000;
00014 
00015 JPEGCamera::JPEGCamera(PinName tx, PinName rx) : Serial(tx, rx) {
00016     baud(38400);
00017     state = READY;
00018 }
00019 
00020 bool JPEGCamera::setPictureSize(JPEGCamera::PictureSize size, bool doReset) {
00021     char buf[9] = {0x56, 0x00, 0x31, 0x05, 0x04, 0x01, 0x00, 0x19, (char) size};
00022     int ret = sendReceive(buf, sizeof buf, 5);
00023 
00024     if (ret == 5 && buf[0] == 0x76) {
00025         if (doReset)
00026             reset();
00027         return true;
00028     } else
00029         return false;
00030 }
00031 
00032 bool JPEGCamera::isReady() {
00033     return state == READY;
00034 }
00035 
00036 bool JPEGCamera::isProcessing() {
00037    // printf("Im here/n2");
00038     return state == PROCESSING;
00039 }
00040 
00041 bool JPEGCamera::processPicture_ref() {
00042     if (state == PROCESSING) {
00043         if (address < imageSize) {
00044             char data[1024];
00045             int size = readData(data, min(sizeof(data), imageSize - address), address);
00046             int ret = fwrite(data, size, 1, fp);
00047             if (ret > 0)
00048                 address += size;
00049             if (ret == 0 || address >= imageSize) {
00050                 stopPictures();
00051                 fclose(fp);
00052                 wait(0.1); // ????
00053                 state = ret > 0 ? READY : ERROR;
00054             }
00055         }
00056     }
00057     return state == PROCESSING || state == READY;
00058 }
00059 
00060 
00061 bool JPEGCamera::takePicture(char *filename) {
00062     //printf("Im here1O/n");
00063     if (state == READY) {
00064         fp = fopen(filename, "wb");      
00065         if (fp != 0) {
00066             if (takePicture()) {
00067                 imageSize = getImageSize();
00068                 address = 0;
00069                 state = PROCESSING;
00070             } else {
00071                 fclose(fp);
00072                 printf("takePicture(%s) failed", filename);
00073                 state = ERROR;
00074             }
00075         } else {
00076             printf("fopen() failed");
00077             state = ERROR;
00078         }
00079     }
00080    // printf("Im here1C/n");
00081     return state != ERROR;
00082 }
00083 
00084 bool JPEGCamera::processPicture() {
00085     if (state == PROCESSING) {
00086         if (address < imageSize) {
00087             char data[1024];
00088             int size = readData(data, min(sizeof(data), imageSize - address), address);
00089             int ret = fwrite(data, size, 1, fp);
00090             if (ret > 0)
00091                 address += size;
00092             if (ret == 0 || address >= imageSize) {
00093                 stopPictures();
00094                 fclose(fp);
00095                 wait(0.1); // ????
00096                 state = ret > 0 ? READY : ERROR;
00097             }
00098         }
00099     }
00100 
00101     return state == PROCESSING || state == READY;
00102 }
00103 
00104 bool JPEGCamera::reset() {
00105     char buf[4] = {0x56, 0x00, 0x26, 0x00};
00106     int ret = sendReceive(buf, sizeof buf, 4);
00107     if (ret == 4 && buf[0] == 0x76) {
00108         wait(4.0);
00109         state = READY;
00110     } else {
00111         state = ERROR;
00112     }
00113     return state == READY;
00114 }
00115 
00116 bool JPEGCamera::takePicture() {
00117     char buf[5] = {0x56, 0x00, 0x36, 0x01, 0x00};
00118     int ret = sendReceive(buf, sizeof buf, 5);
00119 
00120     return ret == 5 && buf[0] == 0x76;
00121 }
00122 
00123 bool JPEGCamera::stopPictures() {
00124     char buf[5] = {0x56, 0x00, 0x36, 0x01, 0x03};
00125     int ret = sendReceive(buf, sizeof buf, 5);
00126 
00127     return ret == 4 && buf[0] == 0x76;
00128 }
00129 
00130 int JPEGCamera::getSizeOfimage(void){
00131     return imageSize;
00132 }
00133 
00134 int JPEGCamera::getImageSize() {
00135     char buf[9] = {0x56, 0x00, 0x34, 0x01, 0x00};
00136     int ret = sendReceive(buf, sizeof buf, 9);
00137 
00138     //The size is in the last 2 characters of the response.
00139     return (ret == 9 && buf[0] == 0x76) ? (buf[7] << 8 | buf[8]) : 0;
00140 }
00141 
00142 int JPEGCamera::readData(char *dataBuf, int size, int address) {
00143     char buf[16] = {0x56, 0x00, 0x32, 0x0C, 0x00, 0x0A, 0x00, 0x00,
00144                     address >> 8, address & 255, 0x00, 0x00, size >> 8, size & 255, 0x00, 0x0A
00145                    };
00146     int ret = sendReceive(buf, sizeof buf, 5);
00147 
00148     return (ret == 5 && buf[0] == 0x76) ? receive(dataBuf, size, DATA_TIMEOUT) : 0;
00149 }
00150 
00151 int JPEGCamera::sendReceive(char *buf, int sendSize, int receiveSize) {
00152     while (readable()) getc();
00153 
00154     for (int i = 0; i < sendSize; i++) putc(buf[i]);
00155 
00156     return receive(buf, receiveSize, RESPONSE_TIMEOUT);
00157 }
00158 
00159 int JPEGCamera::receive(char *buf, int size, int timeout) {
00160     timer.start();
00161     timer.reset();
00162 
00163     int i = 0;
00164     while (i < size && timer.read_ms() < timeout) {
00165             if (readable())
00166             buf[i++] = getc();
00167     }
00168 
00169     return i;
00170 }