Library for LinkSprite Y201 JPEG serial camera.

Files at this revision

API Documentation at this revision

Comitter:
ashleymills
Date:
Fri Jul 20 08:59:03 2012 +0000
Parent:
9:fbd80a803857
Commit message:
Added change baud rate, then removed it, as this apparently reprograms the camera ROM !! Nice product linksprite

Changed in this revision

Y201.cpp Show annotated file Show diff for this revision Revisions of this file
Y201.h Show annotated file Show diff for this revision Revisions of this file
diff -r fbd80a803857 -r fd12c96da974 Y201.cpp
--- a/Y201.cpp	Thu Jul 19 15:48:25 2012 +0000
+++ b/Y201.cpp	Fri Jul 20 08:59:03 2012 +0000
@@ -1,18 +1,24 @@
 #include "Y201.h"
 #include "rtos.h"
 
-const int Y201::resetSeq       [4] = {0x56,0x00,0x26,0x00};
-const int Y201::resetSeqAck    [4] = {0x76,0x00,0x26,0x00};
-const int Y201::takePicSeq     [5] = {0x56,0x00,0x36,0x01,0x00};
-const int Y201::takePicSeqAck  [5] = {0x76,0x00,0x36,0x00,0x00};
-const int Y201::set160x120     [9] = {0x56,0x00,0x31,0x05,0x04,0x01,0x00,0x19,0x22};
-const int Y201::set320x240     [9] = {0x56,0x00,0x31,0x05,0x04,0x01,0x00,0x19,0x11};
-const int Y201::set640x480     [9] = {0x56,0x00,0x31,0x05,0x04,0x01,0x00,0x19,0x00};
-const int Y201::setSizeAck     [5] = {0x76,0x00,0x31,0x00,0x00};
-const int Y201::readFileSize   [5] = {0x56,0x00,0x34,0x01,0x00};
-const int Y201::readFileSizeAck[7] = {0x76,0x00,0x34,0x00,0x04,0x00,0x00};
-const int Y201::readFileHead   [8] = {0x56,0x00,0x32,0x0C,0x00,0x0A,0x00,0x00};
-const int Y201::readFileAck    [5] = {0x76,0x00,0x32,0x00,0x00};
+const char Y201::resetSeq            [4] = {0x56,0x00,0x26,0x00};
+const char Y201::resetSeqAck         [4] = {0x76,0x00,0x26,0x00};
+const char Y201::takePicSeq          [5] = {0x56,0x00,0x36,0x01,0x00};
+const char Y201::takePicSeqAck       [5] = {0x76,0x00,0x36,0x00,0x00};
+const char Y201::set160x120          [9] = {0x56,0x00,0x31,0x05,0x04,0x01,0x00,0x19,0x22};
+const char Y201::set320x240          [9] = {0x56,0x00,0x31,0x05,0x04,0x01,0x00,0x19,0x11};
+const char Y201::set640x480          [9] = {0x56,0x00,0x31,0x05,0x04,0x01,0x00,0x19,0x00};
+const char Y201::setSizeAck          [5] = {0x76,0x00,0x31,0x00,0x00};
+const char Y201::readFileSize        [5] = {0x56,0x00,0x34,0x01,0x00};
+const char Y201::readFileSizeAck     [7] = {0x76,0x00,0x34,0x00,0x04,0x00,0x00};
+const char Y201::readFileHead        [8] = {0x56,0x00,0x32,0x0C,0x00,0x0A,0x00,0x00};
+const char Y201::readFileAck         [5] = {0x76,0x00,0x32,0x00,0x00};
+//const char Y201::changeBaudRateSeq   [5] = {0x56,0x00,0x24,0x03,0x01};
+//const char Y201::changeBaudRateAck   [5] = {0x76,0x00,0x24,0x00,0x00};
+const char Y201::enterPowerSavingSeq [7] = {0x56,0x00,0x3E,0x03,0x00,0x01,0x01};
+const char Y201::enterPowerSavingAck [5] = {0x76,0x00,0x3E,0x00,0x00};
+const char Y201::exitPowerSavingSeq  [7] = {0x56,0x00,0x3E,0x03,0x00,0x01,0x00};
+const char Y201::exitPowerSavingAck  [5] = {0x76,0x00,0x3E,0x00,0x00};
 
 void Y201::trash() {
     // wait for trash...
@@ -22,6 +28,8 @@
 }
 
 bool Y201::readImage(int startAddress, int readLen, uint8_t *readBuffer) {
+   trash(); // camera sends random crap after sending certain responses, so need to clear it
+   //Thread::wait(5);
    putSeq(readFileHead,readFileHeadLen);
    putc(startAddress>>8); // Start Address MSB
    putc(startAddress&255); // Start Address LSB
@@ -39,28 +47,25 @@
       while(nread<readLen) {
          if(readable()) {
             uint8_t c = getc();
-           // fprintf(stdout, "[%02x]", c);
+            // fprintf(stdout, "[%02x]", c);
             readBuffer[nread] = c;
             nread++;
             t.reset();
-         }
-         else
-         {
-           if(t.read_ms()>3000)
-           {
-             fprintf(stdout, "Blocked!\n Missed %d bytes over %d\nLast byte read is %02x\n", readLen - nread, readLen, readBuffer[nread-1]);
-             return false;
-           }
+         } else {
+            if(t.read_ms()>3000) {
+               fprintf(stdout, "Blocked!\n Missed %d bytes over %d\nLast byte read is %02x\n", readLen - nread, readLen, readBuffer[nread-1]);
+               return false;
+            }
          }
      }
      Thread::wait(1);
-  } else {
-     return false;
-  }
-  return true;
+   } else {
+      return false;
+   }
+   return true;
 }
 
-bool Y201::waitFor(const int *seq, const int seqLen) {
+bool Y201::waitFor(const char *seq, const int seqLen) {
     int spos = 0;
     long timeout = 100;
     Timer timer;
@@ -108,7 +113,7 @@
     return true;
 }
 
-void Y201::putSeq(const int *seq, int seqLen) {
+void Y201::putSeq(const char *seq, int seqLen) {
     while(seqLen--) {
         putc(*seq++);
     }
@@ -141,6 +146,16 @@
    return waitFor(takePicSeqAck,takePicSeqAckLen);
 }
 
+bool Y201::enterPowerSaving() {
+    putSeq(enterPowerSavingSeq,enterPowerSavingLen);
+    return waitFor(enterPowerSavingAck,enterPowerSavingAckLen);
+}
+
+bool Y201::exitPowerSaving() {
+    putSeq(exitPowerSavingSeq,exitPowerSavingLen);
+    return waitFor(exitPowerSavingAck,exitPowerSavingAckLen);
+}
+
 bool Y201::readImageSize(int *fileSize) {
    putSeq(readFileSize,readFileSizeLen);
    bool ret = waitFor(readFileSizeAck,readFileSizeAckLen);
@@ -166,3 +181,59 @@
   }
   return ret;
 }
+
+/*
+
+THIS SIMPLY DOESN'T WORK -- Known bug, LinkSprite quality products
+
+
+bool Y201::changeBaudRate(Y201BaudRate baudRate) {
+   // put the prefix
+   putSeq(changeBaudRateSeq,changeBaudRateSeqLen);
+   int br = 9600;
+   
+   // put the baud rate
+   switch(baudRate) {
+      case Y201::e9600:
+         putc(0xAE);
+         putc(0xC8);
+         br = 9600;
+      break;
+      
+      case Y201::e19200:
+         putc(0x56);
+         putc(0xE4);
+         br = 19200;
+      break;
+      
+      case Y201::e38400:
+         putc(0x2A);
+         putc(0xF2);
+         br = 38400;
+      break;
+      
+      case Y201::e57600:
+         putc(0x1C);
+         putc(0x4C);
+         br = 57600;
+      break;
+      
+      case Y201::e115200:
+         putc(0xAE);
+         putc(0xC8);
+         br = 115200;
+      break;
+      
+      default:
+         putc(0xAE);
+         putc(0xC8);
+         br = 9600;
+      break;
+   }
+   
+   baud(br);
+   
+   // wait for the ack
+   return waitFor(changeBaudRateAck,changeBaudRateAckLen);
+}
+*/
\ No newline at end of file
diff -r fbd80a803857 -r fd12c96da974 Y201.h
--- a/Y201.h	Thu Jul 19 15:48:25 2012 +0000
+++ b/Y201.h	Fri Jul 20 08:59:03 2012 +0000
@@ -1,5 +1,5 @@
 #include "mbed.h"
-#define MODSERIAL_DEFAULT_RX_BUFFER_SIZE 512
+#define MODSERIAL_DEFAULT_RX_BUFFER_SIZE 1024
 #define MODSERIAL_DEFAULT_TX_BUFFER_SIZE 64
 #include "MODSERIAL.h"
 #pragma once
@@ -9,36 +9,60 @@
 private:
 
     // serial commands to drive camera
-    static const int resetSeq        [4];
-    static const int resetSeqLen        = 4;
-    static const int resetSeqAck     [4];
-    static const int resetSeqAckLen     = 4;
-    static const int takePicSeq      [5];
-    static const int takePicSeqLen      = 5;
-    static const int takePicSeqAck   [5];
-    static const int takePicSeqAckLen   = 5;
-    static const int set160x120      [9];
-    static const int set320x240      [9];
-    static const int set640x480      [9];
-    static const int setSizeLen         = 9;
-    static const int setSizeAck      [5];
-    static const int setSizeAckLen      = 5;
-    static const int readFileSize    [5];
-    static const int readFileSizeLen    = 5;
-    static const int readFileSizeAck [7];
-    static const int readFileSizeAckLen = 7;
-    static const int readFileHead    [8];
-    static const int readFileHeadLen    = 8;
-    static const int readFileAck     [5];
-    static const int readFileAckLen     = 5;
+    static const char resetSeq         [4];
+    static const int  resetSeqLen          = 4;
+    static const char resetSeqAck      [4];
+    static const int  resetSeqAckLen       = 4;
+    static const char takePicSeq       [5];
+    static const int  takePicSeqLen        = 5;
+    static const char takePicSeqAck    [5];
+    static const int  takePicSeqAckLen     = 5;
+    static const char set160x120       [9];
+    static const char set320x240       [9];
+    static const char set640x480       [9];
+    static const int  setSizeLen           = 9;
+    static const char setSizeAck       [5];
+    static const int  setSizeAckLen        = 5;
+    static const char readFileSize     [5];
+    static const int  readFileSizeLen      = 5;
+    static const char readFileSizeAck  [7];
+    static const int  readFileSizeAckLen   = 7;
+    static const char readFileHead     [8];
+    static const int  readFileHeadLen      = 8;
+    static const char readFileAck      [5];
+    static const int  readFileAckLen       = 5;
+    /*
+    static const char changeBaudRateSeq  [5];
+    static const int  changeBaudRateSeqLen = 5;
+    static const char changeBaudRateAck  [5];
+    static const int  changeBaudRateAckLen = 5;
+    */
+    static const char enterPowerSavingSeq[7];
+    static const int  enterPowerSavingLen  = 7;
+    static const char enterPowerSavingAck[5];
+    static const int  enterPowerSavingAckLen = 5;
+    static const char exitPowerSavingSeq [7];
+    static const int  exitPowerSavingLen     = 7;
+    static const char exitPowerSavingAck[5];
+    static const int  exitPowerSavingAckLen  = 5;
     
 public:
     
     enum Y201ImageSize {
-        e160x120,
-        e320x240,
-        e640x480
+       e160x120,
+       e320x240,
+       e640x480
     };
+    
+    /*
+    enum Y201BaudRate {
+       e9600,
+       e19200,
+       e38400,
+       e57600,
+       e115200
+    };
+    */
 
     Y201(PinName tx, PinName rx, const char *name = NULL);
 
@@ -46,11 +70,14 @@
     bool takePicture();
     bool readImageSize(int *fileSize);
     bool readImage(int startAddress, int readLen, uint8_t *readBuffer);
+    //bool changeBaudRate(Y201BaudRate baudRate);
+    bool enterPowerSaving();
+    bool exitPowerSaving();
     void trash();
     
     bool waitForInt(int bytes, int *fileSize);
-    bool waitFor(const int *seq, const int seqLen);
-    void putSeq(const int *seq, int seqLen);
+    bool waitFor(const char *seq, const int seqLen);
+    void putSeq(const char *seq, int seqLen);
     bool reset();
 
 };