QC Control software

Dependencies:   mbed

Fork of dgps by Colin Stearns

Committer:
dylanembed123
Date:
Mon May 05 13:20:35 2014 +0000
Revision:
66:5d43988d100c
Parent:
14:6be57da62283
Final Project;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dylanembed123 7:c75d5e5e6bfc 1 /***************************************************
dylanembed123 7:c75d5e5e6bfc 2 This is a library for the Adafruit TTL JPEG Camera (VC0706 chipset)
dylanembed123 7:c75d5e5e6bfc 3
dylanembed123 7:c75d5e5e6bfc 4 Pick one up today in the adafruit shop!
dylanembed123 7:c75d5e5e6bfc 5 ------> http://www.adafruit.com/products/397
dylanembed123 7:c75d5e5e6bfc 6
dylanembed123 7:c75d5e5e6bfc 7 These displays use Serial to communicate, 2 pins are required to interface
dylanembed123 7:c75d5e5e6bfc 8
dylanembed123 7:c75d5e5e6bfc 9 Adafruit invests time and resources providing this open source code,
dylanembed123 7:c75d5e5e6bfc 10 please support Adafruit and open-source hardware by purchasing
dylanembed123 7:c75d5e5e6bfc 11 products from Adafruit!
dylanembed123 7:c75d5e5e6bfc 12
dylanembed123 7:c75d5e5e6bfc 13 Written by Limor Fried/Ladyada for Adafruit Industries.
dylanembed123 7:c75d5e5e6bfc 14 BSD license, all text above must be included in any redistribution
dylanembed123 7:c75d5e5e6bfc 15 ****************************************************/
dylanembed123 7:c75d5e5e6bfc 16
dylanembed123 7:c75d5e5e6bfc 17
dylanembed123 7:c75d5e5e6bfc 18 #include "camera.h"
dylanembed123 7:c75d5e5e6bfc 19
dylanembed123 7:c75d5e5e6bfc 20 Serial* Camera::camera=NULL;
dylanembed123 7:c75d5e5e6bfc 21 uint8_t Camera::serialNum=0;
dylanembed123 7:c75d5e5e6bfc 22
dylanembed123 7:c75d5e5e6bfc 23 Serial& Camera::getSerial(){
dylanembed123 7:c75d5e5e6bfc 24 if(camera==NULL){
dylanembed123 7:c75d5e5e6bfc 25 // Init Camera
dylanembed123 7:c75d5e5e6bfc 26 camera=new Serial(CAMERAPINTX,CAMERAPINRX);
dylanembed123 7:c75d5e5e6bfc 27 camera->baud(CAMERABAUD);
dylanembed123 7:c75d5e5e6bfc 28 serialNum=0;
dylanembed123 7:c75d5e5e6bfc 29 Camera temp;
dylanembed123 7:c75d5e5e6bfc 30 temp.reset();
dylanembed123 7:c75d5e5e6bfc 31 }
dylanembed123 7:c75d5e5e6bfc 32 return *camera;
dylanembed123 7:c75d5e5e6bfc 33 }
dylanembed123 7:c75d5e5e6bfc 34
dylanembed123 7:c75d5e5e6bfc 35 bool Camera::reset() {
dylanembed123 7:c75d5e5e6bfc 36 uint8_t args[] = {0x0};
dylanembed123 7:c75d5e5e6bfc 37 return runCommand(VC0706_RESET, args, 1, 5);
dylanembed123 7:c75d5e5e6bfc 38 }
dylanembed123 7:c75d5e5e6bfc 39
dylanembed123 7:c75d5e5e6bfc 40 /*
dylanembed123 7:c75d5e5e6bfc 41 // Initialization code used by all constructor types
dylanembed123 7:c75d5e5e6bfc 42 void Camera::common_init(void) {
dylanembed123 7:c75d5e5e6bfc 43 swSerial = NULL;
dylanembed123 7:c75d5e5e6bfc 44 hwSerial = NULL;
dylanembed123 7:c75d5e5e6bfc 45 frameptr = 0;
dylanembed123 7:c75d5e5e6bfc 46 bufferLen = 0;
dylanembed123 7:c75d5e5e6bfc 47 serialNum = 0;
dylanembed123 7:c75d5e5e6bfc 48 }
dylanembed123 7:c75d5e5e6bfc 49
dylanembed123 7:c75d5e5e6bfc 50 // Constructor when using SoftwareSerial or NewSoftSerial
dylanembed123 7:c75d5e5e6bfc 51 #if ARDUINO >= 100
dylanembed123 7:c75d5e5e6bfc 52 Camera::Camera(SoftwareSerial *ser) {
dylanembed123 7:c75d5e5e6bfc 53 #else
dylanembed123 7:c75d5e5e6bfc 54 Camera::Camera(NewSoftSerial *ser) {
dylanembed123 7:c75d5e5e6bfc 55 #endif
dylanembed123 7:c75d5e5e6bfc 56 common_init(); // Set everything to common state, then...
dylanembed123 7:c75d5e5e6bfc 57 swSerial = ser; // ...override swSerial with value passed.
dylanembed123 7:c75d5e5e6bfc 58 }
dylanembed123 7:c75d5e5e6bfc 59
dylanembed123 7:c75d5e5e6bfc 60 // Constructor when using HardwareSerial
dylanembed123 7:c75d5e5e6bfc 61 Camera::Camera(HardwareSerial *ser) {
dylanembed123 7:c75d5e5e6bfc 62 common_init(); // Set everything to common state, then...
dylanembed123 7:c75d5e5e6bfc 63 hwSerial = ser; // ...override hwSerial with value passed.
dylanembed123 7:c75d5e5e6bfc 64 }
dylanembed123 7:c75d5e5e6bfc 65
dylanembed123 7:c75d5e5e6bfc 66 boolean Camera::begin(uint16_t baud) {
dylanembed123 7:c75d5e5e6bfc 67 if(swSerial) swSerial->begin(baud);
dylanembed123 7:c75d5e5e6bfc 68 else hwSerial->begin(baud);
dylanembed123 7:c75d5e5e6bfc 69 return reset();
dylanembed123 7:c75d5e5e6bfc 70 }
dylanembed123 7:c75d5e5e6bfc 71
dylanembed123 7:c75d5e5e6bfc 72
dylanembed123 7:c75d5e5e6bfc 73 boolean Camera::motionDetected() {
dylanembed123 7:c75d5e5e6bfc 74 if (readResponse(4, 200) != 4) {
dylanembed123 7:c75d5e5e6bfc 75 return false;
dylanembed123 7:c75d5e5e6bfc 76 }
dylanembed123 7:c75d5e5e6bfc 77 if (! verifyResponse(VC0706_COMM_MOTION_DETECTED))
dylanembed123 7:c75d5e5e6bfc 78 return false;
dylanembed123 7:c75d5e5e6bfc 79
dylanembed123 7:c75d5e5e6bfc 80 return true;
dylanembed123 7:c75d5e5e6bfc 81 }
dylanembed123 7:c75d5e5e6bfc 82
dylanembed123 7:c75d5e5e6bfc 83
dylanembed123 7:c75d5e5e6bfc 84 uint8_t Camera::setMotionStatus(uint8_t x, uint8_t d1, uint8_t d2) {
dylanembed123 7:c75d5e5e6bfc 85 uint8_t args[] = {0x03, x, d1, d2};
dylanembed123 7:c75d5e5e6bfc 86
dylanembed123 7:c75d5e5e6bfc 87 return runCommand(VC0706_MOTION_CTRL, args, sizeof(args), 5);
dylanembed123 7:c75d5e5e6bfc 88 }
dylanembed123 7:c75d5e5e6bfc 89
dylanembed123 7:c75d5e5e6bfc 90
dylanembed123 7:c75d5e5e6bfc 91 uint8_t Camera::getMotionStatus(uint8_t x) {
dylanembed123 7:c75d5e5e6bfc 92 uint8_t args[] = {0x01, x};
dylanembed123 7:c75d5e5e6bfc 93
dylanembed123 7:c75d5e5e6bfc 94 return runCommand(VC0706_MOTION_STATUS, args, sizeof(args), 5);
dylanembed123 7:c75d5e5e6bfc 95 }
dylanembed123 7:c75d5e5e6bfc 96
dylanembed123 7:c75d5e5e6bfc 97
dylanembed123 7:c75d5e5e6bfc 98 boolean Camera::setMotionDetect(boolean flag) {
dylanembed123 7:c75d5e5e6bfc 99 if (! setMotionStatus(VC0706_MOTIONCONTROL,
dylanembed123 7:c75d5e5e6bfc 100 VC0706_UARTMOTION, VC0706_ACTIVATEMOTION))
dylanembed123 7:c75d5e5e6bfc 101 return false;
dylanembed123 7:c75d5e5e6bfc 102
dylanembed123 7:c75d5e5e6bfc 103 uint8_t args[] = {0x01, flag};
dylanembed123 7:c75d5e5e6bfc 104
dylanembed123 7:c75d5e5e6bfc 105 runCommand(VC0706_COMM_MOTION_CTRL, args, sizeof(args), 5);
dylanembed123 7:c75d5e5e6bfc 106 }
dylanembed123 7:c75d5e5e6bfc 107
dylanembed123 7:c75d5e5e6bfc 108
dylanembed123 7:c75d5e5e6bfc 109
dylanembed123 7:c75d5e5e6bfc 110 boolean Camera::getMotionDetect(void) {
dylanembed123 7:c75d5e5e6bfc 111 uint8_t args[] = {0x0};
dylanembed123 7:c75d5e5e6bfc 112
dylanembed123 7:c75d5e5e6bfc 113 if (! runCommand(VC0706_COMM_MOTION_STATUS, args, 1, 6))
dylanembed123 7:c75d5e5e6bfc 114 return false;
dylanembed123 7:c75d5e5e6bfc 115
dylanembed123 7:c75d5e5e6bfc 116 return camerabuff[5];
dylanembed123 7:c75d5e5e6bfc 117 }
dylanembed123 7:c75d5e5e6bfc 118 */
dylanembed123 7:c75d5e5e6bfc 119 uint8_t Camera::getImageSize() {
dylanembed123 7:c75d5e5e6bfc 120 uint8_t args[] = {0x4, 0x4, 0x1, 0x00, 0x19};
dylanembed123 7:c75d5e5e6bfc 121 if (! runCommand(VC0706_READ_DATA, args, sizeof(args), 6))
dylanembed123 7:c75d5e5e6bfc 122 return 0xFF;
dylanembed123 7:c75d5e5e6bfc 123
dylanembed123 7:c75d5e5e6bfc 124 return camerabuff[5];
dylanembed123 7:c75d5e5e6bfc 125 }
dylanembed123 7:c75d5e5e6bfc 126
dylanembed123 7:c75d5e5e6bfc 127 bool Camera::setImageSize(uint8_t x) {
dylanembed123 7:c75d5e5e6bfc 128 uint8_t args[] = {0x05, 0x04, 0x01, 0x00, 0x19, x};
dylanembed123 7:c75d5e5e6bfc 129 return runCommand(VC0706_WRITE_DATA, args, sizeof(args), 5);
dylanembed123 7:c75d5e5e6bfc 130 }
dylanembed123 7:c75d5e5e6bfc 131 /*
dylanembed123 7:c75d5e5e6bfc 132 // downsize image control
dylanembed123 7:c75d5e5e6bfc 133
dylanembed123 7:c75d5e5e6bfc 134 uint8_t Camera::getDownsize(void) {
dylanembed123 7:c75d5e5e6bfc 135 uint8_t args[] = {0x0};
dylanembed123 7:c75d5e5e6bfc 136 if (! runCommand(VC0706_DOWNSIZE_STATUS, args, 1, 6))
dylanembed123 7:c75d5e5e6bfc 137 return -1;
dylanembed123 7:c75d5e5e6bfc 138
dylanembed123 7:c75d5e5e6bfc 139 return camerabuff[5];
dylanembed123 7:c75d5e5e6bfc 140 }
dylanembed123 7:c75d5e5e6bfc 141
dylanembed123 7:c75d5e5e6bfc 142 boolean Camera::setDownsize(uint8_t newsize) {
dylanembed123 7:c75d5e5e6bfc 143 uint8_t args[] = {0x01, newsize};
dylanembed123 7:c75d5e5e6bfc 144
dylanembed123 7:c75d5e5e6bfc 145 return runCommand(VC0706_DOWNSIZE_CTRL, args, 2, 5);
dylanembed123 7:c75d5e5e6bfc 146 }
dylanembed123 7:c75d5e5e6bfc 147
dylanembed123 7:c75d5e5e6bfc 148 // other high level commands
dylanembed123 7:c75d5e5e6bfc 149 */
dylanembed123 7:c75d5e5e6bfc 150 char * Camera::getVersion(void) {
dylanembed123 7:c75d5e5e6bfc 151 uint8_t args[] = {0x01};
dylanembed123 7:c75d5e5e6bfc 152
dylanembed123 7:c75d5e5e6bfc 153 sendCommand(VC0706_GEN_VERSION, args, 1);
dylanembed123 7:c75d5e5e6bfc 154 // get reply
dylanembed123 7:c75d5e5e6bfc 155 if (!readResponse(CAMERABUFFSIZ, 200)){
dylanembed123 7:c75d5e5e6bfc 156 return 0;
dylanembed123 7:c75d5e5e6bfc 157 }
dylanembed123 7:c75d5e5e6bfc 158 camerabuff[bufferLen] = 0; // end it!
dylanembed123 7:c75d5e5e6bfc 159 return (char *)camerabuff; // return it!
dylanembed123 7:c75d5e5e6bfc 160 }
dylanembed123 7:c75d5e5e6bfc 161 /*
dylanembed123 7:c75d5e5e6bfc 162
dylanembed123 7:c75d5e5e6bfc 163 // high level photo comamnds
dylanembed123 7:c75d5e5e6bfc 164
dylanembed123 7:c75d5e5e6bfc 165 void Camera::OSD(uint8_t x, uint8_t y, char *str) {
dylanembed123 7:c75d5e5e6bfc 166 if (strlen(str) > 14) { str[13] = 0; }
dylanembed123 7:c75d5e5e6bfc 167
dylanembed123 7:c75d5e5e6bfc 168 uint8_t args[17] = {strlen(str), strlen(str)-1, (y & 0xF) | ((x & 0x3) << 4)};
dylanembed123 7:c75d5e5e6bfc 169
dylanembed123 7:c75d5e5e6bfc 170 for (uint8_t i=0; i<strlen(str); i++) {
dylanembed123 7:c75d5e5e6bfc 171 char c = str[i];
dylanembed123 7:c75d5e5e6bfc 172 if ((c >= '0') && (c <= '9')) {
dylanembed123 7:c75d5e5e6bfc 173 str[i] -= '0';
dylanembed123 7:c75d5e5e6bfc 174 } else if ((c >= 'A') && (c <= 'Z')) {
dylanembed123 7:c75d5e5e6bfc 175 str[i] -= 'A';
dylanembed123 7:c75d5e5e6bfc 176 str[i] += 10;
dylanembed123 7:c75d5e5e6bfc 177 } else if ((c >= 'a') && (c <= 'z')) {
dylanembed123 7:c75d5e5e6bfc 178 str[i] -= 'a';
dylanembed123 7:c75d5e5e6bfc 179 str[i] += 36;
dylanembed123 7:c75d5e5e6bfc 180 }
dylanembed123 7:c75d5e5e6bfc 181
dylanembed123 7:c75d5e5e6bfc 182 args[3+i] = str[i];
dylanembed123 7:c75d5e5e6bfc 183 }
dylanembed123 7:c75d5e5e6bfc 184
dylanembed123 7:c75d5e5e6bfc 185 runCommand(VC0706_OSD_ADD_CHAR, args, strlen(str)+3, 5);
dylanembed123 7:c75d5e5e6bfc 186 printBuff();
dylanembed123 7:c75d5e5e6bfc 187 }
dylanembed123 7:c75d5e5e6bfc 188
dylanembed123 7:c75d5e5e6bfc 189 boolean Camera::setCompression(uint8_t c) {
dylanembed123 7:c75d5e5e6bfc 190 uint8_t args[] = {0x5, 0x1, 0x1, 0x12, 0x04, c};
dylanembed123 7:c75d5e5e6bfc 191 return runCommand(VC0706_WRITE_DATA, args, sizeof(args), 5);
dylanembed123 7:c75d5e5e6bfc 192 }
dylanembed123 7:c75d5e5e6bfc 193
dylanembed123 7:c75d5e5e6bfc 194 uint8_t Camera::getCompression(void) {
dylanembed123 7:c75d5e5e6bfc 195 uint8_t args[] = {0x4, 0x1, 0x1, 0x12, 0x04};
dylanembed123 7:c75d5e5e6bfc 196 runCommand(VC0706_READ_DATA, args, sizeof(args), 6);
dylanembed123 7:c75d5e5e6bfc 197 printBuff();
dylanembed123 7:c75d5e5e6bfc 198 return camerabuff[5];
dylanembed123 7:c75d5e5e6bfc 199 }
dylanembed123 7:c75d5e5e6bfc 200
dylanembed123 7:c75d5e5e6bfc 201 boolean Camera::setPTZ(uint16_t wz, uint16_t hz, uint16_t pan, uint16_t tilt) {
dylanembed123 7:c75d5e5e6bfc 202 uint8_t args[] = {0x08, wz >> 8, wz,
dylanembed123 7:c75d5e5e6bfc 203 hz >> 8, wz,
dylanembed123 7:c75d5e5e6bfc 204 pan>>8, pan,
dylanembed123 7:c75d5e5e6bfc 205 tilt>>8, tilt};
dylanembed123 7:c75d5e5e6bfc 206
dylanembed123 7:c75d5e5e6bfc 207 return (! runCommand(VC0706_SET_ZOOM, args, sizeof(args), 5));
dylanembed123 7:c75d5e5e6bfc 208 }
dylanembed123 7:c75d5e5e6bfc 209
dylanembed123 7:c75d5e5e6bfc 210
dylanembed123 7:c75d5e5e6bfc 211 boolean Camera::getPTZ(uint16_t &w, uint16_t &h, uint16_t &wz, uint16_t &hz, uint16_t &pan, uint16_t &tilt) {
dylanembed123 7:c75d5e5e6bfc 212 uint8_t args[] = {0x0};
dylanembed123 7:c75d5e5e6bfc 213
dylanembed123 7:c75d5e5e6bfc 214 if (! runCommand(VC0706_GET_ZOOM, args, sizeof(args), 16))
dylanembed123 7:c75d5e5e6bfc 215 return false;
dylanembed123 7:c75d5e5e6bfc 216 printBuff();
dylanembed123 7:c75d5e5e6bfc 217
dylanembed123 7:c75d5e5e6bfc 218 w = camerabuff[5];
dylanembed123 7:c75d5e5e6bfc 219 w <<= 8;
dylanembed123 7:c75d5e5e6bfc 220 w |= camerabuff[6];
dylanembed123 7:c75d5e5e6bfc 221
dylanembed123 7:c75d5e5e6bfc 222 h = camerabuff[7];
dylanembed123 7:c75d5e5e6bfc 223 h <<= 8;
dylanembed123 7:c75d5e5e6bfc 224 h |= camerabuff[8];
dylanembed123 7:c75d5e5e6bfc 225
dylanembed123 7:c75d5e5e6bfc 226 wz = camerabuff[9];
dylanembed123 7:c75d5e5e6bfc 227 wz <<= 8;
dylanembed123 7:c75d5e5e6bfc 228 wz |= camerabuff[10];
dylanembed123 7:c75d5e5e6bfc 229
dylanembed123 7:c75d5e5e6bfc 230 hz = camerabuff[11];
dylanembed123 7:c75d5e5e6bfc 231 hz <<= 8;
dylanembed123 7:c75d5e5e6bfc 232 hz |= camerabuff[12];
dylanembed123 7:c75d5e5e6bfc 233
dylanembed123 7:c75d5e5e6bfc 234 pan = camerabuff[13];
dylanembed123 7:c75d5e5e6bfc 235 pan <<= 8;
dylanembed123 7:c75d5e5e6bfc 236 pan |= camerabuff[14];
dylanembed123 7:c75d5e5e6bfc 237
dylanembed123 7:c75d5e5e6bfc 238 tilt = camerabuff[15];
dylanembed123 7:c75d5e5e6bfc 239 tilt <<= 8;
dylanembed123 7:c75d5e5e6bfc 240 tilt |= camerabuff[16];
dylanembed123 7:c75d5e5e6bfc 241
dylanembed123 7:c75d5e5e6bfc 242 return true;
dylanembed123 7:c75d5e5e6bfc 243 }
dylanembed123 7:c75d5e5e6bfc 244
dylanembed123 7:c75d5e5e6bfc 245 */
dylanembed123 7:c75d5e5e6bfc 246 bool Camera::takePicture() {
dylanembed123 7:c75d5e5e6bfc 247 frameptr = 0;
dylanembed123 7:c75d5e5e6bfc 248 return cameraFrameBuffCtrl(VC0706_STOPCURRENTFRAME);
dylanembed123 7:c75d5e5e6bfc 249 }
dylanembed123 14:6be57da62283 250
dylanembed123 14:6be57da62283 251 bool Camera::resumeVideo() {
dylanembed123 7:c75d5e5e6bfc 252 return cameraFrameBuffCtrl(VC0706_RESUMEFRAME);
dylanembed123 7:c75d5e5e6bfc 253 }
dylanembed123 14:6be57da62283 254 /*
dylanembed123 7:c75d5e5e6bfc 255 boolean Camera::TVon() {
dylanembed123 7:c75d5e5e6bfc 256 uint8_t args[] = {0x1, 0x1};
dylanembed123 7:c75d5e5e6bfc 257 return runCommand(VC0706_TVOUT_CTRL, args, sizeof(args), 5);
dylanembed123 7:c75d5e5e6bfc 258 }
dylanembed123 7:c75d5e5e6bfc 259 boolean Camera::TVoff() {
dylanembed123 7:c75d5e5e6bfc 260 uint8_t args[] = {0x1, 0x0};
dylanembed123 7:c75d5e5e6bfc 261 return runCommand(VC0706_TVOUT_CTRL, args, sizeof(args), 5);
dylanembed123 7:c75d5e5e6bfc 262 }
dylanembed123 7:c75d5e5e6bfc 263 */
dylanembed123 7:c75d5e5e6bfc 264 bool Camera::cameraFrameBuffCtrl(uint8_t command) {
dylanembed123 7:c75d5e5e6bfc 265 uint8_t args[] = {0x1, command};
dylanembed123 7:c75d5e5e6bfc 266 return runCommand(VC0706_FBUF_CTRL, args, sizeof(args), 5);
dylanembed123 7:c75d5e5e6bfc 267 }
dylanembed123 7:c75d5e5e6bfc 268
dylanembed123 7:c75d5e5e6bfc 269 uint32_t Camera::frameLength(void) {
dylanembed123 7:c75d5e5e6bfc 270 uint8_t args[] = {0x01, 0x00};
dylanembed123 7:c75d5e5e6bfc 271 if (!runCommand(VC0706_GET_FBUF_LEN, args, sizeof(args), 9))
dylanembed123 7:c75d5e5e6bfc 272 return 0;
dylanembed123 7:c75d5e5e6bfc 273
dylanembed123 7:c75d5e5e6bfc 274 uint32_t len;
dylanembed123 7:c75d5e5e6bfc 275 len = camerabuff[5];
dylanembed123 7:c75d5e5e6bfc 276 len <<= 8;
dylanembed123 7:c75d5e5e6bfc 277 len |= camerabuff[6];
dylanembed123 7:c75d5e5e6bfc 278 len <<= 8;
dylanembed123 7:c75d5e5e6bfc 279 len |= camerabuff[7];
dylanembed123 7:c75d5e5e6bfc 280 len <<= 8;
dylanembed123 7:c75d5e5e6bfc 281 len |= camerabuff[8];
dylanembed123 7:c75d5e5e6bfc 282
dylanembed123 7:c75d5e5e6bfc 283 return len;
dylanembed123 7:c75d5e5e6bfc 284 }
dylanembed123 7:c75d5e5e6bfc 285 /*
dylanembed123 7:c75d5e5e6bfc 286
dylanembed123 7:c75d5e5e6bfc 287 uint8_t Camera::available(void) {
dylanembed123 7:c75d5e5e6bfc 288 return bufferLen;
dylanembed123 7:c75d5e5e6bfc 289 }
dylanembed123 7:c75d5e5e6bfc 290
dylanembed123 7:c75d5e5e6bfc 291 */
dylanembed123 7:c75d5e5e6bfc 292 uint8_t * Camera::readPicture(uint8_t n,uint8_t* outSize) {
dylanembed123 7:c75d5e5e6bfc 293 uint8_t args[] = {0x0C, 0x0, 0x0A,
dylanembed123 7:c75d5e5e6bfc 294 0, 0, frameptr >> 8, frameptr & 0xFF,
dylanembed123 7:c75d5e5e6bfc 295 0, 0, 0, n,
dylanembed123 7:c75d5e5e6bfc 296 CAMERADELAY >> 8, CAMERADELAY & 0xFF};
dylanembed123 7:c75d5e5e6bfc 297
dylanembed123 7:c75d5e5e6bfc 298 if (! runCommand(VC0706_READ_FBUF, args, sizeof(args), 5, false))
dylanembed123 7:c75d5e5e6bfc 299 return 0;
dylanembed123 7:c75d5e5e6bfc 300
dylanembed123 7:c75d5e5e6bfc 301
dylanembed123 7:c75d5e5e6bfc 302 // read into the buffer PACKETLEN!
dylanembed123 7:c75d5e5e6bfc 303 int outputSize=readResponse(n+5, CAMERADELAY)-5;
dylanembed123 7:c75d5e5e6bfc 304 if (outputSize == 0)
dylanembed123 7:c75d5e5e6bfc 305 return 0;
dylanembed123 7:c75d5e5e6bfc 306 if(outSize!=NULL)*outSize=outputSize;
dylanembed123 7:c75d5e5e6bfc 307
dylanembed123 7:c75d5e5e6bfc 308 frameptr += outputSize;
dylanembed123 7:c75d5e5e6bfc 309
dylanembed123 7:c75d5e5e6bfc 310 return camerabuff;
dylanembed123 7:c75d5e5e6bfc 311 }
dylanembed123 7:c75d5e5e6bfc 312
dylanembed123 7:c75d5e5e6bfc 313
dylanembed123 7:c75d5e5e6bfc 314 //
dylanembed123 7:c75d5e5e6bfc 315 // Low level commands
dylanembed123 7:c75d5e5e6bfc 316 //
dylanembed123 7:c75d5e5e6bfc 317
dylanembed123 7:c75d5e5e6bfc 318 bool Camera::runCommand(uint8_t cmd, uint8_t *args, uint8_t argn, uint8_t resplen, bool flushflag) {
dylanembed123 7:c75d5e5e6bfc 319 // flush out anything in the buffer?
dylanembed123 7:c75d5e5e6bfc 320 if (flushflag) {
dylanembed123 7:c75d5e5e6bfc 321 readResponse(100, 10);
dylanembed123 7:c75d5e5e6bfc 322 }
dylanembed123 7:c75d5e5e6bfc 323
dylanembed123 7:c75d5e5e6bfc 324 sendCommand(cmd, args, argn);
dylanembed123 7:c75d5e5e6bfc 325 if (readResponse(resplen, 200) != resplen)
dylanembed123 7:c75d5e5e6bfc 326 return false;
dylanembed123 7:c75d5e5e6bfc 327 if (! verifyResponse(cmd))
dylanembed123 7:c75d5e5e6bfc 328 return false;
dylanembed123 7:c75d5e5e6bfc 329 return true;
dylanembed123 7:c75d5e5e6bfc 330 }
dylanembed123 7:c75d5e5e6bfc 331
dylanembed123 7:c75d5e5e6bfc 332
dylanembed123 7:c75d5e5e6bfc 333 void Camera::sendCommand(uint8_t cmd, uint8_t args[], uint8_t argn) {
dylanembed123 7:c75d5e5e6bfc 334 getSerial().putc(0x56);
dylanembed123 7:c75d5e5e6bfc 335 getSerial().putc(serialNum);
dylanembed123 7:c75d5e5e6bfc 336 getSerial().putc(cmd);
dylanembed123 7:c75d5e5e6bfc 337
dylanembed123 7:c75d5e5e6bfc 338 for (uint8_t i=0; i<argn; i++) {
dylanembed123 7:c75d5e5e6bfc 339 getSerial().putc(args[i]);
dylanembed123 7:c75d5e5e6bfc 340 }
dylanembed123 7:c75d5e5e6bfc 341 }
dylanembed123 7:c75d5e5e6bfc 342
dylanembed123 7:c75d5e5e6bfc 343 uint8_t Camera::readResponse(uint8_t numbytes, uint8_t timeout) {
dylanembed123 7:c75d5e5e6bfc 344 uint16_t counter = 0;
dylanembed123 7:c75d5e5e6bfc 345 bufferLen = 0;
dylanembed123 7:c75d5e5e6bfc 346
dylanembed123 7:c75d5e5e6bfc 347 while ((timeout*100 != counter) && (bufferLen != numbytes)){
dylanembed123 7:c75d5e5e6bfc 348 int avail = getSerial().readable();
dylanembed123 7:c75d5e5e6bfc 349 if (avail <= 0) {
dylanembed123 7:c75d5e5e6bfc 350 wait_us(10);
dylanembed123 7:c75d5e5e6bfc 351 counter++;
dylanembed123 7:c75d5e5e6bfc 352 continue;
dylanembed123 7:c75d5e5e6bfc 353 }
dylanembed123 7:c75d5e5e6bfc 354 counter = 0;
dylanembed123 7:c75d5e5e6bfc 355 // there's a byte!
dylanembed123 7:c75d5e5e6bfc 356 camerabuff[bufferLen++] = getSerial().getc();
dylanembed123 7:c75d5e5e6bfc 357 }
dylanembed123 7:c75d5e5e6bfc 358 return bufferLen;
dylanembed123 7:c75d5e5e6bfc 359 }
dylanembed123 7:c75d5e5e6bfc 360
dylanembed123 7:c75d5e5e6bfc 361 bool Camera::verifyResponse(uint8_t command) {
dylanembed123 7:c75d5e5e6bfc 362 if ((camerabuff[0] != 0x76) ||
dylanembed123 7:c75d5e5e6bfc 363 (camerabuff[1] != serialNum) ||
dylanembed123 7:c75d5e5e6bfc 364 (camerabuff[2] != command) ||
dylanembed123 7:c75d5e5e6bfc 365 (camerabuff[3] != 0x0))
dylanembed123 7:c75d5e5e6bfc 366 return false;
dylanembed123 7:c75d5e5e6bfc 367 return true;
dylanembed123 7:c75d5e5e6bfc 368
dylanembed123 7:c75d5e5e6bfc 369 }
dylanembed123 7:c75d5e5e6bfc 370
dylanembed123 7:c75d5e5e6bfc 371 void Camera::printBuff() {
dylanembed123 7:c75d5e5e6bfc 372 }