Library for LinkSprite Y201 JPEG serial camera.
Y201.cpp
- Committer:
- ashleymills
- Date:
- 2012-06-29
- Revision:
- 2:7d8a6087f2e5
- Parent:
- 1:30a6aeda21c2
- Child:
- 4:6e575c19681f
File content as of revision 2:7d8a6087f2e5:
#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++; } } Thread::wait(1); } 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 = 1000000000; 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 false; } } } 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) : MODSERIAL(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; }