-
Notifications
You must be signed in to change notification settings - Fork 0
/
uCamII.cpp
403 lines (326 loc) · 8.11 KB
/
uCamII.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
/*
* uCam.cpp
*
* http://www.4dsystems.com.au/productpages/uCAM-II/downloads/uCAM-II_datasheet_R_1_4.pdf
*/
#include "uCamII.h"
//#include <fstream>
//#include <iostream>
#define cameraDebugSerial
//void print(){
// std::ofstream fout;
// fout.open("picture.jpg");
//}
//
//void closeFile(){
// fout.close();
//}
// From http://pastebin.com/ZuMu48w7
byte _SYNC_COMMAND[6] = {0xAA, 0x0D, 0x00, 0x00, 0x00, 0x00};
byte _RESET[6] = {0xAA, 0x08, 0x00, 0x00, 0x00, 0x00};
byte _SYNC_ACK_REPLY[6] = {0xAA, 0x0E, 0x0D, 0x00, 0x00, 0x00};
byte _SYNC_ACK_REPLY_EXT[6] = {0xAA, 0x0D, 0x00, 0x00, 0x00, 0x00};
byte _SYNC_FINAL_COMMAND[6] = {0xAA, 0x0E, 0x00, 0x00, 0xF5, 0x00};
byte _INITIAL_COMMAND[6] = {0xAA, 0x01, 0x00, 0x07, 0x07, 0x07};
byte _GENERIC_ACK_REPLY[6] = {0xAA, 0x0E, 0x00, 0x00, 0x00, 0x00};
byte _PACK_SIZE[6] = {0xAA, 0x06, 0x08, UCAMII_BUF_SIZE + 6, 0x00, 0x00};
byte _SNAPSHOT[6] = {0xAA, 0x05, 0x00, 0x00, 0x00, 0x00};
byte _GET_PICTURE[6] = {0xAA, 0x04, 0x01, 0x00, 0x00, 0x00};
//void print();
UCAMII::UCAMII() {
this->image_pos = 0;
this->package_no = 0;
}
boolean UCAMII::init()
{
#ifdef cameraDebugSerial
Serial.println("Intitial is starting to be sent");
#endif
if (this->attempt_sync())
{
#ifdef cameraDebugSerial
Serial.println("\n\rCam has ACKED the SYNC");
#endif
return true;
}
return false;
}
int UCAMII::attempt_sync()
{
int attempts = 0;
byte cam_reply;
int ack_success = 0;
int last_reply = 0;
while (attempts < 60 && ack_success == 0) {
// Flush
while (Serial1.available());
delay(200);
#ifdef cameraDebugSerial
Serial.println("Sending SYNC...");
#endif
for (int i = 0; i < 6; i++) {
Serial1.write(_SYNC_COMMAND[i]);
}
if (this->wait_for_bytes(_SYNC_ACK_REPLY) ) {
if (this->wait_for_bytes(_SYNC_ACK_REPLY_EXT)) {
delay(50);
#ifdef cameraDebugSerial
Serial.println("\r\nSending FINAL SYNC...");
#endif
for (int i = 0; i < 6; i++) {
Serial1.write(_SYNC_FINAL_COMMAND[i]);
}
return 1;
}
}
}
return 0;
}
// Return number of packages ready
int UCAMII::numberOfPackages() {
return this->imageSize / UCAMII_BUF_SIZE;
}
int UCAMII::send_initial() {
// flush
while (Serial1.available() > 0) {
//while (Serial1.available()) {
Serial1.read();
}
delay(100);
#ifdef cameraDebugSerial
Serial.println("Sending INITIALISE...");
#endif
for (int i = 0; i < 6; i++) {
Serial1.write(_INITIAL_COMMAND[i]);
}
// @todo why 500 delay?
delay(500);
if (this->wait_for_bytes(_GENERIC_ACK_REPLY)) {
#ifdef cameraDebugSerial
Serial.println("INITIALISE success");
#endif
return 1;
}
#ifdef cameraDebugSerial
Serial.println("INITIALISE fail");
#endif
return 0;
}
boolean UCAMII::wait_for_bytes(byte command[6]) {
byte cam_reply;
int i = 0;
int received;
short found_bytes;
found_bytes = 0;
// @todo millis() wait, millis() wrap around watch out
// unsigned long start = millis();
#ifdef cameraDebugSerial
Serial.print("\r\nWAIT: ");
for (i = 0; i < 6; i++) {
Serial.print("0x");
Serial.print(command[i], HEX);
Serial.print(" ");
}
Serial.print("\r\nGOT : ");
i = 0;
#endif
while (Serial1.available()) {
cam_reply = Serial1.read();
if (i < 6 ) {
if ((cam_reply == command[i]) || command[i] == 0x00) {
found_bytes++;
i++;
}
}
#ifdef cameraDebugSerial
Serial.print("0x");
Serial.print(cam_reply, HEX);
Serial.println(" ");
#endif
received++;
if (found_bytes == 6) {
return true;
}
}
return false;
}
bool UCAMII::takePicture() {
if (send_initial()) {
if (this->set_package_size()) {
if (this->do_snapshot()) {
if (get_picture()) {
return 1;
}
}
}
}
}
int UCAMII::set_package_size() {
byte ack[6] = {0xAA, 0x0E, 0x06, 0x00, 0x00, 0x00};
delay(100);
#ifdef cameraDebugSerial
Serial.println("Sending packet size...");
#endif
for (int i = 0; i < 6; i++) {
Serial1.write(_PACK_SIZE[i]);
#ifdef cameraDebugSerial
Serial.print(_PACK_SIZE[i], HEX);
Serial.print(" ");
#endif
}
// @todo why 500 delay?
delay(500);
if (this->wait_for_bytes(ack)) {
#ifdef cameraDebugSerial
Serial.println("\r\npacket size success");
#endif
return 1;
}
#ifdef cameraDebugSerial
Serial.println("packet size fail");
#endif
return 0;
}
int UCAMII::do_snapshot() {
byte ack[6] = {0xAA, 0x0E, 0x05, 0x00, 0x00, 0x00};
delay(100);
#ifdef cameraDebugSerial
Serial.println("Sending snapshot...");
#endif
for (int i = 0; i < 6; i++)
{
Serial1.write(_SNAPSHOT[i]);
}
// @todo why 500 delay?
delay(500);
if (this->wait_for_bytes(ack)) {
#ifdef cameraDebugSerial
Serial.println("snapshot success");
#endif
return 1;
}
#ifdef cameraDebugSerial
Serial.println("snapshot fail");
#endif
return 0;
}
int UCAMII::get_picture() {
byte ack[6] = {0xAA, 0x0E, 0x04, 0x00, 0x00, 0x00};
unsigned long imageSize;
short i;
delay(100);
#ifdef cameraDebugSerial
Serial.println("Sending get picture...");
#endif
for (int i = 0; i < 6; i++)
{
Serial1.write(_GET_PICTURE[i]);
}
// @todo why 500 delay?
delay(500);
if (this->wait_for_bytes(ack)) {
#ifdef cameraDebugSerial
Serial.println("picture success");
#endif
// get the 6 bytes ACK
for (i = 0; i <= 5; i++) {
ack[i] = 0;
while (!Serial1.available());
ack[i] = Serial1.read();
// last 3 bytes are the image size
#ifdef cameraDebugSerial
Serial.print(i, DEC);
Serial.print(" value: ");
Serial.println(ack[i], HEX);
#endif
}
imageSize = 0;
imageSize = (imageSize << 8) | ack[5];
imageSize = (imageSize << 8) | ack[4];
imageSize = (imageSize << 8) | ack[3];
this->imageSize = imageSize;
this->image_pos = this->imageSize;
if (imageSize > 0) {
return 1;
}
}
#ifdef cameraDebugSerial
Serial.println("picture fail");
#endif
return 0;
}
// @todo return false if time exceeded
int UCAMII::getData() {
//Serial.println("THIS IS BEING RAN");
//Serial1.begin(115200);
unsigned char high = (unsigned char)(this->package_no >> 8);
unsigned char low = this->package_no & 0xff;
byte my_ack[6] = {0xAA, 0x0E, 0x00, 0x00, low, high};
int i = 0;
byte s;
int bytes;
// request bytes
for (int i = 0; i < 6; i++) {
Serial1.write(my_ack[i]);
}
// Set number of bytes we should wait for
if (this->image_pos < UCAMII_BUF_SIZE) {
bytes = this->image_pos + 6;
} else {
bytes = UCAMII_BUF_SIZE + 6;
}
#ifdef cameraDebugSerial
// Serial.print("REMAINING: ");
// Serial.print(this->image_pos, DEC);
// Serial.print(" BYTES PER CHUNK: ");
// Serial.println(bytes, DEC);
#endif
for (i = 0; i < bytes; i++) {
while (!Serial1.available());
// while (Serial1.available()) {
// Serial.println("The serial is not available.");
// Serial.println("Attempting to flush the serial.");
// Serial1.end();
// s = Serial1.read();
// break;
// } // wait for bytes
s = Serial1.read();
// Skip first 4 and last 2, Page 10 of the datasheet
if (i >= 4 && i < bytes - 2) {
#ifdef cameraDebugSerial
//Serial.print("*");
#endif
this->imgBuffer[i - 4] = s;
this->image_pos--; //hitting 0 before it needs to hit 0? add print statements
}
#ifdef cameraDebugSerial
//Serial.print(this->imgBuffer[s], HEX);
//Serial.print(HEX);
//fout << (this->imgBuffer[s], HEX);
//Serial.print(" ");
//fout << " ";
#endif
}
#ifdef cameraDebugSerial
Serial.println("");
#endif
this->package_no++;
if(this->image_pos <= 0) {
// send the final thank you goodbye package
my_ack[4] = 0xF0;
my_ack[5] = 0xF0;
for (int i = 0; i < 6; i++) {
Serial1.write(my_ack[i]);
}
if(this->image_pos == 0) {
for(int i = 0;i<6;i++){
Serial1.write(_RESET[i]);
}
//Serial.println("RESET SUCCESSFUL");
this->image_pos = 0;
this->package_no = 0;
}
}
return bytes-6;
}
//closeFile();