mbed serial interface for the OV528 camera (uCAM TTL 120 from 4D systems).
Dependents: mbed_uCAM_TTL120_20141118
Library for interfacing the mbed to the OV528 jpeg engine camera from Omnivision. I tested this program using the uCAM TTL 120 from 4D systems, now obsolete.
Above is a saved jpeg image from the camera at 480x640 resolution
Camera and TFT screen setup
Closeup of the TFT screen displaying a 60x80 pixel 16 bit color bitmap image. The RGB (565) pixel colors were reversed when read from the camera relative to the TFT screen so a bit mask and shift had to be applied when displaying the pixel color.
bit mask and shift
int k=12; for(int i=0;i<60;i++){ for(int j=0;j<80;j++){ //returned 16-bit color is 565(RGB) pixel_col = (imageData[k] << 8); k++; pixel_col += imageData[k]; k++; // fputc(((picture[k] >> 8) & 0xff), fp); //write pixel high byte to file // fputc(picture[k] & 0xff, fp); //write low byte //swap R and B bits in returned pixel for TFT display int TFT_pix_col = 0; TFT_pix_col += (pixel_col & 0x001f) << 11; TFT_pix_col += (pixel_col & 0xf800) >> 11; TFT_pix_col += (pixel_col & 0x07e0); TFT.pixel(j, i, TFT_pix_col); //Do something with the pixel } }
uCAM_TTL120.cpp@2:f80257456c6e, 2014-11-18 (annotated)
- Committer:
- jebradshaw
- Date:
- Tue Nov 18 20:26:06 2014 +0000
- Revision:
- 2:f80257456c6e
- Parent:
- 0:fcc0eaeeb35c
- Child:
- 3:85db6ea8c0a4
improvements on GetACK and set baud rate now works
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jebradshaw | 0:fcc0eaeeb35c | 1 | // Library for uCAM-TTL120 from Saelig |
jebradshaw | 0:fcc0eaeeb35c | 2 | |
jebradshaw | 0:fcc0eaeeb35c | 3 | #include "uCAM_TTL120.h" |
jebradshaw | 0:fcc0eaeeb35c | 4 | #define __DEBUG |
jebradshaw | 0:fcc0eaeeb35c | 5 | |
jebradshaw | 0:fcc0eaeeb35c | 6 | //----------------- uCAM Functions ------------------------------ |
jebradshaw | 0:fcc0eaeeb35c | 7 | |
jebradshaw | 0:fcc0eaeeb35c | 8 | uCAM_TTL120::uCAM_TTL120(PinName tx, PinName rx) : _cam(tx, rx) { |
jebradshaw | 0:fcc0eaeeb35c | 9 | _cam.baud(115200); //Initial baud rate |
jebradshaw | 0:fcc0eaeeb35c | 10 | timerCam = new Timer(); |
jebradshaw | 0:fcc0eaeeb35c | 11 | |
jebradshaw | 0:fcc0eaeeb35c | 12 | timerCam->start(); |
jebradshaw | 0:fcc0eaeeb35c | 13 | } |
jebradshaw | 0:fcc0eaeeb35c | 14 | |
jebradshaw | 0:fcc0eaeeb35c | 15 | int uCAM_TTL120::uCAM_read(char *str, int numchars, float timeout){ |
jebradshaw | 0:fcc0eaeeb35c | 16 | int i = 0; |
jebradshaw | 0:fcc0eaeeb35c | 17 | float t_start = timerCam->read(); |
jebradshaw | 0:fcc0eaeeb35c | 18 | |
jebradshaw | 0:fcc0eaeeb35c | 19 | while((timerCam->read() < (t_start + timeout)) && (i < numchars)){ |
jebradshaw | 0:fcc0eaeeb35c | 20 | if(_cam.readable()){ |
jebradshaw | 0:fcc0eaeeb35c | 21 | str[i] = _cam.getc(); |
jebradshaw | 0:fcc0eaeeb35c | 22 | i++; |
jebradshaw | 0:fcc0eaeeb35c | 23 | } |
jebradshaw | 0:fcc0eaeeb35c | 24 | } |
jebradshaw | 0:fcc0eaeeb35c | 25 | if(timerCam->read() >= (t_start + timeout)){ |
jebradshaw | 0:fcc0eaeeb35c | 26 | return -1; |
jebradshaw | 0:fcc0eaeeb35c | 27 | } |
jebradshaw | 0:fcc0eaeeb35c | 28 | if(i >= numchars){ |
jebradshaw | 0:fcc0eaeeb35c | 29 | return -2; |
jebradshaw | 0:fcc0eaeeb35c | 30 | } |
jebradshaw | 0:fcc0eaeeb35c | 31 | |
jebradshaw | 0:fcc0eaeeb35c | 32 | return i; |
jebradshaw | 0:fcc0eaeeb35c | 33 | } |
jebradshaw | 0:fcc0eaeeb35c | 34 | |
jebradshaw | 0:fcc0eaeeb35c | 35 | void uCAM_TTL120::uCAM_Command_Send(int command,char p1,char p2,char p3,char p4) |
jebradshaw | 0:fcc0eaeeb35c | 36 | { |
jebradshaw | 0:fcc0eaeeb35c | 37 | _cam.putc((char)(command >> 8) & 0xff); |
jebradshaw | 0:fcc0eaeeb35c | 38 | _cam.putc((char)(command & 0xff)); |
jebradshaw | 0:fcc0eaeeb35c | 39 | _cam.putc(p1); |
jebradshaw | 0:fcc0eaeeb35c | 40 | _cam.putc(p2); |
jebradshaw | 0:fcc0eaeeb35c | 41 | _cam.putc(p3); |
jebradshaw | 0:fcc0eaeeb35c | 42 | _cam.putc(p4); |
jebradshaw | 0:fcc0eaeeb35c | 43 | } |
jebradshaw | 0:fcc0eaeeb35c | 44 | |
jebradshaw | 2:f80257456c6e | 45 | int uCAM_TTL120::uCAM_GetACK(int command) |
jebradshaw | 0:fcc0eaeeb35c | 46 | { |
jebradshaw | 0:fcc0eaeeb35c | 47 | char ser_read[7]; |
jebradshaw | 2:f80257456c6e | 48 | int i; |
jebradshaw | 0:fcc0eaeeb35c | 49 | |
jebradshaw | 2:f80257456c6e | 50 | for(i=0;i<7;i++) //clear the string |
jebradshaw | 0:fcc0eaeeb35c | 51 | ser_read[i] = 0; |
jebradshaw | 0:fcc0eaeeb35c | 52 | |
jebradshaw | 2:f80257456c6e | 53 | wait(.1); |
jebradshaw | 0:fcc0eaeeb35c | 54 | |
jebradshaw | 2:f80257456c6e | 55 | //read serial buffer and wait for ACK (0xAA0E0DXX0000) |
jebradshaw | 2:f80257456c6e | 56 | uCAM_read(ser_read, 6, .1); |
jebradshaw | 2:f80257456c6e | 57 | if((ser_read[0] == 0xAA) && |
jebradshaw | 2:f80257456c6e | 58 | (ser_read[1] == 0x0E)){ |
jebradshaw | 2:f80257456c6e | 59 | if((command & 0xff) == ser_read[2]) |
jebradshaw | 0:fcc0eaeeb35c | 60 | return 1; |
jebradshaw | 2:f80257456c6e | 61 | } |
jebradshaw | 2:f80257456c6e | 62 | else |
jebradshaw | 0:fcc0eaeeb35c | 63 | return 0; |
jebradshaw | 0:fcc0eaeeb35c | 64 | } |
jebradshaw | 0:fcc0eaeeb35c | 65 | |
jebradshaw | 0:fcc0eaeeb35c | 66 | int uCAM_TTL120::uCAM_Connect(void) |
jebradshaw | 0:fcc0eaeeb35c | 67 | { |
jebradshaw | 0:fcc0eaeeb35c | 68 | char ser_read[20]; |
jebradshaw | 0:fcc0eaeeb35c | 69 | int i, retries; |
jebradshaw | 0:fcc0eaeeb35c | 70 | |
jebradshaw | 0:fcc0eaeeb35c | 71 | for(i=0;i<20;i++) //clear the string |
jebradshaw | 0:fcc0eaeeb35c | 72 | ser_read[i] = 0; |
jebradshaw | 0:fcc0eaeeb35c | 73 | retries=0; |
jebradshaw | 0:fcc0eaeeb35c | 74 | |
jebradshaw | 0:fcc0eaeeb35c | 75 | while(retries < 60) |
jebradshaw | 0:fcc0eaeeb35c | 76 | { |
jebradshaw | 0:fcc0eaeeb35c | 77 | uCAM_FlushBuffer(); |
jebradshaw | 0:fcc0eaeeb35c | 78 | |
jebradshaw | 0:fcc0eaeeb35c | 79 | //Transmit SYNC command |
jebradshaw | 0:fcc0eaeeb35c | 80 | uCAM_Command_Send(uCAM_SYNC,0x00,0x00,0x00,0x00); |
jebradshaw | 0:fcc0eaeeb35c | 81 | |
jebradshaw | 0:fcc0eaeeb35c | 82 | wait(.2); |
jebradshaw | 0:fcc0eaeeb35c | 83 | |
jebradshaw | 0:fcc0eaeeb35c | 84 | //read serial buffer and wait for ACK (0xAA0E0DXX0000) |
jebradshaw | 0:fcc0eaeeb35c | 85 | uCAM_read(ser_read, 6, .01); |
jebradshaw | 0:fcc0eaeeb35c | 86 | |
jebradshaw | 0:fcc0eaeeb35c | 87 | if((ser_read[0] == 0xAA) && |
jebradshaw | 0:fcc0eaeeb35c | 88 | (ser_read[1] == 0x0E) && |
jebradshaw | 0:fcc0eaeeb35c | 89 | (ser_read[2] == 0x0D) && //skip ser_read[3] |
jebradshaw | 0:fcc0eaeeb35c | 90 | (ser_read[4] == 0x00)) |
jebradshaw | 0:fcc0eaeeb35c | 91 | { |
jebradshaw | 0:fcc0eaeeb35c | 92 | //after receiving ACK, wait for SYNC (0xAA0D00000000) |
jebradshaw | 2:f80257456c6e | 93 | uCAM_read(ser_read, 6, .100); |
jebradshaw | 2:f80257456c6e | 94 | |
jebradshaw | 0:fcc0eaeeb35c | 95 | if((ser_read[0] == 0xAA) && |
jebradshaw | 0:fcc0eaeeb35c | 96 | (ser_read[1] == 0x0D) && |
jebradshaw | 0:fcc0eaeeb35c | 97 | (ser_read[2] == 0x00) && |
jebradshaw | 0:fcc0eaeeb35c | 98 | (ser_read[3] == 0x00) && |
jebradshaw | 0:fcc0eaeeb35c | 99 | (ser_read[4] == 0x00) && |
jebradshaw | 0:fcc0eaeeb35c | 100 | (ser_read[5] == 0x00)) |
jebradshaw | 0:fcc0eaeeb35c | 101 | { |
jebradshaw | 0:fcc0eaeeb35c | 102 | //Transmit ACK command |
jebradshaw | 0:fcc0eaeeb35c | 103 | uCAM_Command_Send(uCAM_ACK,0x0D,0x00,0x00,0x00); |
jebradshaw | 0:fcc0eaeeb35c | 104 | |
jebradshaw | 0:fcc0eaeeb35c | 105 | printf("\r\n uCAM 120 Initialized\r\nDelaying 2 seconds for AGC and AEC \r\n circuits to stabilise before image capture."); |
jebradshaw | 2:f80257456c6e | 106 | /* wait(.5); //2 second delay before capturing first image |
jebradshaw | 0:fcc0eaeeb35c | 107 | printf("."); |
jebradshaw | 0:fcc0eaeeb35c | 108 | wait(.5); |
jebradshaw | 0:fcc0eaeeb35c | 109 | printf("."); |
jebradshaw | 0:fcc0eaeeb35c | 110 | wait(.5); |
jebradshaw | 0:fcc0eaeeb35c | 111 | printf("."); |
jebradshaw | 0:fcc0eaeeb35c | 112 | wait(.5); |
jebradshaw | 0:fcc0eaeeb35c | 113 | printf(".\r\nFinished\r\n"); |
jebradshaw | 2:f80257456c6e | 114 | */ |
jebradshaw | 0:fcc0eaeeb35c | 115 | return 1; //Camera connection successful |
jebradshaw | 0:fcc0eaeeb35c | 116 | } |
jebradshaw | 0:fcc0eaeeb35c | 117 | } |
jebradshaw | 0:fcc0eaeeb35c | 118 | retries++; |
jebradshaw | 0:fcc0eaeeb35c | 119 | } |
jebradshaw | 0:fcc0eaeeb35c | 120 | if(retries == 60) |
jebradshaw | 0:fcc0eaeeb35c | 121 | return 0; |
jebradshaw | 0:fcc0eaeeb35c | 122 | |
jebradshaw | 0:fcc0eaeeb35c | 123 | return -1; |
jebradshaw | 0:fcc0eaeeb35c | 124 | } |
jebradshaw | 0:fcc0eaeeb35c | 125 | |
jebradshaw | 0:fcc0eaeeb35c | 126 | int uCAM_TTL120::uCAM_send_INITIAL_80x60_16RAW(void) |
jebradshaw | 0:fcc0eaeeb35c | 127 | { |
jebradshaw | 0:fcc0eaeeb35c | 128 | char ser_read[7]; |
jebradshaw | 0:fcc0eaeeb35c | 129 | int i; |
jebradshaw | 0:fcc0eaeeb35c | 130 | |
jebradshaw | 0:fcc0eaeeb35c | 131 | uCAM_Command_Send(uCAM_INITIAL,0x00,0x06,0x01,0x00); // p2=0x06-16 bit color(RAW,565(RGB)) |
jebradshaw | 0:fcc0eaeeb35c | 132 | // p3=0x01-80 x 60 bit resolution // p4=0x01-JPEG res 80 x 64 |
jebradshaw | 0:fcc0eaeeb35c | 133 | wait(.02); |
jebradshaw | 0:fcc0eaeeb35c | 134 | |
jebradshaw | 0:fcc0eaeeb35c | 135 | for(i=0;i<7;i++) //clear the string |
jebradshaw | 0:fcc0eaeeb35c | 136 | ser_read[i] = 0; |
jebradshaw | 0:fcc0eaeeb35c | 137 | |
jebradshaw | 0:fcc0eaeeb35c | 138 | //read serial C buffer and wait for ACK (0xAA0E0DXX0000) |
jebradshaw | 0:fcc0eaeeb35c | 139 | uCAM_read(ser_read, 6, 100); |
jebradshaw | 0:fcc0eaeeb35c | 140 | |
jebradshaw | 0:fcc0eaeeb35c | 141 | if((ser_read[0] == 0xAA) && |
jebradshaw | 0:fcc0eaeeb35c | 142 | (ser_read[1] == 0x0E) && |
jebradshaw | 0:fcc0eaeeb35c | 143 | (ser_read[2] == 0x01)){ |
jebradshaw | 0:fcc0eaeeb35c | 144 | return 1; |
jebradshaw | 0:fcc0eaeeb35c | 145 | } |
jebradshaw | 0:fcc0eaeeb35c | 146 | else |
jebradshaw | 0:fcc0eaeeb35c | 147 | return 0; |
jebradshaw | 0:fcc0eaeeb35c | 148 | } |
jebradshaw | 0:fcc0eaeeb35c | 149 | |
jebradshaw | 0:fcc0eaeeb35c | 150 | int uCAM_TTL120::uCAM_send_INITIAL_80x60_2RAW(void) |
jebradshaw | 0:fcc0eaeeb35c | 151 | { |
jebradshaw | 0:fcc0eaeeb35c | 152 | char ser_read[7]; |
jebradshaw | 0:fcc0eaeeb35c | 153 | int i; |
jebradshaw | 0:fcc0eaeeb35c | 154 | uCAM_Command_Send(uCAM_INITIAL,0x00,0x01,0x01,0x00); // p2=0x01-2 bit GRAY (RAW) |
jebradshaw | 0:fcc0eaeeb35c | 155 | // p3=0x01-80 x 60 bit resolution |
jebradshaw | 0:fcc0eaeeb35c | 156 | // p4=0x01-JPEG res 80 x 64 |
jebradshaw | 0:fcc0eaeeb35c | 157 | wait(.02); |
jebradshaw | 0:fcc0eaeeb35c | 158 | |
jebradshaw | 0:fcc0eaeeb35c | 159 | for(i=0;i<7;i++) //clear the string |
jebradshaw | 0:fcc0eaeeb35c | 160 | ser_read[i] = 0; |
jebradshaw | 0:fcc0eaeeb35c | 161 | |
jebradshaw | 0:fcc0eaeeb35c | 162 | //read serial C buffer and wait for ACK (0xAA0E0DXX0000) |
jebradshaw | 0:fcc0eaeeb35c | 163 | uCAM_read(ser_read, 6, .1); |
jebradshaw | 0:fcc0eaeeb35c | 164 | |
jebradshaw | 0:fcc0eaeeb35c | 165 | if((ser_read[0] == 0xAA) && |
jebradshaw | 0:fcc0eaeeb35c | 166 | (ser_read[1] == 0x0E) && |
jebradshaw | 0:fcc0eaeeb35c | 167 | (ser_read[2] == 0x01)) |
jebradshaw | 0:fcc0eaeeb35c | 168 | { |
jebradshaw | 0:fcc0eaeeb35c | 169 | wait(.050); |
jebradshaw | 0:fcc0eaeeb35c | 170 | return 1; |
jebradshaw | 0:fcc0eaeeb35c | 171 | } |
jebradshaw | 0:fcc0eaeeb35c | 172 | else |
jebradshaw | 0:fcc0eaeeb35c | 173 | return 0; |
jebradshaw | 0:fcc0eaeeb35c | 174 | } |
jebradshaw | 0:fcc0eaeeb35c | 175 | |
jebradshaw | 0:fcc0eaeeb35c | 176 | int uCAM_TTL120::uCAM_send_INITIAL_128x128_4RAW(void) |
jebradshaw | 0:fcc0eaeeb35c | 177 | { |
jebradshaw | 0:fcc0eaeeb35c | 178 | char ser_read[7]; |
jebradshaw | 0:fcc0eaeeb35c | 179 | int i; |
jebradshaw | 0:fcc0eaeeb35c | 180 | uCAM_Command_Send(uCAM_INITIAL,0x00,0x02,0x09,0x00); // p2=0x02-4 bit GRAY (RAW) |
jebradshaw | 0:fcc0eaeeb35c | 181 | // p3=0x09- 128x128 bit resolution |
jebradshaw | 0:fcc0eaeeb35c | 182 | // p4=0x01-JPEG res 80 x 64 |
jebradshaw | 0:fcc0eaeeb35c | 183 | wait(.02); |
jebradshaw | 0:fcc0eaeeb35c | 184 | |
jebradshaw | 0:fcc0eaeeb35c | 185 | for(i=0;i<7;i++) //clear the string |
jebradshaw | 0:fcc0eaeeb35c | 186 | ser_read[i] = 0; |
jebradshaw | 0:fcc0eaeeb35c | 187 | |
jebradshaw | 0:fcc0eaeeb35c | 188 | //read serial buffer and wait for ACK (0xAA0E0DXX0000) |
jebradshaw | 0:fcc0eaeeb35c | 189 | uCAM_read(ser_read, 6, 100); |
jebradshaw | 0:fcc0eaeeb35c | 190 | |
jebradshaw | 0:fcc0eaeeb35c | 191 | if((ser_read[0] == 0xAA) && |
jebradshaw | 0:fcc0eaeeb35c | 192 | (ser_read[1] == 0x0E) && |
jebradshaw | 0:fcc0eaeeb35c | 193 | (ser_read[2] == 0x01)) |
jebradshaw | 0:fcc0eaeeb35c | 194 | { |
jebradshaw | 0:fcc0eaeeb35c | 195 | wait(.05); |
jebradshaw | 0:fcc0eaeeb35c | 196 | return 1; |
jebradshaw | 0:fcc0eaeeb35c | 197 | } |
jebradshaw | 0:fcc0eaeeb35c | 198 | else |
jebradshaw | 0:fcc0eaeeb35c | 199 | return 0; |
jebradshaw | 0:fcc0eaeeb35c | 200 | } |
jebradshaw | 0:fcc0eaeeb35c | 201 | int uCAM_TTL120::uCAM_send_SNAPSHOT(void) |
jebradshaw | 0:fcc0eaeeb35c | 202 | { |
jebradshaw | 2:f80257456c6e | 203 | uCAM_Command_Send(uCAM_SNAPSHOT,0x01,0x00,0x00,0x00); // p1=0x01-Uncompressed Image |
jebradshaw | 2:f80257456c6e | 204 | // p2 and p3 = 0, current frame |
jebradshaw | 2:f80257456c6e | 205 | wait(.05); |
jebradshaw | 0:fcc0eaeeb35c | 206 | |
jebradshaw | 2:f80257456c6e | 207 | if(uCAM_GetACK(uCAM_SNAPSHOT)) |
jebradshaw | 0:fcc0eaeeb35c | 208 | return 1; |
jebradshaw | 2:f80257456c6e | 209 | else |
jebradshaw | 2:f80257456c6e | 210 | return 0; |
jebradshaw | 0:fcc0eaeeb35c | 211 | } |
jebradshaw | 0:fcc0eaeeb35c | 212 | |
jebradshaw | 0:fcc0eaeeb35c | 213 | int uCAM_TTL120::uCAM_send_GET_PICTURE_80x60_16COL_RAW(void) |
jebradshaw | 0:fcc0eaeeb35c | 214 | { |
jebradshaw | 0:fcc0eaeeb35c | 215 | char ser_read[7]; |
jebradshaw | 0:fcc0eaeeb35c | 216 | char c; |
jebradshaw | 0:fcc0eaeeb35c | 217 | unsigned long pic_bytes; |
jebradshaw | 0:fcc0eaeeb35c | 218 | unsigned int i, j, k; |
jebradshaw | 0:fcc0eaeeb35c | 219 | unsigned int serbytes_read; |
jebradshaw | 0:fcc0eaeeb35c | 220 | unsigned int pixel_col; |
jebradshaw | 0:fcc0eaeeb35c | 221 | |
jebradshaw | 0:fcc0eaeeb35c | 222 | pic_bytes = 0; |
jebradshaw | 0:fcc0eaeeb35c | 223 | c=0; |
jebradshaw | 0:fcc0eaeeb35c | 224 | serbytes_read = 0; |
jebradshaw | 0:fcc0eaeeb35c | 225 | |
jebradshaw | 0:fcc0eaeeb35c | 226 | for(i=0;i<100;i++) |
jebradshaw | 0:fcc0eaeeb35c | 227 | picture[i] = 0; |
jebradshaw | 0:fcc0eaeeb35c | 228 | |
jebradshaw | 0:fcc0eaeeb35c | 229 | while(_cam.readable()){ //flush the buffer |
jebradshaw | 0:fcc0eaeeb35c | 230 | char c = _cam.getc(); |
jebradshaw | 0:fcc0eaeeb35c | 231 | } |
jebradshaw | 0:fcc0eaeeb35c | 232 | |
jebradshaw | 0:fcc0eaeeb35c | 233 | uCAM_Command_Send(uCAM_GET_PICTURE,0x01,0x00,0x00,0x00); // p1=0x01-Snapshot picture |
jebradshaw | 0:fcc0eaeeb35c | 234 | wait(.3); |
jebradshaw | 0:fcc0eaeeb35c | 235 | |
jebradshaw | 0:fcc0eaeeb35c | 236 | //read serial C buffer and wait for ACK (0xAA0E0DXX0000) |
jebradshaw | 0:fcc0eaeeb35c | 237 | c=uCAM_read(ser_read, 6, 1); |
jebradshaw | 0:fcc0eaeeb35c | 238 | wait(.3); |
jebradshaw | 0:fcc0eaeeb35c | 239 | |
jebradshaw | 0:fcc0eaeeb35c | 240 | if(c==6) //received 6 bytes back |
jebradshaw | 0:fcc0eaeeb35c | 241 | { |
jebradshaw | 0:fcc0eaeeb35c | 242 | if((ser_read[0] == 0xAA) && //first 2 bytes indicate it was a DATA packet |
jebradshaw | 0:fcc0eaeeb35c | 243 | (ser_read[1] == 0x0E) && |
jebradshaw | 0:fcc0eaeeb35c | 244 | (ser_read[2] == 0x04)) //returned get pic |
jebradshaw | 0:fcc0eaeeb35c | 245 | { |
jebradshaw | 0:fcc0eaeeb35c | 246 | //read serial C buffer and wait for ACK (0xAA0E0DXX0000) |
jebradshaw | 0:fcc0eaeeb35c | 247 | c=uCAM_read(ser_read, 6, 1); |
jebradshaw | 0:fcc0eaeeb35c | 248 | |
jebradshaw | 0:fcc0eaeeb35c | 249 | if(c==6) //received 6 bytes back |
jebradshaw | 0:fcc0eaeeb35c | 250 | { |
jebradshaw | 0:fcc0eaeeb35c | 251 | if((ser_read[0] == 0xAA) && //first 2 bytes indicate it was a DATA packet |
jebradshaw | 0:fcc0eaeeb35c | 252 | (ser_read[1] == 0x0A)) |
jebradshaw | 0:fcc0eaeeb35c | 253 | { |
jebradshaw | 0:fcc0eaeeb35c | 254 | pic_bytes = (unsigned long)ser_read[3]; |
jebradshaw | 0:fcc0eaeeb35c | 255 | pic_bytes += (unsigned long)ser_read[4] << 8; |
jebradshaw | 0:fcc0eaeeb35c | 256 | pic_bytes += (unsigned long)ser_read[5] << 16; |
jebradshaw | 0:fcc0eaeeb35c | 257 | |
jebradshaw | 0:fcc0eaeeb35c | 258 | serbytes_read = uCAM_read(picture, pic_bytes, .200); |
jebradshaw | 0:fcc0eaeeb35c | 259 | |
jebradshaw | 0:fcc0eaeeb35c | 260 | if(serbytes_read == pic_bytes) |
jebradshaw | 0:fcc0eaeeb35c | 261 | { |
jebradshaw | 0:fcc0eaeeb35c | 262 | k=0; |
jebradshaw | 0:fcc0eaeeb35c | 263 | for(i=0;i<60;i++) |
jebradshaw | 0:fcc0eaeeb35c | 264 | { |
jebradshaw | 0:fcc0eaeeb35c | 265 | for(j=0;j<80;j++) |
jebradshaw | 0:fcc0eaeeb35c | 266 | { |
jebradshaw | 0:fcc0eaeeb35c | 267 | pixel_col = picture[k]; |
jebradshaw | 0:fcc0eaeeb35c | 268 | k++; |
jebradshaw | 0:fcc0eaeeb35c | 269 | // printf("%d ", pixel_col); |
jebradshaw | 0:fcc0eaeeb35c | 270 | // uLCD144_Put_Pixel(j, i, pixel_col); //Do something with the pixel |
jebradshaw | 0:fcc0eaeeb35c | 271 | } |
jebradshaw | 0:fcc0eaeeb35c | 272 | } |
jebradshaw | 0:fcc0eaeeb35c | 273 | |
jebradshaw | 0:fcc0eaeeb35c | 274 | uCAM_Command_Send(uCAM_ACK, 0x0A, 0x00, 0x01, 0x00); |
jebradshaw | 0:fcc0eaeeb35c | 275 | |
jebradshaw | 0:fcc0eaeeb35c | 276 | return pic_bytes; |
jebradshaw | 0:fcc0eaeeb35c | 277 | } |
jebradshaw | 0:fcc0eaeeb35c | 278 | else |
jebradshaw | 0:fcc0eaeeb35c | 279 | return -4; |
jebradshaw | 0:fcc0eaeeb35c | 280 | } |
jebradshaw | 0:fcc0eaeeb35c | 281 | else |
jebradshaw | 0:fcc0eaeeb35c | 282 | return -3; |
jebradshaw | 0:fcc0eaeeb35c | 283 | } |
jebradshaw | 0:fcc0eaeeb35c | 284 | } |
jebradshaw | 0:fcc0eaeeb35c | 285 | else |
jebradshaw | 0:fcc0eaeeb35c | 286 | return -2; |
jebradshaw | 0:fcc0eaeeb35c | 287 | } |
jebradshaw | 0:fcc0eaeeb35c | 288 | return -1; |
jebradshaw | 0:fcc0eaeeb35c | 289 | } |
jebradshaw | 0:fcc0eaeeb35c | 290 | |
jebradshaw | 0:fcc0eaeeb35c | 291 | int uCAM_TTL120::uCAM_send_GET_PICTURE_80x60_2GRAY_RAW(void) |
jebradshaw | 0:fcc0eaeeb35c | 292 | { |
jebradshaw | 0:fcc0eaeeb35c | 293 | char ser_read[7]; |
jebradshaw | 0:fcc0eaeeb35c | 294 | char c; |
jebradshaw | 0:fcc0eaeeb35c | 295 | unsigned long pic_bytes; |
jebradshaw | 0:fcc0eaeeb35c | 296 | unsigned int pixel_col; |
jebradshaw | 0:fcc0eaeeb35c | 297 | unsigned int i, j, k; |
jebradshaw | 0:fcc0eaeeb35c | 298 | int m; |
jebradshaw | 0:fcc0eaeeb35c | 299 | unsigned int serbytes_read; |
jebradshaw | 0:fcc0eaeeb35c | 300 | int temp; |
jebradshaw | 0:fcc0eaeeb35c | 301 | |
jebradshaw | 0:fcc0eaeeb35c | 302 | pic_bytes = 0; |
jebradshaw | 0:fcc0eaeeb35c | 303 | c=0; |
jebradshaw | 0:fcc0eaeeb35c | 304 | serbytes_read = 0; |
jebradshaw | 0:fcc0eaeeb35c | 305 | |
jebradshaw | 0:fcc0eaeeb35c | 306 | for(i=0;i<100;i++) |
jebradshaw | 0:fcc0eaeeb35c | 307 | picture[i] = 0; |
jebradshaw | 0:fcc0eaeeb35c | 308 | |
jebradshaw | 0:fcc0eaeeb35c | 309 | while(_cam.readable()){ //flush the buffer |
jebradshaw | 0:fcc0eaeeb35c | 310 | char c = _cam.getc(); |
jebradshaw | 0:fcc0eaeeb35c | 311 | } |
jebradshaw | 0:fcc0eaeeb35c | 312 | |
jebradshaw | 0:fcc0eaeeb35c | 313 | uCAM_Command_Send(uCAM_GET_PICTURE,0x01,0x00,0x00,0x00); // p1=0x01-Snapshot picture |
jebradshaw | 0:fcc0eaeeb35c | 314 | wait(.3); |
jebradshaw | 0:fcc0eaeeb35c | 315 | |
jebradshaw | 0:fcc0eaeeb35c | 316 | //read serial C buffer and wait for ACK (0xAA0E0DXX0000) |
jebradshaw | 0:fcc0eaeeb35c | 317 | c=uCAM_read(ser_read, 6, 1); |
jebradshaw | 0:fcc0eaeeb35c | 318 | wait(.2); |
jebradshaw | 0:fcc0eaeeb35c | 319 | |
jebradshaw | 0:fcc0eaeeb35c | 320 | if(c==6) //received 6 bytes back |
jebradshaw | 0:fcc0eaeeb35c | 321 | { |
jebradshaw | 0:fcc0eaeeb35c | 322 | if((ser_read[0] == 0xAA) && //first 2 bytes indicate it was a DATA packet |
jebradshaw | 0:fcc0eaeeb35c | 323 | (ser_read[1] == 0x0E) && |
jebradshaw | 0:fcc0eaeeb35c | 324 | (ser_read[2] == 0x04)) //returned get pic |
jebradshaw | 0:fcc0eaeeb35c | 325 | { |
jebradshaw | 0:fcc0eaeeb35c | 326 | //read serial C buffer and wait for ACK (0xAA0E0DXX0000) |
jebradshaw | 0:fcc0eaeeb35c | 327 | c=uCAM_read(ser_read, 6, 1); |
jebradshaw | 0:fcc0eaeeb35c | 328 | |
jebradshaw | 0:fcc0eaeeb35c | 329 | if(c==6) //received 6 bytes back |
jebradshaw | 0:fcc0eaeeb35c | 330 | { |
jebradshaw | 0:fcc0eaeeb35c | 331 | if((ser_read[0] == 0xAA) && //first 2 bytes indicate it was a DATA packet |
jebradshaw | 0:fcc0eaeeb35c | 332 | (ser_read[1] == 0x0A)) |
jebradshaw | 0:fcc0eaeeb35c | 333 | { |
jebradshaw | 0:fcc0eaeeb35c | 334 | pic_bytes = (unsigned long)ser_read[3]; |
jebradshaw | 0:fcc0eaeeb35c | 335 | pic_bytes += (unsigned long)ser_read[4] << 8; |
jebradshaw | 0:fcc0eaeeb35c | 336 | pic_bytes += (unsigned long)ser_read[5] << 16; |
jebradshaw | 0:fcc0eaeeb35c | 337 | |
jebradshaw | 0:fcc0eaeeb35c | 338 | serbytes_read = uCAM_read(picture, pic_bytes, 200); |
jebradshaw | 0:fcc0eaeeb35c | 339 | |
jebradshaw | 0:fcc0eaeeb35c | 340 | if(serbytes_read == pic_bytes) |
jebradshaw | 0:fcc0eaeeb35c | 341 | { |
jebradshaw | 0:fcc0eaeeb35c | 342 | k=0; |
jebradshaw | 0:fcc0eaeeb35c | 343 | for(i=0;i<60;i++) |
jebradshaw | 0:fcc0eaeeb35c | 344 | { |
jebradshaw | 0:fcc0eaeeb35c | 345 | for(j=0;j<80;) |
jebradshaw | 0:fcc0eaeeb35c | 346 | { |
jebradshaw | 0:fcc0eaeeb35c | 347 | temp = picture[k]; |
jebradshaw | 0:fcc0eaeeb35c | 348 | for(m=14;m>=0;m-=2) |
jebradshaw | 0:fcc0eaeeb35c | 349 | { |
jebradshaw | 0:fcc0eaeeb35c | 350 | pixel_col = (temp >> m) & 0x03; |
jebradshaw | 0:fcc0eaeeb35c | 351 | switch(pixel_col) |
jebradshaw | 0:fcc0eaeeb35c | 352 | { |
jebradshaw | 0:fcc0eaeeb35c | 353 | case 0: |
jebradshaw | 0:fcc0eaeeb35c | 354 | // uLCD144_Put_Pixel(j, i, 0x0000); //0x0000 |
jebradshaw | 0:fcc0eaeeb35c | 355 | break; |
jebradshaw | 0:fcc0eaeeb35c | 356 | case 1: |
jebradshaw | 0:fcc0eaeeb35c | 357 | // uLCD144_Put_Pixel(j, i, 0x0180); //0x39e7 |
jebradshaw | 0:fcc0eaeeb35c | 358 | break; |
jebradshaw | 0:fcc0eaeeb35c | 359 | case 2: |
jebradshaw | 0:fcc0eaeeb35c | 360 | // uLCD144_Put_Pixel(j, i, 0xc209); //0x7bef |
jebradshaw | 0:fcc0eaeeb35c | 361 | break; |
jebradshaw | 0:fcc0eaeeb35c | 362 | case 3: |
jebradshaw | 0:fcc0eaeeb35c | 363 | // uLCD144_Put_Pixel(j, i, 0xFFFF); //0xFFFF |
jebradshaw | 0:fcc0eaeeb35c | 364 | break; |
jebradshaw | 0:fcc0eaeeb35c | 365 | default: |
jebradshaw | 0:fcc0eaeeb35c | 366 | // uLCD144_Put_Pixel(j, i, 0x0000); |
jebradshaw | 0:fcc0eaeeb35c | 367 | break; |
jebradshaw | 0:fcc0eaeeb35c | 368 | } |
jebradshaw | 0:fcc0eaeeb35c | 369 | j++; |
jebradshaw | 0:fcc0eaeeb35c | 370 | } |
jebradshaw | 0:fcc0eaeeb35c | 371 | k++; |
jebradshaw | 0:fcc0eaeeb35c | 372 | } |
jebradshaw | 0:fcc0eaeeb35c | 373 | } |
jebradshaw | 0:fcc0eaeeb35c | 374 | |
jebradshaw | 0:fcc0eaeeb35c | 375 | uCAM_Command_Send(uCAM_ACK, 0x0A, 0x00, 0x01, 0x00); |
jebradshaw | 0:fcc0eaeeb35c | 376 | |
jebradshaw | 0:fcc0eaeeb35c | 377 | return pic_bytes; |
jebradshaw | 0:fcc0eaeeb35c | 378 | } |
jebradshaw | 0:fcc0eaeeb35c | 379 | else |
jebradshaw | 0:fcc0eaeeb35c | 380 | return -4; |
jebradshaw | 0:fcc0eaeeb35c | 381 | } |
jebradshaw | 0:fcc0eaeeb35c | 382 | else |
jebradshaw | 0:fcc0eaeeb35c | 383 | return -3; |
jebradshaw | 0:fcc0eaeeb35c | 384 | } |
jebradshaw | 0:fcc0eaeeb35c | 385 | } |
jebradshaw | 0:fcc0eaeeb35c | 386 | else |
jebradshaw | 0:fcc0eaeeb35c | 387 | return -2; |
jebradshaw | 0:fcc0eaeeb35c | 388 | } |
jebradshaw | 0:fcc0eaeeb35c | 389 | return -1; |
jebradshaw | 0:fcc0eaeeb35c | 390 | } |
jebradshaw | 0:fcc0eaeeb35c | 391 | |
jebradshaw | 0:fcc0eaeeb35c | 392 | int uCAM_TTL120::uCAM_send_GET_PICTURE_128x128_4GRAY_RAW(void) |
jebradshaw | 0:fcc0eaeeb35c | 393 | { |
jebradshaw | 0:fcc0eaeeb35c | 394 | char ser_read[7]; |
jebradshaw | 0:fcc0eaeeb35c | 395 | char c; |
jebradshaw | 0:fcc0eaeeb35c | 396 | unsigned long pic_bytes; |
jebradshaw | 0:fcc0eaeeb35c | 397 | unsigned int pixel_col; |
jebradshaw | 0:fcc0eaeeb35c | 398 | unsigned int i, j, k; |
jebradshaw | 0:fcc0eaeeb35c | 399 | unsigned int serbytes_read; |
jebradshaw | 0:fcc0eaeeb35c | 400 | int temp; |
jebradshaw | 0:fcc0eaeeb35c | 401 | |
jebradshaw | 0:fcc0eaeeb35c | 402 | pic_bytes = 0; |
jebradshaw | 0:fcc0eaeeb35c | 403 | c=0; |
jebradshaw | 0:fcc0eaeeb35c | 404 | serbytes_read = 0; |
jebradshaw | 0:fcc0eaeeb35c | 405 | |
jebradshaw | 0:fcc0eaeeb35c | 406 | for(i=0;i<100;i++) |
jebradshaw | 0:fcc0eaeeb35c | 407 | picture[i] = 0; |
jebradshaw | 0:fcc0eaeeb35c | 408 | |
jebradshaw | 0:fcc0eaeeb35c | 409 | while(_cam.readable()){ //flush the buffer |
jebradshaw | 0:fcc0eaeeb35c | 410 | char c = _cam.getc(); |
jebradshaw | 0:fcc0eaeeb35c | 411 | } |
jebradshaw | 0:fcc0eaeeb35c | 412 | |
jebradshaw | 0:fcc0eaeeb35c | 413 | uCAM_Command_Send(uCAM_GET_PICTURE,0x01,0x00,0x00,0x00); // p1=0x01-Snapshot picture |
jebradshaw | 0:fcc0eaeeb35c | 414 | wait(.3); |
jebradshaw | 0:fcc0eaeeb35c | 415 | |
jebradshaw | 0:fcc0eaeeb35c | 416 | //read serial C buffer and wait for ACK (0xAA0E0DXX0000) |
jebradshaw | 0:fcc0eaeeb35c | 417 | c=uCAM_read(ser_read, 6, 1); |
jebradshaw | 0:fcc0eaeeb35c | 418 | wait(.2); |
jebradshaw | 0:fcc0eaeeb35c | 419 | |
jebradshaw | 0:fcc0eaeeb35c | 420 | if(c==6) //received 6 bytes back |
jebradshaw | 0:fcc0eaeeb35c | 421 | { |
jebradshaw | 0:fcc0eaeeb35c | 422 | if((ser_read[0] == 0xAA) && //first 2 bytes indicate it was a DATA packet |
jebradshaw | 0:fcc0eaeeb35c | 423 | (ser_read[1] == 0x0E) && |
jebradshaw | 0:fcc0eaeeb35c | 424 | (ser_read[2] == 0x04)) //returned get pic |
jebradshaw | 0:fcc0eaeeb35c | 425 | { |
jebradshaw | 0:fcc0eaeeb35c | 426 | //read serial C buffer and wait for ACK (0xAA0E0DXX0000) |
jebradshaw | 0:fcc0eaeeb35c | 427 | c=uCAM_read(ser_read, 6, 1); |
jebradshaw | 0:fcc0eaeeb35c | 428 | |
jebradshaw | 0:fcc0eaeeb35c | 429 | if(c==6) //received 6 bytes back |
jebradshaw | 0:fcc0eaeeb35c | 430 | { |
jebradshaw | 0:fcc0eaeeb35c | 431 | if((ser_read[0] == 0xAA) && //first 2 bytes indicate it was a DATA packet |
jebradshaw | 0:fcc0eaeeb35c | 432 | (ser_read[1] == 0x0A)) |
jebradshaw | 0:fcc0eaeeb35c | 433 | { |
jebradshaw | 0:fcc0eaeeb35c | 434 | pic_bytes = (unsigned long)ser_read[3]; |
jebradshaw | 0:fcc0eaeeb35c | 435 | pic_bytes += (unsigned long)ser_read[4] << 8; |
jebradshaw | 0:fcc0eaeeb35c | 436 | pic_bytes += (unsigned long)ser_read[5] << 16; |
jebradshaw | 0:fcc0eaeeb35c | 437 | |
jebradshaw | 0:fcc0eaeeb35c | 438 | serbytes_read = uCAM_read(picture, pic_bytes, .2); |
jebradshaw | 0:fcc0eaeeb35c | 439 | |
jebradshaw | 0:fcc0eaeeb35c | 440 | if(serbytes_read == pic_bytes) |
jebradshaw | 0:fcc0eaeeb35c | 441 | { |
jebradshaw | 0:fcc0eaeeb35c | 442 | k=0; |
jebradshaw | 0:fcc0eaeeb35c | 443 | for(i=0;i<128;i++) |
jebradshaw | 0:fcc0eaeeb35c | 444 | { |
jebradshaw | 0:fcc0eaeeb35c | 445 | for(j=0;j<128;) |
jebradshaw | 0:fcc0eaeeb35c | 446 | { |
jebradshaw | 0:fcc0eaeeb35c | 447 | temp = picture[k]; |
jebradshaw | 0:fcc0eaeeb35c | 448 | pixel_col = (temp >> 12) & 0x000F; |
jebradshaw | 0:fcc0eaeeb35c | 449 | pixel_col |= ((temp >> 12) & 0x000F) << 6; |
jebradshaw | 0:fcc0eaeeb35c | 450 | pixel_col |= ((temp >> 12) & 0x000F) << 11; |
jebradshaw | 0:fcc0eaeeb35c | 451 | // uLCD144_Put_Pixel(j, i, pixel_col); |
jebradshaw | 0:fcc0eaeeb35c | 452 | printf("%c", pixel_col); |
jebradshaw | 0:fcc0eaeeb35c | 453 | j++; |
jebradshaw | 0:fcc0eaeeb35c | 454 | |
jebradshaw | 0:fcc0eaeeb35c | 455 | pixel_col = (temp >> 8) & 0x000F; |
jebradshaw | 0:fcc0eaeeb35c | 456 | pixel_col |= ((temp >> 8) & 0x000F) << 6; |
jebradshaw | 0:fcc0eaeeb35c | 457 | pixel_col |= ((temp >> 8) & 0x000F) << 11; |
jebradshaw | 0:fcc0eaeeb35c | 458 | // uLCD144_Put_Pixel(j, i, pixel_col); |
jebradshaw | 0:fcc0eaeeb35c | 459 | printf("%c", pixel_col); |
jebradshaw | 0:fcc0eaeeb35c | 460 | j++; |
jebradshaw | 0:fcc0eaeeb35c | 461 | |
jebradshaw | 0:fcc0eaeeb35c | 462 | pixel_col = (temp >> 4) & 0x000F; |
jebradshaw | 0:fcc0eaeeb35c | 463 | pixel_col |= ((temp >> 4) & 0x000F) << 6; |
jebradshaw | 0:fcc0eaeeb35c | 464 | pixel_col |= ((temp >> 4) & 0x000F) << 11; |
jebradshaw | 0:fcc0eaeeb35c | 465 | // uLCD144_Put_Pixel(j, i, pixel_col); |
jebradshaw | 0:fcc0eaeeb35c | 466 | printf("%c", pixel_col); |
jebradshaw | 0:fcc0eaeeb35c | 467 | j++; |
jebradshaw | 0:fcc0eaeeb35c | 468 | |
jebradshaw | 0:fcc0eaeeb35c | 469 | pixel_col = temp & 0x000F; |
jebradshaw | 0:fcc0eaeeb35c | 470 | pixel_col |= (temp & 0x000F) << 6; |
jebradshaw | 0:fcc0eaeeb35c | 471 | pixel_col |= (temp & 0x000F) << 11; |
jebradshaw | 0:fcc0eaeeb35c | 472 | // uLCD144_Put_Pixel(j, i, pixel_col); |
jebradshaw | 0:fcc0eaeeb35c | 473 | printf("%c", pixel_col); |
jebradshaw | 0:fcc0eaeeb35c | 474 | j++; |
jebradshaw | 0:fcc0eaeeb35c | 475 | |
jebradshaw | 0:fcc0eaeeb35c | 476 | k++; |
jebradshaw | 0:fcc0eaeeb35c | 477 | } |
jebradshaw | 0:fcc0eaeeb35c | 478 | } |
jebradshaw | 0:fcc0eaeeb35c | 479 | |
jebradshaw | 0:fcc0eaeeb35c | 480 | uCAM_Command_Send(uCAM_ACK, 0x0A, 0x00, 0x01, 0x00); |
jebradshaw | 0:fcc0eaeeb35c | 481 | |
jebradshaw | 0:fcc0eaeeb35c | 482 | return pic_bytes; |
jebradshaw | 0:fcc0eaeeb35c | 483 | } |
jebradshaw | 0:fcc0eaeeb35c | 484 | else |
jebradshaw | 0:fcc0eaeeb35c | 485 | return -4; |
jebradshaw | 0:fcc0eaeeb35c | 486 | } |
jebradshaw | 0:fcc0eaeeb35c | 487 | else |
jebradshaw | 0:fcc0eaeeb35c | 488 | return -3; |
jebradshaw | 0:fcc0eaeeb35c | 489 | } |
jebradshaw | 0:fcc0eaeeb35c | 490 | } |
jebradshaw | 0:fcc0eaeeb35c | 491 | else |
jebradshaw | 0:fcc0eaeeb35c | 492 | return -2; |
jebradshaw | 0:fcc0eaeeb35c | 493 | } |
jebradshaw | 0:fcc0eaeeb35c | 494 | return -1; |
jebradshaw | 0:fcc0eaeeb35c | 495 | } |
jebradshaw | 0:fcc0eaeeb35c | 496 | |
jebradshaw | 0:fcc0eaeeb35c | 497 | void uCAM_TTL120::uCAM_set_baud(void) |
jebradshaw | 0:fcc0eaeeb35c | 498 | { |
jebradshaw | 2:f80257456c6e | 499 | uCAM_Command_Send(uCAM_SET_BAUD_RATE,0x03,0x00,0x00,0x00); // set baud to |
jebradshaw | 2:f80257456c6e | 500 | wait(.05); |
jebradshaw | 2:f80257456c6e | 501 | |
jebradshaw | 2:f80257456c6e | 502 | if(uCAM_GetACK(uCAM_SET_BAUD_RATE)){ |
jebradshaw | 2:f80257456c6e | 503 | printf("Baud rate sucessfully changed"); |
jebradshaw | 2:f80257456c6e | 504 | } |
jebradshaw | 2:f80257456c6e | 505 | else{ |
jebradshaw | 2:f80257456c6e | 506 | printf("Baud rate NOT sucessfully changed"); |
jebradshaw | 2:f80257456c6e | 507 | } |
jebradshaw | 2:f80257456c6e | 508 | |
jebradshaw | 2:f80257456c6e | 509 | _cam.baud(921600); |
jebradshaw | 2:f80257456c6e | 510 | // uCAM_Command_Send(uCAM_SET_BAUD_RATE,0x02,0x00,0x00,0x00); // set baud to |
jebradshaw | 2:f80257456c6e | 511 | // _cam.baud(1228800); |
jebradshaw | 0:fcc0eaeeb35c | 512 | } |
jebradshaw | 0:fcc0eaeeb35c | 513 | |
jebradshaw | 0:fcc0eaeeb35c | 514 | void uCAM_TTL120::uCAM_TakePic_RAW_16COLOR_80x60(void) |
jebradshaw | 0:fcc0eaeeb35c | 515 | { |
jebradshaw | 0:fcc0eaeeb35c | 516 | uCAM_send_INITIAL_80x60_16RAW(); |
jebradshaw | 0:fcc0eaeeb35c | 517 | wait(.1); |
jebradshaw | 0:fcc0eaeeb35c | 518 | uCAM_send_SNAPSHOT(); |
jebradshaw | 0:fcc0eaeeb35c | 519 | wait(.1); |
jebradshaw | 0:fcc0eaeeb35c | 520 | uCAM_send_GET_PICTURE_80x60_16COL_RAW(); |
jebradshaw | 0:fcc0eaeeb35c | 521 | } |
jebradshaw | 0:fcc0eaeeb35c | 522 | |
jebradshaw | 0:fcc0eaeeb35c | 523 | void uCAM_TTL120::uCAM_TakePic_RAW_2GRAY_80x60(void) |
jebradshaw | 0:fcc0eaeeb35c | 524 | { |
jebradshaw | 0:fcc0eaeeb35c | 525 | uCAM_send_INITIAL_80x60_2RAW(); |
jebradshaw | 0:fcc0eaeeb35c | 526 | wait(.1); |
jebradshaw | 0:fcc0eaeeb35c | 527 | uCAM_send_SNAPSHOT(); |
jebradshaw | 0:fcc0eaeeb35c | 528 | wait(.1); |
jebradshaw | 0:fcc0eaeeb35c | 529 | |
jebradshaw | 0:fcc0eaeeb35c | 530 | while(uCAM_send_GET_PICTURE_80x60_2GRAY_RAW() < 0){ |
jebradshaw | 0:fcc0eaeeb35c | 531 | wait(.1); |
jebradshaw | 0:fcc0eaeeb35c | 532 | uCAM_send_SNAPSHOT(); |
jebradshaw | 0:fcc0eaeeb35c | 533 | wait(.2); |
jebradshaw | 0:fcc0eaeeb35c | 534 | } |
jebradshaw | 0:fcc0eaeeb35c | 535 | } |
jebradshaw | 0:fcc0eaeeb35c | 536 | |
jebradshaw | 0:fcc0eaeeb35c | 537 | void uCAM_TTL120::uCAM_TakePic_RAW_4GRAY_128x128(void) |
jebradshaw | 0:fcc0eaeeb35c | 538 | { |
jebradshaw | 0:fcc0eaeeb35c | 539 | uCAM_send_INITIAL_128x128_4RAW(); |
jebradshaw | 0:fcc0eaeeb35c | 540 | wait(.100); |
jebradshaw | 0:fcc0eaeeb35c | 541 | uCAM_send_SNAPSHOT(); |
jebradshaw | 0:fcc0eaeeb35c | 542 | wait(.100); |
jebradshaw | 0:fcc0eaeeb35c | 543 | while(uCAM_send_GET_PICTURE_128x128_4GRAY_RAW() < 0){ |
jebradshaw | 0:fcc0eaeeb35c | 544 | wait(.100); |
jebradshaw | 0:fcc0eaeeb35c | 545 | uCAM_send_SNAPSHOT(); |
jebradshaw | 0:fcc0eaeeb35c | 546 | wait(.200); |
jebradshaw | 0:fcc0eaeeb35c | 547 | } |
jebradshaw | 0:fcc0eaeeb35c | 548 | } |
jebradshaw | 0:fcc0eaeeb35c | 549 | |
jebradshaw | 0:fcc0eaeeb35c | 550 | void uCAM_TTL120::uCAM_FlushBuffer(void){ |
jebradshaw | 0:fcc0eaeeb35c | 551 | while(_cam.readable()){ //flush the buffer |
jebradshaw | 0:fcc0eaeeb35c | 552 | char c = _cam.getc(); |
jebradshaw | 0:fcc0eaeeb35c | 553 | } |
jebradshaw | 0:fcc0eaeeb35c | 554 | } |
jebradshaw | 2:f80257456c6e | 555 | |
jebradshaw | 2:f80257456c6e | 556 | int uCAM_TTL120::uCAM_get_jpeg(FILE *fileName){ |
jebradshaw | 2:f80257456c6e | 557 | |
jebradshaw | 2:f80257456c6e | 558 | char ser_read[7]; |
jebradshaw | 2:f80257456c6e | 559 | unsigned long pic_bytes; |
jebradshaw | 2:f80257456c6e | 560 | |
jebradshaw | 2:f80257456c6e | 561 | uCAM_FlushBuffer(); |
jebradshaw | 2:f80257456c6e | 562 | uCAM_Command_Send(uCAM_INITIAL,0x00,0x07,0x07,0x07); // JPEG,640 x 480,JPEG |
jebradshaw | 2:f80257456c6e | 563 | //cam.uCAM_Command_Send(uCAM_INITIAL,0x00,0x07,0x07,0x03); // JPEG,160 x 120,JPEG |
jebradshaw | 2:f80257456c6e | 564 | wait(.05); |
jebradshaw | 2:f80257456c6e | 565 | |
jebradshaw | 2:f80257456c6e | 566 | if(uCAM_GetACK(uCAM_INITIAL)){ //INITIAL ACK |
jebradshaw | 2:f80257456c6e | 567 | printf("ACK received on uCAM_INITIAL\r\n"); |
jebradshaw | 2:f80257456c6e | 568 | } |
jebradshaw | 2:f80257456c6e | 569 | else{ |
jebradshaw | 2:f80257456c6e | 570 | printf("Did not receive ACK on uCAM_INITIAL\r\n"); |
jebradshaw | 2:f80257456c6e | 571 | } |
jebradshaw | 2:f80257456c6e | 572 | |
jebradshaw | 2:f80257456c6e | 573 | uCAM_FlushBuffer(); |
jebradshaw | 2:f80257456c6e | 574 | uCAM_Command_Send(uCAM_SET_PACKAGE_SIZE,0x08,0x00,0x02,0x00); //512 bytes |
jebradshaw | 2:f80257456c6e | 575 | |
jebradshaw | 2:f80257456c6e | 576 | wait(.05); |
jebradshaw | 2:f80257456c6e | 577 | |
jebradshaw | 2:f80257456c6e | 578 | if(uCAM_GetACK(uCAM_SET_PACKAGE_SIZE)){ //SET PACKAGE SIZE ACK |
jebradshaw | 2:f80257456c6e | 579 | printf("ACK received on uCAM_SET_PACKAGE_SIZE\r\n"); |
jebradshaw | 2:f80257456c6e | 580 | } |
jebradshaw | 2:f80257456c6e | 581 | else{ |
jebradshaw | 2:f80257456c6e | 582 | printf("Did not receive ACK on SET PACKAGE SIZE\r\n"); |
jebradshaw | 2:f80257456c6e | 583 | } |
jebradshaw | 2:f80257456c6e | 584 | |
jebradshaw | 2:f80257456c6e | 585 | uCAM_FlushBuffer(); |
jebradshaw | 2:f80257456c6e | 586 | uCAM_Command_Send(uCAM_SNAPSHOT,0x00,0x00,0x00,0x00); //JPEG |
jebradshaw | 2:f80257456c6e | 587 | |
jebradshaw | 2:f80257456c6e | 588 | if(uCAM_GetACK(uCAM_SNAPSHOT)){ //uCAM_SNAPSHOT ACK |
jebradshaw | 2:f80257456c6e | 589 | printf("ACK received on uCAM_SNAPSHOT\r\n"); |
jebradshaw | 2:f80257456c6e | 590 | } |
jebradshaw | 2:f80257456c6e | 591 | else{ |
jebradshaw | 2:f80257456c6e | 592 | printf("Did not receive ACK on uCAM_SNAPSHOT\r\n"); |
jebradshaw | 2:f80257456c6e | 593 | } |
jebradshaw | 2:f80257456c6e | 594 | |
jebradshaw | 2:f80257456c6e | 595 | pic_bytes = 0; |
jebradshaw | 2:f80257456c6e | 596 | |
jebradshaw | 2:f80257456c6e | 597 | uCAM_FlushBuffer(); |
jebradshaw | 2:f80257456c6e | 598 | uCAM_Command_Send(uCAM_GET_PICTURE,0x01,0x00,0x00,0x00); // p1=0x01-Snapshot picture |
jebradshaw | 2:f80257456c6e | 599 | wait(.1); |
jebradshaw | 2:f80257456c6e | 600 | |
jebradshaw | 2:f80257456c6e | 601 | if(uCAM_GetACK(uCAM_GET_PICTURE)) //returned get pic |
jebradshaw | 2:f80257456c6e | 602 | { |
jebradshaw | 2:f80257456c6e | 603 | //read serial buffer and wait for ACK (0xAA0E0DXX0000) |
jebradshaw | 2:f80257456c6e | 604 | uCAM_read(ser_read, 6, 1); |
jebradshaw | 2:f80257456c6e | 605 | |
jebradshaw | 2:f80257456c6e | 606 | if((ser_read[0] == 0xAA) && //first 2 bytes indicate it was a DATA packet |
jebradshaw | 2:f80257456c6e | 607 | (ser_read[1] == 0x0A) && |
jebradshaw | 2:f80257456c6e | 608 | (ser_read[2] == 0x01)) //DATA from snapshot ACK |
jebradshaw | 2:f80257456c6e | 609 | { |
jebradshaw | 2:f80257456c6e | 610 | pic_bytes = (unsigned long)ser_read[3]; |
jebradshaw | 2:f80257456c6e | 611 | pic_bytes += (unsigned long)ser_read[4] << 8; |
jebradshaw | 2:f80257456c6e | 612 | pic_bytes += (unsigned long)ser_read[5] << 16; |
jebradshaw | 2:f80257456c6e | 613 | |
jebradshaw | 2:f80257456c6e | 614 | int packID=0; |
jebradshaw | 2:f80257456c6e | 615 | char packID_l, packID_h; |
jebradshaw | 2:f80257456c6e | 616 | |
jebradshaw | 2:f80257456c6e | 617 | // pc.printf("\r\n"); |
jebradshaw | 2:f80257456c6e | 618 | int numPackages = pic_bytes / (512 - 6); |
jebradshaw | 2:f80257456c6e | 619 | |
jebradshaw | 2:f80257456c6e | 620 | while(numPackages>=0){ |
jebradshaw | 2:f80257456c6e | 621 | packID_h = (packID >> 8) & 0xff; |
jebradshaw | 2:f80257456c6e | 622 | packID_l = packID & 0xff; |
jebradshaw | 2:f80257456c6e | 623 | |
jebradshaw | 2:f80257456c6e | 624 | uCAM_FlushBuffer(); |
jebradshaw | 2:f80257456c6e | 625 | uCAM_Command_Send(uCAM_ACK, 0x00, 0x00, packID_l, packID_h); |
jebradshaw | 2:f80257456c6e | 626 | |
jebradshaw | 2:f80257456c6e | 627 | char ID_ret[2]; |
jebradshaw | 2:f80257456c6e | 628 | uCAM_read(ID_ret, 2, .2); |
jebradshaw | 2:f80257456c6e | 629 | |
jebradshaw | 2:f80257456c6e | 630 | char dataSize[2]; |
jebradshaw | 2:f80257456c6e | 631 | uCAM_read(dataSize, 2, .2); |
jebradshaw | 2:f80257456c6e | 632 | |
jebradshaw | 2:f80257456c6e | 633 | char imageData[506]; //512 - 6 |
jebradshaw | 2:f80257456c6e | 634 | int imageDataSize = ((dataSize[1] & 0xff) << 8) + (dataSize[0] & 0xff); |
jebradshaw | 2:f80257456c6e | 635 | |
jebradshaw | 2:f80257456c6e | 636 | //uCAM_read(imageData, imageDataSize, .2); |
jebradshaw | 2:f80257456c6e | 637 | for(int i=0;i<imageDataSize;i++) |
jebradshaw | 2:f80257456c6e | 638 | imageData[i] = _cam.getc(); |
jebradshaw | 2:f80257456c6e | 639 | |
jebradshaw | 2:f80257456c6e | 640 | char verifyCode[2]; |
jebradshaw | 2:f80257456c6e | 641 | uCAM_read(verifyCode, 2, .2); |
jebradshaw | 2:f80257456c6e | 642 | |
jebradshaw | 2:f80257456c6e | 643 | int vCodeCheck=0; |
jebradshaw | 2:f80257456c6e | 644 | vCodeCheck += ID_ret[0]; |
jebradshaw | 2:f80257456c6e | 645 | vCodeCheck += ID_ret[1]; |
jebradshaw | 2:f80257456c6e | 646 | vCodeCheck += dataSize[0]; |
jebradshaw | 2:f80257456c6e | 647 | vCodeCheck += dataSize[1]; |
jebradshaw | 2:f80257456c6e | 648 | |
jebradshaw | 2:f80257456c6e | 649 | for(int i=0;i<imageDataSize;i++) |
jebradshaw | 2:f80257456c6e | 650 | vCodeCheck += imageData[i]; |
jebradshaw | 2:f80257456c6e | 651 | |
jebradshaw | 2:f80257456c6e | 652 | if((verifyCode[0] & 0xff) == (vCodeCheck & 0xff)){ |
jebradshaw | 2:f80257456c6e | 653 | //pc.printf("\r\nPack ID = %d\r\n", packID);//debugging |
jebradshaw | 2:f80257456c6e | 654 | for(int j=0;j<imageDataSize;j++){ |
jebradshaw | 2:f80257456c6e | 655 | fputc(imageData[j], fileName); |
jebradshaw | 2:f80257456c6e | 656 | //pc.printf("%2X ", imageData[j]); |
jebradshaw | 2:f80257456c6e | 657 | } |
jebradshaw | 2:f80257456c6e | 658 | |
jebradshaw | 2:f80257456c6e | 659 | packID++; |
jebradshaw | 2:f80257456c6e | 660 | numPackages--; |
jebradshaw | 2:f80257456c6e | 661 | } |
jebradshaw | 2:f80257456c6e | 662 | else{ |
jebradshaw | 2:f80257456c6e | 663 | printf("\r\nBad Data in transfer. ReTransfer\r\n"); |
jebradshaw | 2:f80257456c6e | 664 | return -2; |
jebradshaw | 2:f80257456c6e | 665 | } |
jebradshaw | 2:f80257456c6e | 666 | } |
jebradshaw | 2:f80257456c6e | 667 | uCAM_Command_Send(uCAM_ACK, 0x00, 0x00, 0xf0, 0xf0); |
jebradshaw | 2:f80257456c6e | 668 | return 0; //return 0 on success |
jebradshaw | 2:f80257456c6e | 669 | } |
jebradshaw | 2:f80257456c6e | 670 | }//data packet |
jebradshaw | 2:f80257456c6e | 671 | return -1; |
jebradshaw | 2:f80257456c6e | 672 | } //function |