static CameraPacket getPacket(void) { T2CON = 0x0020; // disabled, prescaled, internal oscillator TMR2 = 0; // clear the timer PR2 = CLOCKS_PER_SEC/64/10 - 1; // interrupt at 1kHz (1ms) _T2IE = ON; // enable Timer2 to interrupt _T2IF = OFF; // clear the interrupt flag _T2IP = 4; // set priority to real-time T2CONbits.TON = 1; CameraPacket inPacket = newCameraPacket(); while(cameraComPort.size() < 8) if(!T2CONbits.TON) break; cameraComPort.receive(inPacket.raw,8); return inPacket; }
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 }