test

Fork of CameraC1098 by Tadao Iida

Committer:
hepta2ume
Date:
Thu Aug 03 04:28:14 2017 +0000
Revision:
1:135493341acc
Parent:
0:5a6468b4164d
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sunifu 0:5a6468b4164d 1 #include "CameraC1098.h"
sunifu 0:5a6468b4164d 2
sunifu 0:5a6468b4164d 3 #define WAITIDLE waitIdle
sunifu 0:5a6468b4164d 4 #define SENDFUNC sendBytes
sunifu 0:5a6468b4164d 5 #define RECVFUNC recvBytes
sunifu 0:5a6468b4164d 6 #define WAITFUNC waitRecv
hepta2ume 1:135493341acc 7 int num=0;
hepta2ume 1:135493341acc 8 FILE *fp_jpeg;
sunifu 0:5a6468b4164d 9
hepta2ume 1:135493341acc 10 CameraC1098::CameraC1098(PinName tx, PinName rx, int baud) : serial(tx, rx)
hepta2ume 1:135493341acc 11 {
hepta2ume 1:135493341acc 12 serial._baud(baud);
sunifu 0:5a6468b4164d 13 }
sunifu 0:5a6468b4164d 14
hepta2ume 1:135493341acc 15 CameraC1098::~CameraC1098()
hepta2ume 1:135493341acc 16 {
sunifu 0:5a6468b4164d 17 }
sunifu 0:5a6468b4164d 18
hepta2ume 1:135493341acc 19 CameraC1098::ErrorNumber CameraC1098::sync()
hepta2ume 1:135493341acc 20 {
sunifu 0:5a6468b4164d 21 for (int i = 0; i < SYNCMAX; i++) {
sunifu 0:5a6468b4164d 22 if (NoError == sendSync()) {
sunifu 0:5a6468b4164d 23 if (NoError == recvAckOrNck()) {
sunifu 0:5a6468b4164d 24 if (NoError == recvSync()) {
sunifu 0:5a6468b4164d 25 if (NoError == sendAck(0x0D, 0x00)) {
hepta2ume 1:135493341acc 26
sunifu 0:5a6468b4164d 27 wait(2.0);
sunifu 0:5a6468b4164d 28 return NoError;
sunifu 0:5a6468b4164d 29 }
sunifu 0:5a6468b4164d 30 }
sunifu 0:5a6468b4164d 31 }
sunifu 0:5a6468b4164d 32 }
sunifu 0:5a6468b4164d 33 wait_ms(50);
sunifu 0:5a6468b4164d 34 }
sunifu 0:5a6468b4164d 35 return UnexpectedReply;
sunifu 0:5a6468b4164d 36 }
sunifu 0:5a6468b4164d 37
hepta2ume 1:135493341acc 38 CameraC1098::ErrorNumber CameraC1098::init(Baud baud,JpegResolution jr)
hepta2ume 1:135493341acc 39 {
sunifu 0:5a6468b4164d 40 int i ;
sunifu 0:5a6468b4164d 41 ErrorNumber en;
sunifu 0:5a6468b4164d 42 WAITIDLE();
sunifu 0:5a6468b4164d 43 setmbedBaud((Baud)(0x07)) ;
hepta2ume 1:135493341acc 44
sunifu 0:5a6468b4164d 45 for ( i = 1 ; i < 7 ; i++ ) {
sunifu 0:5a6468b4164d 46 if ( NoError == sendSync() ) {
hepta2ume 1:135493341acc 47 if ( NoError == recvAckOrNck() ) {
sunifu 0:5a6468b4164d 48 if ( NoError == recvSync() ) {
sunifu 0:5a6468b4164d 49 if ( NoError == sendAck(0x0D, 0x00) ) {
sunifu 0:5a6468b4164d 50 en = sendInitial(baud,jr);
sunifu 0:5a6468b4164d 51 if (NoError != en) {
sunifu 0:5a6468b4164d 52 return en;
hepta2ume 1:135493341acc 53 }
sunifu 0:5a6468b4164d 54 en = recvAckOrNck();
sunifu 0:5a6468b4164d 55 if (NoError != en) {
sunifu 0:5a6468b4164d 56 return en;
hepta2ume 1:135493341acc 57 }
sunifu 0:5a6468b4164d 58 wait_ms(50) ;
hepta2ume 1:135493341acc 59 setmbedBaud(baud);
hepta2ume 1:135493341acc 60 //wait_ms(50) ;
sunifu 0:5a6468b4164d 61 static bool alreadySetupPackageSize = false;
sunifu 0:5a6468b4164d 62 if (!alreadySetupPackageSize) {
sunifu 0:5a6468b4164d 63 en = sendSetPackageSize(packageSize);
sunifu 0:5a6468b4164d 64 if (NoError != en) {
sunifu 0:5a6468b4164d 65 return en;
sunifu 0:5a6468b4164d 66 }
sunifu 0:5a6468b4164d 67 WAITFUNC();
sunifu 0:5a6468b4164d 68 en = recvAckOrNck();
sunifu 0:5a6468b4164d 69 if (NoError != en) {
sunifu 0:5a6468b4164d 70 return en;
sunifu 0:5a6468b4164d 71 }
sunifu 0:5a6468b4164d 72 alreadySetupPackageSize = true;
sunifu 0:5a6468b4164d 73 }
hepta2ume 1:135493341acc 74
sunifu 0:5a6468b4164d 75 wait(2.0);
hepta2ume 1:135493341acc 76 return (ErrorNumber)NoError;
hepta2ume 1:135493341acc 77
sunifu 0:5a6468b4164d 78 }
hepta2ume 1:135493341acc 79 }
hepta2ume 1:135493341acc 80 } else {
hepta2ume 1:135493341acc 81 setmbedBaud((Baud)(i+1)) ;
sunifu 0:5a6468b4164d 82 }
sunifu 0:5a6468b4164d 83 }
hepta2ume 1:135493341acc 84 wait_ms(50);
hepta2ume 1:135493341acc 85 }
sunifu 0:5a6468b4164d 86 return UnexpectedReply;
sunifu 0:5a6468b4164d 87 }
sunifu 0:5a6468b4164d 88
sunifu 0:5a6468b4164d 89
hepta2ume 1:135493341acc 90 CameraC1098::ErrorNumber CameraC1098::getJpegSnapshotPicture()
hepta2ume 1:135493341acc 91 {
sunifu 0:5a6468b4164d 92 WAITIDLE();
sunifu 0:5a6468b4164d 93 ErrorNumber en;
sunifu 0:5a6468b4164d 94
sunifu 0:5a6468b4164d 95
sunifu 0:5a6468b4164d 96 en = sendSnapshot();
sunifu 0:5a6468b4164d 97 if (NoError != en) {
sunifu 0:5a6468b4164d 98 return en;
sunifu 0:5a6468b4164d 99 }
sunifu 0:5a6468b4164d 100 WAITFUNC();
sunifu 0:5a6468b4164d 101 en = recvAckOrNck();
sunifu 0:5a6468b4164d 102 if (NoError != en) {
sunifu 0:5a6468b4164d 103 return en;
sunifu 0:5a6468b4164d 104 }
sunifu 0:5a6468b4164d 105
sunifu 0:5a6468b4164d 106 en = sendGetPicture();
sunifu 0:5a6468b4164d 107 if (NoError != en) {
sunifu 0:5a6468b4164d 108 return en;
sunifu 0:5a6468b4164d 109 }
sunifu 0:5a6468b4164d 110 WAITFUNC();
sunifu 0:5a6468b4164d 111 en = recvAckOrNck();
sunifu 0:5a6468b4164d 112 if (NoError != en) {
sunifu 0:5a6468b4164d 113 return en;
sunifu 0:5a6468b4164d 114 }
sunifu 0:5a6468b4164d 115
sunifu 0:5a6468b4164d 116 /*
sunifu 0:5a6468b4164d 117 * Data : snapshot picture
sunifu 0:5a6468b4164d 118 */
sunifu 0:5a6468b4164d 119 uint32_t length = 0;
sunifu 0:5a6468b4164d 120 WAITFUNC();
sunifu 0:5a6468b4164d 121 en = recvData(&length);
sunifu 0:5a6468b4164d 122 if (NoError != en) {
sunifu 0:5a6468b4164d 123 return en;
sunifu 0:5a6468b4164d 124 }
sunifu 0:5a6468b4164d 125 en = sendAck(0x00, 0);
sunifu 0:5a6468b4164d 126 if (NoError != en) {
sunifu 0:5a6468b4164d 127 return en;
sunifu 0:5a6468b4164d 128 }
sunifu 0:5a6468b4164d 129
sunifu 0:5a6468b4164d 130 char databuf[packageSize - 6];
sunifu 0:5a6468b4164d 131 uint16_t pkg_total = length / (packageSize - 6);
sunifu 0:5a6468b4164d 132 for (int i = 0; i <= (int)pkg_total; i++) {
sunifu 0:5a6468b4164d 133 uint16_t checksum = 0;
sunifu 0:5a6468b4164d 134 // ID.
sunifu 0:5a6468b4164d 135 char idbuf[2];
sunifu 0:5a6468b4164d 136 WAITFUNC();
sunifu 0:5a6468b4164d 137 if (!RECVFUNC(idbuf, sizeof(idbuf))) {
sunifu 0:5a6468b4164d 138 return (ErrorNumber)UnexpectedReply;
sunifu 0:5a6468b4164d 139 }
sunifu 0:5a6468b4164d 140 checksum += idbuf[0];
sunifu 0:5a6468b4164d 141 checksum += idbuf[1];
sunifu 0:5a6468b4164d 142 uint16_t id = (idbuf[1] << 8) | (idbuf[0] << 0);
sunifu 0:5a6468b4164d 143 if (id != i) {
sunifu 0:5a6468b4164d 144 return (ErrorNumber)UnexpectedReply;
sunifu 0:5a6468b4164d 145 }
sunifu 0:5a6468b4164d 146
sunifu 0:5a6468b4164d 147 // Size of the data.
sunifu 0:5a6468b4164d 148 char dsbuf[2];
sunifu 0:5a6468b4164d 149 WAITFUNC();
sunifu 0:5a6468b4164d 150 if (!RECVFUNC(dsbuf, sizeof(dsbuf))) {
sunifu 0:5a6468b4164d 151 return (ErrorNumber)UnexpectedReply;
sunifu 0:5a6468b4164d 152 }
sunifu 0:5a6468b4164d 153
sunifu 0:5a6468b4164d 154 // Received the data.
sunifu 0:5a6468b4164d 155 checksum += dsbuf[0];
sunifu 0:5a6468b4164d 156 checksum += dsbuf[1];
sunifu 0:5a6468b4164d 157 uint16_t ds = (dsbuf[1] << 8) | (dsbuf[0] << 0);
sunifu 0:5a6468b4164d 158 WAITFUNC();
sunifu 0:5a6468b4164d 159 if (!RECVFUNC(&databuf[0], ds)) {
sunifu 0:5a6468b4164d 160 return (ErrorNumber)UnexpectedReply;
sunifu 0:5a6468b4164d 161 }
sunifu 0:5a6468b4164d 162 for (int j = 0; j < ds; j++) {
sunifu 0:5a6468b4164d 163 checksum += databuf[j];
sunifu 0:5a6468b4164d 164 }
sunifu 0:5a6468b4164d 165
sunifu 0:5a6468b4164d 166 // Verify code.
sunifu 0:5a6468b4164d 167 char vcbuf[2];
sunifu 0:5a6468b4164d 168 WAITFUNC();
sunifu 0:5a6468b4164d 169 if (!RECVFUNC(vcbuf, sizeof(vcbuf))) {
sunifu 0:5a6468b4164d 170 return (ErrorNumber)UnexpectedReply;
sunifu 0:5a6468b4164d 171 }
sunifu 0:5a6468b4164d 172 uint16_t vc = (vcbuf[1] << 8) | (vcbuf[0] << 0);
sunifu 0:5a6468b4164d 173 if (vc != (checksum & 0xff)) {
sunifu 0:5a6468b4164d 174 return (ErrorNumber)UnexpectedReply;
sunifu 0:5a6468b4164d 175 }
sunifu 0:5a6468b4164d 176
sunifu 0:5a6468b4164d 177 /*
sunifu 0:5a6468b4164d 178 * Call a call back function.
sunifu 0:5a6468b4164d 179 * You can block this function while working.
sunifu 0:5a6468b4164d 180 */
hepta2ume 1:135493341acc 181 size_t siz = ds;
hepta2ume 1:135493341acc 182 for (int ii = 0; ii < (int)siz; ii++) {
hepta2ume 1:135493341acc 183 fprintf(fp_jpeg,"%02x ", databuf[ii]);
hepta2ume 1:135493341acc 184 if(++num%16==0) fprintf(fp_jpeg,"\r\n");
hepta2ume 1:135493341acc 185 }
sunifu 0:5a6468b4164d 186 /*
sunifu 0:5a6468b4164d 187 * We should wait for camera working before reply a ACK.
sunifu 0:5a6468b4164d 188 */
sunifu 0:5a6468b4164d 189 wait_ms(100);
sunifu 0:5a6468b4164d 190 en = sendAck(0x00, 1 + i);
sunifu 0:5a6468b4164d 191 if (NoError != en) {
sunifu 0:5a6468b4164d 192 return en;
sunifu 0:5a6468b4164d 193 }
sunifu 0:5a6468b4164d 194 }
sunifu 0:5a6468b4164d 195
sunifu 0:5a6468b4164d 196 return (ErrorNumber)NoError;
sunifu 0:5a6468b4164d 197 }
sunifu 0:5a6468b4164d 198
sunifu 0:5a6468b4164d 199
sunifu 0:5a6468b4164d 200
hepta2ume 1:135493341acc 201 CameraC1098::ErrorNumber CameraC1098::sendInitial(Baud baud, JpegResolution jr)
hepta2ume 1:135493341acc 202 {
sunifu 0:5a6468b4164d 203 char send[COMMAND_LENGTH];
hepta2ume 1:135493341acc 204
sunifu 0:5a6468b4164d 205 send[0] = 0xAA;
sunifu 0:5a6468b4164d 206 send[1] = 0x01;
sunifu 0:5a6468b4164d 207 send[2] = (char)baud;
sunifu 0:5a6468b4164d 208 send[3] = 0x07;
sunifu 0:5a6468b4164d 209 send[4] = 0x00;
sunifu 0:5a6468b4164d 210 send[5] = (char)jr;
sunifu 0:5a6468b4164d 211
sunifu 0:5a6468b4164d 212 if (!SENDFUNC(send, sizeof(send))) {
sunifu 0:5a6468b4164d 213 return (ErrorNumber)SendRegisterTimeout;
sunifu 0:5a6468b4164d 214 }
sunifu 0:5a6468b4164d 215
sunifu 0:5a6468b4164d 216 return (ErrorNumber)NoError;
sunifu 0:5a6468b4164d 217 }
sunifu 0:5a6468b4164d 218
hepta2ume 1:135493341acc 219 CameraC1098::ErrorNumber CameraC1098::sendGetPicture(void)
hepta2ume 1:135493341acc 220 {
sunifu 0:5a6468b4164d 221 char send[COMMAND_LENGTH];
sunifu 0:5a6468b4164d 222
sunifu 0:5a6468b4164d 223 send[0] = 0xAA;
sunifu 0:5a6468b4164d 224 send[1] = 0x04;
sunifu 0:5a6468b4164d 225 send[2] = 0x01;
sunifu 0:5a6468b4164d 226 send[3] = 0x00;
sunifu 0:5a6468b4164d 227 send[4] = 0x00;
sunifu 0:5a6468b4164d 228 send[5] = 0x00;
sunifu 0:5a6468b4164d 229
sunifu 0:5a6468b4164d 230 if (!SENDFUNC(send, sizeof(send))) {
sunifu 0:5a6468b4164d 231 return (ErrorNumber)SendRegisterTimeout;
sunifu 0:5a6468b4164d 232 }
sunifu 0:5a6468b4164d 233 return (ErrorNumber)NoError;
sunifu 0:5a6468b4164d 234 }
sunifu 0:5a6468b4164d 235
hepta2ume 1:135493341acc 236 CameraC1098::ErrorNumber CameraC1098::sendSnapshot(void)
hepta2ume 1:135493341acc 237 {
sunifu 0:5a6468b4164d 238 char send[COMMAND_LENGTH];
sunifu 0:5a6468b4164d 239 send[0] = 0xAA;
sunifu 0:5a6468b4164d 240 send[1] = 0x05;
sunifu 0:5a6468b4164d 241 send[2] = 0x00;
sunifu 0:5a6468b4164d 242 send[3] = 0x00;
sunifu 0:5a6468b4164d 243 send[4] = 0x00;
sunifu 0:5a6468b4164d 244 send[5] = 0x00;
sunifu 0:5a6468b4164d 245
sunifu 0:5a6468b4164d 246 if (!SENDFUNC(send, sizeof(send))) {
sunifu 0:5a6468b4164d 247 return (ErrorNumber)SendRegisterTimeout;
sunifu 0:5a6468b4164d 248 }
sunifu 0:5a6468b4164d 249 return (ErrorNumber)NoError;
sunifu 0:5a6468b4164d 250 }
sunifu 0:5a6468b4164d 251
hepta2ume 1:135493341acc 252 CameraC1098::ErrorNumber CameraC1098::sendSetPackageSize(uint16_t packageSize)
hepta2ume 1:135493341acc 253 {
sunifu 0:5a6468b4164d 254 char send[COMMAND_LENGTH];
sunifu 0:5a6468b4164d 255 send[0] = 0xAA;
sunifu 0:5a6468b4164d 256 send[1] = 0x06;
sunifu 0:5a6468b4164d 257 send[2] = 0x08;
sunifu 0:5a6468b4164d 258 send[3] = (packageSize >> 0) & 0xff;
sunifu 0:5a6468b4164d 259 send[4] = (packageSize >> 8) & 0xff;
sunifu 0:5a6468b4164d 260 send[5] = 0x00;
sunifu 0:5a6468b4164d 261
sunifu 0:5a6468b4164d 262 if (!SENDFUNC(send, sizeof(send))) {
sunifu 0:5a6468b4164d 263 return (ErrorNumber)SendRegisterTimeout;
sunifu 0:5a6468b4164d 264 }
sunifu 0:5a6468b4164d 265 return (ErrorNumber)NoError;
sunifu 0:5a6468b4164d 266 }
sunifu 0:5a6468b4164d 267
hepta2ume 1:135493341acc 268 void CameraC1098::setmbedBaud(Baud baud)
hepta2ume 1:135493341acc 269 {
hepta2ume 1:135493341acc 270 switch((int)baud) {
hepta2ume 1:135493341acc 271 case 0x02:
hepta2ume 1:135493341acc 272 serial._baud(460800);
hepta2ume 1:135493341acc 273 break;
hepta2ume 1:135493341acc 274 case 0x03:
hepta2ume 1:135493341acc 275 serial._baud(230400);
hepta2ume 1:135493341acc 276 break;
hepta2ume 1:135493341acc 277 case 0x04:
hepta2ume 1:135493341acc 278 serial._baud(115200);
hepta2ume 1:135493341acc 279 break;
hepta2ume 1:135493341acc 280 case 0x05:
hepta2ume 1:135493341acc 281 serial._baud(57600);
hepta2ume 1:135493341acc 282 break;
hepta2ume 1:135493341acc 283 case 0x06:
hepta2ume 1:135493341acc 284 serial._baud((int)28800);
hepta2ume 1:135493341acc 285 break;
hepta2ume 1:135493341acc 286 case 0x07:
hepta2ume 1:135493341acc 287 serial._baud(14400);
hepta2ume 1:135493341acc 288 break;
hepta2ume 1:135493341acc 289 default:
hepta2ume 1:135493341acc 290 serial._baud(14400);
sunifu 0:5a6468b4164d 291 }
sunifu 0:5a6468b4164d 292 }
sunifu 0:5a6468b4164d 293
sunifu 0:5a6468b4164d 294
hepta2ume 1:135493341acc 295 CameraC1098::ErrorNumber CameraC1098::sendReset(ResetType specialReset)
hepta2ume 1:135493341acc 296 {
sunifu 0:5a6468b4164d 297 char send[COMMAND_LENGTH];
sunifu 0:5a6468b4164d 298 send[0] = 0xAA;
sunifu 0:5a6468b4164d 299 send[1] = 0x08;
sunifu 0:5a6468b4164d 300 send[2] = 0x00;
sunifu 0:5a6468b4164d 301 send[3] = 0x00;
sunifu 0:5a6468b4164d 302 send[4] = 0x00;
sunifu 0:5a6468b4164d 303 send[5] = specialReset;
sunifu 0:5a6468b4164d 304 /*
sunifu 0:5a6468b4164d 305 * Special reset : If the parameter is 0xFF, the command is a special Reset command and the firmware responds to it immediately.
sunifu 0:5a6468b4164d 306 */
sunifu 0:5a6468b4164d 307
sunifu 0:5a6468b4164d 308 if (!SENDFUNC(send, sizeof(send))) {
sunifu 0:5a6468b4164d 309 return (ErrorNumber)SendRegisterTimeout;
sunifu 0:5a6468b4164d 310 }
sunifu 0:5a6468b4164d 311
sunifu 0:5a6468b4164d 312 return (ErrorNumber)NoError;
sunifu 0:5a6468b4164d 313 }
sunifu 0:5a6468b4164d 314
sunifu 0:5a6468b4164d 315
hepta2ume 1:135493341acc 316 CameraC1098::ErrorNumber CameraC1098::recvData(uint32_t *length)
hepta2ume 1:135493341acc 317 {
sunifu 0:5a6468b4164d 318 char recv[COMMAND_LENGTH];
sunifu 0:5a6468b4164d 319 if (!RECVFUNC(recv, sizeof(recv))) {
sunifu 0:5a6468b4164d 320 return (ErrorNumber)UnexpectedReply;
sunifu 0:5a6468b4164d 321 }
sunifu 0:5a6468b4164d 322 if ((0xAA != recv[0]) || (0x0A != recv[1])) {
sunifu 0:5a6468b4164d 323 return (ErrorNumber)UnexpectedReply;
sunifu 0:5a6468b4164d 324 }
sunifu 0:5a6468b4164d 325 recv[2] = (char)0x01;
sunifu 0:5a6468b4164d 326 *length = (recv[5] << 16) | (recv[4] << 8) | (recv[3] << 0);
sunifu 0:5a6468b4164d 327 return (ErrorNumber)NoError;
sunifu 0:5a6468b4164d 328 }
sunifu 0:5a6468b4164d 329
hepta2ume 1:135493341acc 330 CameraC1098::ErrorNumber CameraC1098::sendSync()
hepta2ume 1:135493341acc 331 {
sunifu 0:5a6468b4164d 332 char send[COMMAND_LENGTH];
sunifu 0:5a6468b4164d 333 send[0] = 0xAA;
sunifu 0:5a6468b4164d 334 send[1] = 0x0D;
sunifu 0:5a6468b4164d 335 send[2] = 0x00;
sunifu 0:5a6468b4164d 336 send[3] = 0x00;
sunifu 0:5a6468b4164d 337 send[4] = 0x00;
sunifu 0:5a6468b4164d 338 send[5] = 0x00;
hepta2ume 1:135493341acc 339
hepta2ume 1:135493341acc 340
sunifu 0:5a6468b4164d 341 if (!SENDFUNC(send, sizeof(send))) {
sunifu 0:5a6468b4164d 342 return (ErrorNumber)SendRegisterTimeout;
sunifu 0:5a6468b4164d 343 }
sunifu 0:5a6468b4164d 344 return (ErrorNumber)NoError;
sunifu 0:5a6468b4164d 345 }
sunifu 0:5a6468b4164d 346
hepta2ume 1:135493341acc 347 CameraC1098::ErrorNumber CameraC1098::recvSync()
hepta2ume 1:135493341acc 348 {
sunifu 0:5a6468b4164d 349 char recv[COMMAND_LENGTH];
hepta2ume 1:135493341acc 350
sunifu 0:5a6468b4164d 351 if (!RECVFUNC(recv, sizeof(recv))) {
sunifu 0:5a6468b4164d 352 return (ErrorNumber)UnexpectedReply;
sunifu 0:5a6468b4164d 353 }
sunifu 0:5a6468b4164d 354 if ((0xAA != recv[0]) || (0x0D != recv[1])) {
sunifu 0:5a6468b4164d 355 return (ErrorNumber)UnexpectedReply;
sunifu 0:5a6468b4164d 356 }
sunifu 0:5a6468b4164d 357 return (ErrorNumber)NoError;
sunifu 0:5a6468b4164d 358 }
sunifu 0:5a6468b4164d 359
hepta2ume 1:135493341acc 360 CameraC1098::ErrorNumber CameraC1098::sendAck(uint8_t commandId, uint16_t packageId)
hepta2ume 1:135493341acc 361 {
sunifu 0:5a6468b4164d 362 char send[COMMAND_LENGTH];
sunifu 0:5a6468b4164d 363
sunifu 0:5a6468b4164d 364 send[0] = 0xAA;
sunifu 0:5a6468b4164d 365 send[1] = 0x0E;
sunifu 0:5a6468b4164d 366 send[2] = commandId;
sunifu 0:5a6468b4164d 367 send[3] = 0x00; // ACK counter is not used.
sunifu 0:5a6468b4164d 368 send[4] = (packageId >> 0) & 0xff;
sunifu 0:5a6468b4164d 369 send[5] = (packageId >> 8) & 0xff;
sunifu 0:5a6468b4164d 370 if (!SENDFUNC(send, sizeof(send))) {
sunifu 0:5a6468b4164d 371 return (ErrorNumber)SendRegisterTimeout;
sunifu 0:5a6468b4164d 372 }
sunifu 0:5a6468b4164d 373 return (ErrorNumber)NoError;
sunifu 0:5a6468b4164d 374 }
sunifu 0:5a6468b4164d 375
hepta2ume 1:135493341acc 376 CameraC1098::ErrorNumber CameraC1098::recvAckOrNck()
hepta2ume 1:135493341acc 377 {
sunifu 0:5a6468b4164d 378 char recv[COMMAND_LENGTH];
hepta2ume 1:135493341acc 379
sunifu 0:5a6468b4164d 380 if (!RECVFUNC(recv, sizeof(recv))) {
sunifu 0:5a6468b4164d 381 return (ErrorNumber)UnexpectedReply;
sunifu 0:5a6468b4164d 382 }
sunifu 0:5a6468b4164d 383 if ((0xAA == recv[0]) && (0x0E == recv[1])) {
sunifu 0:5a6468b4164d 384 return (ErrorNumber)NoError;
sunifu 0:5a6468b4164d 385 }
sunifu 0:5a6468b4164d 386 if ((0xAA == recv[0]) && (0x0F == recv[1]) && (0x00 == recv[2])) {
sunifu 0:5a6468b4164d 387 return (ErrorNumber)NoError;
hepta2ume 1:135493341acc 388 }
sunifu 0:5a6468b4164d 389 if ((0xAA == recv[0]) && (0x0F == recv[1])) {
sunifu 0:5a6468b4164d 390 return (ErrorNumber)recv[4];
sunifu 0:5a6468b4164d 391 }
sunifu 0:5a6468b4164d 392 return (ErrorNumber)UnexpectedReply;
sunifu 0:5a6468b4164d 393 }
sunifu 0:5a6468b4164d 394
hepta2ume 1:135493341acc 395 bool CameraC1098::sendBytes(char *buf, size_t len, int timeout_us)
hepta2ume 1:135493341acc 396 {
sunifu 0:5a6468b4164d 397 for (uint32_t i = 0; i < (uint32_t)len; i++) {
sunifu 0:5a6468b4164d 398 int cnt = 0;
sunifu 0:5a6468b4164d 399 while (!serial.writeable()) {
sunifu 0:5a6468b4164d 400 wait_us(1);
sunifu 0:5a6468b4164d 401 cnt++;
sunifu 0:5a6468b4164d 402 if (timeout_us < cnt) {
sunifu 0:5a6468b4164d 403 return false;
sunifu 0:5a6468b4164d 404 }
sunifu 0:5a6468b4164d 405 }
sunifu 0:5a6468b4164d 406 serial.putc(buf[i]);
sunifu 0:5a6468b4164d 407 }
sunifu 0:5a6468b4164d 408 return true;
sunifu 0:5a6468b4164d 409 }
sunifu 0:5a6468b4164d 410
hepta2ume 1:135493341acc 411 bool CameraC1098::recvBytes(char *buf, size_t len, int timeout_us)
hepta2ume 1:135493341acc 412 {
sunifu 0:5a6468b4164d 413 for (uint32_t i = 0; i < (uint32_t)len; i++) {
sunifu 0:5a6468b4164d 414 int cnt = 0;
hepta2ume 1:135493341acc 415 while (!serial.rreadable()) {
sunifu 0:5a6468b4164d 416 wait_us(1);
sunifu 0:5a6468b4164d 417 cnt++;
sunifu 0:5a6468b4164d 418 if (timeout_us < cnt) {
sunifu 0:5a6468b4164d 419 return false;
sunifu 0:5a6468b4164d 420 }
sunifu 0:5a6468b4164d 421 }
hepta2ume 1:135493341acc 422 buf[i] = serial.ggetc();
hepta2ume 1:135493341acc 423 }
hepta2ume 1:135493341acc 424 return true;
hepta2ume 1:135493341acc 425 }
hepta2ume 1:135493341acc 426
hepta2ume 1:135493341acc 427 bool CameraC1098::waitRecv()
hepta2ume 1:135493341acc 428 {
hepta2ume 1:135493341acc 429 while (!serial.rreadable()) {
hepta2ume 1:135493341acc 430 }
hepta2ume 1:135493341acc 431 return true;
hepta2ume 1:135493341acc 432 }
hepta2ume 1:135493341acc 433
hepta2ume 1:135493341acc 434 bool CameraC1098::waitIdle()
hepta2ume 1:135493341acc 435 {
hepta2ume 1:135493341acc 436 while (serial.rreadable()) {
hepta2ume 1:135493341acc 437 serial.ggetc();
sunifu 0:5a6468b4164d 438 }
sunifu 0:5a6468b4164d 439 return true;
sunifu 0:5a6468b4164d 440 }
sunifu 0:5a6468b4164d 441
hepta2ume 1:135493341acc 442 void CameraC1098::Sync(void)
hepta2ume 1:135493341acc 443 {
hepta2ume 1:135493341acc 444 CameraC1098::ErrorNumber err = CameraC1098::NoError;
hepta2ume 1:135493341acc 445
hepta2ume 1:135493341acc 446 err = sync();
hepta2ume 1:135493341acc 447 int count=0;
hepta2ume 1:135493341acc 448 while(err) {
hepta2ume 1:135493341acc 449 printf("count = %d\r\n",count);
hepta2ume 1:135493341acc 450 switch(count) {
hepta2ume 1:135493341acc 451 case 0:
hepta2ume 1:135493341acc 452 setmbedBaud(CameraC1098::Baud14400);
hepta2ume 1:135493341acc 453 break;
hepta2ume 1:135493341acc 454 case 1:
hepta2ume 1:135493341acc 455 setmbedBaud(CameraC1098::Baud115200);
hepta2ume 1:135493341acc 456 break;
hepta2ume 1:135493341acc 457 default:
hepta2ume 1:135493341acc 458 count=0;
hepta2ume 1:135493341acc 459 }
hepta2ume 1:135493341acc 460 count++;
hepta2ume 1:135493341acc 461 err = sync();
hepta2ume 1:135493341acc 462 printf("init err1=%d\r\n", err);
hepta2ume 1:135493341acc 463 if(!err) {
hepta2ume 1:135493341acc 464 printf("to 115200\r\n");
hepta2ume 1:135493341acc 465 init(CameraC1098::Baud115200, CameraC1098::JpegResolution320x240);
hepta2ume 1:135493341acc 466 printf("init err2=%d\r\n", err);
hepta2ume 1:135493341acc 467 setmbedBaud(CameraC1098::Baud115200);
hepta2ume 1:135493341acc 468 printf("init err3=%d\r\n", err);
hepta2ume 1:135493341acc 469 }
sunifu 0:5a6468b4164d 470 }
sunifu 0:5a6468b4164d 471 }
sunifu 0:5a6468b4164d 472
hepta2ume 1:135493341acc 473 void CameraC1098::jpeg_snapshot(int CAPTURE_FRAMES)
hepta2ume 1:135493341acc 474 {
hepta2ume 1:135493341acc 475 for (int i = 0; i < CAPTURE_FRAMES; i++) {
hepta2ume 1:135493341acc 476 char fname[64];
hepta2ume 1:135493341acc 477 snprintf(fname, sizeof(fname), "/fs/test%d.txt",i);
hepta2ume 1:135493341acc 478 fp_jpeg = fopen(fname, "w");
hepta2ume 1:135493341acc 479 getJpegSnapshotPicture();
hepta2ume 1:135493341acc 480 fclose(fp_jpeg);
sunifu 0:5a6468b4164d 481 }
hepta2ume 1:135493341acc 482 }