diff options
-rw-r--r-- | camera.c | 200 | ||||
-rw-r--r-- | camera.h | 16 |
2 files changed, 181 insertions, 35 deletions
@@ -48,18 +48,18 @@ bool runCommand (Camera *cam, byte cmd, byte args[], int respLen) { return false; if (verifyResponse(cam, cmd)) return false; - else return true; + return true; } // Reads from the camera and returns how many bytes it read -int readResponse (Camera *cam, int nBytes, int timeout) { +int readResponse (Camera *cam, int nBytes, unsigned int timeout) { int counter = 0; - int bufferLen = 0; + cam->bufferLen = 0; // read while below timeout and while the buffer // is still smaller than expected while(timeout >= counter && cam->bufferLen <= nBytes) { ssize_t bytesRead = read(cam->fd, &(cam->buff) + cam->bufferLen, 1); // read one byte at a time - bufferLen++; + cam->bufferLen++; // bytesRead will be 0 or -1 if no data was received if (bytesRead <= 0) { delay(1); @@ -87,6 +87,7 @@ bool cameraFrameBuffCtrl (Camera *cam, byte cmd) { } bool takePicture (Camera *cam) { + cam->frameptr = 0; return cameraFrameBuffCtrl(cam, VC0706_STOPCURRENTFRAME); } @@ -95,7 +96,174 @@ bool reset (Camera *cam) { return runCommand(cam, VC0706_RESET, args, 5); } -size_t bufferToFile (Camera *cam, char *path) { +bool TVon (Camera *cam) { + byte args[] = { 0x1, 0x1 }; + return runCommand(cam, VC0706_TVOUT_CTRL, args, 5); +} + +bool TVOff (Camera *cam) { + byte args[] = { 0x1, 0x0 }; + return runCommand(cam, VC0706_TVOUT_CTRL, args, 5); +} + +byte *readPicture (Camera *cam, byte n) { + byte args[] = { 0x0C, 0x0, 0x0A, + 0, 0, cam->frameptr >> 8, cam->frameptr & 0xFF, + 0, 0, 0, n, + DELAY >> 8, DELAY & 0xFF }; + if (!runCommand(cam, VC0706_READ_FBUF, args, 5)) + return 0; + if (readResponse(cam, n + 5, DELAY) == 0) + return 0; + + cam->frameptr += n; + + return &(cam->buff[0]); +} + +bool resumeVideo (Camera *cam) { + return cameraFrameBuffCtrl(cam, VC0706_RESUMEFRAME); +} +uint32_t frameLength (Camera *cam) { + byte args[] = { 0x01, 0x00 }; + if (!runCommand(cam, VC0706_GET_FBUF_LEN, args, 9)) + return 0; + + uint32_t len; + len = cam->buff[5]; + len <<= 8; + len |= cam->buff[6]; + len <<= 8; + len |= cam->buff[7]; + len <<= 8; + len |= cam->buff[8]; + + return len; +} + +char *getVersion (Camera *cam) { + byte args[] = { 0x01 }; + sendCommand(cam, VC0706_GEN_VERSION, args); + if (!readResponse(cam, BUFF_SIZE, 200)) + return 0; + cam->buff[cam->bufferLen] = 0; + return (char*)&(cam->buff); +} + +byte available (Camera *cam) { + return cam->bufferLen; +} + +byte getDownsize (Camera *cam) { + byte args[] = { 0x0 }; + if (!runCommand(cam, VC0706_DOWNSIZE_STATUS, args, 6)) + return -1; + return cam->buff[5]; +} + +bool setDownsize(Camera *cam, byte newSize) { + byte args[] = { 0x01, newSize }; + return runCommand(cam, VC0706_DOWNSIZE_CTRL, args, 5); +} + +int getImageSize (Camera *cam) { + byte args[] = { 0x4, 0x4, 0x1, 0x00, 0x19 }; + if (!runCommand(cam, VC0706_READ_DATA, args, 6)) + return -1; + return cam->buff[5]; +} + +bool setImageSize (Camera *cam, byte x) { + byte args[] = { 0x05, 0x04, 0x01, 0x00, 0x19, x }; + return runCommand(cam, VC0706_WRITE_DATA, args, 5); +} + +bool getMotionDetect (Camera *cam) { + byte args[] = { 0x0 }; + if (!runCommand(cam, VC0706_COMM_MOTION_STATUS, args, 6)) + return false; + return cam->buff[5]; +} + +byte getMotionStatus(Camera *cam, byte x) { + byte args[] = { 0x01, x }; + return runCommand(cam, VC0706_MOTION_STATUS, args, 5); +} + +bool motionDetected (Camera *cam) { + if (readResponse(cam, 4, 200) != 4) + return false; + if (!verifyResponse(cam, VC0706_COMM_MOTION_DETECTED)) + return false; + return true; +} + +bool setMotionDetect (Camera *cam, bool flag) { + if (!setMotionStatus(cam, VC0706_MOTIONCONTROL, VC0706_UARTMOTION, VC0706_ACTIVATEMOTION)) + return false; + byte args[] = { 0x1, flag }; + return runCommand(cam, VC0706_MOTION_STATUS, args, 5); +} + +bool setMotionStatus (Camera *cam, byte x, byte d1, byte d2) { + byte args[] = { 0x03, x, d1, d2 }; + return runCommand(cam, VC0706_MOTION_CTRL, args, 5); +} + +byte getCompression (Camera *cam) { + byte args[] = { 0x4, 0x1, 0x1, 0x12, 0x04 }; + runCommand(cam, VC0706_READ_DATA, args, 6); + return cam->buff[5]; +} + +bool setCompression (Camera *cam, byte c) { + byte args[] = { 0x5, 0x1, 0x1, 0x12, 0x04, c }; + return runCommand(cam, VC0706_WRITE_DATA, args, 5); +} + +bool getPTZ(Camera *cam, uint16_t *w, uint16_t *h, uint16_t *wz, uint16_t *hz, uint16_t *pan, uint16_t *tilt) { + byte args[] = {0x0}; + + if (!runCommand(cam, VC0706_GET_ZOOM, args, 16)) + return false; + *w = cam->buff[5]; + *w <<= 8; + *w |= cam->buff[6]; + + *h = cam->buff[7]; + *h <<= 8; + *h |= cam->buff[8]; + + *wz = cam->buff[9]; + *wz <<= 8; + *wz |= cam->buff[10]; + + *hz = cam->buff[11]; + *hz <<= 8; + *hz |= cam->buff[12]; + + *pan = cam->buff[13]; + *pan <<= 8; + *pan |= cam->buff[14]; + + *tilt = cam->buff[15]; + *tilt <<= 8; + *tilt |= cam->buff[16]; + + return true; +} + +bool setPTZ(Camera *cam, uint16_t wz, uint16_t hz, uint16_t pan, uint16_t tilt) { + byte args[] = { + 0x08, wz >> 8, wz, + hz >> 8, wz, + pan>>8, pan, + tilt>>8, tilt + }; + return !runCommand(cam, VC0706_SET_ZOOM, args, 5); +} + +size_t camToFile (Camera *cam, char *path) { FILE *photo = fopen(path, "w"); if (photo != NULL) { LE_INFO("File pointer for %s is valid", path); @@ -108,25 +276,3 @@ size_t bufferToFile (Camera *cam, char *path) { return -1; } } - -// TODO implement these -bool TVon (Camera *cam); -bool TVOff (Camera *cam); -byte *readPicture (Camera *cam); -bool resumeVideo (Camera *cam); -uint32_t frameLength (Camera *cam); -char *getVersion (Camera *cam); -byte available (Camera *cam); -byte getDownsize (Camera *cam); -bool setDownsize(Camera *cam, int newSize); -int getImageSize (Camera *cam); -bool setImageSize (Camera *cam, int size); -bool getMotionDetect (Camera *cam); -byte getMotionStatus(Camera *cam, byte x); -bool motionDetected (Camera *cam); -bool setMotionDetect (Camera *cam, bool flag); -bool setMotionStatus (Camera *cam, byte x, byte d1, byte d2); -byte getCompression (Camera *cam); -bool setCompression(Camera *cam, byte c); -bool getPTZ(Camera *cam, uint16_t *w, uint16_t *h, uint16_t *wz, uint16_t *hz, uint16_t *pan, uint16_t *tilt); -bool setPTZ(Camera *cam, uint16_t wz, uint16_t hz, uint16_t pan, uint16_t tilt); @@ -54,33 +54,33 @@ typedef struct { uint16_t frameptr; } Camera; -void delay (unsigned int secs); +void delay (unsigned int msecs); int openCameraFd (); void sendCommand (Camera *cam, byte cmd, byte args[]); bool runCommand (Camera *cam, byte cmd, byte args[], int respLen); -int readResponse (Camera *cam, int nBytes, int timeout); +int readResponse (Camera *cam, int nBytes, unsigned int timeout); bool verifyResponse (Camera *cam, byte cmd); bool cameraFrameBuffCtrl (Camera *cam, byte cmd); bool takePicture (Camera *cam); -size_t bufferToFile (Camera *cam, char *path); bool reset (Camera *cam); bool TVon (Camera *cam); bool TVOff (Camera *cam); -byte *readPicture (Camera *cam); +byte *readPicture (Camera *cam, byte n); bool resumeVideo (Camera *cam); uint32_t frameLength (Camera *cam); char *getVersion (Camera *cam); byte available (Camera *cam); byte getDownsize (Camera *cam); -bool setDownsize(Camera *cam, int newSize); +bool setDownsize(Camera *cam, byte newSize); int getImageSize (Camera *cam); -bool setImageSize (Camera *cam, int size); +bool setImageSize (Camera *cam, byte x); bool getMotionDetect (Camera *cam); byte getMotionStatus(Camera *cam, byte x); bool motionDetected (Camera *cam); -bool setMotionDetect (bool flag); -bool setMotionStatus (byte x, byte d1, byte d2); +bool setMotionDetect (Camera *cam, bool flag); +bool setMotionStatus (Camera *cam, byte x, byte d1, byte d2); byte getCompression (Camera *cam); bool setCompression(Camera *cam, byte c); bool getPTZ(Camera *cam, uint16_t *w, uint16_t *h, uint16_t *wz, uint16_t *hz, uint16_t *pan, uint16_t *tilt); bool setPTZ(Camera *cam, uint16_t wz, uint16_t hz, uint16_t pan, uint16_t tilt); +size_t camToFile (Camera *cam, char *path); |