this camera C328 http sdcard file server example is for academy

Dependencies:   CameraC328 SDFileSystem WIZnetInterface mbed

Insert below jpg file in your SDcard to take a picture in Web Server /media/uploads/IOP/take_a_picture.zip

Files at this revision

API Documentation at this revision

Comitter:
IOP
Date:
Wed Aug 05 23:53:43 2015 +0000
Parent:
1:a878f7545221
Child:
3:e6025c5b25e4
Commit message:
change code to take a picture right after open take_a_picture.jpg page

Changed in this revision

CameraC328.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/CameraC328.lib	Tue Jul 28 07:53:41 2015 +0000
+++ b/CameraC328.lib	Wed Aug 05 23:53:43 2015 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/users/shintamainjp/code/CameraC328/#49cfda6c547f
+https://developer.mbed.org/users/IOP/code/CameraC328/#483cfd88da75
--- a/main.cpp	Tue Jul 28 07:53:41 2015 +0000
+++ b/main.cpp	Wed Aug 05 23:53:43 2015 +0000
@@ -12,7 +12,7 @@
 #define IP      "192.168.240.100"
 #define MASK    "255.255.255.0"
 #define GATEWAY "192.168.240.1"
-#define PORT    80
+#define PORT     80
 
 Serial pc (USBTX,USBRX);    // tx, rx
 SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "wfs"); // the pinout on the mbed
@@ -56,8 +56,10 @@
 
     err = camera.sync();
     if (CameraC328::NoError == err) {
+        printf("[Camera Initializing..]\r\n");
         printf("[ OK ] : CameraC328::sync\r\n");
     } else {
+        printf("[Camera Initializing..]\r\n");
         printf("[FAIL] : CameraC328::sync (Error=%02X)\r\n", (int)err);
     }
 }
@@ -75,22 +77,22 @@
 #endif
     if (CameraC328::NoError == err) {
         printf("[ OK ] : CameraC328::init\r\n");
+        printf("\r\n[Start taking a picture]\r\n");
     } else {
         printf("[FAIL] : CameraC328::init (Error=%02X)\r\n", (int)err);
-    }
-
+    }    
     for (int i = 0; i < CAPTURE_FRAMES; i++) {
         char fname[64];
-        snprintf(fname, sizeof(fname), "/wfs/jpss%04d.jpg", i);
+        snprintf(fname, sizeof(fname), "/wfs/Snapshot%02d.jpg",i);
         fp_jpeg = fopen(fname, "w");
 
-        err = camera.getJpegSnapshotPicture(jpeg_callback);
+        err = camera.getJpegSnapshotPicture(jpeg_callback);     // call the snapshot function
+        
         if (CameraC328::NoError == err) {
-            printf("[ OK ] : CameraC328::getJpegSnapshotPicture\r\n");
+            printf("[ OK ] : CameraC328::getJpegSnapshotPicture[%d]\r\n",i);
         } else {
-            printf("[FAIL] : CameraC328::getJpegSnapshotPicture (Error=%02X)\r\n", (int)err);
-        }
-
+            printf("[FAIL] : CameraC328::getJpegSnapshotPicture[%d] (Error=%02X)\r\n",i, (int)err);
+        }       
         fclose(fp_jpeg);
     }
 }
@@ -139,12 +141,12 @@
     snprintf(sentBuffer, sizeof(sentBuffer),"%s%s\r\n",sentBuffer,line_response);    //append to sentBuffer
 
     if (EnDebugMSG)
-        printf("\n-->sent Header--\n");
+        printf("\r\n-->sent Header--\r\n");
 
     client.send_all(sentBuffer,strlen(sentBuffer));
     if (EnDebugMSG) {
         printf(sentBuffer);
-        printf("\n--end Header-- bytes:%d",strlen(sentBuffer));
+        printf("\r\n--end Header-- bytes:%d",strlen(sentBuffer));
     }
     wait(0.2);  //200ms important for browser!
 }
@@ -153,7 +155,7 @@
 {
     client.send_all(line,length_line);
     if (EnDebugMSG)
-        printf("\n-->send HTML line:\n%s ...Ok!",line);
+        printf("\r\n-->send HTML line:\r\n%s ...Ok!",line);
     wait(0.01);
 }
 
@@ -161,13 +163,13 @@
 {
     send_HTTP_header("HTTP/1.1", status_code, title, "text/html", -1);
     if (EnDebugMSG)
-        printf("\n-->send_error...\n");
+        printf("\r\n-->send_error...\r\n");
     sentBuffer[0]=NULL; //clear buffer
     sprintf(line_response, "<!DOCTYPE html>\r\n<html>\r\n<head>\r\n<title>%d %s</title>\r\n</head>\r\n", status_code, title);
     snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
     sprintf(line_response, "<body><center><h2><center>%d %s</center></h2>\r\n",status_code, title );
     snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
-    sprintf(line_response, "%s\r\n", body_text ); 
+    sprintf(line_response, "%s\r\n", body_text );
     snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
     sprintf(line_response, "<p>mbed HTTP File Server</p>\r\n</center></body></html>\r\n");
     snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
@@ -179,16 +181,18 @@
     char *mime_type = {0};
     unsigned int bytes_for_send=0;
     long long filesize, all_send_bytes = 0;
-    
-    if (strcmp(file_path,"/wfs//take_a_picture.jpg") == 0)      
-        take_picture = 1;
-    else take_picture = 0;
 
     mime_type =  get_mime_type( path_file );
     snprintf(file_path, sizeof(file_path),"/wfs/%s",path_file);
+    
+    if (strcmp(file_path,"/wfs//take_a_picture.jpg") == 0){
+        take_picture = 1;
+    }
+    else take_picture = 0;
+    
     if (EnDebugMSG) {
-        printf("\n-->from send_file:%s",file_path);
-        printf("\n-->from send_file mime type:%s",mime_type);
+        printf("\r\n-->from send_file:%s",file_path);
+        printf("\r\n-->from send_file mime type:%s",mime_type);
     }
 
     if (Mystat(path_file, &myStatBuf)) { //fault with file
@@ -214,13 +218,13 @@
         fread (sentBuffer,1,bytes_for_send,fp);
         filesize -= bytes_for_send;
         if (EnDebugMSG)
-            printf("\n---bytes_for_send...%d",bytes_for_send);
+            printf("\r\n---bytes_for_send...%d",bytes_for_send);
         client.send_all(sentBuffer,bytes_for_send);
         //Thread::wait(10);
         all_send_bytes += bytes_for_send;
     }
     if (EnDebugMSG)
-        printf("\n---buffer fill end - all ...%lld", all_send_bytes);
+        printf("\r\n---buffer fill end - all ...%lld", all_send_bytes);
     //binary send
 
     sprintf(line_response, "\r\n");
@@ -244,11 +248,11 @@
     char timeBuf[40];
 
     if (EnDebugMSG)
-        printf("\n-->from send_directory:%s",path);
+        printf("\r\n-->from send_directory:%s",path);
     snprintf(file_path,sizeof(file_path),"/wfs%s",path);
     DIR *d = opendir(file_path);
     if (EnDebugMSG && d!=NULL)
-        printf("\n-->from send_directory:%s ...open OK",file_path);
+        printf("\r\n-->from send_directory:%s ...open OK",file_path);
     if (d==NULL) {   //error open dir
         send_HTML_error( 403, "Forbidden", "403 - Directory access forbidden.");
         return -1;
@@ -261,6 +265,7 @@
     sprintf(line_response,"<meta content=\"text/html; charset=iso-8859-1\" http-equiv=\"Content-Type\"></head>\n");
     snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
     
+    
     sprintf(line_response,"<body><center>\n<h3>Index of %s</h3>\n", path);
     snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
     send_HTML_line(sentBuffer, strlen(sentBuffer));
@@ -279,16 +284,16 @@
     snprintf(process_name,posOfLastSlash+1,"%s",path);
     if (EnDebugMSG)
         printf("\r\n>>process_name=%s",process_name);
-    sprintf(line_response,"<tr><td align=\"left\"><a href=\"%s\">../refresh</a></td></tr>\n",process_name);
+    sprintf(line_response,"<tr><td align=\"left\"><a href=\"%s\">../</a></td></tr>\n",process_name);
     snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
     while((p = readdir(d)) != NULL) {
         if (EnDebugMSG)
-            printf("\n   :%s",p->d_name);
+            printf("\r\n   :%s",p->d_name);
         sprintf(file_path,"%s/%s",path,p->d_name);
         Mystat( file_path, &sb );
         if (get_dirInfo(file_path)==0 ) { //this is directory path
             if (EnDebugMSG)
-                printf("\nDIR");
+                printf("\r\nDIR");
             sprintf(line_response, "<tr><td align=\"left\"><a href=\"%s\">%s</a><br></td></tr>\n",file_path,p->d_name);
             if (strlen(line_response)>(sizeof(sentBuffer)-strlen(sentBuffer))) { //buffer must be sent
                 send_HTML_line(sentBuffer, strlen(sentBuffer));
@@ -298,7 +303,7 @@
 
         } else {    //this is file
             if (EnDebugMSG)
-                printf("\nFILE");
+                printf("\r\nFILE");
             timeinfo = localtime (&sb.st_mtime);
             //strftime(timeBuf,40, "%I:%M:%S %p (%Y/%m/%d)\r\n", localtime(&sb.st_mtime));
             strftime(timeBuf, 40, "%c", timeinfo);
@@ -339,9 +344,9 @@
     //get URL
     snprintf(sURL,strlen(tmpBuffer)-strlen(sProtocol),"%s\r\n", tmpBuffer);   //URL is between Method and Protocol
 
-    printf("\nParse Method:%s",sMethod);
-    printf("\nParse URL:%s",sURL);
-    printf("\nParse PROTOCOL:%s",sProtocol);
+    printf("\r\nParse Method:%s",sMethod);
+    printf("\r\nParse URL:%s",sURL);
+    printf("\r\nParse PROTOCOL:%s",sProtocol);
     printf("\n\r\n");
 }
 
@@ -360,7 +365,7 @@
     if (sURL[strlen(sURL)-1]=='/') {
         sURL[strlen(sURL)-1]=sURL[strlen(sURL)];  //delete last symbol
         if (EnDebugMSG)
-            printf("\n delete last:%s",sURL);
+            printf("\r\n delete last:%s",sURL);
     }
     gdi= get_dirInfo(sURL);
     gfi= get_fileInfo(sURL);
@@ -369,7 +374,7 @@
             return send_directory(sURL);
         }
         if (EnDebugMSG)
-            printf("\n404-br File not found or...(Fresult is:%d)",gfi);
+            printf("\r\n404-br File not found or...(Fresult is:%d)",gfi);
         send_HTML_error( 404, "Not Found","404 - The requested resource could not be found.");
         return 404;
     } else {    //==0  found
@@ -381,7 +386,8 @@
 }
 
 int main()
-{
+{   
+    *(volatile uint32_t *)(0x41001014) = 0x0060100; //clock setting 48MHz
     uint8_t MAC_Addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; 
     ledTick.attach(&ledTickfunc,0.5);
     //ledTick.detach();
@@ -412,15 +418,8 @@
         //blocking mode(never timeout)
         if(svr.accept(client)<0) {
             printf("failed to accept connection.\n\r");
-        } 
-        else if (take_picture) {                    // take a picture function
-            printf("start taking a picture\r\n");
-            
-            sync();
-            test_jpeg_snapshot_picture();   
-                    
-            take_picture = 0;
-        }else {
+        }
+        else {
             //client.set_blocking(false,5000);    //5000=5sec
             printf("connection success!\n\rIP: %s\n\r",client.get_address());
             clientIsConnected = true;
@@ -451,10 +450,19 @@
             }//while
             printf("close connection.\n\rHTTP server is listening...\n\r\n");
             client.close();
-            wait(0.05);
-
+            wait(0.05); 
+            
+            // take a picture function
+            if (take_picture) {
+            
+                sync();
+                test_jpeg_snapshot_picture();   
+                    
+                take_picture = 0;
+            }
             led2 = false;
         }
     }
 
 }
+