Library for LinkSprite Y201 JPEG serial camera.

Files at this revision

API Documentation at this revision

Comitter:
ashleymills
Date:
Sat Jun 02 11:47:56 2012 +0000
Child:
1:30a6aeda21c2
Commit message:
[mbed] converted /3GCamera/Y201

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Y201.cpp	Sat Jun 02 11:47:56 2012 +0000
@@ -0,0 +1,153 @@
+#include "Y201.h"
+#include "rtos.h"
+
+void Y201::trash() {
+    // wait for trash...
+    while(readable()) {
+        int x = getc();
+    }
+}
+
+bool Y201::readImage(int startAddress, int readLen, uint8_t *readBuffer) {
+   putSeq(readFileHead,readFileHeadLen);
+   putc(startAddress>>8); // Start Address MSB
+   putc(startAddress&255); // Start Address LSB
+   putc(0x00);
+   putc(0x00);
+   putc(readLen>>8); // Length of file MSB
+   putc(readLen&255); // length of file LSB
+   putc(0x00); // Interval MSB
+   putc(0x0A); // Interval LSB
+   int nread = 0;
+   if(waitFor(readFileAck,readFileAckLen)) {
+      Thread::wait(1);
+      while(nread<readLen) {
+         if(readable()) {
+            uint8_t c = getc();
+            readBuffer[nread] = c;
+            //printf("Read(%d): 0x%x\r\n",nread,readBuffer[nread]);
+            nread++;
+         }
+     }
+  } else {
+     printf("Error during file read\r\n");
+     return false;
+  }
+  return true;
+}
+
+bool Y201::waitFor(const int *seq, const int seqLen) {
+    int spos = 0;
+    long timeout = 100000000;
+    long timer = 0;
+    while(spos<seqLen) {
+        if(readable()) {
+            int c = getc();
+            //printf("read %x\r\n",c);
+            if(seq[spos]==c) {
+                spos++;
+            } else {
+                return false;
+            }
+        } else {
+            if(timer<timeout) {
+                timer++;
+            } else {
+                printf("timeout\r\n");
+                return false;
+            }
+        }
+    }
+    return true;
+}
+
+bool Y201::waitForInt(int bytes, int *fileSize) {
+   int spos = 0;
+   long timeout = 100000000;
+   long timer = 0;
+   *fileSize = 0;
+   while(spos<bytes) {
+        if(readable()) {
+            uint8_t val = getc();
+            if(spos==0) {
+                *fileSize += (val<<8);
+            } else {
+                *fileSize += val;
+            }
+            
+            spos++;
+            
+        } else {
+            if(timer<timeout) {
+                timer++;
+            } else {
+                return -1;
+            }
+        }
+    }
+    return true;
+}
+
+void Y201::putSeq(const int *seq, int seqLen) {
+    while(seqLen--) {
+        //printf("sent %x\r\n",*seq);
+        putc(*seq++);
+    }
+}
+
+Y201::Y201(PinName tx, PinName rx, const char *name) : Serial(tx,rx,name) {
+    baud(38400);
+}
+
+bool Y201::setImageSize(Y201ImageSize size) {
+    switch(size) {
+        case Y201::e640x480:
+            putSeq(set640x480,setSizeLen);
+        break;
+        
+        case Y201::e160x120:
+            putSeq(set160x120,setSizeLen);
+        break;
+        
+        case Y201::e320x240:
+            putSeq(set320x240,setSizeLen);
+        break;
+    }
+    return waitFor(setSizeAck,setSizeAckLen);
+}
+
+bool Y201::takePicture() {
+   putSeq(takePicSeq,takePicSeqLen);
+   Thread::wait(50);
+   return waitFor(takePicSeqAck,takePicSeqAckLen);
+}
+
+bool Y201::readImageSize(int *fileSize) {
+   putSeq(readFileSize,readFileSizeLen);
+   bool ret = waitFor(readFileSizeAck,readFileSizeAckLen);
+   if(!ret)
+      return false;
+  return waitForInt(2,fileSize);
+}
+
+bool Y201::reset() {
+  putSeq(resetSeq,resetSeqLen);
+  bool ret = waitFor(resetSeqAck,resetSeqAckLen);
+
+  // wait for trash
+  int count = 3;
+  printf("Waiting ");
+  while(count) {
+    printf("%d ",count);
+    count--;
+    if(readable()) {
+        while(readable()) {
+            int c = getc();
+            printf("READ %x\r\n",c);
+        }
+    }
+    Thread::wait(1000);
+  }
+  printf("done.\r\n");
+  return ret;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Y201.h	Sat Jun 02 11:47:56 2012 +0000
@@ -0,0 +1,53 @@
+#include "mbed.h"
+#pragma once
+
+class Y201 : Serial {
+
+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;
+    
+public:
+    
+    enum Y201ImageSize {
+        e160x120,
+        e320x240,
+        e640x480
+    };
+
+	Y201(PinName tx, PinName rx, const char *name = NULL);
+
+	bool setImageSize(Y201ImageSize size);
+    bool takePicture();
+    bool readImageSize(int *fileSize);
+    bool readImage(int startAddress, int readLen, uint8_t *readBuffer);
+    void trash();
+    
+	bool waitForInt(int bytes, int *fileSize);
+	bool waitFor(const int *seq, const int seqLen);
+	void putSeq(const int *seq, int seqLen);
+	bool reset();
+
+};