removed wait with timer
Dependents: Gaurd_Server2 Gaurd_Server
Fork of JPEGCamera by
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 return state == PROCESSING; 00038 } 00039 00040 bool JPEGCamera::takePicture(char *filename) { 00041 if (state == READY) { 00042 fp = fopen(filename, "wb"); 00043 if (fp != 0) { 00044 if (takePicture()) { 00045 imageSize = getImageSize(); 00046 address = 0; 00047 state = PROCESSING; 00048 } else { 00049 fclose(fp); 00050 printf("takePicture(%s) failed", filename); 00051 state = ERROR; 00052 } 00053 } else { 00054 printf("fopen() failed"); 00055 state = ERROR; 00056 } 00057 } 00058 return state != ERROR; 00059 } 00060 00061 bool JPEGCamera::processPicture() { 00062 if (state == PROCESSING) { 00063 if (address < imageSize) { 00064 char data[1024]; 00065 int size = readData(data, min(sizeof(data), imageSize - address), address); 00066 int ret = fwrite(data, size, 1, fp); 00067 if (ret > 0) 00068 address += size; 00069 if (ret == 0 || address >= imageSize) { 00070 stopPictures(); 00071 fclose(fp); 00072 wait(0.1); // ???? 00073 state = ret > 0 ? READY : ERROR; 00074 } 00075 } 00076 } 00077 00078 return state == PROCESSING || state == READY; 00079 } 00080 00081 bool JPEGCamera::reset() { 00082 char buf[4] = {0x56, 0x00, 0x26, 0x00}; 00083 int ret = sendReceive(buf, sizeof buf, 4); 00084 if (ret == 4 && buf[0] == 0x76) { 00085 //wait(4.0); 00086 timer.start(); 00087 timer.reset(); 00088 while(timer.read()<=4); 00089 state = READY; 00090 } else { 00091 state = ERROR; 00092 } 00093 return state == READY; 00094 } 00095 00096 bool JPEGCamera::takePicture() { 00097 char buf[5] = {0x56, 0x00, 0x36, 0x01, 0x00}; 00098 int ret = sendReceive(buf, sizeof buf, 5); 00099 00100 return ret == 5 && buf[0] == 0x76; 00101 } 00102 00103 bool JPEGCamera::stopPictures() { 00104 char buf[5] = {0x56, 0x00, 0x36, 0x01, 0x03}; 00105 int ret = sendReceive(buf, sizeof buf, 5); 00106 00107 return ret == 4 && buf[0] == 0x76; 00108 } 00109 00110 int JPEGCamera::getImageSize() { 00111 char buf[9] = {0x56, 0x00, 0x34, 0x01, 0x00}; 00112 int ret = sendReceive(buf, sizeof buf, 9); 00113 00114 //The size is in the last 2 characters of the response. 00115 return (ret == 9 && buf[0] == 0x76) ? (buf[7] << 8 | buf[8]) : 0; 00116 } 00117 00118 int JPEGCamera::readData(char *dataBuf, int size, int address) { 00119 char buf[16] = {0x56, 0x00, 0x32, 0x0C, 0x00, 0x0A, 0x00, 0x00, 00120 address >> 8, address & 255, 0x00, 0x00, size >> 8, size & 255, 0x00, 0x0A 00121 }; 00122 int ret = sendReceive(buf, sizeof buf, 5); 00123 00124 return (ret == 5 && buf[0] == 0x76) ? receive(dataBuf, size, DATA_TIMEOUT) : 0; 00125 } 00126 00127 int JPEGCamera::sendReceive(char *buf, int sendSize, int receiveSize) { 00128 while (readable()) getc(); 00129 00130 for (int i = 0; i < sendSize; i++) putc(buf[i]); 00131 00132 return receive(buf, receiveSize, RESPONSE_TIMEOUT); 00133 } 00134 00135 int JPEGCamera::receive(char *buf, int size, int timeout) { 00136 timer.start(); 00137 timer.reset(); 00138 00139 int i = 0; 00140 while (i < size && timer.read_ms() < timeout) { 00141 if (readable()) 00142 buf[i++] = getc(); 00143 } 00144 00145 return i; 00146 }
Generated on Thu Jul 14 2022 01:41:02 by 1.7.2