This is a test program for Camera_LS_Y201 class.

Dependencies:   Camera_LS_Y201 SDFileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Camera_LS_Y201.h"
00003 #include "SDFileSystem.h"
00004  
00005 #define DEBMSG      printf
00006 #define NEWLINE()   printf("\r\n")
00007  
00008 #define USE_SDCARD 0
00009  
00010 #if USE_SDCARD
00011 #define FILENAME    "/sd/IMG_%04d.jpg"
00012 SDFileSystem fs(p5, p6, p7, p8, "sd");
00013 #else
00014 #define FILENAME    "/local/IMG_%04d.jpg"
00015 LocalFileSystem fs("local");
00016 #endif
00017 Camera_LS_Y201 cam1(p13, p14);
00018  
00019 typedef struct work {
00020     FILE *fp;
00021 } work_t;
00022  
00023 work_t work;
00024  
00025 /**
00026  * Callback function for readJpegFileContent.
00027  *
00028  * @param buf A pointer to a buffer.
00029  * @param siz A size of the buffer.
00030  */
00031 void callback_func(int done, int total, uint8_t *buf, size_t siz) {
00032     fwrite(buf, siz, 1, work.fp);
00033  
00034     static int n = 0;
00035     int tmp = done * 100 / total;
00036     if (n != tmp) {
00037         n = tmp;
00038         DEBMSG("Writing...: %3d%%", n);
00039         NEWLINE();
00040     }
00041 }
00042  
00043 /**
00044  * Capture.
00045  *
00046  * @param cam A pointer to a camera object.
00047  * @param filename The file name.
00048  *
00049  * @return Return 0 if it succeed.
00050  */
00051 int capture(Camera_LS_Y201 *cam, char *filename) {
00052     /*
00053      * Take a picture.
00054      */
00055     if (cam->takePicture() != 0) {
00056         return -1;
00057     }
00058     DEBMSG("Captured.");
00059     NEWLINE();
00060  
00061     /*
00062      * Open file.
00063      */
00064     work.fp = fopen(filename, "wb");
00065     if (work.fp == NULL) {
00066         return -2;
00067     }
00068  
00069     /*
00070      * Read the content.
00071      */
00072     DEBMSG("%s", filename);
00073     NEWLINE();
00074     if (cam->readJpegFileContent(callback_func) != 0) {
00075         fclose(work.fp);
00076         return -3;
00077     }
00078     fclose(work.fp);
00079  
00080     /*
00081      * Stop taking pictures.
00082      */
00083     cam->stopTakingPictures();
00084  
00085     return 0;
00086 }
00087  
00088 /**
00089  * Entry point.
00090  */
00091 int main(void) {
00092     DEBMSG("Camera module");
00093     NEWLINE();
00094     DEBMSG("Resetting...");
00095     NEWLINE();
00096     wait(1);
00097  
00098     if (cam1.reset() == 0) {
00099         DEBMSG("Reset OK.");
00100         NEWLINE();
00101     } else {
00102         DEBMSG("Reset fail.");
00103         NEWLINE();
00104         error("Reset fail.");
00105     }
00106     wait(1);
00107  
00108     int cnt = 0;
00109     while (1) {
00110         char fname[64];
00111         snprintf(fname, sizeof(fname) - 1, FILENAME, cnt);
00112         int r = capture(&cam1, fname);
00113         if (r == 0) {
00114             DEBMSG("[%04d]:OK.", cnt);
00115             NEWLINE();
00116         } else {
00117             DEBMSG("[%04d]:NG. (code=%d)", cnt, r);
00118             NEWLINE();
00119             error("Failure.");
00120         }
00121         cnt++;
00122     }
00123 }