Skip to content
Browse files

Arduino Mega compatible (pls follow code comments!)

  • Loading branch information...
1 parent db5df3a commit b33587268420e55242db799456ce47985566063b @PaintYourDragon PaintYourDragon committed
Showing with 234 additions and 82 deletions.
  1. +76 −37 VC0706.cpp
  2. +10 −13 VC0706.h
  3. +69 −13 examples/MotionDetect/MotionDetect.pde
  4. +79 −19 examples/Snapshot/Snapshot.pde
View
113 VC0706.cpp
@@ -3,23 +3,37 @@
#include "VC0706.h"
+// Initialization code used by all constructor types
+void VC0706::common_init(void) {
+ swSerial = NULL;
+ hwSerial = NULL;
+ frameptr = 0;
+ bufferLen = 0;
+ serialNum = 0;
+}
+// Constructor when using SoftwareSerial or NewSoftSerial
#if ARDUINO >= 100
VC0706::VC0706(SoftwareSerial *ser) {
#else
VC0706::VC0706(NewSoftSerial *ser) {
#endif
- camera = ser;
- frameptr = 0;
- bufferLen = 0;
+ common_init(); // Set everything to common state, then...
+ swSerial = ser; // ...override swSerial with value passed.
+}
+
+// Constructor when using HardwareSerial
+VC0706::VC0706(HardwareSerial *ser) {
+ common_init(); // Set everything to common state, then...
+ hwSerial = ser; // ...override hwSerial with value passed.
}
boolean VC0706::begin(uint16_t baud) {
- camera->begin(baud);
+ if(swSerial) swSerial->begin(baud);
+ else hwSerial->begin(baud);
return reset();
}
-
boolean VC0706::reset() {
uint8_t args[] = {0x0};
@@ -284,50 +298,75 @@ boolean VC0706::runCommand(uint8_t cmd, uint8_t *args, uint8_t argn,
}
void VC0706::sendCommand(uint8_t cmd, uint8_t args[] = 0, uint8_t argn = 0) {
+ if(swSerial) {
#if ARDUINO >= 100
- camera->write((byte)0x56);
- camera->write((byte)cameraSerial);
- camera->write((byte)cmd);
-
- for (uint8_t i=0; i<argn; i++) {
- camera->write((byte)args[i]);
- //Serial.print(" 0x");
- //Serial.print(args[i], HEX);
- }
+ 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
- camera->print(0x56, BYTE);
- camera->print(cameraSerial, BYTE);
- camera->print(cmd, BYTE);
-
- for (uint8_t i=0; i<argn; i++) {
- camera->print(args[i], BYTE);
- //Serial.print(" 0x");
- //Serial.print(args[i], HEX);
- }
+ 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 {
+#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
+ }
}
-
uint8_t VC0706::readResponse(uint8_t numbytes, uint8_t timeout) {
uint8_t counter = 0;
- bufferLen=0;
+ bufferLen = 0;
+ int avail;
- while ((timeout != counter) && (bufferLen != numbytes)){
- if (! camera->available()) {
- delay(1);
- counter++;
- continue;
- }
- counter = 0;
- // there's a byte!
- camerabuff[bufferLen++] = camera->read();
- }
- return bufferLen;
+ while ((timeout != counter) && (bufferLen != numbytes)){
+ avail = swSerial ? swSerial->available() : hwSerial->available();
+ if (avail <= 0) {
+ delay(1);
+ counter++;
+ continue;
+ }
+ counter = 0;
+ // there's a byte!
+ camerabuff[bufferLen++] = swSerial ? swSerial->read() : hwSerial->read();
+ }
+ return bufferLen;
}
boolean VC0706::verifyResponse(uint8_t command) {
if ((camerabuff[0] != 0x76) ||
- (camerabuff[1] != cameraSerial) ||
+ (camerabuff[1] != serialNum) ||
(camerabuff[2] != command) ||
(camerabuff[3] != 0x0))
return false;
View
23 VC0706.h
@@ -1,7 +1,6 @@
// This is a simple VC0706 library that doesnt suck
// (c) adafruit - MIT license - https://github.com/adafruit/
-
#if ARDUINO >= 100
#include "Arduino.h"
#include "SoftwareSerial.h"
@@ -10,7 +9,6 @@
#include "NewSoftSerial.h"
#endif
-
#define VC0706_RESET 0x26
#define VC0706_GEN_VERSION 0x11
#define VC0706_READ_FBUF 0x32
@@ -51,10 +49,11 @@
class VC0706 {
public:
#if ARDUINO >= 100
- VC0706(SoftwareSerial *ser);
+ VC0706(SoftwareSerial *ser); // Constructor when using SoftwareSerial
#else
- VC0706(NewSoftSerial *ser);
+ VC0706(NewSoftSerial *ser); // Constructor when using NewSoftSerial
#endif
+ VC0706(HardwareSerial *ser); // Constructor when using HardwareSerial
boolean begin(uint16_t baud = 38400);
boolean reset(void);
boolean TVon(void);
@@ -84,20 +83,18 @@ class VC0706 {
void OSD(uint8_t x, uint8_t y, char *s); // isnt supported by the chip :(
private:
- uint8_t _rx, _tx;
- uint16_t baud;
- uint8_t cameraSerial;
-
- uint8_t camerabuff[CAMERABUFFSIZ+1];
- uint8_t bufferLen;
+ uint8_t serialNum;
+ uint8_t camerabuff[CAMERABUFFSIZ+1];
+ uint8_t bufferLen;
uint16_t frameptr;
-
#if ARDUINO >= 100
- SoftwareSerial *camera;
+ SoftwareSerial *swSerial;
#else
- NewSoftSerial *camera;
+ NewSoftSerial *swSerial;
#endif
+ HardwareSerial *hwSerial;
+ void common_init(void);
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);
View
82 examples/MotionDetect/MotionDetect.pde
@@ -1,9 +1,20 @@
-// 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
-
+// This is a motion-detect camera sketch using the VC0706 library.
+// 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, saving it to the SD card.
+// Public domain.
+
+// If using an Arduino Mega (1280, 2560 or ADK) in conjunction
+// with an SD card shield designed for conventional Arduinos
+// (Uno, etc.), it's necessary to edit the library file:
+// libraries/SD/utility/Sd2Card.h
+// Look for this line:
+// #define MEGA_SOFT_SPI 0
+// change to:
+// #define MEGA_SOFT_SPI 1
+// This is NOT required if using an SD card breakout interfaced
+// directly to the SPI bus of the Mega (pins 50-53), or if using
+// a non-Mega, Uno-style board.
#include <VC0706.h>
#include <SD.h>
@@ -13,20 +24,65 @@
#include <NewSoftSerial.h>
#endif
-// This is the SD card chip select line, 10 is common
+// SD card chip select line varies among boards/shields:
+// Adafruit SD shields and modules: pin 10
+// Arduino Ethernet shield: pin 4
+// Sparkfun SD shield: pin 8
+// Arduino Mega w/hardware SPI: pin 53
+// Teensy 2.0: pin 0
+// Teensy++ 2.0: pin 20
#define chipSelect 10
-// This is the camera pin connection. Connect the camera TX
-// to pin 2, camera RX to pin 3
+
+// Pins for camera connection are configurable.
+// With the Arduino Uno, etc., most pins can be used, except for
+// those already in use for the SD card (10 through 13 plus
+// chipSelect, if other than pin 10).
+// With the Arduino Mega, the choices are a bit more involved:
+// 1) You can still use SoftwareSerial and connect the camera to
+// a variety of pins...BUT the selection is limited. The TX
+// pin from the camera (RX on the Arduino, and the first
+// argument to SoftwareSerial()) MUST be one of: 62, 63, 64,
+// 65, 66, 67, 68, or 69. If MEGA_SOFT_SPI is set (and using
+// a conventional Arduino SD shield), pins 50, 51, 52 and 53
+// are also available. The RX pin from the camera (TX on
+// Arduino, second argument to SoftwareSerial()) can be any
+// pin, again excepting those used by the SD card.
+// 2) You can use any of the additional three hardware UARTs on
+// the Mega board (labeled as RX1/TX1, RX2/TX2, RX3,TX3),
+// but must specifically use the two pins defined by that
+// UART; they are not configurable. In this case, pass the
+// desired Serial object (rather than a SoftwareSerial
+// object) to the VC0706 constructor.
+
+// Using SoftwareSerial (Arduino 1.0+) or NewSoftSerial (Arduino 0023 & prior):
#if ARDUINO >= 100
+// On Uno: camera TX connected to pin 2, camera RX to pin 3:
SoftwareSerial cameraconnection = SoftwareSerial(2, 3);
+// On Mega: camera TX connected to pin 69 (A15), camera RX to pin 3:
+//SoftwareSerial cameraconnection = SoftwareSerial(69, 3);
#else
NewSoftSerial cameraconnection = NewSoftSerial(2, 3);
#endif
-// pass the serial connection to the camera object
VC0706 cam = VC0706(&cameraconnection);
+// Using hardware serial on Mega: camera TX conn. to RX1,
+// camera RX to TX1, no SoftwareSerial object is required:
+//VC0706 cam = VC0706(&Serial1);
+
void setup() {
+
+ // When using hardware SPI, the SS pin MUST be set to an
+ // output (even if not connected or used). If left as a
+ // floating input w/SPI on, this can cause lockuppage.
+#if !defined(SOFTWARE_SPI)
+#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
+ if(chipSelect != 53) pinMode(53, OUTPUT); // SS on Mega
+#else
+ if(chipSelect != 10) pinMode(10, OUTPUT); // SS on Uno, etc.
+#endif
+#endif
+
Serial.begin(9600);
Serial.println("VC0706 Camera test");
@@ -113,10 +169,10 @@ void loop() {
Serial.print("Writing image to "); Serial.print(filename);
- while (jpglen != 0) {
- // read 64 bytes at a time;
+ while (jpglen > 0) {
+ // read 32 bytes at a time;
uint8_t *buffer;
- uint8_t bytesToRead = min(64, jpglen);
+ uint8_t bytesToRead = min(32, jpglen); // change 32 to 64 for a speedup but may not work with all setups!
buffer = cam.readPicture(bytesToRead);
imgFile.write(buffer, bytesToRead);
View
98 examples/Snapshot/Snapshot.pde
@@ -1,7 +1,19 @@
-// 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
+// This is a basic snapshot sketch using the VC0706 library.
+// On start, the Arduino will find the camera and SD card and
+// then snap a photo, saving it to the SD card.
+// Public domain.
+
+// If using an Arduino Mega (1280, 2560 or ADK) in conjunction
+// with an SD card shield designed for conventional Arduinos
+// (Uno, etc.), it's necessary to edit the library file:
+// libraries/SD/utility/Sd2Card.h
+// Look for this line:
+// #define MEGA_SOFT_SPI 0
+// change to:
+// #define MEGA_SOFT_SPI 1
+// This is NOT required if using an SD card breakout interfaced
+// directly to the SPI bus of the Mega (pins 50-53), or if using
+// a non-Mega, Uno-style board.
#include <VC0706.h>
#include <SD.h>
@@ -11,19 +23,65 @@
#include <NewSoftSerial.h>
#endif
-// This is the SD card chip select line, 10 is common
+// SD card chip select line varies among boards/shields:
+// Adafruit SD shields and modules: pin 10
+// Arduino Ethernet shield: pin 4
+// Sparkfun SD shield: pin 8
+// Arduino Mega w/hardware SPI: pin 53
+// Teensy 2.0: pin 0
+// Teensy++ 2.0: pin 20
#define chipSelect 10
-// This is the camera pin connection. Connect the camera TX
-// to pin 2, camera RX to pin 3
+
+// Pins for camera connection are configurable.
+// With the Arduino Uno, etc., most pins can be used, except for
+// those already in use for the SD card (10 through 13 plus
+// chipSelect, if other than pin 10).
+// With the Arduino Mega, the choices are a bit more involved:
+// 1) You can still use SoftwareSerial and connect the camera to
+// a variety of pins...BUT the selection is limited. The TX
+// pin from the camera (RX on the Arduino, and the first
+// argument to SoftwareSerial()) MUST be one of: 62, 63, 64,
+// 65, 66, 67, 68, or 69. If MEGA_SOFT_SPI is set (and using
+// a conventional Arduino SD shield), pins 50, 51, 52 and 53
+// are also available. The RX pin from the camera (TX on
+// Arduino, second argument to SoftwareSerial()) can be any
+// pin, again excepting those used by the SD card.
+// 2) You can use any of the additional three hardware UARTs on
+// the Mega board (labeled as RX1/TX1, RX2/TX2, RX3,TX3),
+// but must specifically use the two pins defined by that
+// UART; they are not configurable. In this case, pass the
+// desired Serial object (rather than a SoftwareSerial
+// object) to the VC0706 constructor.
+
+// Using SoftwareSerial (Arduino 1.0+) or NewSoftSerial (Arduino 0023 & prior):
#if ARDUINO >= 100
+// On Uno: camera TX connected to pin 2, camera RX to pin 3:
SoftwareSerial cameraconnection = SoftwareSerial(2, 3);
+// On Mega: camera TX connected to pin 69 (A15), camera RX to pin 3:
+//SoftwareSerial cameraconnection = SoftwareSerial(69, 3);
#else
NewSoftSerial cameraconnection = NewSoftSerial(2, 3);
#endif
-// pass the serial connection to the camera object
VC0706 cam = VC0706(&cameraconnection);
+// Using hardware serial on Mega: camera TX conn. to RX1,
+// camera RX to TX1, no SoftwareSerial object is required:
+//VC0706 cam = VC0706(&Serial1);
+
+
void setup() {
+
+ // When using hardware SPI, the SS pin MUST be set to an
+ // output (even if not connected or used). If left as a
+ // floating input w/SPI on, this can cause lockuppage.
+#if !defined(SOFTWARE_SPI)
+#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
+ if(chipSelect != 53) pinMode(53, OUTPUT); // SS on Mega
+#else
+ if(chipSelect != 10) pinMode(10, OUTPUT); // SS on Uno, etc.
+#endif
+#endif
+
Serial.begin(9600);
Serial.println("VC0706 Camera snapshot test");
@@ -90,32 +148,34 @@ void setup() {
// Get the size of the image (frame) taken
uint16_t jpglen = cam.frameLength();
+ Serial.print("Storing ");
Serial.print(jpglen, DEC);
- Serial.println(" byte image");
+ Serial.print(" 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;
+ byte wCount = 0; // For counting # of writes
+ while (jpglen > 0) {
+ // read 32 bytes at a time;
uint8_t *buffer;
- uint8_t bytesToRead = min(32, jpglen); // change 32 to 64 for a speedup but may not work with all setups!
+ uint8_t bytesToRead = min(32, jpglen); // change 32 to 64 for a speedup but may not work with all setups!
buffer = cam.readPicture(bytesToRead);
imgFile.write(buffer, bytesToRead);
+ if(++wCount >= 64) { // Every 2K, give a little feedback so it doesn't appear locked up
+ Serial.print('.');
+ wCount = 0;
+ }
//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");
+ Serial.println("done!");
+ Serial.print(time); Serial.println(" ms elapsed");
}
-
-
-
void loop() {
}
-

0 comments on commit b335872

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