Skip to content
Browse files

fixed resumevideo ('datasheet' was wrong) - added two examples

  • Loading branch information...
1 parent 5c4fe2c commit 119313eae0f6701dcd20ee4b37bfe699f66a3810 @ladyada ladyada committed
Showing with 293 additions and 7 deletions.
  1. +48 −4 VC0706.cpp
  2. +10 −3 VC0706.h
  3. +125 −0 examples/MotionDetect/MotionDetect.pde
  4. +110 −0 examples/Snapshot/Snapshot.pde
View
52 VC0706.cpp
@@ -114,11 +114,47 @@ char * VC0706::getVersion(void) {
/****************** high level photo comamnds */
+boolean 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 VC0706::takePicture() {
frameptr = 0;
return cameraFrameBuffCtrl(VC0706_STOPCURRENTFRAME);
}
+boolean VC0706::resumeVideo() {
+ return cameraFrameBuffCtrl(VC0706_RESUMEFRAME);
+}
+
+boolean VC0706::TVon() {
+ uint8_t args[] = {0x1, 0x1};
+ return runCommand(VC0706_TVOUT_CTRL, args, sizeof(args), 5);
+}
+boolean VC0706::TVoff() {
+ uint8_t args[] = {0x1, 0x0};
+ return runCommand(VC0706_TVOUT_CTRL, args, sizeof(args), 5);
+}
boolean VC0706::cameraFrameBuffCtrl(uint8_t command) {
uint8_t args[] = {0x1, command};
@@ -138,6 +174,7 @@ uint32_t VC0706::frameLength(void) {
len |= camerabuff[7];
len <<= 8;
len |= camerabuff[8];
+
return len;
}
@@ -153,12 +190,15 @@ uint8_t * VC0706::readPicture(uint8_t n) {
0, 0, 0, n,
CAMERADELAY >> 8, CAMERADELAY & 0xFF};
- if (! runCommand(VC0706_READ_FBUF, args, sizeof(args), 5))
+ if (! runCommand(VC0706_READ_FBUF, args, sizeof(args), 5, false))
return 0;
+
// read into the buffer PACKETLEN!
- if (readResponse(n+5, 250) == 0)
+ if (readResponse(n+5, CAMERADELAY) == 0)
return 0;
+
+
frameptr += n;
return camerabuff;
@@ -168,9 +208,11 @@ uint8_t * VC0706::readPicture(uint8_t n) {
boolean VC0706::runCommand(uint8_t cmd, uint8_t *args, uint8_t argn,
- uint8_t resplen) {
+ uint8_t resplen, boolean flushflag) {
// flush out anything in the buffer?
- readResponse(100, 200);
+ if (flushflag) {
+ readResponse(100, 10);
+ }
sendCommand(cmd, args, argn);
if (readResponse(resplen, 200) != resplen)
@@ -187,6 +229,8 @@ void VC0706::sendCommand(uint8_t cmd, uint8_t args[] = 0, uint8_t argn = 0) {
for (uint8_t i=0; i<argn; i++) {
camera->print(args[i], BYTE);
+ //Serial.print(" 0x");
+ //Serial.print(args[i], HEX);
}
}
View
13 VC0706.h
@@ -20,11 +20,14 @@
#define VC0706_COMM_MOTION_DETECTED 0x39
#define VC0706_MOTION_CTRL 0x42
#define VC0706_MOTION_STATUS 0x43
+#define VC0706_TVOUT_CTRL 0x44
+#define VC0706_OSD_ADD_CHAR 0x45
#define VC0706_STOPCURRENTFRAME 0x0
#define VC0706_STOPNEXTFRAME 0x1
-#define VC0706_RESUMEFRAME 0x2
-#define VC0706_STEPFRAME 0x3
+#define VC0706_RESUMEFRAME 0x3
+#define VC0706_STEPFRAME 0x2
+
#define VC0706_640x480 0x00
#define VC0706_320x240 0x11
#define VC0706_160x120 0x22
@@ -43,8 +46,11 @@ class VC0706 {
VC0706(NewSoftSerial *ser);
boolean begin(uint16_t baud = 38400);
boolean reset(void);
+ boolean TVon(void);
+ boolean TVoff(void);
boolean takePicture(void);
uint8_t *readPicture(uint8_t n);
+ boolean resumeVideo(void);
uint32_t frameLength(void);
char *getVersion(void);
uint8_t available();
@@ -58,6 +64,7 @@ class VC0706 {
boolean setMotionDetect(boolean f);
boolean setMotionStatus(uint8_t x, uint8_t d1, uint8_t d2);
boolean cameraFrameBuffCtrl(uint8_t command);
+ boolean OSD(uint8_t x, uint8_t y, char *str);
private:
uint8_t _rx, _tx;
@@ -70,7 +77,7 @@ class VC0706 {
NewSoftSerial *camera;
- boolean runCommand(uint8_t cmd, uint8_t args[], uint8_t argn, uint8_t resp);
+ boolean runCommand(uint8_t cmd, uint8_t args[], uint8_t argn, uint8_t resp, boolean flushflag = true);
void sendCommand(uint8_t cmd, uint8_t args[], uint8_t argn);
uint8_t readResponse(uint8_t numbytes, uint8_t timeout);
boolean verifyResponse(uint8_t command);
View
125 examples/MotionDetect/MotionDetect.pde
@@ -0,0 +1,125 @@
+// This is a motion-detect camera sketch using the VC0706 library
+// Basically, on start, the Arduino will find the camera
+// and SD card and turn on motion detection. If motion is detected,
+// the camera will snap a photo that is saved to the SD card
+// public domain
+
+
+#include <VC0706.h>
+#include <NewSoftSerial.h>
+#include <SD.h>
+
+// This is the SD card chip select line, 10 is common
+#define chipSelect 10
+// This is the camera pin connection. Connect the camera TX
+// to pin 2, camera RX to pin 3
+NewSoftSerial cameraconnection = NewSoftSerial(2, 3);
+// pass the serial connection to the camera object
+VC0706 cam = VC0706(&cameraconnection);
+
+
+void setup() {
+ Serial.begin(9600);
+ Serial.println("VC0706 Camera test");
+
+ // see if the card is present and can be initialized:
+ if (!SD.begin(chipSelect)) {
+ Serial.println("Card failed, or not present");
+ // don't do anything more:
+ return;
+ }
+
+ // Try to locate the camera
+ if (cam.begin()) {
+ Serial.println("Camera Found:");
+ } else {
+ Serial.println("No camera found?");
+ return;
+ }
+ // Print out the camera version information (optional)
+ char *reply = cam.getVersion();
+ if (reply == 0) {
+ Serial.print("Failed to get version");
+ } else {
+ Serial.println("-----------------");
+ Serial.print(reply);
+ Serial.println("-----------------");
+ }
+
+ // Set the picture size - you can choose one of 640x480, 320x240 or 160x120
+ // Remember that bigger pictures take longer to transmit!
+
+ //cam.setImageSize(VC0706_640x480); // biggest
+ cam.setImageSize(VC0706_320x240); // medium
+ //cam.setImageSize(VC0706_160x120); // small
+
+ // You can read the size back from the camera (optional, but maybe useful?)
+ uint8_t imgsize = cam.getImageSize();
+ Serial.print("Image size: ");
+ if (imgsize == VC0706_640x480) Serial.println("640x480");
+ if (imgsize == VC0706_320x240) Serial.println("320x240");
+ if (imgsize == VC0706_160x120) Serial.println("160x120");
+
+
+ // Motion detection system can alert you when the camera 'sees' motion!
+ cam.setMotionDetect(true); // turn it on
+ //cam.setMotionDetect(false); // turn it off (default)
+
+ // You can also verify whether motion detection is active!
+ Serial.print("Motion detection is ");
+ if (cam.getMotionDetect())
+ Serial.println("ON");
+ else
+ Serial.println("OFF");
+}
+
+
+
+
+void loop() {
+ if (cam.motionDetected()) {
+ Serial.println("Motion!");
+ cam.setMotionDetect(false);
+
+ if (! cam.takePicture())
+ Serial.println("Failed to snap!");
+ else
+ Serial.println("Picture taken!");
+
+ char filename[13];
+ strcpy(filename, "IMAGE00.JPG");
+ for (int i = 0; i < 100; i++) {
+ filename[5] = '0' + i/10;
+ filename[6] = '0' + i%10;
+ // create if does not exist, do not open existing, write, sync after write
+ if (! SD.exists(filename)) {
+ break;
+ }
+ }
+
+ File imgFile = SD.open(filename, FILE_WRITE);
+
+ uint16_t jpglen = cam.frameLength();
+ Serial.print(jpglen, DEC);
+ Serial.println(" byte image");
+
+ Serial.print("Writing image to "); Serial.print(filename);
+
+ while (jpglen != 0) {
+ // read 64 bytes at a time;
+ uint8_t *buffer;
+ uint8_t bytesToRead = min(64, jpglen);
+ buffer = cam.readPicture(bytesToRead);
+ imgFile.write(buffer, bytesToRead);
+
+ //Serial.print("Read "); Serial.print(bytesToRead, DEC); Serial.println(" bytes");
+
+ jpglen -= bytesToRead;
+ }
+ imgFile.close();
+ Serial.println("...Done!");
+ cam.resumeVideo();
+ cam.setMotionDetect(true);
+ }
+}
+
View
110 examples/Snapshot/Snapshot.pde
@@ -0,0 +1,110 @@
+// This is a basic snapshot sketch using the VC0706 library
+// Basically, on start, the Arduino will find the camera
+// and SD card and snap a photo that is saved to the SD card
+// public domain
+
+#include <VC0706.h>
+#include <NewSoftSerial.h>
+#include <SD.h>
+
+// This is the SD card chip select line, 10 is common
+#define chipSelect 10
+// This is the camera pin connection. Connect the camera TX
+// to pin 2, camera RX to pin 3
+NewSoftSerial cameraconnection = NewSoftSerial(2, 3);
+// pass the serial connection to the camera object
+VC0706 cam = VC0706(&cameraconnection);
+
+void setup() {
+ Serial.begin(9600);
+ Serial.println("VC0706 Camera snapshot test");
+
+ // see if the card is present and can be initialized:
+ if (!SD.begin(chipSelect)) {
+ Serial.println("Card failed, or not present");
+ // don't do anything more:
+ return;
+ }
+
+ // Try to locate the camera
+ if (cam.begin()) {
+ Serial.println("Camera Found:");
+ } else {
+ Serial.println("No camera found?");
+ return;
+ }
+ // Print out the camera version information (optional)
+ char *reply = cam.getVersion();
+ if (reply == 0) {
+ Serial.print("Failed to get version");
+ } else {
+ Serial.println("-----------------");
+ Serial.print(reply);
+ Serial.println("-----------------");
+ }
+
+ // Set the picture size - you can choose one of 640x480, 320x240 or 160x120
+ // Remember that bigger pictures take longer to transmit!
+
+ cam.setImageSize(VC0706_640x480); // biggest
+ //cam.setImageSize(VC0706_320x240); // medium
+ //cam.setImageSize(VC0706_160x120); // small
+
+ // You can read the size back from the camera (optional, but maybe useful?)
+ uint8_t imgsize = cam.getImageSize();
+ Serial.print("Image size: ");
+ if (imgsize == VC0706_640x480) Serial.println("640x480");
+ if (imgsize == VC0706_320x240) Serial.println("320x240");
+ if (imgsize == VC0706_160x120) Serial.println("160x120");
+
+ if (! cam.takePicture())
+ Serial.println("Failed to snap!");
+ else
+ Serial.println("Picture taken!");
+
+ // Create an image with the name IMAGExx.JPG
+ char filename[13];
+ strcpy(filename, "IMAGE00.JPG");
+ for (int i = 0; i < 100; i++) {
+ filename[5] = '0' + i/10;
+ filename[6] = '0' + i%10;
+ // create if does not exist, do not open existing, write, sync after write
+ if (! SD.exists(filename)) {
+ break;
+ }
+ }
+
+ // Open the file for writing
+ File imgFile = SD.open(filename, FILE_WRITE);
+
+ // Get the size of the image (frame) taken
+ uint16_t jpglen = cam.frameLength();
+ Serial.print(jpglen, DEC);
+ Serial.println(" byte image");
+
+ int32_t time = millis();
+ pinMode(8, OUTPUT);
+ // Read all the data up to # bytes!
+ while (jpglen != 0) {
+ // read 64 bytes at a time;
+ uint8_t *buffer;
+ uint8_t bytesToRead = min(64, jpglen);
+ buffer = cam.readPicture(bytesToRead);
+ imgFile.write(buffer, bytesToRead);
+ //Serial.print("Read "); Serial.print(bytesToRead, DEC); Serial.println(" bytes");
+ jpglen -= bytesToRead;
+ }
+ imgFile.close();
+
+ time = millis() - time;
+ Serial.println("Done!");
+ Serial.print("Took "); Serial.print(time); Serial.println(" ms");
+}
+
+
+
+
+void loop() {
+}
+
+

0 comments on commit 119313e

Please sign in to comment.
Something went wrong with that request. Please try again.