this will take a image from C328 serial camera and store those images in mbed flash as html and this html page is uploaded into the server at ip:192.168.1.2

Dependencies:   mbed

Committer:
mitesh2patel
Date:
Wed Dec 15 15:01:56 2010 +0000
Revision:
0:e1a0471e5ffb

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mitesh2patel 0:e1a0471e5ffb 1 /**
mitesh2patel 0:e1a0471e5ffb 2 * C328-7640 device driver class (Version 0.0.6)
mitesh2patel 0:e1a0471e5ffb 3 * Reference documents: C328-7640 User Manual v3.0 2004.8.19
mitesh2patel 0:e1a0471e5ffb 4 *
mitesh2patel 0:e1a0471e5ffb 5 * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
mitesh2patel 0:e1a0471e5ffb 6 * http://shinta.main.jp/
mitesh2patel 0:e1a0471e5ffb 7 */
mitesh2patel 0:e1a0471e5ffb 8
mitesh2patel 0:e1a0471e5ffb 9 #include "CameraC328.h"
mitesh2patel 0:e1a0471e5ffb 10
mitesh2patel 0:e1a0471e5ffb 11 #define WAITIDLE waitIdle
mitesh2patel 0:e1a0471e5ffb 12 #define SENDFUNC sendBytes
mitesh2patel 0:e1a0471e5ffb 13 #define RECVFUNC recvBytes
mitesh2patel 0:e1a0471e5ffb 14 #define WAITFUNC waitRecv
mitesh2patel 0:e1a0471e5ffb 15
mitesh2patel 0:e1a0471e5ffb 16 /**
mitesh2patel 0:e1a0471e5ffb 17 * Constructor.
mitesh2patel 0:e1a0471e5ffb 18 *
mitesh2patel 0:e1a0471e5ffb 19 * @param tx A pin for transmit.
mitesh2patel 0:e1a0471e5ffb 20 * @param rx A pin for receive.
mitesh2patel 0:e1a0471e5ffb 21 * @param baud Baud rate. (Default is Baud19200.)
mitesh2patel 0:e1a0471e5ffb 22 */
mitesh2patel 0:e1a0471e5ffb 23 CameraC328::CameraC328(PinName tx, PinName rx, Baud baud) : serial(tx, rx) {
mitesh2patel 0:e1a0471e5ffb 24 serial.baud((int)baud);
mitesh2patel 0:e1a0471e5ffb 25 }
mitesh2patel 0:e1a0471e5ffb 26
mitesh2patel 0:e1a0471e5ffb 27 /**
mitesh2patel 0:e1a0471e5ffb 28 * Destructor.
mitesh2patel 0:e1a0471e5ffb 29 */
mitesh2patel 0:e1a0471e5ffb 30 CameraC328::~CameraC328() {
mitesh2patel 0:e1a0471e5ffb 31 }
mitesh2patel 0:e1a0471e5ffb 32
mitesh2patel 0:e1a0471e5ffb 33 /**
mitesh2patel 0:e1a0471e5ffb 34 * Make a sync. for baud rate.
mitesh2patel 0:e1a0471e5ffb 35 */
mitesh2patel 0:e1a0471e5ffb 36 CameraC328::ErrorNumber CameraC328::sync() {
mitesh2patel 0:e1a0471e5ffb 37 WAITIDLE();
mitesh2patel 0:e1a0471e5ffb 38
mitesh2patel 0:e1a0471e5ffb 39 for (int i = 0; i < SYNCMAX; i++) {
mitesh2patel 0:e1a0471e5ffb 40 if (NoError == sendSync()) {
mitesh2patel 0:e1a0471e5ffb 41 if (NoError == recvAckOrNck()) {
mitesh2patel 0:e1a0471e5ffb 42 if (NoError == recvSync()) {
mitesh2patel 0:e1a0471e5ffb 43 if (NoError == sendAck(0x0D, 0x00)) {
mitesh2patel 0:e1a0471e5ffb 44 /*
mitesh2patel 0:e1a0471e5ffb 45 * After synchronization, the camera needs a little time for AEC and AGC to be stable.
mitesh2patel 0:e1a0471e5ffb 46 * Users should wait for 1-2 seconds before capturing the first picture.
mitesh2patel 0:e1a0471e5ffb 47 */
mitesh2patel 0:e1a0471e5ffb 48 wait(2);
mitesh2patel 0:e1a0471e5ffb 49 return NoError;
mitesh2patel 0:e1a0471e5ffb 50 }
mitesh2patel 0:e1a0471e5ffb 51 }
mitesh2patel 0:e1a0471e5ffb 52 }
mitesh2patel 0:e1a0471e5ffb 53 }
mitesh2patel 0:e1a0471e5ffb 54 wait_ms(50);
mitesh2patel 0:e1a0471e5ffb 55 }
mitesh2patel 0:e1a0471e5ffb 56 return UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 57 }
mitesh2patel 0:e1a0471e5ffb 58
mitesh2patel 0:e1a0471e5ffb 59 /**
mitesh2patel 0:e1a0471e5ffb 60 * Initialize.
mitesh2patel 0:e1a0471e5ffb 61 *
mitesh2patel 0:e1a0471e5ffb 62 * @param ct Color type.
mitesh2patel 0:e1a0471e5ffb 63 * @param rr Raw resolution.
mitesh2patel 0:e1a0471e5ffb 64 * @param jr JPEG resolution.
mitesh2patel 0:e1a0471e5ffb 65 */
mitesh2patel 0:e1a0471e5ffb 66 CameraC328::ErrorNumber CameraC328::init(ColorType ct, RawResolution rr, JpegResolution jr) {
mitesh2patel 0:e1a0471e5ffb 67 WAITIDLE();
mitesh2patel 0:e1a0471e5ffb 68 ErrorNumber en;
mitesh2patel 0:e1a0471e5ffb 69
mitesh2patel 0:e1a0471e5ffb 70 en = sendInitial(ct, rr, jr);
mitesh2patel 0:e1a0471e5ffb 71 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 72 return en;
mitesh2patel 0:e1a0471e5ffb 73 }
mitesh2patel 0:e1a0471e5ffb 74 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 75 en = recvAckOrNck();
mitesh2patel 0:e1a0471e5ffb 76 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 77 return en;
mitesh2patel 0:e1a0471e5ffb 78 }
mitesh2patel 0:e1a0471e5ffb 79
mitesh2patel 0:e1a0471e5ffb 80 static bool alreadySetupPackageSize = false;
mitesh2patel 0:e1a0471e5ffb 81 if (!alreadySetupPackageSize) {
mitesh2patel 0:e1a0471e5ffb 82 en = sendSetPackageSize(packageSize);
mitesh2patel 0:e1a0471e5ffb 83 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 84 return en;
mitesh2patel 0:e1a0471e5ffb 85 }
mitesh2patel 0:e1a0471e5ffb 86 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 87 en = recvAckOrNck();
mitesh2patel 0:e1a0471e5ffb 88 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 89 return en;
mitesh2patel 0:e1a0471e5ffb 90 }
mitesh2patel 0:e1a0471e5ffb 91 alreadySetupPackageSize = true;
mitesh2patel 0:e1a0471e5ffb 92 }
mitesh2patel 0:e1a0471e5ffb 93
mitesh2patel 0:e1a0471e5ffb 94 return (ErrorNumber)NoError;
mitesh2patel 0:e1a0471e5ffb 95 }
mitesh2patel 0:e1a0471e5ffb 96
mitesh2patel 0:e1a0471e5ffb 97 /**
mitesh2patel 0:e1a0471e5ffb 98 * Get uncompressed snapshot picture.
mitesh2patel 0:e1a0471e5ffb 99 *
mitesh2patel 0:e1a0471e5ffb 100 * @param func A pointer to a callback function.
mitesh2patel 0:e1a0471e5ffb 101 * Please do NOT block this callback function.
mitesh2patel 0:e1a0471e5ffb 102 * Because the camera module transmit image datas continuously.
mitesh2patel 0:e1a0471e5ffb 103 * @return Status of the error.
mitesh2patel 0:e1a0471e5ffb 104 */
mitesh2patel 0:e1a0471e5ffb 105 CameraC328::ErrorNumber CameraC328::getUncompressedSnapshotPicture(void(*func)(size_t done, size_t total, char c)) {
mitesh2patel 0:e1a0471e5ffb 106 WAITIDLE();
mitesh2patel 0:e1a0471e5ffb 107 ErrorNumber en;
mitesh2patel 0:e1a0471e5ffb 108
mitesh2patel 0:e1a0471e5ffb 109 en = sendSnapshot(UncompressedPicture, 0);
mitesh2patel 0:e1a0471e5ffb 110 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 111 return en;
mitesh2patel 0:e1a0471e5ffb 112 }
mitesh2patel 0:e1a0471e5ffb 113 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 114 en = recvAckOrNck();
mitesh2patel 0:e1a0471e5ffb 115 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 116 return en;
mitesh2patel 0:e1a0471e5ffb 117 }
mitesh2patel 0:e1a0471e5ffb 118
mitesh2patel 0:e1a0471e5ffb 119 en = sendGetPicture(SnapshotPicture);
mitesh2patel 0:e1a0471e5ffb 120 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 121 return en;
mitesh2patel 0:e1a0471e5ffb 122 }
mitesh2patel 0:e1a0471e5ffb 123 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 124 en = recvAckOrNck();
mitesh2patel 0:e1a0471e5ffb 125 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 126 return en;
mitesh2patel 0:e1a0471e5ffb 127 }
mitesh2patel 0:e1a0471e5ffb 128
mitesh2patel 0:e1a0471e5ffb 129 /*
mitesh2patel 0:e1a0471e5ffb 130 * image data
mitesh2patel 0:e1a0471e5ffb 131 */
mitesh2patel 0:e1a0471e5ffb 132 DataType dt;
mitesh2patel 0:e1a0471e5ffb 133 uint32_t length = 0;
mitesh2patel 0:e1a0471e5ffb 134 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 135 en = recvData(&dt, &length);
mitesh2patel 0:e1a0471e5ffb 136 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 137 return en;
mitesh2patel 0:e1a0471e5ffb 138 }
mitesh2patel 0:e1a0471e5ffb 139 size_t imgcnt = 0;
mitesh2patel 0:e1a0471e5ffb 140 for (int i = 0; i < (int)length; i++) {
mitesh2patel 0:e1a0471e5ffb 141 char c;
mitesh2patel 0:e1a0471e5ffb 142 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 143 if (!RECVFUNC(&c, 1)) {
mitesh2patel 0:e1a0471e5ffb 144 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 145 }
mitesh2patel 0:e1a0471e5ffb 146 imgcnt++;
mitesh2patel 0:e1a0471e5ffb 147
mitesh2patel 0:e1a0471e5ffb 148 /*
mitesh2patel 0:e1a0471e5ffb 149 * Call a call back function.
mitesh2patel 0:e1a0471e5ffb 150 * Please do not block this function.
mitesh2patel 0:e1a0471e5ffb 151 */
mitesh2patel 0:e1a0471e5ffb 152 func(imgcnt, length, c);
mitesh2patel 0:e1a0471e5ffb 153 }
mitesh2patel 0:e1a0471e5ffb 154
mitesh2patel 0:e1a0471e5ffb 155 /*
mitesh2patel 0:e1a0471e5ffb 156 * ACK
mitesh2patel 0:e1a0471e5ffb 157 */
mitesh2patel 0:e1a0471e5ffb 158 en = sendAck(0x0A, 0x00);
mitesh2patel 0:e1a0471e5ffb 159 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 160 return en;
mitesh2patel 0:e1a0471e5ffb 161 }
mitesh2patel 0:e1a0471e5ffb 162
mitesh2patel 0:e1a0471e5ffb 163 return (ErrorNumber)NoError;
mitesh2patel 0:e1a0471e5ffb 164 }
mitesh2patel 0:e1a0471e5ffb 165
mitesh2patel 0:e1a0471e5ffb 166 /**
mitesh2patel 0:e1a0471e5ffb 167 * Get uncompressed preview picture.
mitesh2patel 0:e1a0471e5ffb 168 *
mitesh2patel 0:e1a0471e5ffb 169 * @param func A pointer to a callback function.
mitesh2patel 0:e1a0471e5ffb 170 * Please do NOT block this callback function.
mitesh2patel 0:e1a0471e5ffb 171 * Because the camera module transmit image datas continuously.
mitesh2patel 0:e1a0471e5ffb 172 * @return Status of the error.
mitesh2patel 0:e1a0471e5ffb 173 */
mitesh2patel 0:e1a0471e5ffb 174 CameraC328::ErrorNumber CameraC328::getUncompressedPreviewPicture(void(*func)(size_t done, size_t total, char c)) {
mitesh2patel 0:e1a0471e5ffb 175 WAITIDLE();
mitesh2patel 0:e1a0471e5ffb 176 ErrorNumber en;
mitesh2patel 0:e1a0471e5ffb 177
mitesh2patel 0:e1a0471e5ffb 178 en = sendGetPicture(PreviewPicture);
mitesh2patel 0:e1a0471e5ffb 179 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 180 return en;
mitesh2patel 0:e1a0471e5ffb 181 }
mitesh2patel 0:e1a0471e5ffb 182 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 183 en = recvAckOrNck();
mitesh2patel 0:e1a0471e5ffb 184 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 185 return en;
mitesh2patel 0:e1a0471e5ffb 186 }
mitesh2patel 0:e1a0471e5ffb 187
mitesh2patel 0:e1a0471e5ffb 188 /*
mitesh2patel 0:e1a0471e5ffb 189 * image data
mitesh2patel 0:e1a0471e5ffb 190 */
mitesh2patel 0:e1a0471e5ffb 191 DataType dt;
mitesh2patel 0:e1a0471e5ffb 192 uint32_t length = 0;
mitesh2patel 0:e1a0471e5ffb 193 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 194 en = recvData(&dt, &length);
mitesh2patel 0:e1a0471e5ffb 195 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 196 return en;
mitesh2patel 0:e1a0471e5ffb 197 }
mitesh2patel 0:e1a0471e5ffb 198 size_t imgcnt = 0;
mitesh2patel 0:e1a0471e5ffb 199 for (int i = 0; i < (int)length; i++) {
mitesh2patel 0:e1a0471e5ffb 200 char c;
mitesh2patel 0:e1a0471e5ffb 201 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 202 if (!RECVFUNC(&c, 1)) {
mitesh2patel 0:e1a0471e5ffb 203 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 204 }
mitesh2patel 0:e1a0471e5ffb 205 imgcnt++;
mitesh2patel 0:e1a0471e5ffb 206
mitesh2patel 0:e1a0471e5ffb 207 /*
mitesh2patel 0:e1a0471e5ffb 208 * Call a call back function.
mitesh2patel 0:e1a0471e5ffb 209 * Please do not block this function.
mitesh2patel 0:e1a0471e5ffb 210 */
mitesh2patel 0:e1a0471e5ffb 211 func(imgcnt, length, c);
mitesh2patel 0:e1a0471e5ffb 212 }
mitesh2patel 0:e1a0471e5ffb 213
mitesh2patel 0:e1a0471e5ffb 214 /*
mitesh2patel 0:e1a0471e5ffb 215 * ACK
mitesh2patel 0:e1a0471e5ffb 216 */
mitesh2patel 0:e1a0471e5ffb 217 en = sendAck(0x0A, 0x00);
mitesh2patel 0:e1a0471e5ffb 218 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 219 return en;
mitesh2patel 0:e1a0471e5ffb 220 }
mitesh2patel 0:e1a0471e5ffb 221
mitesh2patel 0:e1a0471e5ffb 222 return (ErrorNumber)NoError;
mitesh2patel 0:e1a0471e5ffb 223 }
mitesh2patel 0:e1a0471e5ffb 224
mitesh2patel 0:e1a0471e5ffb 225 /**
mitesh2patel 0:e1a0471e5ffb 226 * Get JPEG snapshot picture.
mitesh2patel 0:e1a0471e5ffb 227 *
mitesh2patel 0:e1a0471e5ffb 228 * @param func A pointer to a callback function.
mitesh2patel 0:e1a0471e5ffb 229 * You can block this function until saving the image datas.
mitesh2patel 0:e1a0471e5ffb 230 * @return Status of the error.
mitesh2patel 0:e1a0471e5ffb 231 */
mitesh2patel 0:e1a0471e5ffb 232 CameraC328::ErrorNumber CameraC328::getJpegSnapshotPicture(void(*func)(char *buf, size_t siz)) {
mitesh2patel 0:e1a0471e5ffb 233 WAITIDLE();
mitesh2patel 0:e1a0471e5ffb 234 ErrorNumber en;
mitesh2patel 0:e1a0471e5ffb 235
mitesh2patel 0:e1a0471e5ffb 236 en = sendSnapshot(CompressedPicture, 1);
mitesh2patel 0:e1a0471e5ffb 237 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 238 return en;
mitesh2patel 0:e1a0471e5ffb 239 }
mitesh2patel 0:e1a0471e5ffb 240 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 241 en = recvAckOrNck();
mitesh2patel 0:e1a0471e5ffb 242 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 243 return en;
mitesh2patel 0:e1a0471e5ffb 244 }
mitesh2patel 0:e1a0471e5ffb 245
mitesh2patel 0:e1a0471e5ffb 246 en = sendGetPicture(SnapshotPicture);
mitesh2patel 0:e1a0471e5ffb 247 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 248 return en;
mitesh2patel 0:e1a0471e5ffb 249 }
mitesh2patel 0:e1a0471e5ffb 250 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 251 en = recvAckOrNck();
mitesh2patel 0:e1a0471e5ffb 252 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 253 return en;
mitesh2patel 0:e1a0471e5ffb 254 }
mitesh2patel 0:e1a0471e5ffb 255
mitesh2patel 0:e1a0471e5ffb 256 /*
mitesh2patel 0:e1a0471e5ffb 257 * Data : snapshot picture
mitesh2patel 0:e1a0471e5ffb 258 */
mitesh2patel 0:e1a0471e5ffb 259 DataType dt;
mitesh2patel 0:e1a0471e5ffb 260 uint32_t length = 0;
mitesh2patel 0:e1a0471e5ffb 261 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 262 en = recvData(&dt, &length);
mitesh2patel 0:e1a0471e5ffb 263 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 264 return en;
mitesh2patel 0:e1a0471e5ffb 265 }
mitesh2patel 0:e1a0471e5ffb 266 en = sendAck(0x00, 0);
mitesh2patel 0:e1a0471e5ffb 267 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 268 return en;
mitesh2patel 0:e1a0471e5ffb 269 }
mitesh2patel 0:e1a0471e5ffb 270
mitesh2patel 0:e1a0471e5ffb 271 char databuf[packageSize - 6];
mitesh2patel 0:e1a0471e5ffb 272 uint16_t pkg_total = length / (packageSize - 6);
mitesh2patel 0:e1a0471e5ffb 273 for (int i = 0; i <= (int)pkg_total; i++) {
mitesh2patel 0:e1a0471e5ffb 274 uint16_t checksum = 0;
mitesh2patel 0:e1a0471e5ffb 275 // ID.
mitesh2patel 0:e1a0471e5ffb 276 char idbuf[2];
mitesh2patel 0:e1a0471e5ffb 277 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 278 if (!RECVFUNC(idbuf, sizeof(idbuf))) {
mitesh2patel 0:e1a0471e5ffb 279 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 280 }
mitesh2patel 0:e1a0471e5ffb 281 checksum += idbuf[0];
mitesh2patel 0:e1a0471e5ffb 282 checksum += idbuf[1];
mitesh2patel 0:e1a0471e5ffb 283 uint16_t id = (idbuf[1] << 8) | (idbuf[0] << 0);
mitesh2patel 0:e1a0471e5ffb 284 if (id != i) {
mitesh2patel 0:e1a0471e5ffb 285 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 286 }
mitesh2patel 0:e1a0471e5ffb 287
mitesh2patel 0:e1a0471e5ffb 288 // Size of the data.
mitesh2patel 0:e1a0471e5ffb 289 char dsbuf[2];
mitesh2patel 0:e1a0471e5ffb 290 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 291 if (!RECVFUNC(dsbuf, sizeof(dsbuf))) {
mitesh2patel 0:e1a0471e5ffb 292 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 293 }
mitesh2patel 0:e1a0471e5ffb 294
mitesh2patel 0:e1a0471e5ffb 295 // Received the data.
mitesh2patel 0:e1a0471e5ffb 296 checksum += dsbuf[0];
mitesh2patel 0:e1a0471e5ffb 297 checksum += dsbuf[1];
mitesh2patel 0:e1a0471e5ffb 298 uint16_t ds = (dsbuf[1] << 8) | (dsbuf[0] << 0);
mitesh2patel 0:e1a0471e5ffb 299 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 300 if (!RECVFUNC(&databuf[0], ds)) {
mitesh2patel 0:e1a0471e5ffb 301 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 302 }
mitesh2patel 0:e1a0471e5ffb 303 for (int j = 0; j < ds; j++) {
mitesh2patel 0:e1a0471e5ffb 304 checksum += databuf[j];
mitesh2patel 0:e1a0471e5ffb 305 }
mitesh2patel 0:e1a0471e5ffb 306
mitesh2patel 0:e1a0471e5ffb 307 // Verify code.
mitesh2patel 0:e1a0471e5ffb 308 char vcbuf[2];
mitesh2patel 0:e1a0471e5ffb 309 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 310 if (!RECVFUNC(vcbuf, sizeof(vcbuf))) {
mitesh2patel 0:e1a0471e5ffb 311 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 312 }
mitesh2patel 0:e1a0471e5ffb 313 uint16_t vc = (vcbuf[1] << 8) | (vcbuf[0] << 0);
mitesh2patel 0:e1a0471e5ffb 314 if (vc != (checksum & 0xff)) {
mitesh2patel 0:e1a0471e5ffb 315 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 316 }
mitesh2patel 0:e1a0471e5ffb 317
mitesh2patel 0:e1a0471e5ffb 318 /*
mitesh2patel 0:e1a0471e5ffb 319 * Call a call back function.
mitesh2patel 0:e1a0471e5ffb 320 * You can block this function while working.
mitesh2patel 0:e1a0471e5ffb 321 */
mitesh2patel 0:e1a0471e5ffb 322 func(databuf, ds);
mitesh2patel 0:e1a0471e5ffb 323
mitesh2patel 0:e1a0471e5ffb 324 /*
mitesh2patel 0:e1a0471e5ffb 325 * We should wait for camera working before reply a ACK.
mitesh2patel 0:e1a0471e5ffb 326 */
mitesh2patel 0:e1a0471e5ffb 327 wait_ms(100);
mitesh2patel 0:e1a0471e5ffb 328 en = sendAck(0x00, 1 + i);
mitesh2patel 0:e1a0471e5ffb 329 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 330 return en;
mitesh2patel 0:e1a0471e5ffb 331 }
mitesh2patel 0:e1a0471e5ffb 332 }
mitesh2patel 0:e1a0471e5ffb 333
mitesh2patel 0:e1a0471e5ffb 334 return (ErrorNumber)NoError;
mitesh2patel 0:e1a0471e5ffb 335 }
mitesh2patel 0:e1a0471e5ffb 336
mitesh2patel 0:e1a0471e5ffb 337 /**
mitesh2patel 0:e1a0471e5ffb 338 * Get JPEG preview picture.
mitesh2patel 0:e1a0471e5ffb 339 *
mitesh2patel 0:e1a0471e5ffb 340 * @param func A pointer to a callback function.
mitesh2patel 0:e1a0471e5ffb 341 * You can block this function until saving the image datas.
mitesh2patel 0:e1a0471e5ffb 342 * @return Status of the error.
mitesh2patel 0:e1a0471e5ffb 343 */
mitesh2patel 0:e1a0471e5ffb 344 CameraC328::ErrorNumber CameraC328::getJpegPreviewPicture(void(*func)(char *buf, size_t siz)) {
mitesh2patel 0:e1a0471e5ffb 345 WAITIDLE();
mitesh2patel 0:e1a0471e5ffb 346 ErrorNumber en;
mitesh2patel 0:e1a0471e5ffb 347
mitesh2patel 0:e1a0471e5ffb 348 en = sendGetPicture(JpegPreviewPicture);
mitesh2patel 0:e1a0471e5ffb 349 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 350 return en;
mitesh2patel 0:e1a0471e5ffb 351 }
mitesh2patel 0:e1a0471e5ffb 352 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 353 en = recvAckOrNck();
mitesh2patel 0:e1a0471e5ffb 354 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 355 return en;
mitesh2patel 0:e1a0471e5ffb 356 }
mitesh2patel 0:e1a0471e5ffb 357
mitesh2patel 0:e1a0471e5ffb 358 /*
mitesh2patel 0:e1a0471e5ffb 359 * Data : JPEG preview picture
mitesh2patel 0:e1a0471e5ffb 360 */
mitesh2patel 0:e1a0471e5ffb 361 DataType dt;
mitesh2patel 0:e1a0471e5ffb 362 uint32_t length = 0;
mitesh2patel 0:e1a0471e5ffb 363 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 364 en = recvData(&dt, &length);
mitesh2patel 0:e1a0471e5ffb 365 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 366 return en;
mitesh2patel 0:e1a0471e5ffb 367 }
mitesh2patel 0:e1a0471e5ffb 368 en = sendAck(0x00, 0);
mitesh2patel 0:e1a0471e5ffb 369 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 370 return en;
mitesh2patel 0:e1a0471e5ffb 371 }
mitesh2patel 0:e1a0471e5ffb 372
mitesh2patel 0:e1a0471e5ffb 373 char databuf[packageSize - 6];
mitesh2patel 0:e1a0471e5ffb 374 uint16_t pkg_total = length / (packageSize - 6);
mitesh2patel 0:e1a0471e5ffb 375 for (int i = 0; i <= (int)pkg_total; i++) {
mitesh2patel 0:e1a0471e5ffb 376 uint16_t checksum = 0;
mitesh2patel 0:e1a0471e5ffb 377 // ID.
mitesh2patel 0:e1a0471e5ffb 378 char idbuf[2];
mitesh2patel 0:e1a0471e5ffb 379 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 380 if (!RECVFUNC(idbuf, sizeof(idbuf))) {
mitesh2patel 0:e1a0471e5ffb 381 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 382 }
mitesh2patel 0:e1a0471e5ffb 383 checksum += idbuf[0];
mitesh2patel 0:e1a0471e5ffb 384 checksum += idbuf[1];
mitesh2patel 0:e1a0471e5ffb 385 uint16_t id = (idbuf[1] << 8) | (idbuf[0] << 0);
mitesh2patel 0:e1a0471e5ffb 386 if (id != i) {
mitesh2patel 0:e1a0471e5ffb 387 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 388 }
mitesh2patel 0:e1a0471e5ffb 389
mitesh2patel 0:e1a0471e5ffb 390 // Size of the data.
mitesh2patel 0:e1a0471e5ffb 391 char dsbuf[2];
mitesh2patel 0:e1a0471e5ffb 392 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 393 if (!RECVFUNC(dsbuf, sizeof(dsbuf))) {
mitesh2patel 0:e1a0471e5ffb 394 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 395 }
mitesh2patel 0:e1a0471e5ffb 396
mitesh2patel 0:e1a0471e5ffb 397 // Received the data.
mitesh2patel 0:e1a0471e5ffb 398 checksum += dsbuf[0];
mitesh2patel 0:e1a0471e5ffb 399 checksum += dsbuf[1];
mitesh2patel 0:e1a0471e5ffb 400 uint16_t ds = (dsbuf[1] << 8) | (dsbuf[0] << 0);
mitesh2patel 0:e1a0471e5ffb 401 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 402 if (!RECVFUNC(&databuf[0], ds)) {
mitesh2patel 0:e1a0471e5ffb 403 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 404 }
mitesh2patel 0:e1a0471e5ffb 405 for (int j = 0; j < ds; j++) {
mitesh2patel 0:e1a0471e5ffb 406 checksum += databuf[j];
mitesh2patel 0:e1a0471e5ffb 407 }
mitesh2patel 0:e1a0471e5ffb 408
mitesh2patel 0:e1a0471e5ffb 409 // Verify code.
mitesh2patel 0:e1a0471e5ffb 410 char vcbuf[2];
mitesh2patel 0:e1a0471e5ffb 411 WAITFUNC();
mitesh2patel 0:e1a0471e5ffb 412 if (!RECVFUNC(vcbuf, sizeof(vcbuf))) {
mitesh2patel 0:e1a0471e5ffb 413 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 414 }
mitesh2patel 0:e1a0471e5ffb 415 uint16_t vc = (vcbuf[1] << 8) | (vcbuf[0] << 0);
mitesh2patel 0:e1a0471e5ffb 416 if (vc != (checksum & 0xff)) {
mitesh2patel 0:e1a0471e5ffb 417 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 418 }
mitesh2patel 0:e1a0471e5ffb 419
mitesh2patel 0:e1a0471e5ffb 420 /*
mitesh2patel 0:e1a0471e5ffb 421 * Call a call back function.
mitesh2patel 0:e1a0471e5ffb 422 * You can block this function while working.
mitesh2patel 0:e1a0471e5ffb 423 */
mitesh2patel 0:e1a0471e5ffb 424 func(databuf, ds);
mitesh2patel 0:e1a0471e5ffb 425
mitesh2patel 0:e1a0471e5ffb 426 /*
mitesh2patel 0:e1a0471e5ffb 427 * We should wait for camera working before reply a ACK.
mitesh2patel 0:e1a0471e5ffb 428 */
mitesh2patel 0:e1a0471e5ffb 429 wait_ms(100);
mitesh2patel 0:e1a0471e5ffb 430 en = sendAck(0x00, 1 + i);
mitesh2patel 0:e1a0471e5ffb 431 if (NoError != en) {
mitesh2patel 0:e1a0471e5ffb 432 return en;
mitesh2patel 0:e1a0471e5ffb 433 }
mitesh2patel 0:e1a0471e5ffb 434 }
mitesh2patel 0:e1a0471e5ffb 435
mitesh2patel 0:e1a0471e5ffb 436 return (ErrorNumber)NoError;
mitesh2patel 0:e1a0471e5ffb 437 }
mitesh2patel 0:e1a0471e5ffb 438
mitesh2patel 0:e1a0471e5ffb 439 CameraC328::ErrorNumber CameraC328::sendInitial(ColorType ct, RawResolution rr, JpegResolution jr) {
mitesh2patel 0:e1a0471e5ffb 440 char send[COMMAND_LENGTH];
mitesh2patel 0:e1a0471e5ffb 441
mitesh2patel 0:e1a0471e5ffb 442 send[0] = 0xAA;
mitesh2patel 0:e1a0471e5ffb 443 send[1] = 0x01;
mitesh2patel 0:e1a0471e5ffb 444 send[2] = 0x00;
mitesh2patel 0:e1a0471e5ffb 445 send[3] = (char)ct;
mitesh2patel 0:e1a0471e5ffb 446 send[4] = (char)rr;
mitesh2patel 0:e1a0471e5ffb 447 send[5] = (char)jr;
mitesh2patel 0:e1a0471e5ffb 448
mitesh2patel 0:e1a0471e5ffb 449 if (!SENDFUNC(send, sizeof(send))) {
mitesh2patel 0:e1a0471e5ffb 450 return (ErrorNumber)SendRegisterTimeout;
mitesh2patel 0:e1a0471e5ffb 451 }
mitesh2patel 0:e1a0471e5ffb 452 return (ErrorNumber)NoError;
mitesh2patel 0:e1a0471e5ffb 453 }
mitesh2patel 0:e1a0471e5ffb 454
mitesh2patel 0:e1a0471e5ffb 455 CameraC328::ErrorNumber CameraC328::sendGetPicture(PictureType pt) {
mitesh2patel 0:e1a0471e5ffb 456 char send[COMMAND_LENGTH];
mitesh2patel 0:e1a0471e5ffb 457
mitesh2patel 0:e1a0471e5ffb 458 send[0] = 0xAA;
mitesh2patel 0:e1a0471e5ffb 459 send[1] = 0x04;
mitesh2patel 0:e1a0471e5ffb 460 send[2] = (char)pt;
mitesh2patel 0:e1a0471e5ffb 461 send[3] = 0x00;
mitesh2patel 0:e1a0471e5ffb 462 send[4] = 0x00;
mitesh2patel 0:e1a0471e5ffb 463 send[5] = 0x00;
mitesh2patel 0:e1a0471e5ffb 464
mitesh2patel 0:e1a0471e5ffb 465 if (!SENDFUNC(send, sizeof(send))) {
mitesh2patel 0:e1a0471e5ffb 466 return (ErrorNumber)SendRegisterTimeout;
mitesh2patel 0:e1a0471e5ffb 467 }
mitesh2patel 0:e1a0471e5ffb 468 return (ErrorNumber)NoError;
mitesh2patel 0:e1a0471e5ffb 469 }
mitesh2patel 0:e1a0471e5ffb 470
mitesh2patel 0:e1a0471e5ffb 471 CameraC328::ErrorNumber CameraC328::sendSnapshot(SnapshotType st, uint16_t skipFrames) {
mitesh2patel 0:e1a0471e5ffb 472 char send[COMMAND_LENGTH];
mitesh2patel 0:e1a0471e5ffb 473 send[0] = 0xAA;
mitesh2patel 0:e1a0471e5ffb 474 send[1] = 0x05;
mitesh2patel 0:e1a0471e5ffb 475 send[2] = (char)st;
mitesh2patel 0:e1a0471e5ffb 476 send[3] = (skipFrames >> 0) & 0xff;
mitesh2patel 0:e1a0471e5ffb 477 send[4] = (skipFrames >> 8) & 0xff;
mitesh2patel 0:e1a0471e5ffb 478 send[5] = 0x00;
mitesh2patel 0:e1a0471e5ffb 479
mitesh2patel 0:e1a0471e5ffb 480 if (!SENDFUNC(send, sizeof(send))) {
mitesh2patel 0:e1a0471e5ffb 481 return (ErrorNumber)SendRegisterTimeout;
mitesh2patel 0:e1a0471e5ffb 482 }
mitesh2patel 0:e1a0471e5ffb 483 return (ErrorNumber)NoError;
mitesh2patel 0:e1a0471e5ffb 484 }
mitesh2patel 0:e1a0471e5ffb 485
mitesh2patel 0:e1a0471e5ffb 486 CameraC328::ErrorNumber CameraC328::sendSetPackageSize(uint16_t packageSize) {
mitesh2patel 0:e1a0471e5ffb 487 char send[COMMAND_LENGTH];
mitesh2patel 0:e1a0471e5ffb 488 send[0] = 0xAA;
mitesh2patel 0:e1a0471e5ffb 489 send[1] = 0x06;
mitesh2patel 0:e1a0471e5ffb 490 send[2] = 0x08;
mitesh2patel 0:e1a0471e5ffb 491 send[3] = (packageSize >> 0) & 0xff;
mitesh2patel 0:e1a0471e5ffb 492 send[4] = (packageSize >> 8) & 0xff;
mitesh2patel 0:e1a0471e5ffb 493 send[5] = 0x00;
mitesh2patel 0:e1a0471e5ffb 494
mitesh2patel 0:e1a0471e5ffb 495 if (!SENDFUNC(send, sizeof(send))) {
mitesh2patel 0:e1a0471e5ffb 496 return (ErrorNumber)SendRegisterTimeout;
mitesh2patel 0:e1a0471e5ffb 497 }
mitesh2patel 0:e1a0471e5ffb 498 return (ErrorNumber)NoError;
mitesh2patel 0:e1a0471e5ffb 499 }
mitesh2patel 0:e1a0471e5ffb 500
mitesh2patel 0:e1a0471e5ffb 501 CameraC328::ErrorNumber CameraC328::sendSetBaudrate(Baud baud) {
mitesh2patel 0:e1a0471e5ffb 502 char send[COMMAND_LENGTH];
mitesh2patel 0:e1a0471e5ffb 503
mitesh2patel 0:e1a0471e5ffb 504 static struct baud_list {
mitesh2patel 0:e1a0471e5ffb 505 Baud baud;
mitesh2patel 0:e1a0471e5ffb 506 uint8_t div1st;
mitesh2patel 0:e1a0471e5ffb 507 uint8_t div2nd;
mitesh2patel 0:e1a0471e5ffb 508 } baudtable [] = {
mitesh2patel 0:e1a0471e5ffb 509 { Baud7200, 0xff, 0x01 },
mitesh2patel 0:e1a0471e5ffb 510 { Baud9600, 0xbf, 0x01 },
mitesh2patel 0:e1a0471e5ffb 511 { Baud14400, 0x7f, 0x01 },
mitesh2patel 0:e1a0471e5ffb 512 { Baud19200, 0x5f, 0x01 },
mitesh2patel 0:e1a0471e5ffb 513 { Baud28800, 0x3f, 0x01 },
mitesh2patel 0:e1a0471e5ffb 514 { Baud38400, 0x2f, 0x01 },
mitesh2patel 0:e1a0471e5ffb 515 { Baud57600, 0x1f, 0x01 },
mitesh2patel 0:e1a0471e5ffb 516 { Baud115200, 0x0f, 0x01 }
mitesh2patel 0:e1a0471e5ffb 517 };
mitesh2patel 0:e1a0471e5ffb 518
mitesh2patel 0:e1a0471e5ffb 519 uint8_t div1st = 0x00, div2nd = 0x00;
mitesh2patel 0:e1a0471e5ffb 520 struct baud_list *p = &baudtable[0];
mitesh2patel 0:e1a0471e5ffb 521 for (int i = 0; i < sizeof(baudtable) / sizeof(baudtable[0]); i++) {
mitesh2patel 0:e1a0471e5ffb 522 if (p->baud == baud) {
mitesh2patel 0:e1a0471e5ffb 523 div1st = p->div1st;
mitesh2patel 0:e1a0471e5ffb 524 div2nd = p->div2nd;
mitesh2patel 0:e1a0471e5ffb 525 }
mitesh2patel 0:e1a0471e5ffb 526 p++;
mitesh2patel 0:e1a0471e5ffb 527 }
mitesh2patel 0:e1a0471e5ffb 528
mitesh2patel 0:e1a0471e5ffb 529 send[0] = 0xAA;
mitesh2patel 0:e1a0471e5ffb 530 send[1] = 0x07;
mitesh2patel 0:e1a0471e5ffb 531 send[2] = div1st;
mitesh2patel 0:e1a0471e5ffb 532 send[3] = div2nd;
mitesh2patel 0:e1a0471e5ffb 533 send[4] = 0x00;
mitesh2patel 0:e1a0471e5ffb 534 send[5] = 0x00;
mitesh2patel 0:e1a0471e5ffb 535
mitesh2patel 0:e1a0471e5ffb 536 if (!SENDFUNC(send, sizeof(send))) {
mitesh2patel 0:e1a0471e5ffb 537 return (ErrorNumber)SendRegisterTimeout;
mitesh2patel 0:e1a0471e5ffb 538 }
mitesh2patel 0:e1a0471e5ffb 539
mitesh2patel 0:e1a0471e5ffb 540 return (ErrorNumber)NoError;
mitesh2patel 0:e1a0471e5ffb 541 }
mitesh2patel 0:e1a0471e5ffb 542
mitesh2patel 0:e1a0471e5ffb 543 CameraC328::ErrorNumber CameraC328::sendReset(ResetType rt, bool specialReset) {
mitesh2patel 0:e1a0471e5ffb 544 char send[COMMAND_LENGTH];
mitesh2patel 0:e1a0471e5ffb 545 send[0] = 0xAA;
mitesh2patel 0:e1a0471e5ffb 546 send[1] = 0x08;
mitesh2patel 0:e1a0471e5ffb 547 send[2] = (int)rt;
mitesh2patel 0:e1a0471e5ffb 548 send[3] = 0x00;
mitesh2patel 0:e1a0471e5ffb 549 send[4] = 0x00;
mitesh2patel 0:e1a0471e5ffb 550 send[5] = specialReset ? 0xff : 0x00;
mitesh2patel 0:e1a0471e5ffb 551 /*
mitesh2patel 0:e1a0471e5ffb 552 * Special reset : If the parameter is 0xFF, the command is a special Reset command and the firmware responds to it immediately.
mitesh2patel 0:e1a0471e5ffb 553 */
mitesh2patel 0:e1a0471e5ffb 554
mitesh2patel 0:e1a0471e5ffb 555 if (!SENDFUNC(send, sizeof(send))) {
mitesh2patel 0:e1a0471e5ffb 556 return (ErrorNumber)SendRegisterTimeout;
mitesh2patel 0:e1a0471e5ffb 557 }
mitesh2patel 0:e1a0471e5ffb 558
mitesh2patel 0:e1a0471e5ffb 559 return (ErrorNumber)NoError;
mitesh2patel 0:e1a0471e5ffb 560 }
mitesh2patel 0:e1a0471e5ffb 561
mitesh2patel 0:e1a0471e5ffb 562 CameraC328::ErrorNumber CameraC328::sendPowerOff() {
mitesh2patel 0:e1a0471e5ffb 563 char send[COMMAND_LENGTH];
mitesh2patel 0:e1a0471e5ffb 564 send[0] = 0xAA;
mitesh2patel 0:e1a0471e5ffb 565 send[1] = 0x09;
mitesh2patel 0:e1a0471e5ffb 566 send[2] = 0x00;
mitesh2patel 0:e1a0471e5ffb 567 send[3] = 0x00;
mitesh2patel 0:e1a0471e5ffb 568 send[4] = 0x00;
mitesh2patel 0:e1a0471e5ffb 569 send[5] = 0x00;
mitesh2patel 0:e1a0471e5ffb 570
mitesh2patel 0:e1a0471e5ffb 571 if (!SENDFUNC(send, sizeof(send))) {
mitesh2patel 0:e1a0471e5ffb 572 return (ErrorNumber)SendRegisterTimeout;
mitesh2patel 0:e1a0471e5ffb 573 }
mitesh2patel 0:e1a0471e5ffb 574
mitesh2patel 0:e1a0471e5ffb 575 return (ErrorNumber)NoError;
mitesh2patel 0:e1a0471e5ffb 576 }
mitesh2patel 0:e1a0471e5ffb 577
mitesh2patel 0:e1a0471e5ffb 578 CameraC328::ErrorNumber CameraC328::recvData(DataType *dt, uint32_t *length) {
mitesh2patel 0:e1a0471e5ffb 579 char recv[COMMAND_LENGTH];
mitesh2patel 0:e1a0471e5ffb 580 if (!RECVFUNC(recv, sizeof(recv))) {
mitesh2patel 0:e1a0471e5ffb 581 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 582 }
mitesh2patel 0:e1a0471e5ffb 583 if ((0xAA != recv[0]) || (0x0A != recv[1])) {
mitesh2patel 0:e1a0471e5ffb 584 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 585 }
mitesh2patel 0:e1a0471e5ffb 586 *dt = (DataType)recv[2];
mitesh2patel 0:e1a0471e5ffb 587 *length = (recv[5] << 16) | (recv[4] << 8) | (recv[3] << 0);
mitesh2patel 0:e1a0471e5ffb 588 return (ErrorNumber)NoError;
mitesh2patel 0:e1a0471e5ffb 589 }
mitesh2patel 0:e1a0471e5ffb 590
mitesh2patel 0:e1a0471e5ffb 591 CameraC328::ErrorNumber CameraC328::sendSync() {
mitesh2patel 0:e1a0471e5ffb 592 char send[COMMAND_LENGTH];
mitesh2patel 0:e1a0471e5ffb 593 send[0] = 0xAA;
mitesh2patel 0:e1a0471e5ffb 594 send[1] = 0x0D;
mitesh2patel 0:e1a0471e5ffb 595 send[2] = 0x00;
mitesh2patel 0:e1a0471e5ffb 596 send[3] = 0x00;
mitesh2patel 0:e1a0471e5ffb 597 send[4] = 0x00;
mitesh2patel 0:e1a0471e5ffb 598 send[5] = 0x00;
mitesh2patel 0:e1a0471e5ffb 599 if (!SENDFUNC(send, sizeof(send))) {
mitesh2patel 0:e1a0471e5ffb 600 return (ErrorNumber)SendRegisterTimeout;
mitesh2patel 0:e1a0471e5ffb 601 }
mitesh2patel 0:e1a0471e5ffb 602 return (ErrorNumber)NoError;
mitesh2patel 0:e1a0471e5ffb 603 }
mitesh2patel 0:e1a0471e5ffb 604
mitesh2patel 0:e1a0471e5ffb 605 CameraC328::ErrorNumber CameraC328::recvSync() {
mitesh2patel 0:e1a0471e5ffb 606 char recv[COMMAND_LENGTH];
mitesh2patel 0:e1a0471e5ffb 607 if (!RECVFUNC(recv, sizeof(recv))) {
mitesh2patel 0:e1a0471e5ffb 608 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 609 }
mitesh2patel 0:e1a0471e5ffb 610 if ((0xAA != recv[0]) || (0x0D != recv[1])) {
mitesh2patel 0:e1a0471e5ffb 611 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 612 }
mitesh2patel 0:e1a0471e5ffb 613 return (ErrorNumber)NoError;
mitesh2patel 0:e1a0471e5ffb 614 }
mitesh2patel 0:e1a0471e5ffb 615
mitesh2patel 0:e1a0471e5ffb 616 /**
mitesh2patel 0:e1a0471e5ffb 617 * Send ACK.
mitesh2patel 0:e1a0471e5ffb 618 *
mitesh2patel 0:e1a0471e5ffb 619 * @param commandId The command with that ID is acknowledged by this command.
mitesh2patel 0:e1a0471e5ffb 620 * @param packageId For acknowledging Data command, these two bytes represent the requested package ID. While for acknowledging other commands, these two bytes are set to 00h.
mitesh2patel 0:e1a0471e5ffb 621 */
mitesh2patel 0:e1a0471e5ffb 622 CameraC328::ErrorNumber CameraC328::sendAck(uint8_t commandId, uint16_t packageId) {
mitesh2patel 0:e1a0471e5ffb 623 char send[COMMAND_LENGTH];
mitesh2patel 0:e1a0471e5ffb 624 send[0] = 0xAA;
mitesh2patel 0:e1a0471e5ffb 625 send[1] = 0x0E;
mitesh2patel 0:e1a0471e5ffb 626 send[2] = commandId;
mitesh2patel 0:e1a0471e5ffb 627 send[3] = 0x00; // ACK counter is not used.
mitesh2patel 0:e1a0471e5ffb 628 send[4] = (packageId >> 0) & 0xff;
mitesh2patel 0:e1a0471e5ffb 629 send[5] = (packageId >> 8) & 0xff;
mitesh2patel 0:e1a0471e5ffb 630 if (!SENDFUNC(send, sizeof(send))) {
mitesh2patel 0:e1a0471e5ffb 631 return (ErrorNumber)SendRegisterTimeout;
mitesh2patel 0:e1a0471e5ffb 632 }
mitesh2patel 0:e1a0471e5ffb 633 return (ErrorNumber)NoError;
mitesh2patel 0:e1a0471e5ffb 634 }
mitesh2patel 0:e1a0471e5ffb 635
mitesh2patel 0:e1a0471e5ffb 636 /**
mitesh2patel 0:e1a0471e5ffb 637 * Receive ACK or NCK.
mitesh2patel 0:e1a0471e5ffb 638 *
mitesh2patel 0:e1a0471e5ffb 639 * @return Error number.
mitesh2patel 0:e1a0471e5ffb 640 */
mitesh2patel 0:e1a0471e5ffb 641 CameraC328::ErrorNumber CameraC328::recvAckOrNck() {
mitesh2patel 0:e1a0471e5ffb 642 char recv[COMMAND_LENGTH];
mitesh2patel 0:e1a0471e5ffb 643 if (!RECVFUNC(recv, sizeof(recv))) {
mitesh2patel 0:e1a0471e5ffb 644 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 645 }
mitesh2patel 0:e1a0471e5ffb 646 if ((0xAA == recv[0]) && (0x0E == recv[1])) {
mitesh2patel 0:e1a0471e5ffb 647 return (ErrorNumber)NoError;
mitesh2patel 0:e1a0471e5ffb 648 }
mitesh2patel 0:e1a0471e5ffb 649 if ((0xAA == recv[0]) && (0x0F == recv[1])) {
mitesh2patel 0:e1a0471e5ffb 650 return (ErrorNumber)recv[4];
mitesh2patel 0:e1a0471e5ffb 651 }
mitesh2patel 0:e1a0471e5ffb 652 return (ErrorNumber)UnexpectedReply;
mitesh2patel 0:e1a0471e5ffb 653 }
mitesh2patel 0:e1a0471e5ffb 654
mitesh2patel 0:e1a0471e5ffb 655 /**
mitesh2patel 0:e1a0471e5ffb 656 * Send bytes to camera module.
mitesh2patel 0:e1a0471e5ffb 657 *
mitesh2patel 0:e1a0471e5ffb 658 * @param buf Pointer to the data buffer.
mitesh2patel 0:e1a0471e5ffb 659 * @param len Length of the data buffer.
mitesh2patel 0:e1a0471e5ffb 660 *
mitesh2patel 0:e1a0471e5ffb 661 * @return True if the data sended.
mitesh2patel 0:e1a0471e5ffb 662 */
mitesh2patel 0:e1a0471e5ffb 663 bool CameraC328::sendBytes(char *buf, size_t len, int timeout_us) {
mitesh2patel 0:e1a0471e5ffb 664 for (uint32_t i = 0; i < (uint32_t)len; i++) {
mitesh2patel 0:e1a0471e5ffb 665 int cnt = 0;
mitesh2patel 0:e1a0471e5ffb 666 while (!serial.writeable()) {
mitesh2patel 0:e1a0471e5ffb 667 wait_us(1);
mitesh2patel 0:e1a0471e5ffb 668 cnt++;
mitesh2patel 0:e1a0471e5ffb 669 if (timeout_us < cnt) {
mitesh2patel 0:e1a0471e5ffb 670 return false;
mitesh2patel 0:e1a0471e5ffb 671 }
mitesh2patel 0:e1a0471e5ffb 672 }
mitesh2patel 0:e1a0471e5ffb 673 serial.putc(buf[i]);
mitesh2patel 0:e1a0471e5ffb 674 }
mitesh2patel 0:e1a0471e5ffb 675 return true;
mitesh2patel 0:e1a0471e5ffb 676 }
mitesh2patel 0:e1a0471e5ffb 677
mitesh2patel 0:e1a0471e5ffb 678 /**
mitesh2patel 0:e1a0471e5ffb 679 * Receive bytes from camera module.
mitesh2patel 0:e1a0471e5ffb 680 *
mitesh2patel 0:e1a0471e5ffb 681 * @param buf Pointer to the data buffer.
mitesh2patel 0:e1a0471e5ffb 682 * @param len Length of the data buffer.
mitesh2patel 0:e1a0471e5ffb 683 *
mitesh2patel 0:e1a0471e5ffb 684 * @return True if the data received.
mitesh2patel 0:e1a0471e5ffb 685 */
mitesh2patel 0:e1a0471e5ffb 686 bool CameraC328::recvBytes(char *buf, size_t len, int timeout_us) {
mitesh2patel 0:e1a0471e5ffb 687 for (uint32_t i = 0; i < (uint32_t)len; i++) {
mitesh2patel 0:e1a0471e5ffb 688 int cnt = 0;
mitesh2patel 0:e1a0471e5ffb 689 while (!serial.readable()) {
mitesh2patel 0:e1a0471e5ffb 690 wait_us(1);
mitesh2patel 0:e1a0471e5ffb 691 cnt++;
mitesh2patel 0:e1a0471e5ffb 692 if (timeout_us < cnt) {
mitesh2patel 0:e1a0471e5ffb 693 return false;
mitesh2patel 0:e1a0471e5ffb 694 }
mitesh2patel 0:e1a0471e5ffb 695 }
mitesh2patel 0:e1a0471e5ffb 696 buf[i] = serial.getc();
mitesh2patel 0:e1a0471e5ffb 697 }
mitesh2patel 0:e1a0471e5ffb 698 return true;
mitesh2patel 0:e1a0471e5ffb 699 }
mitesh2patel 0:e1a0471e5ffb 700
mitesh2patel 0:e1a0471e5ffb 701 /**
mitesh2patel 0:e1a0471e5ffb 702 * Wait received.
mitesh2patel 0:e1a0471e5ffb 703 *
mitesh2patel 0:e1a0471e5ffb 704 * @return True if the data received.
mitesh2patel 0:e1a0471e5ffb 705 */
mitesh2patel 0:e1a0471e5ffb 706 bool CameraC328::waitRecv() {
mitesh2patel 0:e1a0471e5ffb 707 while (!serial.readable()) {
mitesh2patel 0:e1a0471e5ffb 708 }
mitesh2patel 0:e1a0471e5ffb 709 return true;
mitesh2patel 0:e1a0471e5ffb 710 }
mitesh2patel 0:e1a0471e5ffb 711
mitesh2patel 0:e1a0471e5ffb 712 /**
mitesh2patel 0:e1a0471e5ffb 713 * Wait idle state.
mitesh2patel 0:e1a0471e5ffb 714 */
mitesh2patel 0:e1a0471e5ffb 715 bool CameraC328::waitIdle() {
mitesh2patel 0:e1a0471e5ffb 716 while (serial.readable()) {
mitesh2patel 0:e1a0471e5ffb 717 serial.getc();
mitesh2patel 0:e1a0471e5ffb 718 }
mitesh2patel 0:e1a0471e5ffb 719 return true;
mitesh2patel 0:e1a0471e5ffb 720 }