static CameraError retrievePic(String imgName) { setPowerOutput(ON); /** * Increasing Power On Period * * Although the data sheet says that the camera needs ~1.5 sec to start-up, * in practice 2 sec makes for a much more reliable situation. */ wait(2000);//1500); // initialize camera and image storage cameraComPort.init(); cameraComPort.baudrate(14400); //imageFile = getFileStream(); imageFile = FSfopen(imgName, "w"); CameraError error = NO_FILE; //if(imageFile.open) if(imageFile) { //FSfclose(FSfopen(imgName, "w")); // erase file //imageFile.open(imgName); // open file error = getPicture(); FSfclose(imageFile); imageFile = NullPtr; //imageFile.close(); } //imageFile.free(); setPowerOutput(OFF); wait(1000); return error; }
static CameraError getPicture(void) { toCam = newCameraPacket(), fromCam = newCameraPacket(); toCam.cmdToken = SYNC; int syncTries = 0; //if(!imageFile.open) if(!imageFile) return NO_FILE; /* try and get sync with camera */ while(!(fromCam.cmdToken == ACK && fromCam.parameter1 == SYNC)) // until ACK received { if(syncTries == 3) cameraComPort.baudrate(115200); cameraComPort.flush(); sendPacket(toCam); // send SYNC command pause(25); fromCam = getPacket(); // get reply if(syncTries < 7) ++syncTries; else return NO_HANDSHAKE; } /* acknowledge camera's sync request */ toCam = newCameraPacket(); // initialize packet toCam.cmdToken = ACK; // acknowledge toCam.parameter1 = SYNC; // the sync syncTries = 0; while(!(fromCam.cmdToken == SYNC)) // wait for camera SYNC request { fromCam = getPacket(); if(syncTries < 7) ++syncTries; else return NO_SYNC; } sendPacket(toCam); /* configure camera */ toCam = newCameraPacket(); // initialize packet toCam.cmdToken = INITIAL; // initial configuration toCam.parameter1 = 0x04; // 115,200 baud rate toCam.parameter2 = 0x87; // compress color toCam.parameter3 = 0x01; // 80x60 preview resolution toCam.parameter4 = 0x07; // 640x480 compression resolution sendPacket(toCam); /* if camera acknowledges changes, change the UART baud rate */ fromCam = getPacket(); if(!(fromCam.cmdToken == ACK && fromCam.parameter1 == INITIAL)) return NO_INITIAL;// if reconfiguration was not successful cameraComPort.baudrate(115200); // change UART baud rate /* specify image quality */ toCam = newCameraPacket(); // initialize packet toCam.cmdToken = QUALITY; // configure picture quality toCam.parameter1 = 0x00; // to be the best sendPacket(toCam); /* if camera acknowledges change, then get an image */ fromCam = getPacket(); if(!(fromCam.cmdToken == ACK && fromCam.parameter1 == QUALITY)) return NO_QUALITY;// if reconfiguration was not successful /* get an image */ toCam = newCameraPacket(); // initialize packet toCam.cmdToken = GET_PIC; // configure picture quality toCam.parameter1 = 0x05; // get full size image sendPacket(toCam); /* if camera acknowledges request, then retrieve image data */ fromCam = getPacket(); if(!(fromCam.cmdToken == ACK && fromCam.parameter1 == GET_PIC)) return NO_GET_PIC; // if request was not successful /* get image size */ fromCam = getPacket(); if(!(fromCam.cmdToken == DATA)) return NO_DATA; // if request was not successful /* read data size */ picsize = bytesToGet = fromCam.parameter2 + fromCam.parameter3 * 0x100LL + fromCam.parameter4 * 0x10000LL; int cnt = 0; /* store the image */ while(bytesToGet > 0) // until all bytes retrieved { tempSize = cameraComPort.receive(tempBuffer, RX_BUFFER_SIZE); // receive the bytes //imageFile.write(tempBuffer,tempSize); // store the bytes FSfwrite(tempBuffer, sizeof(char), tempSize, imageFile); bytesToGet -= tempSize; // update bytes remaining // watch dog counter - bytes are sometimes lost...so loop will hang cnt = tempSize ? 0: cnt + 1; if(cnt > 200) break; } /* acknowledge that data was received */ /* NOT NEEDED */ //toCam = newCameraPacket(); // initialize packet //toCam.cmdToken = ACK; // notify the camera of successful //toCam.parameter1 = DATA; // data retrieval //sendPacket(toCam); pause(30); if(bytesToGet) return LOST_DATA; // not all camera data was gathered else return NO_ERROR; // successful }