Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reformat the entire project #570

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5,911 changes: 2,943 additions & 2,968 deletions ArduCAM/ArduCAM.cpp

Large diffs are not rendered by default.

1,132 changes: 593 additions & 539 deletions ArduCAM/ArduCAM.h

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
// The demo sketch will do the following tasks:
// 1. Set the camera to JEPG output mode.
// 2. if server.on("/capture", HTTP_GET, serverCapture),it can take photo and send to the Web.
// 3.if server.on("/stream", HTTP_GET, serverStream),it can take photo continuously as video
//streaming and send to the Web.
// 3.if server.on("/stream", HTTP_GET, serverStream),it can take photo continuously as video
// streaming and send to the Web.

// This program requires the ArduCAM V4.0.0 (or later) library and ArduCAM ESP8266 2MP camera
// and use Arduino IDE 1.5.8 compiler or above
Expand All @@ -22,92 +22,101 @@
#include <ArduCAM.h>
#include <SPI.h>
#include "memorysaver.h"
#if !(defined ESP8266 )
#if !(defined ESP8266)
#error Please select the ArduCAM ESP8266 UNO board in the Tools/Board
#endif

//This demo can only work on OV2640_MINI_2MP or ARDUCAM_SHIELD_V2 platform.
#if !(defined (OV2640_MINI_2MP)||(defined (ARDUCAM_SHIELD_V2) && defined (OV2640_CAM)))
// This demo can only work on OV2640_MINI_2MP or ARDUCAM_SHIELD_V2 platform.
#if !(defined(OV2640_MINI_2MP) || (defined(ARDUCAM_SHIELD_V2) && defined(OV2640_CAM)))
#error Please select the hardware platform and camera module in the ../libraries/ArduCAM/memorysaver.h file
#endif
// set GPIO16 as the slave select :
const int CS = 16;

//you can change the value of wifiType to select Station or AP mode.
//Default is AP mode.
// you can change the value of wifiType to select Station or AP mode.
// Default is AP mode.
int wifiType = 1; // 0:Station 1:AP

//AP mode configuration
//Default is arducam_esp8266.If you want,you can change the AP_aaid to your favorite name
const char *AP_ssid = "arducam_esp8266";
//Default is no password.If you want to set password,put your password here
// AP mode configuration
// Default is arducam_esp8266.If you want,you can change the AP_aaid to your favorite name
const char *AP_ssid = "arducam_esp8266";
// Default is no password.If you want to set password,put your password here
const char *AP_password = "";

//Station mode you should put your ssid and password
const char *ssid = "SSID"; // Put your SSID here
// Station mode you should put your ssid and password
const char *ssid = "SSID"; // Put your SSID here
const char *password = "PASSWORD"; // Put your PASSWORD here

ESP8266WebServer server(80);

ArduCAM myCAM(OV2640, CS);


void start_capture(){
void start_capture()
{
myCAM.clear_fifo_flag();
myCAM.start_capture();
}

void camCapture(ArduCAM myCAM){
void camCapture(ArduCAM myCAM)
{
WiFiClient client = server.client();

size_t len = myCAM.read_fifo_length();
if (len >= 0x07ffff){
if (len >= 0x07ffff)
{
Serial.println("Over size.");
return;
}else if (len == 0 ){
}
else if (len == 0)
{
Serial.println("Size is 0.");
return;
}

myCAM.CS_LOW();
myCAM.set_fifo_burst();
#if !(defined (ARDUCAM_SHIELD_V2) && defined (OV2640_CAM))
#if !(defined(ARDUCAM_SHIELD_V2) && defined(OV2640_CAM))
SPI.transfer(0xFF);
#endif
if (!client.connected()) return;
#endif
if (!client.connected())
return;
String response = "HTTP/1.1 200 OK\r\n";
response += "Content-Type: image/jpeg\r\n";
response += "Content-Length: " + String(len) + "\r\n\r\n";
server.sendContent(response);

static const size_t bufferSize = 4096;
static uint8_t buffer[bufferSize] = {0xFF};

while (len) {
size_t will_copy = (len < bufferSize) ? len : bufferSize;
SPI.transferBytes(&buffer[0], &buffer[0], will_copy);
if (!client.connected()) break;
client.write(&buffer[0], will_copy);
len -= will_copy;

while (len)
{
size_t will_copy = (len < bufferSize) ? len : bufferSize;
SPI.transferBytes(&buffer[0], &buffer[0], will_copy);
if (!client.connected())
break;
client.write(&buffer[0], will_copy);
len -= will_copy;
}

myCAM.CS_HIGH();
}

void serverCapture(){
void serverCapture()
{
start_capture();
Serial.println("CAM Capturing");

int total_time = 0;

total_time = millis();
while (!myCAM.get_bit(ARDUCHIP_TRIG, CAP_DONE_MASK));
while (!myCAM.get_bit(ARDUCHIP_TRIG, CAP_DONE_MASK))
;
total_time = millis() - total_time;
Serial.print("capture total_time used (in miliseconds):");
Serial.println(total_time, DEC);

total_time = 0;

Serial.println("CAM Capture Done!");
total_time = millis();
camCapture(myCAM);
Expand All @@ -117,72 +126,86 @@ void serverCapture(){
Serial.println("CAM send Done!");
}

void serverStream(){
void serverStream()
{
WiFiClient client = server.client();

String response = "HTTP/1.1 200 OK\r\n";
response += "Content-Type: multipart/x-mixed-replace; boundary=frame\r\n\r\n";
server.sendContent(response);

while (1){

while (1)
{
start_capture();

while (!myCAM.get_bit(ARDUCHIP_TRIG, CAP_DONE_MASK));


while (!myCAM.get_bit(ARDUCHIP_TRIG, CAP_DONE_MASK))
;

size_t len = myCAM.read_fifo_length();
if (len >= 0x07ffff){
if (len >= 0x07ffff)
{
Serial.println("Over size.");
continue;
}else if (len == 0 ){
}
else if (len == 0)
{
Serial.println("Size is 0.");
continue;
}

myCAM.CS_LOW();
myCAM.set_fifo_burst();
#if !(defined (ARDUCAM_SHIELD_V2) && defined (OV2640_CAM))
SPI.transfer(0xFF);
#endif
if (!client.connected()) break;
response = "--frame\r\n";
response += "Content-Type: image/jpeg\r\n\r\n";
server.sendContent(response);

#if !(defined(ARDUCAM_SHIELD_V2) && defined(OV2640_CAM))
SPI.transfer(0xFF);
#endif
if (!client.connected())
break;
response = "--frame\r\n";
response += "Content-Type: image/jpeg\r\n\r\n";
server.sendContent(response);

static const size_t bufferSize = 4096;
static uint8_t buffer[bufferSize] = {0xFF};

while (len) {

while (len)
{
size_t will_copy = (len < bufferSize) ? len : bufferSize;
SPI.transferBytes(&buffer[0], &buffer[0], will_copy);
if (!client.connected()) break;
if (!client.connected())
break;
client.write(&buffer[0], will_copy);
len -= will_copy;
}
myCAM.CS_HIGH();

if (!client.connected()) break;

if (!client.connected())
break;
}
}

void handleNotFound(){
void handleNotFound()
{
String message = "Server is running!\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
server.send(200, "text/plain", message);

if (server.hasArg("ql")){

if (server.hasArg("ql"))
{
int ql = server.arg("ql").toInt();
myCAM.OV2640_set_JPEG_size(ql);delay(1000);
myCAM.OV2640_set_JPEG_size(ql);
delay(1000);
Serial.println("QL change to: " + server.arg("ql"));
}
}

void setup() {
void setup()
{
uint8_t vid, pid;
uint8_t temp;
#if defined(__SAM3X8E__)
Expand All @@ -198,70 +221,79 @@ void setup() {

// initialize SPI:
SPI.begin();
SPI.setFrequency(4000000); //4MHz
SPI.setFrequency(4000000); // 4MHz

//Check if the ArduCAM SPI bus is OK
// Check if the ArduCAM SPI bus is OK
myCAM.write_reg(ARDUCHIP_TEST1, 0x55);
temp = myCAM.read_reg(ARDUCHIP_TEST1);
if (temp != 0x55){
if (temp != 0x55)
{
Serial.println("SPI1 interface Error!");
while(1);
while (1)
;
}

//Check if the camera module type is OV2640
// Check if the camera module type is OV2640
myCAM.wrSensorReg8_8(0xff, 0x01);
myCAM.rdSensorReg8_8(OV2640_CHIPID_HIGH, &vid);
myCAM.rdSensorReg8_8(OV2640_CHIPID_LOW, &pid);
if ((vid != 0x26 ) && (( pid != 0x41 ) || ( pid != 0x42 )))
if ((vid != 0x26) && ((pid != 0x41) || (pid != 0x42)))
Serial.println("Can't find OV2640 module!");
else
else
Serial.println("OV2640 detected.");


//Change to JPEG capture mode and initialize the OV2640 module
// Change to JPEG capture mode and initialize the OV2640 module
myCAM.set_format(JPEG);
myCAM.InitCAM();
myCAM.OV2640_set_JPEG_size(OV2640_320x240);
myCAM.clear_fifo_flag();

if (wifiType == 0){
if(!strcmp(ssid,"SSID")){
Serial.println("Please set your SSID");
while(1);
if (wifiType == 0)
{
if (!strcmp(ssid, "SSID"))
{
Serial.println("Please set your SSID");
while (1)
;
}
if(!strcmp(password,"PASSWORD")){
Serial.println("Please set your PASSWORD");
while(1);
if (!strcmp(password, "PASSWORD"))
{
Serial.println("Please set your PASSWORD");
while (1)
;
}
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("");
Serial.println(WiFi.localIP());
}else if (wifiType == 1){
}
else if (wifiType == 1)
{
Serial.println();
Serial.println();
Serial.print("Share AP: ");
Serial.println(AP_ssid);
Serial.print("The password is: ");
Serial.println(AP_password);

WiFi.mode(WIFI_AP);
WiFi.softAP(AP_ssid, AP_password);
Serial.println("");
Serial.println(WiFi.softAPIP());
}

// Start the server
server.on("/capture", HTTP_GET, serverCapture);
server.on("/stream", HTTP_GET, serverStream);
Expand All @@ -270,7 +302,7 @@ void setup() {
Serial.println("Server started");
}

void loop() {
void loop()
{
server.handleClient();
}

Loading