Library for LinkSprite Y201 JPEG serial camera.
Y201.cpp@7:d7ed03291f0a, 2012-07-19 (annotated)
- Committer:
- ashleymills
- Date:
- Thu Jul 19 07:49:06 2012 +0000
- Revision:
- 7:d7ed03291f0a
- Parent:
- 5:df71c00b97d8
Indentation fixes
Who changed what in which revision?
User | Revision | Line number | New 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)) { |
donatien | 5:df71c00b97d8 | 23 | //Thread::wait(1); |
donatien | 5:df71c00b97d8 | 24 | Timer t; |
donatien | 5:df71c00b97d8 | 25 | t.start(); |
ashleymills | 0:4c0fca059a0a | 26 | while(nread<readLen) { |
ashleymills | 0:4c0fca059a0a | 27 | if(readable()) { |
ashleymills | 0:4c0fca059a0a | 28 | uint8_t c = getc(); |
donatien | 5:df71c00b97d8 | 29 | // fprintf(stdout, "[%02x]", c); |
ashleymills | 0:4c0fca059a0a | 30 | readBuffer[nread] = c; |
ashleymills | 0:4c0fca059a0a | 31 | nread++; |
donatien | 5:df71c00b97d8 | 32 | t.reset(); |
ashleymills | 7:d7ed03291f0a | 33 | } else { |
ashleymills | 7:d7ed03291f0a | 34 | if(t.read_ms()>3000) { |
ashleymills | 7:d7ed03291f0a | 35 | fprintf(stdout, "Blocked!\n Missed %d bytes over %d\nLast byte read is %02x\n", readLen - nread, readLen, readBuffer[nread-1]); |
ashleymills | 7:d7ed03291f0a | 36 | return false; |
ashleymills | 7:d7ed03291f0a | 37 | } |
ashleymills | 0:4c0fca059a0a | 38 | } |
ashleymills | 0:4c0fca059a0a | 39 | } |
donatien | 5:df71c00b97d8 | 40 | //Thread::wait(1); |
ashleymills | 0:4c0fca059a0a | 41 | } else { |
ashleymills | 0:4c0fca059a0a | 42 | return false; |
ashleymills | 0:4c0fca059a0a | 43 | } |
ashleymills | 0:4c0fca059a0a | 44 | return true; |
ashleymills | 0:4c0fca059a0a | 45 | } |
ashleymills | 0:4c0fca059a0a | 46 | |
ashleymills | 0:4c0fca059a0a | 47 | bool Y201::waitFor(const int *seq, const int seqLen) { |
ashleymills | 0:4c0fca059a0a | 48 | int spos = 0; |
donatien | 5:df71c00b97d8 | 49 | long timeout = 100; |
donatien | 5:df71c00b97d8 | 50 | Timer timer; |
donatien | 5:df71c00b97d8 | 51 | timer.start(); |
ashleymills | 0:4c0fca059a0a | 52 | while(spos<seqLen) { |
ashleymills | 0:4c0fca059a0a | 53 | if(readable()) { |
ashleymills | 0:4c0fca059a0a | 54 | int c = getc(); |
ashleymills | 0:4c0fca059a0a | 55 | if(seq[spos]==c) { |
ashleymills | 0:4c0fca059a0a | 56 | spos++; |
ashleymills | 0:4c0fca059a0a | 57 | } else { |
ashleymills | 0:4c0fca059a0a | 58 | return false; |
ashleymills | 0:4c0fca059a0a | 59 | } |
ashleymills | 0:4c0fca059a0a | 60 | } else { |
donatien | 5:df71c00b97d8 | 61 | if(timer.read_ms()>timeout) { |
ashleymills | 0:4c0fca059a0a | 62 | return false; |
ashleymills | 0:4c0fca059a0a | 63 | } |
ashleymills | 0:4c0fca059a0a | 64 | } |
ashleymills | 0:4c0fca059a0a | 65 | } |
ashleymills | 0:4c0fca059a0a | 66 | return true; |
ashleymills | 0:4c0fca059a0a | 67 | } |
ashleymills | 0:4c0fca059a0a | 68 | |
ashleymills | 0:4c0fca059a0a | 69 | bool Y201::waitForInt(int bytes, int *fileSize) { |
ashleymills | 0:4c0fca059a0a | 70 | int spos = 0; |
donatien | 5:df71c00b97d8 | 71 | long timeout = 100; |
donatien | 5:df71c00b97d8 | 72 | Timer timer; |
donatien | 5:df71c00b97d8 | 73 | timer.start(); |
ashleymills | 0:4c0fca059a0a | 74 | *fileSize = 0; |
ashleymills | 0:4c0fca059a0a | 75 | while(spos<bytes) { |
ashleymills | 0:4c0fca059a0a | 76 | if(readable()) { |
ashleymills | 0:4c0fca059a0a | 77 | uint8_t val = getc(); |
ashleymills | 0:4c0fca059a0a | 78 | if(spos==0) { |
ashleymills | 0:4c0fca059a0a | 79 | *fileSize += (val<<8); |
ashleymills | 0:4c0fca059a0a | 80 | } else { |
ashleymills | 0:4c0fca059a0a | 81 | *fileSize += val; |
ashleymills | 0:4c0fca059a0a | 82 | } |
ashleymills | 0:4c0fca059a0a | 83 | |
ashleymills | 0:4c0fca059a0a | 84 | spos++; |
ashleymills | 0:4c0fca059a0a | 85 | |
ashleymills | 0:4c0fca059a0a | 86 | } else { |
donatien | 5:df71c00b97d8 | 87 | if(timer.read_ms()>timeout) { |
ashleymills | 1:30a6aeda21c2 | 88 | return false; |
ashleymills | 0:4c0fca059a0a | 89 | } |
ashleymills | 0:4c0fca059a0a | 90 | } |
ashleymills | 0:4c0fca059a0a | 91 | } |
ashleymills | 0:4c0fca059a0a | 92 | return true; |
ashleymills | 0:4c0fca059a0a | 93 | } |
ashleymills | 0:4c0fca059a0a | 94 | |
ashleymills | 0:4c0fca059a0a | 95 | void Y201::putSeq(const int *seq, int seqLen) { |
ashleymills | 0:4c0fca059a0a | 96 | while(seqLen--) { |
ashleymills | 0:4c0fca059a0a | 97 | putc(*seq++); |
ashleymills | 0:4c0fca059a0a | 98 | } |
ashleymills | 0:4c0fca059a0a | 99 | } |
ashleymills | 0:4c0fca059a0a | 100 | |
ashleymills | 2:7d8a6087f2e5 | 101 | Y201::Y201(PinName tx, PinName rx, const char *name) : MODSERIAL(tx,rx,name) { |
ashleymills | 0:4c0fca059a0a | 102 | baud(38400); |
ashleymills | 0:4c0fca059a0a | 103 | } |
ashleymills | 0:4c0fca059a0a | 104 | |
ashleymills | 0:4c0fca059a0a | 105 | bool Y201::setImageSize(Y201ImageSize size) { |
ashleymills | 0:4c0fca059a0a | 106 | switch(size) { |
ashleymills | 0:4c0fca059a0a | 107 | case Y201::e640x480: |
ashleymills | 0:4c0fca059a0a | 108 | putSeq(set640x480,setSizeLen); |
ashleymills | 0:4c0fca059a0a | 109 | break; |
ashleymills | 0:4c0fca059a0a | 110 | |
ashleymills | 0:4c0fca059a0a | 111 | case Y201::e160x120: |
ashleymills | 0:4c0fca059a0a | 112 | putSeq(set160x120,setSizeLen); |
ashleymills | 0:4c0fca059a0a | 113 | break; |
ashleymills | 0:4c0fca059a0a | 114 | |
ashleymills | 0:4c0fca059a0a | 115 | case Y201::e320x240: |
ashleymills | 0:4c0fca059a0a | 116 | putSeq(set320x240,setSizeLen); |
ashleymills | 0:4c0fca059a0a | 117 | break; |
ashleymills | 0:4c0fca059a0a | 118 | } |
ashleymills | 0:4c0fca059a0a | 119 | return waitFor(setSizeAck,setSizeAckLen); |
ashleymills | 0:4c0fca059a0a | 120 | } |
ashleymills | 0:4c0fca059a0a | 121 | |
ashleymills | 0:4c0fca059a0a | 122 | bool Y201::takePicture() { |
ashleymills | 0:4c0fca059a0a | 123 | putSeq(takePicSeq,takePicSeqLen); |
ashleymills | 0:4c0fca059a0a | 124 | Thread::wait(50); |
ashleymills | 0:4c0fca059a0a | 125 | return waitFor(takePicSeqAck,takePicSeqAckLen); |
ashleymills | 0:4c0fca059a0a | 126 | } |
ashleymills | 0:4c0fca059a0a | 127 | |
ashleymills | 0:4c0fca059a0a | 128 | bool Y201::readImageSize(int *fileSize) { |
ashleymills | 0:4c0fca059a0a | 129 | putSeq(readFileSize,readFileSizeLen); |
ashleymills | 0:4c0fca059a0a | 130 | bool ret = waitFor(readFileSizeAck,readFileSizeAckLen); |
ashleymills | 0:4c0fca059a0a | 131 | if(!ret) |
ashleymills | 0:4c0fca059a0a | 132 | return false; |
ashleymills | 0:4c0fca059a0a | 133 | return waitForInt(2,fileSize); |
ashleymills | 0:4c0fca059a0a | 134 | } |
ashleymills | 0:4c0fca059a0a | 135 | |
ashleymills | 0:4c0fca059a0a | 136 | bool Y201::reset() { |
ashleymills | 0:4c0fca059a0a | 137 | putSeq(resetSeq,resetSeqLen); |
ashleymills | 0:4c0fca059a0a | 138 | bool ret = waitFor(resetSeqAck,resetSeqAckLen); |
ashleymills | 0:4c0fca059a0a | 139 | |
ashleymills | 0:4c0fca059a0a | 140 | // wait for trash |
ashleymills | 0:4c0fca059a0a | 141 | int count = 3; |
ashleymills | 0:4c0fca059a0a | 142 | while(count) { |
ashleymills | 0:4c0fca059a0a | 143 | count--; |
ashleymills | 0:4c0fca059a0a | 144 | if(readable()) { |
ashleymills | 0:4c0fca059a0a | 145 | while(readable()) { |
ashleymills | 0:4c0fca059a0a | 146 | int c = getc(); |
ashleymills | 0:4c0fca059a0a | 147 | } |
ashleymills | 0:4c0fca059a0a | 148 | } |
ashleymills | 0:4c0fca059a0a | 149 | Thread::wait(1000); |
ashleymills | 0:4c0fca059a0a | 150 | } |
ashleymills | 0:4c0fca059a0a | 151 | return ret; |
ashleymills | 0:4c0fca059a0a | 152 | } |