-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathCrittergram-Capture-Cam.ino
377 lines (294 loc) · 9.65 KB
/
Crittergram-Capture-Cam.ino
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
//Crittergram Capture Cam
//by Julius Schmiedel
//for http://makezine.com/weekendprojects/
//Camera definitions
#define VC0706_PROTOCOL_SIGN 0x56
#define VC0706_SERIAL_NUMBER 0x00
#define VC0706_COMMAND_RESET 0x26
#define VC0706_COMMAND_GEN_VERSION 0x11
#define VC0706_COMMAND_TV_OUT_CTRL 0x44
#define VC0706_COMMAND_OSD_ADD_CHAR 0x45
#define VC0706_COMMAND_DOWNSIZE_SIZE 0x53
#define VC0706_COMMAND_READ_FBUF 0x32
#define FBUF_CURRENT_FRAME 0
#define FBUF_NEXT_FRAME 0
#define VC0706_COMMAND_FBUF_CTRL 0x36
#define VC0706_COMMAND_COMM_MOTION_CTRL 0x37
#define VC0706_COMMAND_COMM_MOTION_DETECTED 0x39
#define VC0706_COMMAND_POWER_SAVE_CTRL 0x3E
#define VC0706_COMMAND_COLOR_CTRL 0x3C
#define VC0706_COMMAND_MOTION_CTRL 0x42
#define VC0706_COMMAND_WRITE_DATA 0x31
#define VC0706_COMMAND_GET_FBUF_LEN 0x34
#define READ_DATA_BLOCK_NO 56
unsigned char tx_counter;
unsigned char tx_vcbuffer[20];
bool tx_ready;
bool rx_ready;
unsigned char rx_counter;
unsigned char VC0706_rx_buffer[80];
uint32_t frame_length=0;
uint32_t vc_frame_address =0;
uint32_t last_data_length=0;
// SD card stuff
#include <SD.h>
File myFile;
// where is the SD card connected? seeed = SS, ethernet shield = 4
const uint8_t SdChipSelect = SS;
char myFileName[16];
int myFileNr=1;
void setup()
{
// we are using the standard SD library. pin 4 = ardunio ethernet shield - pin 10 = seeed sd shield
SD.begin(SdChipSelect);
Serial.begin(115200);
// set jpeg quality
VC0706_compression_ratio(66);
delay(100);
capture_photo("test.jpg");
// turn on power for PIR on pin 8
pinMode(8, OUTPUT);
digitalWrite(8, HIGH);
}
void loop()
{
// create a new file each and every time
while (myFileNr != 0) {
sprintf(myFileName, "CCC%03d.jpg", myFileNr);
if (SD.exists(myFileName) == false) break;
myFileNr++;
}
// wait for movement - PIR out will turn HIGH
while (digitalRead(7) == LOW) {
delay(1);
}
// take picture
capture_photo(myFileName);
delay(10);
}
/*******************************************************************************
* Function Name : capture_photo
* Description : capture a photo and store the file named temp.jpg into SD
*
* Input : None
*
* Output : None
* Return : None
*******************************************************************************/
void capture_photo(char myFileName[]) {
// make sure video engine is running
VC0706_frame_control(3);
delay(10);
// Check to see if the file exists:
// if exists,delete the file:
if(SD.exists(myFileName)) SD.remove(myFileName);
// freeze video enginge
VC0706_frame_control(0);
delay(10);
rx_ready=false;
rx_counter=0;
Serial.end(); // clear all rx buffer
delay(5);
Serial.begin(115200);
//get frame buffer length
VC0706_get_framebuffer_length(0);
delay(10);
buffer_read();
// store frame buffer length for coming reading
frame_length=(VC0706_rx_buffer[5]<<8)+VC0706_rx_buffer[6];
frame_length=frame_length<<16;
frame_length=frame_length+(0x0ff00&(VC0706_rx_buffer[7]<<8))+VC0706_rx_buffer[8];
vc_frame_address =READ_DATA_BLOCK_NO;
myFile=SD.open(myFileName, FILE_WRITE);
while(vc_frame_address<frame_length){
VC0706_read_frame_buffer(vc_frame_address-READ_DATA_BLOCK_NO, READ_DATA_BLOCK_NO);
delay(9);
//get the data with length=READ_DATA_BLOCK_NObytes
rx_ready=false;
rx_counter=0;
buffer_read();
// write data to file
myFile.write(VC0706_rx_buffer+5,READ_DATA_BLOCK_NO);
//read next READ_DATA_BLOCK_NO bytes from frame buffer
vc_frame_address=vc_frame_address+READ_DATA_BLOCK_NO;
}
// get the last data
vc_frame_address=vc_frame_address-READ_DATA_BLOCK_NO;
last_data_length=frame_length-vc_frame_address;
VC0706_read_frame_buffer(vc_frame_address,last_data_length);
delay(9);
//get the data
rx_ready=false;
rx_counter=0;
buffer_read();
myFile.write(VC0706_rx_buffer+5,last_data_length);
myFile.close();
// restart video enginge
VC0706_frame_control(3);
delay(10);
}
/*******************************************************************************
* Function Name : VC0706_read_frame_buffer
* Description : read image data from FBUF
*
* Input : buffer_address(4 bytes); buffer_length(4 bytes) ******YO!*******
*
* Output : None
* Return : None
*******************************************************************************/
void VC0706_read_frame_buffer(unsigned long buffer_address, unsigned long buffer_length)
{
tx_vcbuffer[0]=VC0706_PROTOCOL_SIGN;
tx_vcbuffer[1]=VC0706_SERIAL_NUMBER;
tx_vcbuffer[2]=VC0706_COMMAND_READ_FBUF;
tx_vcbuffer[3]=0x0c;
tx_vcbuffer[4]=FBUF_CURRENT_FRAME;
tx_vcbuffer[5]=0x0a; // 0x0a=data transfer by MCU mode; 0x0f=data transfer by SPI interface
tx_vcbuffer[6]=buffer_address>>24; //starting address
tx_vcbuffer[7]=buffer_address>>16;
tx_vcbuffer[8]=buffer_address>>8;
tx_vcbuffer[9]=buffer_address&0x0ff;
tx_vcbuffer[10]=buffer_length>>24; // data length
tx_vcbuffer[11]=buffer_length>>16;
tx_vcbuffer[12]=buffer_length>>8;
tx_vcbuffer[13]=buffer_length&0x0ff;
tx_vcbuffer[14]=0x00; // delay time
tx_vcbuffer[15]=0x0a;
tx_counter=16;
buffer_send();
}
/*******************************************************************************
* Function Name : VC0706_frame_control
* Description : control frame buffer register ******YO!*******
*
* Input : frame_control=control flag(1byte)
* : 0 = stop current frame ; 1= stop next frame;2=step frame;3 =resume frame;
*
* Output : None
* Return : None
*******************************************************************************/
void VC0706_frame_control(byte frame_control)
{
if(frame_control>3)frame_control=3;
tx_vcbuffer[0]=VC0706_PROTOCOL_SIGN;
tx_vcbuffer[1]=VC0706_SERIAL_NUMBER;
tx_vcbuffer[2]=VC0706_COMMAND_FBUF_CTRL;
tx_vcbuffer[3]=0x01;
tx_vcbuffer[4]=frame_control;
tx_counter=5;
buffer_send();
}
/*******************************************************************************
* Function Name : VC0706_get_framebuffer_length
* Description : get byte-lengths in FBUF
*
* Input : fbuf_type =current or next frame
* 0 = current frame ******YO!*******
* 1 = next frame
*
* Output : None
* Return : None
*******************************************************************************/
void VC0706_get_framebuffer_length(byte fbuf_type)
{
if(fbuf_type>1)fbuf_type=1;
tx_vcbuffer[0]=VC0706_PROTOCOL_SIGN;
tx_vcbuffer[1]=VC0706_SERIAL_NUMBER;
tx_vcbuffer[2]=VC0706_COMMAND_GET_FBUF_LEN;
tx_vcbuffer[3]=0x01;
tx_vcbuffer[4]=fbuf_type;
tx_counter=5;
buffer_send();
}
/*******************************************************************************
* Function Name : VC0706_uart_power_save
* Description : stop current frame for reading
*
* Input : power_on =1 start power-save
* = 0 stop power-save
*
* Output : None
* Return : None
*******************************************************************************/
void VC0706_uart_power_save(byte power_save_on)
{
tx_vcbuffer[0]=VC0706_PROTOCOL_SIGN;
tx_vcbuffer[1]=VC0706_SERIAL_NUMBER;
tx_vcbuffer[2]=VC0706_COMMAND_POWER_SAVE_CTRL;
tx_vcbuffer[3]=0x03;
tx_vcbuffer[4]=00; //power save control mode
tx_vcbuffer[5]=01; // control by UART
tx_vcbuffer[6]=power_save_on; //start power save
tx_counter=7;
buffer_send();
}
/*******************************************************************************
* Function Name : VC0706_compression_ratio
* Description : stop current frame for reading
*
* Input : ration >13(minimum) ******YO!*******
* <63(max)
*
* Output : None
* Return : None
*******************************************************************************/
void VC0706_compression_ratio(int ratio)
{
if(ratio>63)ratio=63;
if(ratio<13)ratio=13;
int vc_comp_ratio=(ratio-13)*4+53;
tx_vcbuffer[0]=VC0706_PROTOCOL_SIGN;
tx_vcbuffer[1]=VC0706_SERIAL_NUMBER;
tx_vcbuffer[2]=VC0706_COMMAND_WRITE_DATA;
tx_vcbuffer[3]=0x05;
tx_vcbuffer[4]=01; //chip register
tx_vcbuffer[5]=0x01; //data num ready to write
tx_vcbuffer[6]=0x12; //register address
tx_vcbuffer[7]=0x04;
tx_vcbuffer[8]=vc_comp_ratio; //data
tx_counter=9;
buffer_send();
}
/*******************************************************************************
* Function Name : buffer_send
* Description : Transmit buffer to VC0706
*
* Input : tx_vcbuffer
*
* Output : None
* Return : None
*******************************************************************************/
void buffer_send()
{
int i=0;
for (i=0;i<tx_counter;i++)
Serial.write(tx_vcbuffer[i]);
tx_ready=true;
}
/*******************************************************************************
* Function Name : buffer_read
* Description : Receive buffer from VC0706
*
* Input : None
*
* Output : rx_buffer, rx_ready
* Return : None
*******************************************************************************/
void buffer_read()
{
bool validity=true;
if (rx_ready) // if something unread in buffer, just quit
return;
rx_counter=0;
VC0706_rx_buffer[0]=0;
while (Serial.available() > 0)
{
VC0706_rx_buffer[rx_counter++]= Serial.read();
//delay(1);
}
if (VC0706_rx_buffer[0]!=0x76)
validity=false;
if (VC0706_rx_buffer[1]!=VC0706_SERIAL_NUMBER)
validity=false;
if (validity) rx_ready=true;
}