Library for LinkSprite Y201 JPEG serial camera.

Committer:
ashleymills
Date:
Sat Jun 02 11:47:56 2012 +0000
Revision:
0:4c0fca059a0a
Child:
1:30a6aeda21c2
[mbed] converted /3GCamera/Y201

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ashleymills 0:4c0fca059a0a 1 #include "Y201.h"
ashleymills 0:4c0fca059a0a 2 #include "rtos.h"
ashleymills 0:4c0fca059a0a 3
ashleymills 0:4c0fca059a0a 4 void Y201::trash() {
ashleymills 0:4c0fca059a0a 5 // wait for trash...
ashleymills 0:4c0fca059a0a 6 while(readable()) {
ashleymills 0:4c0fca059a0a 7 int x = getc();
ashleymills 0:4c0fca059a0a 8 }
ashleymills 0:4c0fca059a0a 9 }
ashleymills 0:4c0fca059a0a 10
ashleymills 0:4c0fca059a0a 11 bool Y201::readImage(int startAddress, int readLen, uint8_t *readBuffer) {
ashleymills 0:4c0fca059a0a 12 putSeq(readFileHead,readFileHeadLen);
ashleymills 0:4c0fca059a0a 13 putc(startAddress>>8); // Start Address MSB
ashleymills 0:4c0fca059a0a 14 putc(startAddress&255); // Start Address LSB
ashleymills 0:4c0fca059a0a 15 putc(0x00);
ashleymills 0:4c0fca059a0a 16 putc(0x00);
ashleymills 0:4c0fca059a0a 17 putc(readLen>>8); // Length of file MSB
ashleymills 0:4c0fca059a0a 18 putc(readLen&255); // length of file LSB
ashleymills 0:4c0fca059a0a 19 putc(0x00); // Interval MSB
ashleymills 0:4c0fca059a0a 20 putc(0x0A); // Interval LSB
ashleymills 0:4c0fca059a0a 21 int nread = 0;
ashleymills 0:4c0fca059a0a 22 if(waitFor(readFileAck,readFileAckLen)) {
ashleymills 0:4c0fca059a0a 23 Thread::wait(1);
ashleymills 0:4c0fca059a0a 24 while(nread<readLen) {
ashleymills 0:4c0fca059a0a 25 if(readable()) {
ashleymills 0:4c0fca059a0a 26 uint8_t c = getc();
ashleymills 0:4c0fca059a0a 27 readBuffer[nread] = c;
ashleymills 0:4c0fca059a0a 28 //printf("Read(%d): 0x%x\r\n",nread,readBuffer[nread]);
ashleymills 0:4c0fca059a0a 29 nread++;
ashleymills 0:4c0fca059a0a 30 }
ashleymills 0:4c0fca059a0a 31 }
ashleymills 0:4c0fca059a0a 32 } else {
ashleymills 0:4c0fca059a0a 33 printf("Error during file read\r\n");
ashleymills 0:4c0fca059a0a 34 return false;
ashleymills 0:4c0fca059a0a 35 }
ashleymills 0:4c0fca059a0a 36 return true;
ashleymills 0:4c0fca059a0a 37 }
ashleymills 0:4c0fca059a0a 38
ashleymills 0:4c0fca059a0a 39 bool Y201::waitFor(const int *seq, const int seqLen) {
ashleymills 0:4c0fca059a0a 40 int spos = 0;
ashleymills 0:4c0fca059a0a 41 long timeout = 100000000;
ashleymills 0:4c0fca059a0a 42 long timer = 0;
ashleymills 0:4c0fca059a0a 43 while(spos<seqLen) {
ashleymills 0:4c0fca059a0a 44 if(readable()) {
ashleymills 0:4c0fca059a0a 45 int c = getc();
ashleymills 0:4c0fca059a0a 46 //printf("read %x\r\n",c);
ashleymills 0:4c0fca059a0a 47 if(seq[spos]==c) {
ashleymills 0:4c0fca059a0a 48 spos++;
ashleymills 0:4c0fca059a0a 49 } else {
ashleymills 0:4c0fca059a0a 50 return false;
ashleymills 0:4c0fca059a0a 51 }
ashleymills 0:4c0fca059a0a 52 } else {
ashleymills 0:4c0fca059a0a 53 if(timer<timeout) {
ashleymills 0:4c0fca059a0a 54 timer++;
ashleymills 0:4c0fca059a0a 55 } else {
ashleymills 0:4c0fca059a0a 56 printf("timeout\r\n");
ashleymills 0:4c0fca059a0a 57 return false;
ashleymills 0:4c0fca059a0a 58 }
ashleymills 0:4c0fca059a0a 59 }
ashleymills 0:4c0fca059a0a 60 }
ashleymills 0:4c0fca059a0a 61 return true;
ashleymills 0:4c0fca059a0a 62 }
ashleymills 0:4c0fca059a0a 63
ashleymills 0:4c0fca059a0a 64 bool Y201::waitForInt(int bytes, int *fileSize) {
ashleymills 0:4c0fca059a0a 65 int spos = 0;
ashleymills 0:4c0fca059a0a 66 long timeout = 100000000;
ashleymills 0:4c0fca059a0a 67 long timer = 0;
ashleymills 0:4c0fca059a0a 68 *fileSize = 0;
ashleymills 0:4c0fca059a0a 69 while(spos<bytes) {
ashleymills 0:4c0fca059a0a 70 if(readable()) {
ashleymills 0:4c0fca059a0a 71 uint8_t val = getc();
ashleymills 0:4c0fca059a0a 72 if(spos==0) {
ashleymills 0:4c0fca059a0a 73 *fileSize += (val<<8);
ashleymills 0:4c0fca059a0a 74 } else {
ashleymills 0:4c0fca059a0a 75 *fileSize += val;
ashleymills 0:4c0fca059a0a 76 }
ashleymills 0:4c0fca059a0a 77
ashleymills 0:4c0fca059a0a 78 spos++;
ashleymills 0:4c0fca059a0a 79
ashleymills 0:4c0fca059a0a 80 } else {
ashleymills 0:4c0fca059a0a 81 if(timer<timeout) {
ashleymills 0:4c0fca059a0a 82 timer++;
ashleymills 0:4c0fca059a0a 83 } else {
ashleymills 0:4c0fca059a0a 84 return -1;
ashleymills 0:4c0fca059a0a 85 }
ashleymills 0:4c0fca059a0a 86 }
ashleymills 0:4c0fca059a0a 87 }
ashleymills 0:4c0fca059a0a 88 return true;
ashleymills 0:4c0fca059a0a 89 }
ashleymills 0:4c0fca059a0a 90
ashleymills 0:4c0fca059a0a 91 void Y201::putSeq(const int *seq, int seqLen) {
ashleymills 0:4c0fca059a0a 92 while(seqLen--) {
ashleymills 0:4c0fca059a0a 93 //printf("sent %x\r\n",*seq);
ashleymills 0:4c0fca059a0a 94 putc(*seq++);
ashleymills 0:4c0fca059a0a 95 }
ashleymills 0:4c0fca059a0a 96 }
ashleymills 0:4c0fca059a0a 97
ashleymills 0:4c0fca059a0a 98 Y201::Y201(PinName tx, PinName rx, const char *name) : Serial(tx,rx,name) {
ashleymills 0:4c0fca059a0a 99 baud(38400);
ashleymills 0:4c0fca059a0a 100 }
ashleymills 0:4c0fca059a0a 101
ashleymills 0:4c0fca059a0a 102 bool Y201::setImageSize(Y201ImageSize size) {
ashleymills 0:4c0fca059a0a 103 switch(size) {
ashleymills 0:4c0fca059a0a 104 case Y201::e640x480:
ashleymills 0:4c0fca059a0a 105 putSeq(set640x480,setSizeLen);
ashleymills 0:4c0fca059a0a 106 break;
ashleymills 0:4c0fca059a0a 107
ashleymills 0:4c0fca059a0a 108 case Y201::e160x120:
ashleymills 0:4c0fca059a0a 109 putSeq(set160x120,setSizeLen);
ashleymills 0:4c0fca059a0a 110 break;
ashleymills 0:4c0fca059a0a 111
ashleymills 0:4c0fca059a0a 112 case Y201::e320x240:
ashleymills 0:4c0fca059a0a 113 putSeq(set320x240,setSizeLen);
ashleymills 0:4c0fca059a0a 114 break;
ashleymills 0:4c0fca059a0a 115 }
ashleymills 0:4c0fca059a0a 116 return waitFor(setSizeAck,setSizeAckLen);
ashleymills 0:4c0fca059a0a 117 }
ashleymills 0:4c0fca059a0a 118
ashleymills 0:4c0fca059a0a 119 bool Y201::takePicture() {
ashleymills 0:4c0fca059a0a 120 putSeq(takePicSeq,takePicSeqLen);
ashleymills 0:4c0fca059a0a 121 Thread::wait(50);
ashleymills 0:4c0fca059a0a 122 return waitFor(takePicSeqAck,takePicSeqAckLen);
ashleymills 0:4c0fca059a0a 123 }
ashleymills 0:4c0fca059a0a 124
ashleymills 0:4c0fca059a0a 125 bool Y201::readImageSize(int *fileSize) {
ashleymills 0:4c0fca059a0a 126 putSeq(readFileSize,readFileSizeLen);
ashleymills 0:4c0fca059a0a 127 bool ret = waitFor(readFileSizeAck,readFileSizeAckLen);
ashleymills 0:4c0fca059a0a 128 if(!ret)
ashleymills 0:4c0fca059a0a 129 return false;
ashleymills 0:4c0fca059a0a 130 return waitForInt(2,fileSize);
ashleymills 0:4c0fca059a0a 131 }
ashleymills 0:4c0fca059a0a 132
ashleymills 0:4c0fca059a0a 133 bool Y201::reset() {
ashleymills 0:4c0fca059a0a 134 putSeq(resetSeq,resetSeqLen);
ashleymills 0:4c0fca059a0a 135 bool ret = waitFor(resetSeqAck,resetSeqAckLen);
ashleymills 0:4c0fca059a0a 136
ashleymills 0:4c0fca059a0a 137 // wait for trash
ashleymills 0:4c0fca059a0a 138 int count = 3;
ashleymills 0:4c0fca059a0a 139 printf("Waiting ");
ashleymills 0:4c0fca059a0a 140 while(count) {
ashleymills 0:4c0fca059a0a 141 printf("%d ",count);
ashleymills 0:4c0fca059a0a 142 count--;
ashleymills 0:4c0fca059a0a 143 if(readable()) {
ashleymills 0:4c0fca059a0a 144 while(readable()) {
ashleymills 0:4c0fca059a0a 145 int c = getc();
ashleymills 0:4c0fca059a0a 146 printf("READ %x\r\n",c);
ashleymills 0:4c0fca059a0a 147 }
ashleymills 0:4c0fca059a0a 148 }
ashleymills 0:4c0fca059a0a 149 Thread::wait(1000);
ashleymills 0:4c0fca059a0a 150 }
ashleymills 0:4c0fca059a0a 151 printf("done.\r\n");
ashleymills 0:4c0fca059a0a 152 return ret;
ashleymills 0:4c0fca059a0a 153 }