-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAdafruit_VC0706.cpp
484 lines (378 loc) · 12.1 KB
/
Adafruit_VC0706.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/***************************************************
This is a library for the Adafruit TTL JPEG Camera (VC0706 chipset)
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/products/397
These displays use Serial to communicate, 2 pins are required to interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include "Adafruit_VC0706.h"
// Initialization code used by all constructor types
void Adafruit_VC0706::common_init(void) {
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
swSerial = NULL;
#endif
hwSerial = NULL;
frameptr = 0;
bufferLen = 0;
serialNum = 0;
}
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
// Constructor when using SoftwareSerial or NewSoftSerial
#if ARDUINO >= 100
Adafruit_VC0706::Adafruit_VC0706(SoftwareSerial *ser) {
#else
Adafruit_VC0706::Adafruit_VC0706(NewSoftSerial *ser) {
#endif
common_init(); // Set everything to common state, then...
swSerial = ser; // ...override swSerial with value passed.
}
#endif
// Constructor when using HardwareSerial
Adafruit_VC0706::Adafruit_VC0706(HardwareSerial *ser) {
common_init(); // Set everything to common state, then...
hwSerial = ser; // ...override hwSerial with value passed.
}
boolean Adafruit_VC0706::begin(uint16_t baud) {
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
if(swSerial) swSerial->begin(baud);
else
#endif
hwSerial->begin(baud);
return reset();
}
boolean Adafruit_VC0706::reset() {
uint8_t args[] = {0x0};
return runCommand(VC0706_RESET, args, 1, 5);
}
// boolean Adafruit_VC0706::motionDetected() {
// if (readResponse(4, 200) != 4) {
// return false;
// }
// if (! verifyResponse(VC0706_COMM_MOTION_DETECTED))
// return false;
// return true;
// }
// boolean Adafruit_VC0706::setMotionStatus(uint8_t x, uint8_t d1, uint8_t d2) {
// uint8_t args[] = {0x03, x, d1, d2};
// return runCommand(VC0706_MOTION_CTRL, args, sizeof(args), 5);
// }
// uint8_t Adafruit_VC0706::getMotionStatus(uint8_t x) {
// uint8_t args[] = {0x01, x};
// return runCommand(VC0706_MOTION_STATUS, args, sizeof(args), 5);
// }
// boolean Adafruit_VC0706::setMotionDetect(boolean flag) {
// if (! setMotionStatus(VC0706_MOTIONCONTROL,
// VC0706_UARTMOTION, VC0706_ACTIVATEMOTION))
// return false;
// uint8_t args[] = {0x01, flag};
// runCommand(VC0706_COMM_MOTION_CTRL, args, sizeof(args), 5);
// }
// boolean Adafruit_VC0706::getMotionDetect(void) {
// uint8_t args[] = {0x0};
// if (! runCommand(VC0706_COMM_MOTION_STATUS, args, 1, 6))
// return false;
// return camerabuff[5];
// }
// uint8_t Adafruit_VC0706::getImageSize() {
// uint8_t args[] = {0x4, 0x4, 0x1, 0x00, 0x19};
// if (! runCommand(VC0706_READ_DATA, args, sizeof(args), 6))
// return -1;
// return camerabuff[5];
// }
boolean Adafruit_VC0706::setImageSize(uint8_t x) {
uint8_t args[] = {0x05, 0x04, 0x01, 0x00, 0x19, x};
return runCommand(VC0706_WRITE_DATA, args, sizeof(args), 5);
}
/****************** downsize image control */
// uint8_t Adafruit_VC0706::getDownsize(void) {
// uint8_t args[] = {0x0};
// if (! runCommand(VC0706_DOWNSIZE_STATUS, args, 1, 6))
// return -1;
// return camerabuff[5];
// }
// boolean Adafruit_VC0706::setDownsize(uint8_t newsize) {
// uint8_t args[] = {0x01, newsize};
// return runCommand(VC0706_DOWNSIZE_CTRL, args, 2, 5);
// }
/***************** other high level commands */
// char * Adafruit_VC0706::getVersion(void) {
// uint8_t args[] = {0x01};
// sendCommand(VC0706_GEN_VERSION, args, 1);
// // get reply
// if (!readResponse(CAMERABUFFSIZ, 200))
// return 0;
// camerabuff[bufferLen] = 0; // end it!
// return (char *)camerabuff; // return it!
// }
/***************** baud rate commands */
// char* Adafruit_VC0706::setBaud9600() {
// uint8_t args[] = {0x03, 0x01, 0xAE, 0xC8};
// sendCommand(VC0706_SET_PORT, args, sizeof(args));
// // get reply
// if (!readResponse(CAMERABUFFSIZ, 200))
// return 0;
// camerabuff[bufferLen] = 0; // end it!
// return (char *)camerabuff; // return it!
// }
// char* Adafruit_VC0706::setBaud19200() {
// uint8_t args[] = {0x03, 0x01, 0x56, 0xE4};
// sendCommand(VC0706_SET_PORT, args, sizeof(args));
// // get reply
// if (!readResponse(CAMERABUFFSIZ, 200))
// return 0;
// camerabuff[bufferLen] = 0; // end it!
// return (char *)camerabuff; // return it!
// }
// char* Adafruit_VC0706::setBaud38400() {
// uint8_t args[] = {0x03, 0x01, 0x2A, 0xF2};
// sendCommand(VC0706_SET_PORT, args, sizeof(args));
// // get reply
// if (!readResponse(CAMERABUFFSIZ, 200))
// return 0;
// camerabuff[bufferLen] = 0; // end it!
// return (char *)camerabuff; // return it!
// }
// char* Adafruit_VC0706::setBaud57600() {
// uint8_t args[] = {0x03, 0x01, 0x1C, 0x1C};
// sendCommand(VC0706_SET_PORT, args, sizeof(args));
// // get reply
// if (!readResponse(CAMERABUFFSIZ, 200))
// return 0;
// camerabuff[bufferLen] = 0; // end it!
// return (char *)camerabuff; // return it!
// }
// char* Adafruit_VC0706::setBaud115200() {
// uint8_t args[] = {0x03, 0x01, 0x0D, 0xA6};
// sendCommand(VC0706_SET_PORT, args, sizeof(args));
// // get reply
// if (!readResponse(CAMERABUFFSIZ, 200))
// return 0;
// camerabuff[bufferLen] = 0; // end it!
// return (char *)camerabuff; // return it!
// }
/****************** high level photo comamnds */
// void Adafruit_VC0706::OSD(uint8_t x, uint8_t y, char *str) {
// if (strlen(str) > 14) { str[13] = 0; }
// uint8_t args[17] = {strlen(str), strlen(str)-1, (y & 0xF) | ((x & 0x3) << 4)};
// for (uint8_t i=0; i<strlen(str); i++) {
// char c = str[i];
// if ((c >= '0') && (c <= '9')) {
// str[i] -= '0';
// } else if ((c >= 'A') && (c <= 'Z')) {
// str[i] -= 'A';
// str[i] += 10;
// } else if ((c >= 'a') && (c <= 'z')) {
// str[i] -= 'a';
// str[i] += 36;
// }
// args[3+i] = str[i];
// }
// runCommand(VC0706_OSD_ADD_CHAR, args, strlen(str)+3, 5);
// printBuff();
// }
// boolean Adafruit_VC0706::setCompression(uint8_t c) {
// uint8_t args[] = {0x5, 0x1, 0x1, 0x12, 0x04, c};
// return runCommand(VC0706_WRITE_DATA, args, sizeof(args), 5);
// }
// uint8_t Adafruit_VC0706::getCompression(void) {
// uint8_t args[] = {0x4, 0x1, 0x1, 0x12, 0x04};
// runCommand(VC0706_READ_DATA, args, sizeof(args), 6);
// printBuff();
// return camerabuff[5];
// }
// boolean Adafruit_VC0706::setPTZ(uint16_t wz, uint16_t hz, uint16_t pan, uint16_t tilt) {
// uint8_t args[] = {0x08, wz >> 8, wz,
// hz >> 8, wz,
// pan>>8, pan,
// tilt>>8, tilt};
// return (! runCommand(VC0706_SET_ZOOM, args, sizeof(args), 5));
// }
// boolean Adafruit_VC0706::getPTZ(uint16_t &w, uint16_t &h, uint16_t &wz, uint16_t &hz, uint16_t &pan, uint16_t &tilt) {
// uint8_t args[] = {0x0};
// if (! runCommand(VC0706_GET_ZOOM, args, sizeof(args), 16))
// return false;
// printBuff();
// w = camerabuff[5];
// w <<= 8;
// w |= camerabuff[6];
// h = camerabuff[7];
// h <<= 8;
// h |= camerabuff[8];
// wz = camerabuff[9];
// wz <<= 8;
// wz |= camerabuff[10];
// hz = camerabuff[11];
// hz <<= 8;
// hz |= camerabuff[12];
// pan = camerabuff[13];
// pan <<= 8;
// pan |= camerabuff[14];
// tilt = camerabuff[15];
// tilt <<= 8;
// tilt |= camerabuff[16];
// return true;
// }
boolean Adafruit_VC0706::takePicture() {
frameptr = 0;
return cameraFrameBuffCtrl(VC0706_STOPCURRENTFRAME);
}
// boolean Adafruit_VC0706::resumeVideo() {
// return cameraFrameBuffCtrl(VC0706_RESUMEFRAME);
// }
// boolean Adafruit_VC0706::TVon() {
// uint8_t args[] = {0x1, 0x1};
// return runCommand(VC0706_TVOUT_CTRL, args, sizeof(args), 5);
// }
// boolean Adafruit_VC0706::TVoff() {
// uint8_t args[] = {0x1, 0x0};
// return runCommand(VC0706_TVOUT_CTRL, args, sizeof(args), 5);
// }
boolean Adafruit_VC0706::cameraFrameBuffCtrl(uint8_t command) {
uint8_t args[] = {0x1, command};
return runCommand(VC0706_FBUF_CTRL, args, sizeof(args), 5);
}
uint32_t Adafruit_VC0706::frameLength(void) {
uint8_t args[] = {0x01, 0x00};
if (!runCommand(VC0706_GET_FBUF_LEN, args, sizeof(args), 9))
return 0;
uint32_t len;
len = camerabuff[5];
len <<= 8;
len |= camerabuff[6];
len <<= 8;
len |= camerabuff[7];
len <<= 8;
len |= camerabuff[8];
return len;
}
uint8_t Adafruit_VC0706::available(void) {
return bufferLen;
}
uint8_t * Adafruit_VC0706::readPicture(uint8_t n) {
uint8_t args[] = {0x0C, 0x0, 0x0A,
0, 0, frameptr >> 8, frameptr & 0xFF,
0, 0, 0, n,
CAMERADELAY >> 8, CAMERADELAY & 0xFF};
if (! runCommand(VC0706_READ_FBUF, args, sizeof(args), 5, false))
return 0;
// read into the buffer PACKETLEN!
if (readResponse(n+5, CAMERADELAY) == 0)
return 0;
frameptr += n;
return camerabuff;
}
/**************** low level commands */
boolean Adafruit_VC0706::runCommand(uint8_t cmd, uint8_t *args, uint8_t argn,
uint8_t resplen, boolean flushflag) {
// flush out anything in the buffer?
if (flushflag) {
// readResponse(100, 10);
// ●●● 変えた!!! ●●●
readResponse(CAMERABUFFSIZ, 10);
}
sendCommand(cmd, args, argn);
if (readResponse(resplen, 200) != resplen)
return false;
if (! verifyResponse(cmd))
return false;
return true;
}
void Adafruit_VC0706::sendCommand(uint8_t cmd, uint8_t args[] = 0, uint8_t argn = 0) {
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
if(swSerial) {
#if ARDUINO >= 100
swSerial->write((byte)0x56);
swSerial->write((byte)serialNum);
swSerial->write((byte)cmd);
for (uint8_t i=0; i<argn; i++) {
swSerial->write((byte)args[i]);
//Serial.print(" 0x");
//Serial.print(args[i], HEX);
}
#else
swSerial->print(0x56, BYTE);
swSerial->print(serialNum, BYTE);
swSerial->print(cmd, BYTE);
for (uint8_t i=0; i<argn; i++) {
swSerial->print(args[i], BYTE);
//Serial.print(" 0x");
//Serial.print(args[i], HEX);
}
#endif
}
else
#endif
{
#if ARDUINO >= 100
hwSerial->write((byte)0x56);
hwSerial->write((byte)serialNum);
hwSerial->write((byte)cmd);
for (uint8_t i=0; i<argn; i++) {
hwSerial->write((byte)args[i]);
//Serial.print(" 0x");
//Serial.print(args[i], HEX);
}
#else
hwSerial->print(0x56, BYTE);
hwSerial->print(serialNum, BYTE);
hwSerial->print(cmd, BYTE);
for (uint8_t i=0; i<argn; i++) {
hwSerial->print(args[i], BYTE);
//Serial.print(" 0x");
//Serial.print(args[i], HEX);
}
#endif
}
//Serial.println();
}
uint8_t Adafruit_VC0706::readResponse(uint8_t numbytes, uint8_t timeout) {
uint8_t counter = 0;
bufferLen = 0;
int avail;
while ((timeout != counter) && (bufferLen != numbytes)){
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
avail = swSerial ? swSerial->available() : hwSerial->available();
#else
avail = hwSerial->available();
#endif
if (avail <= 0) {
delay(1);
counter++;
continue;
}
counter = 0;
// there's a byte!
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
camerabuff[bufferLen++] = swSerial ? swSerial->read() : hwSerial->read();
#else
camerabuff[bufferLen++] = hwSerial->read();
#endif
// ●●● 安全コード.足した!!!●●●
if (bufferLen > (CAMERABUFFSIZ - 1)) {
Serial.println(F("CAM BUF WARNING!!!"));
}
}
//printBuff();
//camerabuff[bufferLen] = 0;
//Serial.println((char*)camerabuff);
return bufferLen;
}
boolean Adafruit_VC0706::verifyResponse(uint8_t command) {
if ((camerabuff[0] != 0x76) ||
(camerabuff[1] != serialNum) ||
(camerabuff[2] != command) ||
(camerabuff[3] != 0x0))
return false;
return true;
}
// void Adafruit_VC0706::printBuff() {
// for (uint8_t i = 0; i< bufferLen; i++) {
// Serial.print(" 0x");
// Serial.print(camerabuff[i], HEX);
// }
// Serial.println();
// }