-
Notifications
You must be signed in to change notification settings - Fork 2
/
cam.cpp
329 lines (278 loc) · 7.56 KB
/
cam.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
#include "cam.h"
const byte cameraAddr = (CAM_ADDR << 5); // addr
unsigned long picTotalLen = 0; // picture length
File myFile;
/**
camera functions
*/
void clearRxBuf()
{
while (CAM_SERIAL.available())
{
CAM_SERIAL.read();
}
}
void cam_sendCmd(char cmd[], int cmd_len)
{
for (char i = 0; i < cmd_len; i++) CAM_SERIAL.print(cmd[i]);
}
void camera_initialize()
{
char cmd[] = {0xaa, 0x0d | cameraAddr, 0x00, 0x00, 0x00, 0x00} ;
unsigned char resp[6];
int init_cnt = 5;
bool init_state = false;
CAM_SERIAL.setTimeout(500);
while (1)
{
init_cnt --;
if (0 == init_cnt) {
break;
}
//clearRxBuf();
cam_sendCmd(cmd, 6);
if (CAM_SERIAL.readBytes((char *)resp, 6) != 6)
{
continue;
}
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x0d && resp[4] == 0 && resp[5] == 0)
{
if (CAM_SERIAL.readBytes((char *)resp, 6) != 6) continue;
if (resp[0] == 0xaa && resp[1] == (0x0d | cameraAddr) && resp[2] == 0 && resp[3] == 0 && resp[4] == 0 && resp[5] == 0) {
init_state = true;
break;
}
}
}
cmd[1] = 0x0e | cameraAddr;
cmd[2] = 0x0d;
cam_sendCmd(cmd, 6);
if (init_state) {
Serial.println("\nCamera initialization done.\n");
} else {
Serial.println("\nCamera initialize failed...\n");
}
}
int preCapture(void)
{
char cmd[] = { 0xaa, 0x01 | cameraAddr, 0x00, 0x07, 0x00, PIC_FMT };
unsigned char resp[6];
int err_cnt = 0;
CAM_SERIAL.setTimeout(100);
while (err_cnt < 10)
{
clearRxBuf();
cam_sendCmd(cmd, 6);
if (CAM_SERIAL.readBytes((char *)resp, 6) != 6) continue;
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x01 && resp[4] == 0 && resp[5] == 0)
{
return 0;
}
err_cnt ++;
}
return -1;
}
int Capture(void)
{
char cmd[] = { 0xaa, 0x06 | cameraAddr, 0x08, PIC_PKT_LEN & 0xff, (PIC_PKT_LEN >> 8) & 0xff , 0};
unsigned char resp[6];
int err_cnt = 0;
CAM_SERIAL.setTimeout(100);
while (err_cnt < 10)
{
clearRxBuf();
cam_sendCmd(cmd, 6);
if (CAM_SERIAL.readBytes((char *)resp, 6) != 6) continue;
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x06 && resp[4] == 0 && resp[5] == 0) break;
err_cnt ++;
}
cmd[1] = 0x05 | cameraAddr;
cmd[2] = 0;
cmd[3] = 0;
cmd[4] = 0;
cmd[5] = 0;
err_cnt = 0;
while (err_cnt < 10)
{
clearRxBuf();
cam_sendCmd(cmd, 6);
if (CAM_SERIAL.readBytes((char *)resp, 6) != 6) continue;
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x05 && resp[4] == 0 && resp[5] == 0) break;
err_cnt ++;
}
cmd[1] = 0x04 | cameraAddr;
cmd[2] = 0x1;
err_cnt = 0;
while (err_cnt < 10)
{
clearRxBuf();
cam_sendCmd(cmd, 6);
if (CAM_SERIAL.readBytes((char *)resp, 6) != 6) continue;
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x04 && resp[4] == 0 && resp[5] == 0)
{
CAM_SERIAL.setTimeout(1000);
if (CAM_SERIAL.readBytes((char *)resp, 6) != 6)
{
continue;
}
if (resp[0] == 0xaa && resp[1] == (0x0a | cameraAddr) && resp[2] == 0x01)
{
picTotalLen = (resp[3]) | (resp[4] << 8) | (resp[5] << 16);
Serial.println("picTotalLen:");
Serial.println(picTotalLen);
break;
}
}
err_cnt ++;
}
return 0;
}
int GetData(void)
{
unsigned int pktCnt = (picTotalLen) / (PIC_PKT_LEN - 6);
if ((picTotalLen % (PIC_PKT_LEN - 6)) != 0) pktCnt += 1;
char cmd[] = { 0xaa, 0x0e | cameraAddr, 0x00, 0x00, 0x00, 0x00 };
unsigned char pkt[PIC_PKT_LEN];
char picName[] = "pic.jpg"; //make a jpg file named "pic.jpg", the file will be removed if it has already exist.
if (SD.exists(picName)) {
SD.remove(picName);
}
myFile = SD.open(picName, FILE_WRITE);
if (!myFile) {
Serial.print("Error: [");
Serial.print(__LINE__);
Serial.print("]: ");
Serial.println("Open file error!");
return -1;
}
CAM_SERIAL.setTimeout(1000);
for (unsigned int i = 0; i < pktCnt; i++)
{
cmd[4] = i & 0xff;
cmd[5] = (i >> 8) & 0xff;
int retry_cnt = 0;
retry:
delay(10);
clearRxBuf();
cam_sendCmd(cmd, 6);
uint16_t cnt = CAM_SERIAL.readBytes((char *)pkt, PIC_PKT_LEN);
unsigned char sum = 0;
for (int y = 0; y < cnt - 2; y++)
{
sum += pkt[y];
}
if (sum != pkt[cnt - 2])
{
if (++retry_cnt < 100) goto retry;
else break;
}
myFile.write((const uint8_t *)&pkt[4], cnt - 6); //write the date getted from camera.
}
cmd[4] = 0xf0;
cmd[5] = 0xf0;
cam_sendCmd(cmd, 6);
myFile.close();
return 0;
}
int sendData(void) {
const int dataSize = 127;
byte dataBuffer[dataSize];
int tail = 0;
// uint32_t time_cnt = 0;
delay(1000);
File photoFile = SD.open("pic.jpg");
if (!photoFile) {
Serial.print("Error: [");
Serial.print(__LINE__);
Serial.print("]: ");
Serial.println("Open photoFile error!");
return -1;
}
while (photoFile.position() < photoFile.size()) { //do when there is bytes in jpg file.
// if(0 == (tail % dataSize)){
// time_cnt = millis();
// }
dataBuffer[tail++] = photoFile.read(); //fullfill the databuffer
if (tail == dataSize) { //if already get dataSize byte from jpg file
int val;
// Serial.print("Read photoFile by 127 bytes: ");
// Serial.println(millis() - time_cnt);
do {
// time_cnt = millis();
int sum = 0;
for (int i = 0; i < dataSize; i++) {
RF_SERIAL.write(dataBuffer[i]); // send the data in buffer
sum += dataBuffer[i];
sum = sum % 0xFF; //calc the check byte.
}
RF_SERIAL.write(sum); //send the check byte.
tail = 0;
// Serial.print("Send dataSize + 1 bytes: ");
// Serial.println(millis() - time_cnt);
while (RF_SERIAL.available() <= 0) {
}
val = RF_SERIAL.read();
if (val == COMM_CAN ) {
Serial.println("Received cancle!");
photoFile.close();
return -2;
} else if (val == COMM_ACK) {
break;
} else if (val != COMM_NAK) {
// Serial.println("Received rubbish!");
photoFile.close();
return -3;
}
} while (val == COMM_NAK);
}
}
if (tail > 0) {
int val;
do {
int sum = 0;
for (int i = 0; i < dataSize; i++) {
if (i < tail) {
RF_SERIAL.write(dataBuffer[i]);
sum += dataBuffer[i];
sum = sum % 0xFF;
} else {
RF_SERIAL.write(0); //if there are no bytes in jpg file, fill it with 0x00;
}
}
Serial.print("[");
Serial.print(__LINE__);
Serial.print("]: ");
Serial.println("Sent last pic data!");
RF_SERIAL.write(sum);
while (RF_SERIAL.available() <= 0) {
}
val = RF_SERIAL.read();
if (val == COMM_CAN ) {
Serial.println("Received cancle!");
photoFile.close();
return -2;
} else if (val == COMM_ACK) {
break;
} else if (val != COMM_NAK) {
Serial.println("Received rubbish!");
photoFile.close();
return -2;
}
} while (val == COMM_NAK);
} /*End of "while (photoFile.position() < photoFile.size())"" */
photoFile.close();
return 0;
}
void cam_init(void) {
// Camera init
pinMode(5, OUTPUT); // CS pin of SD Card Shield
if (!SD.begin(5))
{
Serial.println("sd init failed\n");
// return;
} else {
Serial.println("sd init done.\n");
}
CAM_SERIAL.begin(115200);
camera_initialize();
}