Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 state = READY; 00087 } else { 00088 state = ERROR; 00089 } 00090 return state == READY; 00091 } 00092 00093 bool JPEGCamera::takePicture() { 00094 char buf[5] = {0x56, 0x00, 0x36, 0x01, 0x00}; 00095 int ret = sendReceive(buf, sizeof buf, 5); 00096 00097 return ret == 5 && buf[0] == 0x76; 00098 } 00099 00100 bool JPEGCamera::stopPictures() { 00101 char buf[5] = {0x56, 0x00, 0x36, 0x01, 0x03}; 00102 int ret = sendReceive(buf, sizeof buf, 5); 00103 00104 return ret == 4 && buf[0] == 0x76; 00105 } 00106 00107 int JPEGCamera::getImageSize() { 00108 char buf[9] = {0x56, 0x00, 0x34, 0x01, 0x00}; 00109 int ret = sendReceive(buf, sizeof buf, 9); 00110 00111 //The size is in the last 2 characters of the response. 00112 return (ret == 9 && buf[0] == 0x76) ? (buf[7] << 8 | buf[8]) : 0; 00113 } 00114 00115 int JPEGCamera::readData(char *dataBuf, int size, int address) { 00116 char buf[16] = {0x56, 0x00, 0x32, 0x0C, 0x00, 0x0A, 0x00, 0x00, 00117 address >> 8, address & 255, 0x00, 0x00, size >> 8, size & 255, 0x00, 0x0A 00118 }; 00119 int ret = sendReceive(buf, sizeof buf, 5); 00120 00121 return (ret == 5 && buf[0] == 0x76) ? receive(dataBuf, size, DATA_TIMEOUT) : 0; 00122 } 00123 00124 int JPEGCamera::sendReceive(char *buf, int sendSize, int receiveSize) { 00125 while (readable()) getc(); 00126 00127 for (int i = 0; i < sendSize; i++) putc(buf[i]); 00128 00129 return receive(buf, receiveSize, RESPONSE_TIMEOUT); 00130 } 00131 00132 int JPEGCamera::receive(char *buf, int size, int timeout) { 00133 timer.start(); 00134 timer.reset(); 00135 00136 int i = 0; 00137 while (i < size && timer.read_ms() < timeout) { 00138 if (readable()) 00139 buf[i++] = getc(); 00140 } 00141 00142 return i; 00143 }
Generated on Mon Jul 18 2022 21:07:48 by
1.7.2