mbed serial interface for the OV528 camera (uCAM TTL 120 from 4D systems).

Dependencies:   MODSERIAL

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.

/media/uploads/jebradshaw/pic0003.jpg

Above is a saved jpeg image from the camera at 480x640 resolution

/media/uploads/jebradshaw/fullsizerender.jpg

Camera and TFT screen setup

/media/uploads/jebradshaw/tft_closeup_60x80.jpg

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
                }
             }
Committer:
jebradshaw
Date:
Mon Nov 17 21:03:43 2014 +0000
Revision:
0:fcc0eaeeb35c
Child:
2:f80257456c6e
close to getting jpeg

Who changed what in which revision?

UserRevisionLine numberNew 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 /*
jebradshaw 0:fcc0eaeeb35c 16 int uCAM_TTL120::uCAM_read(char *str, int numchars, float timeout){
jebradshaw 0:fcc0eaeeb35c 17 int i = 0;
jebradshaw 0:fcc0eaeeb35c 18
jebradshaw 0:fcc0eaeeb35c 19 float t_stop = timerCam->read()+timeout;
jebradshaw 0:fcc0eaeeb35c 20
jebradshaw 0:fcc0eaeeb35c 21 while(i < numchars){
jebradshaw 0:fcc0eaeeb35c 22 if(_cam.readable()){
jebradshaw 0:fcc0eaeeb35c 23 str[i] = _cam.getc();
jebradshaw 0:fcc0eaeeb35c 24 i++;
jebradshaw 0:fcc0eaeeb35c 25 }
jebradshaw 0:fcc0eaeeb35c 26 if(timerCam->read() > t_stop)
jebradshaw 0:fcc0eaeeb35c 27 return i;
jebradshaw 0:fcc0eaeeb35c 28 }
jebradshaw 0:fcc0eaeeb35c 29
jebradshaw 0:fcc0eaeeb35c 30 return i;
jebradshaw 0:fcc0eaeeb35c 31 }
jebradshaw 0:fcc0eaeeb35c 32 */
jebradshaw 0:fcc0eaeeb35c 33
jebradshaw 0:fcc0eaeeb35c 34 int uCAM_TTL120::uCAM_read(char *str, int numchars, float timeout){
jebradshaw 0:fcc0eaeeb35c 35 int i = 0;
jebradshaw 0:fcc0eaeeb35c 36 float t_start = timerCam->read();
jebradshaw 0:fcc0eaeeb35c 37
jebradshaw 0:fcc0eaeeb35c 38 while((timerCam->read() < (t_start + timeout)) && (i < numchars)){
jebradshaw 0:fcc0eaeeb35c 39 if(_cam.readable()){
jebradshaw 0:fcc0eaeeb35c 40 str[i] = _cam.getc();
jebradshaw 0:fcc0eaeeb35c 41 i++;
jebradshaw 0:fcc0eaeeb35c 42 }
jebradshaw 0:fcc0eaeeb35c 43 }
jebradshaw 0:fcc0eaeeb35c 44 if(timerCam->read() >= (t_start + timeout)){
jebradshaw 0:fcc0eaeeb35c 45 return -1;
jebradshaw 0:fcc0eaeeb35c 46 }
jebradshaw 0:fcc0eaeeb35c 47 if(i >= numchars){
jebradshaw 0:fcc0eaeeb35c 48 return -2;
jebradshaw 0:fcc0eaeeb35c 49 }
jebradshaw 0:fcc0eaeeb35c 50
jebradshaw 0:fcc0eaeeb35c 51 return i;
jebradshaw 0:fcc0eaeeb35c 52 }
jebradshaw 0:fcc0eaeeb35c 53
jebradshaw 0:fcc0eaeeb35c 54 void uCAM_TTL120::uCAM_Command_Send(int command,char p1,char p2,char p3,char p4)
jebradshaw 0:fcc0eaeeb35c 55 {
jebradshaw 0:fcc0eaeeb35c 56 _cam.putc((char)(command >> 8) & 0xff);
jebradshaw 0:fcc0eaeeb35c 57 _cam.putc((char)(command & 0xff));
jebradshaw 0:fcc0eaeeb35c 58 _cam.putc(p1);
jebradshaw 0:fcc0eaeeb35c 59 _cam.putc(p2);
jebradshaw 0:fcc0eaeeb35c 60 _cam.putc(p3);
jebradshaw 0:fcc0eaeeb35c 61 _cam.putc(p4);
jebradshaw 0:fcc0eaeeb35c 62 }
jebradshaw 0:fcc0eaeeb35c 63
jebradshaw 0:fcc0eaeeb35c 64 int uCAM_TTL120::uCAM_GetACK(void)
jebradshaw 0:fcc0eaeeb35c 65 {
jebradshaw 0:fcc0eaeeb35c 66 char ser_read[7];
jebradshaw 0:fcc0eaeeb35c 67 int i;
jebradshaw 0:fcc0eaeeb35c 68
jebradshaw 0:fcc0eaeeb35c 69 for(i=0;i<7;i++) //clear the string
jebradshaw 0:fcc0eaeeb35c 70 ser_read[i] = 0;
jebradshaw 0:fcc0eaeeb35c 71
jebradshaw 0:fcc0eaeeb35c 72 wait(.2);
jebradshaw 0:fcc0eaeeb35c 73
jebradshaw 0:fcc0eaeeb35c 74 //read serial buffer and wait for ACK (0xAA0E0DXX0000)
jebradshaw 0:fcc0eaeeb35c 75 uCAM_read(ser_read, 6, .1);
jebradshaw 0:fcc0eaeeb35c 76 if((ser_read[0] == 0xAA) &&
jebradshaw 0:fcc0eaeeb35c 77 (ser_read[1] == 0x0E))
jebradshaw 0:fcc0eaeeb35c 78 {
jebradshaw 0:fcc0eaeeb35c 79 return 1;
jebradshaw 0:fcc0eaeeb35c 80 }
jebradshaw 0:fcc0eaeeb35c 81 else
jebradshaw 0:fcc0eaeeb35c 82 return 0;
jebradshaw 0:fcc0eaeeb35c 83 }
jebradshaw 0:fcc0eaeeb35c 84
jebradshaw 0:fcc0eaeeb35c 85 int uCAM_TTL120::uCAM_Connect(void)
jebradshaw 0:fcc0eaeeb35c 86 {
jebradshaw 0:fcc0eaeeb35c 87 char ser_read[20];
jebradshaw 0:fcc0eaeeb35c 88 int i, retries;
jebradshaw 0:fcc0eaeeb35c 89
jebradshaw 0:fcc0eaeeb35c 90 for(i=0;i<20;i++) //clear the string
jebradshaw 0:fcc0eaeeb35c 91 ser_read[i] = 0;
jebradshaw 0:fcc0eaeeb35c 92 retries=0;
jebradshaw 0:fcc0eaeeb35c 93
jebradshaw 0:fcc0eaeeb35c 94 while(retries < 60)
jebradshaw 0:fcc0eaeeb35c 95 {
jebradshaw 0:fcc0eaeeb35c 96 uCAM_FlushBuffer();
jebradshaw 0:fcc0eaeeb35c 97
jebradshaw 0:fcc0eaeeb35c 98 //Transmit SYNC command
jebradshaw 0:fcc0eaeeb35c 99 uCAM_Command_Send(uCAM_SYNC,0x00,0x00,0x00,0x00);
jebradshaw 0:fcc0eaeeb35c 100
jebradshaw 0:fcc0eaeeb35c 101 wait(.2);
jebradshaw 0:fcc0eaeeb35c 102
jebradshaw 0:fcc0eaeeb35c 103 //read serial buffer and wait for ACK (0xAA0E0DXX0000)
jebradshaw 0:fcc0eaeeb35c 104 uCAM_read(ser_read, 6, .01);
jebradshaw 0:fcc0eaeeb35c 105
jebradshaw 0:fcc0eaeeb35c 106 if((ser_read[0] == 0xAA) &&
jebradshaw 0:fcc0eaeeb35c 107 (ser_read[1] == 0x0E) &&
jebradshaw 0:fcc0eaeeb35c 108 (ser_read[2] == 0x0D) && //skip ser_read[3]
jebradshaw 0:fcc0eaeeb35c 109 (ser_read[4] == 0x00))
jebradshaw 0:fcc0eaeeb35c 110 {
jebradshaw 0:fcc0eaeeb35c 111 //after receiving ACK, wait for SYNC (0xAA0D00000000)
jebradshaw 0:fcc0eaeeb35c 112 uCAM_read(ser_read, 6, 100);
jebradshaw 0:fcc0eaeeb35c 113 if((ser_read[0] == 0xAA) &&
jebradshaw 0:fcc0eaeeb35c 114 (ser_read[1] == 0x0D) &&
jebradshaw 0:fcc0eaeeb35c 115 (ser_read[2] == 0x00) &&
jebradshaw 0:fcc0eaeeb35c 116 (ser_read[3] == 0x00) &&
jebradshaw 0:fcc0eaeeb35c 117 (ser_read[4] == 0x00) &&
jebradshaw 0:fcc0eaeeb35c 118 (ser_read[5] == 0x00))
jebradshaw 0:fcc0eaeeb35c 119 {
jebradshaw 0:fcc0eaeeb35c 120 //Transmit ACK command
jebradshaw 0:fcc0eaeeb35c 121 uCAM_Command_Send(uCAM_ACK,0x0D,0x00,0x00,0x00);
jebradshaw 0:fcc0eaeeb35c 122
jebradshaw 0:fcc0eaeeb35c 123 printf("\r\n uCAM 120 Initialized\r\nDelaying 2 seconds for AGC and AEC \r\n circuits to stabilise before image capture.");
jebradshaw 0:fcc0eaeeb35c 124 wait(.5); //2 second delay before capturing first image
jebradshaw 0:fcc0eaeeb35c 125 printf(".");
jebradshaw 0:fcc0eaeeb35c 126 wait(.5);
jebradshaw 0:fcc0eaeeb35c 127 printf(".");
jebradshaw 0:fcc0eaeeb35c 128 wait(.5);
jebradshaw 0:fcc0eaeeb35c 129 printf(".");
jebradshaw 0:fcc0eaeeb35c 130 wait(.5);
jebradshaw 0:fcc0eaeeb35c 131 printf(".\r\nFinished\r\n");
jebradshaw 0:fcc0eaeeb35c 132
jebradshaw 0:fcc0eaeeb35c 133 return 1; //Camera connection successful
jebradshaw 0:fcc0eaeeb35c 134 }
jebradshaw 0:fcc0eaeeb35c 135 }
jebradshaw 0:fcc0eaeeb35c 136 retries++;
jebradshaw 0:fcc0eaeeb35c 137 }
jebradshaw 0:fcc0eaeeb35c 138 if(retries == 60)
jebradshaw 0:fcc0eaeeb35c 139 return 0;
jebradshaw 0:fcc0eaeeb35c 140
jebradshaw 0:fcc0eaeeb35c 141 return -1;
jebradshaw 0:fcc0eaeeb35c 142 }
jebradshaw 0:fcc0eaeeb35c 143
jebradshaw 0:fcc0eaeeb35c 144 int uCAM_TTL120::uCAM_send_INITIAL_80x60_16RAW(void)
jebradshaw 0:fcc0eaeeb35c 145 {
jebradshaw 0:fcc0eaeeb35c 146 char ser_read[7];
jebradshaw 0:fcc0eaeeb35c 147 int i;
jebradshaw 0:fcc0eaeeb35c 148
jebradshaw 0:fcc0eaeeb35c 149 uCAM_Command_Send(uCAM_INITIAL,0x00,0x06,0x01,0x00); // p2=0x06-16 bit color(RAW,565(RGB))
jebradshaw 0:fcc0eaeeb35c 150 // p3=0x01-80 x 60 bit resolution // p4=0x01-JPEG res 80 x 64
jebradshaw 0:fcc0eaeeb35c 151 wait(.02);
jebradshaw 0:fcc0eaeeb35c 152
jebradshaw 0:fcc0eaeeb35c 153 for(i=0;i<7;i++) //clear the string
jebradshaw 0:fcc0eaeeb35c 154 ser_read[i] = 0;
jebradshaw 0:fcc0eaeeb35c 155
jebradshaw 0:fcc0eaeeb35c 156 //read serial C buffer and wait for ACK (0xAA0E0DXX0000)
jebradshaw 0:fcc0eaeeb35c 157 uCAM_read(ser_read, 6, 100);
jebradshaw 0:fcc0eaeeb35c 158
jebradshaw 0:fcc0eaeeb35c 159 if((ser_read[0] == 0xAA) &&
jebradshaw 0:fcc0eaeeb35c 160 (ser_read[1] == 0x0E) &&
jebradshaw 0:fcc0eaeeb35c 161 (ser_read[2] == 0x01)){
jebradshaw 0:fcc0eaeeb35c 162 return 1;
jebradshaw 0:fcc0eaeeb35c 163 }
jebradshaw 0:fcc0eaeeb35c 164 else
jebradshaw 0:fcc0eaeeb35c 165 return 0;
jebradshaw 0:fcc0eaeeb35c 166 }
jebradshaw 0:fcc0eaeeb35c 167
jebradshaw 0:fcc0eaeeb35c 168 int uCAM_TTL120::uCAM_send_INITIAL_80x60_2RAW(void)
jebradshaw 0:fcc0eaeeb35c 169 {
jebradshaw 0:fcc0eaeeb35c 170 char ser_read[7];
jebradshaw 0:fcc0eaeeb35c 171 int i;
jebradshaw 0:fcc0eaeeb35c 172 uCAM_Command_Send(uCAM_INITIAL,0x00,0x01,0x01,0x00); // p2=0x01-2 bit GRAY (RAW)
jebradshaw 0:fcc0eaeeb35c 173 // p3=0x01-80 x 60 bit resolution
jebradshaw 0:fcc0eaeeb35c 174 // p4=0x01-JPEG res 80 x 64
jebradshaw 0:fcc0eaeeb35c 175 wait(.02);
jebradshaw 0:fcc0eaeeb35c 176
jebradshaw 0:fcc0eaeeb35c 177 for(i=0;i<7;i++) //clear the string
jebradshaw 0:fcc0eaeeb35c 178 ser_read[i] = 0;
jebradshaw 0:fcc0eaeeb35c 179
jebradshaw 0:fcc0eaeeb35c 180 //read serial C buffer and wait for ACK (0xAA0E0DXX0000)
jebradshaw 0:fcc0eaeeb35c 181 uCAM_read(ser_read, 6, .1);
jebradshaw 0:fcc0eaeeb35c 182
jebradshaw 0:fcc0eaeeb35c 183 if((ser_read[0] == 0xAA) &&
jebradshaw 0:fcc0eaeeb35c 184 (ser_read[1] == 0x0E) &&
jebradshaw 0:fcc0eaeeb35c 185 (ser_read[2] == 0x01))
jebradshaw 0:fcc0eaeeb35c 186 {
jebradshaw 0:fcc0eaeeb35c 187 wait(.050);
jebradshaw 0:fcc0eaeeb35c 188 return 1;
jebradshaw 0:fcc0eaeeb35c 189 }
jebradshaw 0:fcc0eaeeb35c 190 else
jebradshaw 0:fcc0eaeeb35c 191 return 0;
jebradshaw 0:fcc0eaeeb35c 192 }
jebradshaw 0:fcc0eaeeb35c 193
jebradshaw 0:fcc0eaeeb35c 194 int uCAM_TTL120::uCAM_send_INITIAL_128x128_4RAW(void)
jebradshaw 0:fcc0eaeeb35c 195 {
jebradshaw 0:fcc0eaeeb35c 196 char ser_read[7];
jebradshaw 0:fcc0eaeeb35c 197 int i;
jebradshaw 0:fcc0eaeeb35c 198 uCAM_Command_Send(uCAM_INITIAL,0x00,0x02,0x09,0x00); // p2=0x02-4 bit GRAY (RAW)
jebradshaw 0:fcc0eaeeb35c 199 // p3=0x09- 128x128 bit resolution
jebradshaw 0:fcc0eaeeb35c 200 // p4=0x01-JPEG res 80 x 64
jebradshaw 0:fcc0eaeeb35c 201 wait(.02);
jebradshaw 0:fcc0eaeeb35c 202
jebradshaw 0:fcc0eaeeb35c 203 for(i=0;i<7;i++) //clear the string
jebradshaw 0:fcc0eaeeb35c 204 ser_read[i] = 0;
jebradshaw 0:fcc0eaeeb35c 205
jebradshaw 0:fcc0eaeeb35c 206 //read serial buffer and wait for ACK (0xAA0E0DXX0000)
jebradshaw 0:fcc0eaeeb35c 207 uCAM_read(ser_read, 6, 100);
jebradshaw 0:fcc0eaeeb35c 208
jebradshaw 0:fcc0eaeeb35c 209 if((ser_read[0] == 0xAA) &&
jebradshaw 0:fcc0eaeeb35c 210 (ser_read[1] == 0x0E) &&
jebradshaw 0:fcc0eaeeb35c 211 (ser_read[2] == 0x01))
jebradshaw 0:fcc0eaeeb35c 212 {
jebradshaw 0:fcc0eaeeb35c 213 wait(.05);
jebradshaw 0:fcc0eaeeb35c 214 return 1;
jebradshaw 0:fcc0eaeeb35c 215 }
jebradshaw 0:fcc0eaeeb35c 216 else
jebradshaw 0:fcc0eaeeb35c 217 return 0;
jebradshaw 0:fcc0eaeeb35c 218 }
jebradshaw 0:fcc0eaeeb35c 219 int uCAM_TTL120::uCAM_send_SNAPSHOT(void)
jebradshaw 0:fcc0eaeeb35c 220 {
jebradshaw 0:fcc0eaeeb35c 221 uCAM_Command_Send(uCAM_SNAPSHOT,0x01,0x00,0x00,0x00); // p1=0x01-Uncompressed Image
jebradshaw 0:fcc0eaeeb35c 222 // p2 and p3 = 0, current frame
jebradshaw 0:fcc0eaeeb35c 223 wait(.05);
jebradshaw 0:fcc0eaeeb35c 224
jebradshaw 0:fcc0eaeeb35c 225 if(uCAM_GetACK())
jebradshaw 0:fcc0eaeeb35c 226 return 1;
jebradshaw 0:fcc0eaeeb35c 227 else
jebradshaw 0:fcc0eaeeb35c 228 return 0;
jebradshaw 0:fcc0eaeeb35c 229 }
jebradshaw 0:fcc0eaeeb35c 230
jebradshaw 0:fcc0eaeeb35c 231 int uCAM_TTL120::uCAM_send_GET_PICTURE_80x60_16COL_RAW(void)
jebradshaw 0:fcc0eaeeb35c 232 {
jebradshaw 0:fcc0eaeeb35c 233 char ser_read[7];
jebradshaw 0:fcc0eaeeb35c 234 char c;
jebradshaw 0:fcc0eaeeb35c 235 unsigned long pic_bytes;
jebradshaw 0:fcc0eaeeb35c 236 unsigned int i, j, k;
jebradshaw 0:fcc0eaeeb35c 237 unsigned int serbytes_read;
jebradshaw 0:fcc0eaeeb35c 238 unsigned int pixel_col;
jebradshaw 0:fcc0eaeeb35c 239
jebradshaw 0:fcc0eaeeb35c 240 pic_bytes = 0;
jebradshaw 0:fcc0eaeeb35c 241 c=0;
jebradshaw 0:fcc0eaeeb35c 242 serbytes_read = 0;
jebradshaw 0:fcc0eaeeb35c 243
jebradshaw 0:fcc0eaeeb35c 244 for(i=0;i<100;i++)
jebradshaw 0:fcc0eaeeb35c 245 picture[i] = 0;
jebradshaw 0:fcc0eaeeb35c 246
jebradshaw 0:fcc0eaeeb35c 247 while(_cam.readable()){ //flush the buffer
jebradshaw 0:fcc0eaeeb35c 248 char c = _cam.getc();
jebradshaw 0:fcc0eaeeb35c 249 }
jebradshaw 0:fcc0eaeeb35c 250
jebradshaw 0:fcc0eaeeb35c 251 uCAM_Command_Send(uCAM_GET_PICTURE,0x01,0x00,0x00,0x00); // p1=0x01-Snapshot picture
jebradshaw 0:fcc0eaeeb35c 252 wait(.3);
jebradshaw 0:fcc0eaeeb35c 253
jebradshaw 0:fcc0eaeeb35c 254 //read serial C buffer and wait for ACK (0xAA0E0DXX0000)
jebradshaw 0:fcc0eaeeb35c 255 c=uCAM_read(ser_read, 6, 1);
jebradshaw 0:fcc0eaeeb35c 256 wait(.3);
jebradshaw 0:fcc0eaeeb35c 257
jebradshaw 0:fcc0eaeeb35c 258 if(c==6) //received 6 bytes back
jebradshaw 0:fcc0eaeeb35c 259 {
jebradshaw 0:fcc0eaeeb35c 260 if((ser_read[0] == 0xAA) && //first 2 bytes indicate it was a DATA packet
jebradshaw 0:fcc0eaeeb35c 261 (ser_read[1] == 0x0E) &&
jebradshaw 0:fcc0eaeeb35c 262 (ser_read[2] == 0x04)) //returned get pic
jebradshaw 0:fcc0eaeeb35c 263 {
jebradshaw 0:fcc0eaeeb35c 264 //read serial C buffer and wait for ACK (0xAA0E0DXX0000)
jebradshaw 0:fcc0eaeeb35c 265 c=uCAM_read(ser_read, 6, 1);
jebradshaw 0:fcc0eaeeb35c 266
jebradshaw 0:fcc0eaeeb35c 267 if(c==6) //received 6 bytes back
jebradshaw 0:fcc0eaeeb35c 268 {
jebradshaw 0:fcc0eaeeb35c 269 if((ser_read[0] == 0xAA) && //first 2 bytes indicate it was a DATA packet
jebradshaw 0:fcc0eaeeb35c 270 (ser_read[1] == 0x0A))
jebradshaw 0:fcc0eaeeb35c 271 {
jebradshaw 0:fcc0eaeeb35c 272 pic_bytes = (unsigned long)ser_read[3];
jebradshaw 0:fcc0eaeeb35c 273 pic_bytes += (unsigned long)ser_read[4] << 8;
jebradshaw 0:fcc0eaeeb35c 274 pic_bytes += (unsigned long)ser_read[5] << 16;
jebradshaw 0:fcc0eaeeb35c 275
jebradshaw 0:fcc0eaeeb35c 276 serbytes_read = uCAM_read(picture, pic_bytes, .200);
jebradshaw 0:fcc0eaeeb35c 277
jebradshaw 0:fcc0eaeeb35c 278 if(serbytes_read == pic_bytes)
jebradshaw 0:fcc0eaeeb35c 279 {
jebradshaw 0:fcc0eaeeb35c 280 k=0;
jebradshaw 0:fcc0eaeeb35c 281 for(i=0;i<60;i++)
jebradshaw 0:fcc0eaeeb35c 282 {
jebradshaw 0:fcc0eaeeb35c 283 for(j=0;j<80;j++)
jebradshaw 0:fcc0eaeeb35c 284 {
jebradshaw 0:fcc0eaeeb35c 285 pixel_col = picture[k];
jebradshaw 0:fcc0eaeeb35c 286 k++;
jebradshaw 0:fcc0eaeeb35c 287 // printf("%d ", pixel_col);
jebradshaw 0:fcc0eaeeb35c 288 // uLCD144_Put_Pixel(j, i, pixel_col); //Do something with the pixel
jebradshaw 0:fcc0eaeeb35c 289 }
jebradshaw 0:fcc0eaeeb35c 290 }
jebradshaw 0:fcc0eaeeb35c 291
jebradshaw 0:fcc0eaeeb35c 292 uCAM_Command_Send(uCAM_ACK, 0x0A, 0x00, 0x01, 0x00);
jebradshaw 0:fcc0eaeeb35c 293
jebradshaw 0:fcc0eaeeb35c 294 return pic_bytes;
jebradshaw 0:fcc0eaeeb35c 295 }
jebradshaw 0:fcc0eaeeb35c 296 else
jebradshaw 0:fcc0eaeeb35c 297 return -4;
jebradshaw 0:fcc0eaeeb35c 298 }
jebradshaw 0:fcc0eaeeb35c 299 else
jebradshaw 0:fcc0eaeeb35c 300 return -3;
jebradshaw 0:fcc0eaeeb35c 301 }
jebradshaw 0:fcc0eaeeb35c 302 }
jebradshaw 0:fcc0eaeeb35c 303 else
jebradshaw 0:fcc0eaeeb35c 304 return -2;
jebradshaw 0:fcc0eaeeb35c 305 }
jebradshaw 0:fcc0eaeeb35c 306 return -1;
jebradshaw 0:fcc0eaeeb35c 307 }
jebradshaw 0:fcc0eaeeb35c 308
jebradshaw 0:fcc0eaeeb35c 309 int uCAM_TTL120::uCAM_send_GET_PICTURE_80x60_2GRAY_RAW(void)
jebradshaw 0:fcc0eaeeb35c 310 {
jebradshaw 0:fcc0eaeeb35c 311 char ser_read[7];
jebradshaw 0:fcc0eaeeb35c 312 char c;
jebradshaw 0:fcc0eaeeb35c 313 unsigned long pic_bytes;
jebradshaw 0:fcc0eaeeb35c 314 unsigned int pixel_col;
jebradshaw 0:fcc0eaeeb35c 315 unsigned int i, j, k;
jebradshaw 0:fcc0eaeeb35c 316 int m;
jebradshaw 0:fcc0eaeeb35c 317 unsigned int serbytes_read;
jebradshaw 0:fcc0eaeeb35c 318 int temp;
jebradshaw 0:fcc0eaeeb35c 319
jebradshaw 0:fcc0eaeeb35c 320 pic_bytes = 0;
jebradshaw 0:fcc0eaeeb35c 321 c=0;
jebradshaw 0:fcc0eaeeb35c 322 serbytes_read = 0;
jebradshaw 0:fcc0eaeeb35c 323
jebradshaw 0:fcc0eaeeb35c 324 for(i=0;i<100;i++)
jebradshaw 0:fcc0eaeeb35c 325 picture[i] = 0;
jebradshaw 0:fcc0eaeeb35c 326
jebradshaw 0:fcc0eaeeb35c 327 while(_cam.readable()){ //flush the buffer
jebradshaw 0:fcc0eaeeb35c 328 char c = _cam.getc();
jebradshaw 0:fcc0eaeeb35c 329 }
jebradshaw 0:fcc0eaeeb35c 330
jebradshaw 0:fcc0eaeeb35c 331 uCAM_Command_Send(uCAM_GET_PICTURE,0x01,0x00,0x00,0x00); // p1=0x01-Snapshot picture
jebradshaw 0:fcc0eaeeb35c 332 wait(.3);
jebradshaw 0:fcc0eaeeb35c 333
jebradshaw 0:fcc0eaeeb35c 334 //read serial C buffer and wait for ACK (0xAA0E0DXX0000)
jebradshaw 0:fcc0eaeeb35c 335 c=uCAM_read(ser_read, 6, 1);
jebradshaw 0:fcc0eaeeb35c 336 wait(.2);
jebradshaw 0:fcc0eaeeb35c 337
jebradshaw 0:fcc0eaeeb35c 338 if(c==6) //received 6 bytes back
jebradshaw 0:fcc0eaeeb35c 339 {
jebradshaw 0:fcc0eaeeb35c 340 if((ser_read[0] == 0xAA) && //first 2 bytes indicate it was a DATA packet
jebradshaw 0:fcc0eaeeb35c 341 (ser_read[1] == 0x0E) &&
jebradshaw 0:fcc0eaeeb35c 342 (ser_read[2] == 0x04)) //returned get pic
jebradshaw 0:fcc0eaeeb35c 343 {
jebradshaw 0:fcc0eaeeb35c 344 //read serial C buffer and wait for ACK (0xAA0E0DXX0000)
jebradshaw 0:fcc0eaeeb35c 345 c=uCAM_read(ser_read, 6, 1);
jebradshaw 0:fcc0eaeeb35c 346
jebradshaw 0:fcc0eaeeb35c 347 if(c==6) //received 6 bytes back
jebradshaw 0:fcc0eaeeb35c 348 {
jebradshaw 0:fcc0eaeeb35c 349 if((ser_read[0] == 0xAA) && //first 2 bytes indicate it was a DATA packet
jebradshaw 0:fcc0eaeeb35c 350 (ser_read[1] == 0x0A))
jebradshaw 0:fcc0eaeeb35c 351 {
jebradshaw 0:fcc0eaeeb35c 352 pic_bytes = (unsigned long)ser_read[3];
jebradshaw 0:fcc0eaeeb35c 353 pic_bytes += (unsigned long)ser_read[4] << 8;
jebradshaw 0:fcc0eaeeb35c 354 pic_bytes += (unsigned long)ser_read[5] << 16;
jebradshaw 0:fcc0eaeeb35c 355
jebradshaw 0:fcc0eaeeb35c 356 serbytes_read = uCAM_read(picture, pic_bytes, 200);
jebradshaw 0:fcc0eaeeb35c 357
jebradshaw 0:fcc0eaeeb35c 358 if(serbytes_read == pic_bytes)
jebradshaw 0:fcc0eaeeb35c 359 {
jebradshaw 0:fcc0eaeeb35c 360 k=0;
jebradshaw 0:fcc0eaeeb35c 361 for(i=0;i<60;i++)
jebradshaw 0:fcc0eaeeb35c 362 {
jebradshaw 0:fcc0eaeeb35c 363 for(j=0;j<80;)
jebradshaw 0:fcc0eaeeb35c 364 {
jebradshaw 0:fcc0eaeeb35c 365 temp = picture[k];
jebradshaw 0:fcc0eaeeb35c 366 for(m=14;m>=0;m-=2)
jebradshaw 0:fcc0eaeeb35c 367 {
jebradshaw 0:fcc0eaeeb35c 368 pixel_col = (temp >> m) & 0x03;
jebradshaw 0:fcc0eaeeb35c 369 switch(pixel_col)
jebradshaw 0:fcc0eaeeb35c 370 {
jebradshaw 0:fcc0eaeeb35c 371 case 0:
jebradshaw 0:fcc0eaeeb35c 372 // uLCD144_Put_Pixel(j, i, 0x0000); //0x0000
jebradshaw 0:fcc0eaeeb35c 373 break;
jebradshaw 0:fcc0eaeeb35c 374 case 1:
jebradshaw 0:fcc0eaeeb35c 375 // uLCD144_Put_Pixel(j, i, 0x0180); //0x39e7
jebradshaw 0:fcc0eaeeb35c 376 break;
jebradshaw 0:fcc0eaeeb35c 377 case 2:
jebradshaw 0:fcc0eaeeb35c 378 // uLCD144_Put_Pixel(j, i, 0xc209); //0x7bef
jebradshaw 0:fcc0eaeeb35c 379 break;
jebradshaw 0:fcc0eaeeb35c 380 case 3:
jebradshaw 0:fcc0eaeeb35c 381 // uLCD144_Put_Pixel(j, i, 0xFFFF); //0xFFFF
jebradshaw 0:fcc0eaeeb35c 382 break;
jebradshaw 0:fcc0eaeeb35c 383 default:
jebradshaw 0:fcc0eaeeb35c 384 // uLCD144_Put_Pixel(j, i, 0x0000);
jebradshaw 0:fcc0eaeeb35c 385 break;
jebradshaw 0:fcc0eaeeb35c 386 }
jebradshaw 0:fcc0eaeeb35c 387 j++;
jebradshaw 0:fcc0eaeeb35c 388 }
jebradshaw 0:fcc0eaeeb35c 389 k++;
jebradshaw 0:fcc0eaeeb35c 390 }
jebradshaw 0:fcc0eaeeb35c 391 }
jebradshaw 0:fcc0eaeeb35c 392
jebradshaw 0:fcc0eaeeb35c 393 uCAM_Command_Send(uCAM_ACK, 0x0A, 0x00, 0x01, 0x00);
jebradshaw 0:fcc0eaeeb35c 394
jebradshaw 0:fcc0eaeeb35c 395 return pic_bytes;
jebradshaw 0:fcc0eaeeb35c 396 }
jebradshaw 0:fcc0eaeeb35c 397 else
jebradshaw 0:fcc0eaeeb35c 398 return -4;
jebradshaw 0:fcc0eaeeb35c 399 }
jebradshaw 0:fcc0eaeeb35c 400 else
jebradshaw 0:fcc0eaeeb35c 401 return -3;
jebradshaw 0:fcc0eaeeb35c 402 }
jebradshaw 0:fcc0eaeeb35c 403 }
jebradshaw 0:fcc0eaeeb35c 404 else
jebradshaw 0:fcc0eaeeb35c 405 return -2;
jebradshaw 0:fcc0eaeeb35c 406 }
jebradshaw 0:fcc0eaeeb35c 407 return -1;
jebradshaw 0:fcc0eaeeb35c 408 }
jebradshaw 0:fcc0eaeeb35c 409
jebradshaw 0:fcc0eaeeb35c 410 int uCAM_TTL120::uCAM_send_GET_PICTURE_128x128_4GRAY_RAW(void)
jebradshaw 0:fcc0eaeeb35c 411 {
jebradshaw 0:fcc0eaeeb35c 412 char ser_read[7];
jebradshaw 0:fcc0eaeeb35c 413 char c;
jebradshaw 0:fcc0eaeeb35c 414 unsigned long pic_bytes;
jebradshaw 0:fcc0eaeeb35c 415 unsigned int pixel_col;
jebradshaw 0:fcc0eaeeb35c 416 unsigned int i, j, k;
jebradshaw 0:fcc0eaeeb35c 417 unsigned int serbytes_read;
jebradshaw 0:fcc0eaeeb35c 418 int temp;
jebradshaw 0:fcc0eaeeb35c 419
jebradshaw 0:fcc0eaeeb35c 420 pic_bytes = 0;
jebradshaw 0:fcc0eaeeb35c 421 c=0;
jebradshaw 0:fcc0eaeeb35c 422 serbytes_read = 0;
jebradshaw 0:fcc0eaeeb35c 423
jebradshaw 0:fcc0eaeeb35c 424 for(i=0;i<100;i++)
jebradshaw 0:fcc0eaeeb35c 425 picture[i] = 0;
jebradshaw 0:fcc0eaeeb35c 426
jebradshaw 0:fcc0eaeeb35c 427 while(_cam.readable()){ //flush the buffer
jebradshaw 0:fcc0eaeeb35c 428 char c = _cam.getc();
jebradshaw 0:fcc0eaeeb35c 429 }
jebradshaw 0:fcc0eaeeb35c 430
jebradshaw 0:fcc0eaeeb35c 431 uCAM_Command_Send(uCAM_GET_PICTURE,0x01,0x00,0x00,0x00); // p1=0x01-Snapshot picture
jebradshaw 0:fcc0eaeeb35c 432 wait(.3);
jebradshaw 0:fcc0eaeeb35c 433
jebradshaw 0:fcc0eaeeb35c 434 //read serial C buffer and wait for ACK (0xAA0E0DXX0000)
jebradshaw 0:fcc0eaeeb35c 435 c=uCAM_read(ser_read, 6, 1);
jebradshaw 0:fcc0eaeeb35c 436 wait(.2);
jebradshaw 0:fcc0eaeeb35c 437
jebradshaw 0:fcc0eaeeb35c 438 if(c==6) //received 6 bytes back
jebradshaw 0:fcc0eaeeb35c 439 {
jebradshaw 0:fcc0eaeeb35c 440 if((ser_read[0] == 0xAA) && //first 2 bytes indicate it was a DATA packet
jebradshaw 0:fcc0eaeeb35c 441 (ser_read[1] == 0x0E) &&
jebradshaw 0:fcc0eaeeb35c 442 (ser_read[2] == 0x04)) //returned get pic
jebradshaw 0:fcc0eaeeb35c 443 {
jebradshaw 0:fcc0eaeeb35c 444 //read serial C buffer and wait for ACK (0xAA0E0DXX0000)
jebradshaw 0:fcc0eaeeb35c 445 c=uCAM_read(ser_read, 6, 1);
jebradshaw 0:fcc0eaeeb35c 446
jebradshaw 0:fcc0eaeeb35c 447 if(c==6) //received 6 bytes back
jebradshaw 0:fcc0eaeeb35c 448 {
jebradshaw 0:fcc0eaeeb35c 449 if((ser_read[0] == 0xAA) && //first 2 bytes indicate it was a DATA packet
jebradshaw 0:fcc0eaeeb35c 450 (ser_read[1] == 0x0A))
jebradshaw 0:fcc0eaeeb35c 451 {
jebradshaw 0:fcc0eaeeb35c 452 pic_bytes = (unsigned long)ser_read[3];
jebradshaw 0:fcc0eaeeb35c 453 pic_bytes += (unsigned long)ser_read[4] << 8;
jebradshaw 0:fcc0eaeeb35c 454 pic_bytes += (unsigned long)ser_read[5] << 16;
jebradshaw 0:fcc0eaeeb35c 455
jebradshaw 0:fcc0eaeeb35c 456 serbytes_read = uCAM_read(picture, pic_bytes, .2);
jebradshaw 0:fcc0eaeeb35c 457
jebradshaw 0:fcc0eaeeb35c 458 if(serbytes_read == pic_bytes)
jebradshaw 0:fcc0eaeeb35c 459 {
jebradshaw 0:fcc0eaeeb35c 460 k=0;
jebradshaw 0:fcc0eaeeb35c 461 for(i=0;i<128;i++)
jebradshaw 0:fcc0eaeeb35c 462 {
jebradshaw 0:fcc0eaeeb35c 463 for(j=0;j<128;)
jebradshaw 0:fcc0eaeeb35c 464 {
jebradshaw 0:fcc0eaeeb35c 465 temp = picture[k];
jebradshaw 0:fcc0eaeeb35c 466 pixel_col = (temp >> 12) & 0x000F;
jebradshaw 0:fcc0eaeeb35c 467 pixel_col |= ((temp >> 12) & 0x000F) << 6;
jebradshaw 0:fcc0eaeeb35c 468 pixel_col |= ((temp >> 12) & 0x000F) << 11;
jebradshaw 0:fcc0eaeeb35c 469 // uLCD144_Put_Pixel(j, i, pixel_col);
jebradshaw 0:fcc0eaeeb35c 470 printf("%c", pixel_col);
jebradshaw 0:fcc0eaeeb35c 471 j++;
jebradshaw 0:fcc0eaeeb35c 472
jebradshaw 0:fcc0eaeeb35c 473 pixel_col = (temp >> 8) & 0x000F;
jebradshaw 0:fcc0eaeeb35c 474 pixel_col |= ((temp >> 8) & 0x000F) << 6;
jebradshaw 0:fcc0eaeeb35c 475 pixel_col |= ((temp >> 8) & 0x000F) << 11;
jebradshaw 0:fcc0eaeeb35c 476 // uLCD144_Put_Pixel(j, i, pixel_col);
jebradshaw 0:fcc0eaeeb35c 477 printf("%c", pixel_col);
jebradshaw 0:fcc0eaeeb35c 478 j++;
jebradshaw 0:fcc0eaeeb35c 479
jebradshaw 0:fcc0eaeeb35c 480 pixel_col = (temp >> 4) & 0x000F;
jebradshaw 0:fcc0eaeeb35c 481 pixel_col |= ((temp >> 4) & 0x000F) << 6;
jebradshaw 0:fcc0eaeeb35c 482 pixel_col |= ((temp >> 4) & 0x000F) << 11;
jebradshaw 0:fcc0eaeeb35c 483 // uLCD144_Put_Pixel(j, i, pixel_col);
jebradshaw 0:fcc0eaeeb35c 484 printf("%c", pixel_col);
jebradshaw 0:fcc0eaeeb35c 485 j++;
jebradshaw 0:fcc0eaeeb35c 486
jebradshaw 0:fcc0eaeeb35c 487 pixel_col = temp & 0x000F;
jebradshaw 0:fcc0eaeeb35c 488 pixel_col |= (temp & 0x000F) << 6;
jebradshaw 0:fcc0eaeeb35c 489 pixel_col |= (temp & 0x000F) << 11;
jebradshaw 0:fcc0eaeeb35c 490 // uLCD144_Put_Pixel(j, i, pixel_col);
jebradshaw 0:fcc0eaeeb35c 491 printf("%c", pixel_col);
jebradshaw 0:fcc0eaeeb35c 492 j++;
jebradshaw 0:fcc0eaeeb35c 493
jebradshaw 0:fcc0eaeeb35c 494 k++;
jebradshaw 0:fcc0eaeeb35c 495 }
jebradshaw 0:fcc0eaeeb35c 496 }
jebradshaw 0:fcc0eaeeb35c 497
jebradshaw 0:fcc0eaeeb35c 498 uCAM_Command_Send(uCAM_ACK, 0x0A, 0x00, 0x01, 0x00);
jebradshaw 0:fcc0eaeeb35c 499
jebradshaw 0:fcc0eaeeb35c 500 return pic_bytes;
jebradshaw 0:fcc0eaeeb35c 501 }
jebradshaw 0:fcc0eaeeb35c 502 else
jebradshaw 0:fcc0eaeeb35c 503 return -4;
jebradshaw 0:fcc0eaeeb35c 504 }
jebradshaw 0:fcc0eaeeb35c 505 else
jebradshaw 0:fcc0eaeeb35c 506 return -3;
jebradshaw 0:fcc0eaeeb35c 507 }
jebradshaw 0:fcc0eaeeb35c 508 }
jebradshaw 0:fcc0eaeeb35c 509 else
jebradshaw 0:fcc0eaeeb35c 510 return -2;
jebradshaw 0:fcc0eaeeb35c 511 }
jebradshaw 0:fcc0eaeeb35c 512 return -1;
jebradshaw 0:fcc0eaeeb35c 513 }
jebradshaw 0:fcc0eaeeb35c 514
jebradshaw 0:fcc0eaeeb35c 515 void uCAM_TTL120::uCAM_set_baud(void)
jebradshaw 0:fcc0eaeeb35c 516 {
jebradshaw 0:fcc0eaeeb35c 517 /*uCAM_Command_Send(uCAM_SET_BAUD_RATE,0x03,0x00,0x00,0x00); // set baud to
jebradshaw 0:fcc0eaeeb35c 518 serCopen(921600);*/
jebradshaw 0:fcc0eaeeb35c 519 uCAM_Command_Send(uCAM_SET_BAUD_RATE,0x02,0x00,0x00,0x00); // set baud to
jebradshaw 0:fcc0eaeeb35c 520 _cam.baud(1228800);
jebradshaw 0:fcc0eaeeb35c 521 }
jebradshaw 0:fcc0eaeeb35c 522
jebradshaw 0:fcc0eaeeb35c 523 void uCAM_TTL120::uCAM_TakePic_RAW_16COLOR_80x60(void)
jebradshaw 0:fcc0eaeeb35c 524 {
jebradshaw 0:fcc0eaeeb35c 525 uCAM_send_INITIAL_80x60_16RAW();
jebradshaw 0:fcc0eaeeb35c 526 wait(.1);
jebradshaw 0:fcc0eaeeb35c 527 uCAM_send_SNAPSHOT();
jebradshaw 0:fcc0eaeeb35c 528 wait(.1);
jebradshaw 0:fcc0eaeeb35c 529 uCAM_send_GET_PICTURE_80x60_16COL_RAW();
jebradshaw 0:fcc0eaeeb35c 530 }
jebradshaw 0:fcc0eaeeb35c 531
jebradshaw 0:fcc0eaeeb35c 532 void uCAM_TTL120::uCAM_TakePic_RAW_2GRAY_80x60(void)
jebradshaw 0:fcc0eaeeb35c 533 {
jebradshaw 0:fcc0eaeeb35c 534 uCAM_send_INITIAL_80x60_2RAW();
jebradshaw 0:fcc0eaeeb35c 535 wait(.1);
jebradshaw 0:fcc0eaeeb35c 536 uCAM_send_SNAPSHOT();
jebradshaw 0:fcc0eaeeb35c 537 wait(.1);
jebradshaw 0:fcc0eaeeb35c 538
jebradshaw 0:fcc0eaeeb35c 539 while(uCAM_send_GET_PICTURE_80x60_2GRAY_RAW() < 0){
jebradshaw 0:fcc0eaeeb35c 540 wait(.1);
jebradshaw 0:fcc0eaeeb35c 541 uCAM_send_SNAPSHOT();
jebradshaw 0:fcc0eaeeb35c 542 wait(.2);
jebradshaw 0:fcc0eaeeb35c 543 }
jebradshaw 0:fcc0eaeeb35c 544 }
jebradshaw 0:fcc0eaeeb35c 545
jebradshaw 0:fcc0eaeeb35c 546 void uCAM_TTL120::uCAM_TakePic_RAW_4GRAY_128x128(void)
jebradshaw 0:fcc0eaeeb35c 547 {
jebradshaw 0:fcc0eaeeb35c 548 uCAM_send_INITIAL_128x128_4RAW();
jebradshaw 0:fcc0eaeeb35c 549 wait(.100);
jebradshaw 0:fcc0eaeeb35c 550 uCAM_send_SNAPSHOT();
jebradshaw 0:fcc0eaeeb35c 551 wait(.100);
jebradshaw 0:fcc0eaeeb35c 552 while(uCAM_send_GET_PICTURE_128x128_4GRAY_RAW() < 0){
jebradshaw 0:fcc0eaeeb35c 553 wait(.100);
jebradshaw 0:fcc0eaeeb35c 554 uCAM_send_SNAPSHOT();
jebradshaw 0:fcc0eaeeb35c 555 wait(.200);
jebradshaw 0:fcc0eaeeb35c 556 }
jebradshaw 0:fcc0eaeeb35c 557 }
jebradshaw 0:fcc0eaeeb35c 558
jebradshaw 0:fcc0eaeeb35c 559 void uCAM_TTL120::uCAM_FlushBuffer(void){
jebradshaw 0:fcc0eaeeb35c 560 while(_cam.readable()){ //flush the buffer
jebradshaw 0:fcc0eaeeb35c 561 char c = _cam.getc();
jebradshaw 0:fcc0eaeeb35c 562 }
jebradshaw 0:fcc0eaeeb35c 563 }