test

Fork of CameraC1098 by Tadao Iida

Files at this revision

API Documentation at this revision

Comitter:
hepta2ume
Date:
Thu Aug 03 04:28:14 2017 +0000
Parent:
0:5a6468b4164d
Commit message:
test

Changed in this revision

CameraC1098.cpp Show annotated file Show diff for this revision Revisions of this file
CameraC1098.h Show annotated file Show diff for this revision Revisions of this file
SerialBuffered.cpp Show annotated file Show diff for this revision Revisions of this file
SerialBuffered.h Show annotated file Show diff for this revision Revisions of this file
diff -r 5a6468b4164d -r 135493341acc CameraC1098.cpp
--- a/CameraC1098.cpp	Mon Jul 09 10:23:32 2012 +0000
+++ b/CameraC1098.cpp	Thu Aug 03 04:28:14 2017 +0000
@@ -1,52 +1,29 @@
-/**
- * C1098-SS device driver class (Version 1.0)
- * Reference documents: C1098-SS User Manual v1.0 2012.4.1
- *
- * CameraC328Library
- * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
- * http://shinta.main.jp/
- *
- * CameraC1098-SS Library
- * Copyright (C) 2012 Tadao Iida
- */
-
 #include "CameraC1098.h"
 
 #define WAITIDLE    waitIdle
 #define SENDFUNC    sendBytes
 #define RECVFUNC    recvBytes
 #define WAITFUNC    waitRecv
+int num=0;
+FILE *fp_jpeg;
 
-/**
- * Constructor.
- *
- * @param tx A pin for transmit.
- * @param rx A pin for receive.
- * @param baud Baud rate. (Default is Baud14400.)
- */
-CameraC1098::CameraC1098(PinName tx, PinName rx, int baud) : serial(tx, rx) {
-    serial.baud(baud);
+CameraC1098::CameraC1098(PinName tx, PinName rx, int baud) : serial(tx, rx)
+{
+    serial._baud(baud);
 }
 
-/**
- * Destructor.
- */
-CameraC1098::~CameraC1098() {
+CameraC1098::~CameraC1098()
+{
 }
 
-/**
- * Make a sync. for baud rate.
- */
-CameraC1098::ErrorNumber CameraC1098::sync() {
+CameraC1098::ErrorNumber CameraC1098::sync()
+{
     for (int i = 0; i < SYNCMAX; i++) {
         if (NoError == sendSync()) {
             if (NoError == recvAckOrNck()) {
                 if (NoError == recvSync()) {
                     if (NoError == sendAck(0x0D, 0x00)) {
-                        /*
-                         * After synchronization, the camera needs a little time for AEC and AGC to be stable.
-                         * Users should wait for 1-2 seconds before capturing the first picture.
-                         */
+
                         wait(2.0);
                         return NoError;
                     }
@@ -58,34 +35,29 @@
     return UnexpectedReply;
 }
 
-/**
- * Initialize.
- *
- * @param baud Camera Interface Speed.
- * @param jr JPEG resolution.
- */
-CameraC1098::ErrorNumber CameraC1098::init(Baud baud,JpegResolution jr) {
+CameraC1098::ErrorNumber CameraC1098::init(Baud baud,JpegResolution jr)
+{
     int i ;
     ErrorNumber en;
     WAITIDLE();
     setmbedBaud((Baud)(0x07)) ;
-  
+
     for ( i = 1 ; i < 7 ; i++ ) {
         if ( NoError == sendSync() ) {
-            if ( NoError == recvAckOrNck() ){
+            if ( NoError == recvAckOrNck() ) {
                 if ( NoError == recvSync() ) {
                     if ( NoError == sendAck(0x0D, 0x00) ) {
                         en = sendInitial(baud,jr);
                         if (NoError != en) {
                             return en;
-                        }    
+                        }
                         en = recvAckOrNck();
                         if (NoError != en) {
                             return en;
-                        }      
+                        }
                         wait_ms(50) ;
-                        setmbedBaud(baud);   
-                        //wait_ms(50) ; 
+                        setmbedBaud(baud);
+                        //wait_ms(50) ;
                         static bool alreadySetupPackageSize = false;
                         if (!alreadySetupPackageSize) {
                             en = sendSetPackageSize(packageSize);
@@ -99,33 +71,24 @@
                             }
                             alreadySetupPackageSize = true;
                         }
-                        
+
                         wait(2.0);
-                        return (ErrorNumber)NoError;                                                                 
-                       /*
-                        * After synchronization, the camera needs a little time for AEC and AGC to be stable.
-                        * Users should wait for 1-2 seconds before capturing the first picture.
-                        */
+                        return (ErrorNumber)NoError;
+
                     }
-                }                 
-            }else{
-                setmbedBaud((Baud)(i+1)) ;   
+                }
+            } else {
+                setmbedBaud((Baud)(i+1)) ;
             }
         }
-        wait_ms(50);   
-    }                 
+        wait_ms(50);
+    }
     return UnexpectedReply;
 }
 
 
-/**
- * Get JPEG snapshot picture.
- *
- * @param func A pointer to a callback function.
- *             You can block this function until saving the image datas.
- * @return Status of the error.
- */
-CameraC1098::ErrorNumber CameraC1098::getJpegSnapshotPicture(void(*func)(char *buf, size_t siz)) {
+CameraC1098::ErrorNumber CameraC1098::getJpegSnapshotPicture()
+{
     WAITIDLE();
     ErrorNumber en;
 
@@ -215,8 +178,11 @@
          * Call a call back function.
          * You can block this function while working.
          */
-        func(databuf, ds);
-
+        size_t siz = ds;
+        for (int ii = 0; ii < (int)siz; ii++) {
+            fprintf(fp_jpeg,"%02x ", databuf[ii]);
+            if(++num%16==0) fprintf(fp_jpeg,"\r\n");
+        }
         /*
          * We should wait for camera working before reply a ACK.
          */
@@ -232,9 +198,10 @@
 
 
 
-CameraC1098::ErrorNumber CameraC1098::sendInitial(Baud baud, JpegResolution jr) {
+CameraC1098::ErrorNumber CameraC1098::sendInitial(Baud baud, JpegResolution jr)
+{
     char send[COMMAND_LENGTH];
-    
+
     send[0] = 0xAA;
     send[1] = 0x01;
     send[2] = (char)baud;
@@ -249,7 +216,8 @@
     return (ErrorNumber)NoError;
 }
 
-CameraC1098::ErrorNumber CameraC1098::sendGetPicture(void) {
+CameraC1098::ErrorNumber CameraC1098::sendGetPicture(void)
+{
     char send[COMMAND_LENGTH];
 
     send[0] = 0xAA;
@@ -265,7 +233,8 @@
     return (ErrorNumber)NoError;
 }
 
-CameraC1098::ErrorNumber CameraC1098::sendSnapshot(void) {
+CameraC1098::ErrorNumber CameraC1098::sendSnapshot(void)
+{
     char send[COMMAND_LENGTH];
     send[0] = 0xAA;
     send[1] = 0x05;
@@ -280,7 +249,8 @@
     return (ErrorNumber)NoError;
 }
 
-CameraC1098::ErrorNumber CameraC1098::sendSetPackageSize(uint16_t packageSize) {
+CameraC1098::ErrorNumber CameraC1098::sendSetPackageSize(uint16_t packageSize)
+{
     char send[COMMAND_LENGTH];
     send[0] = 0xAA;
     send[1] = 0x06;
@@ -295,33 +265,35 @@
     return (ErrorNumber)NoError;
 }
 
-void CameraC1098::setmbedBaud(Baud baud){
-    switch((int)baud){
-    case 0x02:
-        serial._baud(460800);
-        break;
-    case 0x03:
-        serial._baud(230400);
-        break;
-    case 0x04:
-        serial._baud(115200);
-        break;
-    case 0x05:
-        serial._baud(57600);
-        break;
-    case 0x06:
-        serial._baud((int)28800);
-        break;
-    case 0x07:
-        serial._baud(14400);
-        break;    
-    default:
-        serial._baud(14400);  
+void CameraC1098::setmbedBaud(Baud baud)
+{
+    switch((int)baud) {
+        case 0x02:
+            serial._baud(460800);
+            break;
+        case 0x03:
+            serial._baud(230400);
+            break;
+        case 0x04:
+            serial._baud(115200);
+            break;
+        case 0x05:
+            serial._baud(57600);
+            break;
+        case 0x06:
+            serial._baud((int)28800);
+            break;
+        case 0x07:
+            serial._baud(14400);
+            break;
+        default:
+            serial._baud(14400);
     }
 }
 
 
-CameraC1098::ErrorNumber CameraC1098::sendReset(ResetType specialReset) {
+CameraC1098::ErrorNumber CameraC1098::sendReset(ResetType specialReset)
+{
     char send[COMMAND_LENGTH];
     send[0] = 0xAA;
     send[1] = 0x08;
@@ -341,7 +313,8 @@
 }
 
 
-CameraC1098::ErrorNumber CameraC1098::recvData(uint32_t *length) {
+CameraC1098::ErrorNumber CameraC1098::recvData(uint32_t *length)
+{
     char recv[COMMAND_LENGTH];
     if (!RECVFUNC(recv, sizeof(recv))) {
         return (ErrorNumber)UnexpectedReply;
@@ -354,7 +327,8 @@
     return (ErrorNumber)NoError;
 }
 
-CameraC1098::ErrorNumber CameraC1098::sendSync() {
+CameraC1098::ErrorNumber CameraC1098::sendSync()
+{
     char send[COMMAND_LENGTH];
     send[0] = 0xAA;
     send[1] = 0x0D;
@@ -362,17 +336,18 @@
     send[3] = 0x00;
     send[4] = 0x00;
     send[5] = 0x00;
-    
-    
+
+
     if (!SENDFUNC(send, sizeof(send))) {
         return (ErrorNumber)SendRegisterTimeout;
     }
     return (ErrorNumber)NoError;
 }
 
-CameraC1098::ErrorNumber CameraC1098::recvSync() {
+CameraC1098::ErrorNumber CameraC1098::recvSync()
+{
     char recv[COMMAND_LENGTH];
-    
+
     if (!RECVFUNC(recv, sizeof(recv))) {
         return (ErrorNumber)UnexpectedReply;
     }
@@ -382,13 +357,8 @@
     return (ErrorNumber)NoError;
 }
 
-/**
- * Send ACK.
- *
- * @param commandId The command with that ID is acknowledged by this command.
- * @param packageId For acknowledging Data command, these two bytes represent the requested package ID. While for acknowledging other commands, these two bytes are set to 00h.
- */
-CameraC1098::ErrorNumber CameraC1098::sendAck(uint8_t commandId, uint16_t packageId) {
+CameraC1098::ErrorNumber CameraC1098::sendAck(uint8_t commandId, uint16_t packageId)
+{
     char send[COMMAND_LENGTH];
 
     send[0] = 0xAA;
@@ -403,14 +373,10 @@
     return (ErrorNumber)NoError;
 }
 
-/**
- * Receive ACK or NCK.
- *
- * @return Error number.
- */
-CameraC1098::ErrorNumber CameraC1098::recvAckOrNck() {
+CameraC1098::ErrorNumber CameraC1098::recvAckOrNck()
+{
     char recv[COMMAND_LENGTH];
-        
+
     if (!RECVFUNC(recv, sizeof(recv))) {
         return (ErrorNumber)UnexpectedReply;
     }
@@ -419,22 +385,15 @@
     }
     if ((0xAA == recv[0]) && (0x0F == recv[1]) && (0x00 == recv[2])) {
         return (ErrorNumber)NoError;
-    }    
+    }
     if ((0xAA == recv[0]) && (0x0F == recv[1])) {
         return (ErrorNumber)recv[4];
     }
     return (ErrorNumber)UnexpectedReply;
 }
 
-/**
- * Send bytes to camera module.
- *
- * @param buf Pointer to the data buffer.
- * @param len Length of the data buffer.
- *
- * @return True if the data sended.
- */
-bool CameraC1098::sendBytes(char *buf, size_t len, int timeout_us) {
+bool CameraC1098::sendBytes(char *buf, size_t len, int timeout_us)
+{
     for (uint32_t i = 0; i < (uint32_t)len; i++) {
         int cnt = 0;
         while (!serial.writeable()) {
@@ -449,46 +408,75 @@
     return true;
 }
 
-/**
- * Receive bytes from camera module.
- *
- * @param buf Pointer to the data buffer.
- * @param len Length of the data buffer.
- *
- * @return True if the data received.
- */
-bool CameraC1098::recvBytes(char *buf, size_t len, int timeout_us) {
+bool CameraC1098::recvBytes(char *buf, size_t len, int timeout_us)
+{
     for (uint32_t i = 0; i < (uint32_t)len; i++) {
         int cnt = 0;
-        while (!serial.readable()) {
+        while (!serial.rreadable()) {
             wait_us(1);
             cnt++;
             if (timeout_us < cnt) {
                 return false;
             }
         }
-        buf[i] = serial.getc();
+        buf[i] = serial.ggetc();
+    }
+    return true;
+}
+
+bool CameraC1098::waitRecv()
+{
+    while (!serial.rreadable()) {
+    }
+    return true;
+}
+
+bool CameraC1098::waitIdle()
+{
+    while (serial.rreadable()) {
+        serial.ggetc();
     }
     return true;
 }
 
-/**
- * Wait received.
- *
- * @return True if the data received.
- */
-bool CameraC1098::waitRecv() {
-    while (!serial.readable()) {
+void CameraC1098::Sync(void)
+{
+    CameraC1098::ErrorNumber err = CameraC1098::NoError;
+
+    err = sync();
+    int count=0;
+    while(err) {
+        printf("count = %d\r\n",count);
+        switch(count) {
+            case 0:
+                setmbedBaud(CameraC1098::Baud14400);
+                break;
+            case 1:
+                setmbedBaud(CameraC1098::Baud115200);
+                break;
+            default:
+                count=0;
+        }
+        count++;
+        err = sync();
+        printf("init err1=%d\r\n", err);
+        if(!err) {
+            printf("to 115200\r\n");
+            init(CameraC1098::Baud115200, CameraC1098::JpegResolution320x240);
+            printf("init err2=%d\r\n", err);
+            setmbedBaud(CameraC1098::Baud115200);
+            printf("init err3=%d\r\n", err);
+        }
     }
-    return true;
 }
 
-/**
- * Wait idle state.
- */
-bool CameraC1098::waitIdle() {
-    while (serial.readable()) {
-        serial.getc();
+void CameraC1098::jpeg_snapshot(int CAPTURE_FRAMES)
+{
+    for (int i = 0; i < CAPTURE_FRAMES; i++) {
+        char fname[64];
+        snprintf(fname, sizeof(fname), "/fs/test%d.txt",i);
+        fp_jpeg = fopen(fname, "w");
+        getJpegSnapshotPicture();
+        fclose(fp_jpeg);
     }
-    return true;
-}
+}
\ No newline at end of file
diff -r 5a6468b4164d -r 135493341acc CameraC1098.h
--- a/CameraC1098.h	Mon Jul 09 10:23:32 2012 +0000
+++ b/CameraC1098.h	Thu Aug 03 04:28:14 2017 +0000
@@ -99,7 +99,7 @@
      *             You can block this function until saving the image datas.
      * @return Status of the error.
      */
-    ErrorNumber getJpegSnapshotPicture(void(*func)(char *buf, size_t siz));
+    ErrorNumber getJpegSnapshotPicture();
 
     /** setmbedBaud
      *  mbed Interface Speed.
@@ -107,18 +107,20 @@
      *  @param baud mbed Interface Speed.
      */
     void setmbedBaud(Baud baud);
+    void Sync(void);
+    void jpeg_snapshot(int CAPTURE_FRAMES);
+    ErrorNumber sendReset(ResetType specialReset);  
     
 private:
     SerialBuffered serial;
     static const int COMMAND_LENGTH = 6;
     static const int SYNCMAX = 60;
     static const int packageSize = 256;
-
     ErrorNumber sendInitial(Baud band, JpegResolution jr);
     ErrorNumber sendGetPicture(void);
     ErrorNumber sendSnapshot(void);    
     ErrorNumber sendSetPackageSize(uint16_t packageSize);
-    ErrorNumber sendReset(ResetType specialReset);  
+    //ErrorNumber sendReset(ResetType specialReset);  
     ErrorNumber recvData(uint32_t *length);
     ErrorNumber sendSync();
     ErrorNumber recvSync();
diff -r 5a6468b4164d -r 135493341acc SerialBuffered.cpp
--- a/SerialBuffered.cpp	Mon Jul 09 10:23:32 2012 +0000
+++ b/SerialBuffered.cpp	Thu Aug 03 04:28:14 2017 +0000
@@ -40,7 +40,7 @@
 size_t SerialBuffered::readBytes(uint8_t *bytes, size_t requested) {
     int i = 0;
     while (i < requested) {
-        int c = getc();
+        int c = ggetc();
         if (c < 0) {
             break;
         }
@@ -55,7 +55,7 @@
  *
  * @return A character. (-1:timeout)
  */
-int SerialBuffered::getc() {
+int SerialBuffered::ggetc() {
     timer.reset();
     timer.start();
     while (indexContentStart == indexContentEnd) {
@@ -79,7 +79,7 @@
 /**
  * Returns 1 if there is a character available to read, 0 otherwise.
  */
-int SerialBuffered::readable() {
+int SerialBuffered::rreadable() {
     return indexContentStart != indexContentEnd;
 }
 
diff -r 5a6468b4164d -r 135493341acc SerialBuffered.h
--- a/SerialBuffered.h	Mon Jul 09 10:23:32 2012 +0000
+++ b/SerialBuffered.h	Thu Aug 03 04:28:14 2017 +0000
@@ -24,12 +24,12 @@
      *
      * @return A character. (-1:timeout)
      */
-    int getc();
+    int ggetc();
 
     /**
      * Returns 1 if there is a character available to read, 0 otherwise.
      */
-    int readable();
+    int rreadable();
 
     /**
      * Set timeout for getc().