shared

Dependencies:   mbed-rtos mbed

Fork of Boboobooo by kao yi

Committer:
Kruskal
Date:
Fri Oct 31 10:54:51 2014 +0000
Revision:
8:089b778962c4
Parent:
7:fe8665daf3e7
:D

Who changed what in which revision?

UserRevisionLine numberNew contents of line
backman 1:82bc25a7b68b 1 #include "mbed.h"
backman 1:82bc25a7b68b 2 #include "camera_api.h"
backman 1:82bc25a7b68b 3
backman 1:82bc25a7b68b 4 #define clk 2 //ms
backman 1:82bc25a7b68b 5
backman 2:c51647d3c14d 6 BX_camera::BX_camera(void){
backman 1:82bc25a7b68b 7
backman 2:c51647d3c14d 8 line_CamR = new FastAnalogIn(PTD5);
backman 2:c51647d3c14d 9 line_CamL= new FastAnalogIn(PTD6,0);
Kruskal 7:fe8665daf3e7 10 cam_clk=new DigitalOut(PTE1);
Kruskal 7:fe8665daf3e7 11 si=new DigitalOut(PTD7);
backman 4:05b21dbca0c7 12
backman 1:82bc25a7b68b 13 }
backman 1:82bc25a7b68b 14
backman 3:c5f2281b3ed2 15 int BX_camera::black_centerR(void){
backman 4:05b21dbca0c7 16
backman 3:c5f2281b3ed2 17 // find center
backman 3:c5f2281b3ed2 18 // case 1 // | //
backman 3:c5f2281b3ed2 19 // case 2 / | /
backman 3:c5f2281b3ed2 20
backman 3:c5f2281b3ed2 21 char find_type=0x00;
backman 3:c5f2281b3ed2 22
backman 3:c5f2281b3ed2 23 int b_end=0;
backman 3:c5f2281b3ed2 24 int b_start=0;
backman 3:c5f2281b3ed2 25
backman 3:c5f2281b3ed2 26 int b2_end=0;
backman 3:c5f2281b3ed2 27 int b2_start=0;
backman 3:c5f2281b3ed2 28
backman 3:c5f2281b3ed2 29
backman 3:c5f2281b3ed2 30 int center;
backman 3:c5f2281b3ed2 31 int j=64;
backman 3:c5f2281b3ed2 32 bool f1=false;
backman 3:c5f2281b3ed2 33 bool f2=false;
backman 3:c5f2281b3ed2 34 bool f3=false;
backman 3:c5f2281b3ed2 35 bool f4=false;
backman 3:c5f2281b3ed2 36
backman 3:c5f2281b3ed2 37
backman 3:c5f2281b3ed2 38 if(sign_line_imageR[64]==' ')
backman 3:c5f2281b3ed2 39 find_type=0x02;
backman 3:c5f2281b3ed2 40 else
backman 3:c5f2281b3ed2 41 find_type=0x01;
backman 3:c5f2281b3ed2 42
backman 3:c5f2281b3ed2 43
backman 3:c5f2281b3ed2 44 for(int i=64;i<128; i++,j--){
backman 3:c5f2281b3ed2 45
backman 3:c5f2281b3ed2 46
backman 3:c5f2281b3ed2 47 switch(find_type){
backman 3:c5f2281b3ed2 48
backman 3:c5f2281b3ed2 49
backman 3:c5f2281b3ed2 50 case 0x01:
backman 3:c5f2281b3ed2 51
backman 3:c5f2281b3ed2 52 if(f1==false&&sign_line_imageR[i]==' '){
backman 3:c5f2281b3ed2 53 if(f1==false){
backman 3:c5f2281b3ed2 54 b_start=i;
backman 3:c5f2281b3ed2 55 f1=true;
backman 3:c5f2281b3ed2 56 }
backman 3:c5f2281b3ed2 57
backman 3:c5f2281b3ed2 58 }
backman 3:c5f2281b3ed2 59 if(f1== true&& f2==false&&sign_line_imageR[i]=='O'){
backman 3:c5f2281b3ed2 60 if(f2==false){
backman 3:c5f2281b3ed2 61 b_end=i-1;
backman 3:c5f2281b3ed2 62 f2=true;
backman 3:c5f2281b3ed2 63 }
backman 3:c5f2281b3ed2 64
backman 3:c5f2281b3ed2 65 }
backman 3:c5f2281b3ed2 66
backman 3:c5f2281b3ed2 67 if(f3==false&&sign_line_imageR[j]==' '){
backman 3:c5f2281b3ed2 68 if(f3==false){
backman 3:c5f2281b3ed2 69 b2_end=j;
backman 3:c5f2281b3ed2 70 f3=true;
backman 3:c5f2281b3ed2 71 }
backman 3:c5f2281b3ed2 72
backman 3:c5f2281b3ed2 73 }
backman 3:c5f2281b3ed2 74 if(f3==true&&f4==false&&sign_line_imageR[j]=='O'){
backman 3:c5f2281b3ed2 75 if(f4==false){
backman 3:c5f2281b3ed2 76 b2_start=j-1;
backman 3:c5f2281b3ed2 77 f4=true;
backman 3:c5f2281b3ed2 78 }
backman 3:c5f2281b3ed2 79
backman 3:c5f2281b3ed2 80 }
backman 3:c5f2281b3ed2 81
backman 3:c5f2281b3ed2 82
backman 3:c5f2281b3ed2 83 break;
backman 3:c5f2281b3ed2 84
backman 3:c5f2281b3ed2 85
backman 3:c5f2281b3ed2 86 case 0x02:
backman 3:c5f2281b3ed2 87
backman 3:c5f2281b3ed2 88 if(sign_line_imageR[i]=='O'){
backman 3:c5f2281b3ed2 89
backman 3:c5f2281b3ed2 90 if(f1==false){
backman 3:c5f2281b3ed2 91 b_end=i;
backman 3:c5f2281b3ed2 92 f1=true;
backman 3:c5f2281b3ed2 93 }
backman 3:c5f2281b3ed2 94 }
backman 3:c5f2281b3ed2 95
backman 3:c5f2281b3ed2 96 if(sign_line_imageR[j]=='O'){
backman 3:c5f2281b3ed2 97
backman 3:c5f2281b3ed2 98 if(f2==false){
backman 3:c5f2281b3ed2 99 b_start=j;
backman 3:c5f2281b3ed2 100 f2=true;
backman 3:c5f2281b3ed2 101 }
backman 3:c5f2281b3ed2 102
backman 3:c5f2281b3ed2 103 }
backman 3:c5f2281b3ed2 104
backman 3:c5f2281b3ed2 105
backman 3:c5f2281b3ed2 106 break;
backman 4:05b21dbca0c7 107
backman 3:c5f2281b3ed2 108 }
backman 3:c5f2281b3ed2 109
backman 3:c5f2281b3ed2 110
backman 3:c5f2281b3ed2 111 }
backman 3:c5f2281b3ed2 112
backman 3:c5f2281b3ed2 113
backman 3:c5f2281b3ed2 114 switch(find_type){
backman 3:c5f2281b3ed2 115
backman 3:c5f2281b3ed2 116 case 0x01:
backman 3:c5f2281b3ed2 117
backman 3:c5f2281b3ed2 118 if((b_end-b_start)>(b2_end-b2_start) )
backman 3:c5f2281b3ed2 119 center=(b_end+b_start)/2;
backman 3:c5f2281b3ed2 120
backman 3:c5f2281b3ed2 121 else
backman 3:c5f2281b3ed2 122 center=(b2_end+b2_start)/2;
backman 3:c5f2281b3ed2 123 break;
backman 3:c5f2281b3ed2 124
backman 3:c5f2281b3ed2 125 case 0x02:
backman 3:c5f2281b3ed2 126 center=(b_end+b_start)/2;
backman 3:c5f2281b3ed2 127
backman 3:c5f2281b3ed2 128 break;
backman 3:c5f2281b3ed2 129 }
backman 3:c5f2281b3ed2 130
backman 3:c5f2281b3ed2 131
backman 3:c5f2281b3ed2 132 return center;
backman 3:c5f2281b3ed2 133
backman 3:c5f2281b3ed2 134 }
backman 3:c5f2281b3ed2 135
backman 3:c5f2281b3ed2 136
backman 3:c5f2281b3ed2 137
backman 3:c5f2281b3ed2 138
backman 3:c5f2281b3ed2 139 int BX_camera::black_centerL(void){
backman 3:c5f2281b3ed2 140
backman 3:c5f2281b3ed2 141 // find center
backman 3:c5f2281b3ed2 142 // case 1 // | //
backman 3:c5f2281b3ed2 143 //case 2 / | /
backman 3:c5f2281b3ed2 144
backman 3:c5f2281b3ed2 145
backman 3:c5f2281b3ed2 146 char find_type=0x00;
backman 3:c5f2281b3ed2 147
backman 3:c5f2281b3ed2 148 int b_end=0;
backman 3:c5f2281b3ed2 149 int b_start=0;
backman 3:c5f2281b3ed2 150 int b2_end=0;
backman 3:c5f2281b3ed2 151 int b2_start=0;
backman 3:c5f2281b3ed2 152
backman 3:c5f2281b3ed2 153
backman 3:c5f2281b3ed2 154 int center;
backman 3:c5f2281b3ed2 155 int j=64;
backman 3:c5f2281b3ed2 156 bool f1=false;
backman 3:c5f2281b3ed2 157 bool f2=false;
backman 3:c5f2281b3ed2 158 bool f3=false;
backman 3:c5f2281b3ed2 159 bool f4=false;
backman 4:05b21dbca0c7 160
backman 3:c5f2281b3ed2 161 if(sign_line_imageL[64]==' ')
backman 3:c5f2281b3ed2 162 find_type=0x02;
backman 3:c5f2281b3ed2 163 else
backman 3:c5f2281b3ed2 164 find_type=0x01;
backman 3:c5f2281b3ed2 165
backman 3:c5f2281b3ed2 166
backman 3:c5f2281b3ed2 167 for(int i=64;i<128; i++,j--){
backman 3:c5f2281b3ed2 168
backman 3:c5f2281b3ed2 169
backman 3:c5f2281b3ed2 170 switch(find_type){
backman 3:c5f2281b3ed2 171
backman 3:c5f2281b3ed2 172
backman 3:c5f2281b3ed2 173 case 0x01:
backman 3:c5f2281b3ed2 174
backman 3:c5f2281b3ed2 175 if(f1==false&&sign_line_imageL[i]==' '){
backman 3:c5f2281b3ed2 176 if(f1==false){
backman 3:c5f2281b3ed2 177 b_start=i;
backman 3:c5f2281b3ed2 178 f1=true;
backman 3:c5f2281b3ed2 179 }
backman 3:c5f2281b3ed2 180
backman 3:c5f2281b3ed2 181 }
backman 3:c5f2281b3ed2 182 if(f1== true&& f2==false&&sign_line_imageL[i]=='O'){
backman 3:c5f2281b3ed2 183 if(f2==false){
backman 3:c5f2281b3ed2 184 b_end=i-1;
backman 3:c5f2281b3ed2 185 f2=true;
backman 3:c5f2281b3ed2 186 }
backman 3:c5f2281b3ed2 187
backman 3:c5f2281b3ed2 188 }
backman 3:c5f2281b3ed2 189
backman 3:c5f2281b3ed2 190 if(f3==false&&sign_line_imageL[j]==' '){
backman 3:c5f2281b3ed2 191 if(f3==false){
backman 3:c5f2281b3ed2 192 b2_end=j;
backman 3:c5f2281b3ed2 193 f3=true;
backman 3:c5f2281b3ed2 194 }
backman 3:c5f2281b3ed2 195
backman 3:c5f2281b3ed2 196 }
backman 3:c5f2281b3ed2 197 if(f3==true&&f4==false&&sign_line_imageL[j]=='O'){
backman 3:c5f2281b3ed2 198 if(f4==false){
backman 3:c5f2281b3ed2 199 b2_start=j-1;
backman 3:c5f2281b3ed2 200 f4=true;
backman 3:c5f2281b3ed2 201 }
backman 3:c5f2281b3ed2 202
backman 3:c5f2281b3ed2 203 }
backman 4:05b21dbca0c7 204
backman 3:c5f2281b3ed2 205 break;
backman 3:c5f2281b3ed2 206
backman 3:c5f2281b3ed2 207
backman 3:c5f2281b3ed2 208 case 0x02:
backman 3:c5f2281b3ed2 209
backman 3:c5f2281b3ed2 210 if(sign_line_imageL[i]=='O'){
backman 3:c5f2281b3ed2 211
backman 3:c5f2281b3ed2 212 if(f1==false){
backman 3:c5f2281b3ed2 213 b_end=i;
backman 3:c5f2281b3ed2 214 f1=true;
backman 3:c5f2281b3ed2 215 }
backman 3:c5f2281b3ed2 216 }
backman 3:c5f2281b3ed2 217
backman 3:c5f2281b3ed2 218 if(sign_line_imageL[j]=='O'){
backman 3:c5f2281b3ed2 219
backman 3:c5f2281b3ed2 220 if(f2==false){
backman 3:c5f2281b3ed2 221 b_start=j;
backman 3:c5f2281b3ed2 222 f2=true;
backman 3:c5f2281b3ed2 223 }
backman 3:c5f2281b3ed2 224
backman 3:c5f2281b3ed2 225 }
backman 3:c5f2281b3ed2 226
backman 3:c5f2281b3ed2 227
backman 4:05b21dbca0c7 228 break;
backman 3:c5f2281b3ed2 229
backman 3:c5f2281b3ed2 230 }
backman 4:05b21dbca0c7 231
backman 3:c5f2281b3ed2 232 }
backman 3:c5f2281b3ed2 233
backman 3:c5f2281b3ed2 234
backman 3:c5f2281b3ed2 235 switch(find_type){
backman 3:c5f2281b3ed2 236
backman 3:c5f2281b3ed2 237 case 0x01:
backman 3:c5f2281b3ed2 238
backman 3:c5f2281b3ed2 239 if((b_end-b_start)>(b2_end-b2_start) )
backman 3:c5f2281b3ed2 240 center=(b_end+b_start)/2;
backman 3:c5f2281b3ed2 241
backman 3:c5f2281b3ed2 242 else
backman 3:c5f2281b3ed2 243 center=(b2_end+b2_start)/2;
backman 3:c5f2281b3ed2 244 break;
backman 3:c5f2281b3ed2 245
backman 3:c5f2281b3ed2 246 case 0x02:
backman 3:c5f2281b3ed2 247 center=(b_end+b_start)/2;
backman 3:c5f2281b3ed2 248
backman 3:c5f2281b3ed2 249 break;
backman 3:c5f2281b3ed2 250 }
backman 3:c5f2281b3ed2 251
backman 3:c5f2281b3ed2 252 return center;
backman 3:c5f2281b3ed2 253
backman 3:c5f2281b3ed2 254 }
backman 3:c5f2281b3ed2 255
backman 4:05b21dbca0c7 256
backman 1:82bc25a7b68b 257
backman 2:c51647d3c14d 258 void BX_camera::read(void){
backman 1:82bc25a7b68b 259
backman 2:c51647d3c14d 260 w_f_vL=0x0000;
backman 2:c51647d3c14d 261 b_f_vL=0xffff;
backman 2:c51647d3c14d 262
backman 2:c51647d3c14d 263 w_f_vR=0x0000;
backman 2:c51647d3c14d 264 b_f_vR=0xffff;
backman 1:82bc25a7b68b 265
backman 1:82bc25a7b68b 266
backman 1:82bc25a7b68b 267
backman 2:c51647d3c14d 268
backman 1:82bc25a7b68b 269 *si=1;
backman 2:c51647d3c14d 270 *cam_clk=1;
backman 1:82bc25a7b68b 271
backman 1:82bc25a7b68b 272 wait_us(30); // tune here
backman 1:82bc25a7b68b 273 *si=0;
backman 1:82bc25a7b68b 274 *cam_clk=0;
backman 1:82bc25a7b68b 275
backman 1:82bc25a7b68b 276
backman 1:82bc25a7b68b 277
backman 2:c51647d3c14d 278 line_CamR->enable();
backman 1:82bc25a7b68b 279
backman 1:82bc25a7b68b 280
backman 2:c51647d3c14d 281 //input 128 //both
backman 1:82bc25a7b68b 282
backman 1:82bc25a7b68b 283 for(int i=0;i<128;i++){
backman 2:c51647d3c14d 284 *cam_clk=1;
backman 2:c51647d3c14d 285 wait_us(5);
backman 1:82bc25a7b68b 286
backman 1:82bc25a7b68b 287
backman 2:c51647d3c14d 288 line_imageR[i]=line_CamR->read_u16();
Kruskal 8:089b778962c4 289
backman 1:82bc25a7b68b 290
backman 1:82bc25a7b68b 291 // big small
backman 2:c51647d3c14d 292 if(line_imageR[i] > w_f_vR)
backman 2:c51647d3c14d 293 w_f_vR=line_imageR[i];
backman 2:c51647d3c14d 294 else if(line_imageR[i] < b_f_vR )
Kruskal 8:089b778962c4 295 b_f_vR = line_imageR[i];
Kruskal 8:089b778962c4 296
Kruskal 8:089b778962c4 297
Kruskal 8:089b778962c4 298 *cam_clk=0;
Kruskal 8:089b778962c4 299 wait_us(3);
Kruskal 8:089b778962c4 300
Kruskal 8:089b778962c4 301
Kruskal 8:089b778962c4 302 }
Kruskal 8:089b778962c4 303
Kruskal 8:089b778962c4 304
Kruskal 8:089b778962c4 305 line_CamR->disable();
Kruskal 8:089b778962c4 306
Kruskal 8:089b778962c4 307
Kruskal 8:089b778962c4 308 *si=1;
Kruskal 8:089b778962c4 309 *cam_clk=1;
Kruskal 8:089b778962c4 310
Kruskal 8:089b778962c4 311 wait_us(30); // tune here
Kruskal 8:089b778962c4 312 *si=0;
Kruskal 8:089b778962c4 313 *cam_clk=0;
Kruskal 8:089b778962c4 314
Kruskal 8:089b778962c4 315
Kruskal 8:089b778962c4 316 line_CamL->enable();
Kruskal 8:089b778962c4 317 //input 128 //both
Kruskal 8:089b778962c4 318
Kruskal 8:089b778962c4 319 for(int i=0;i<128;i++){
Kruskal 8:089b778962c4 320 *cam_clk=1;
Kruskal 8:089b778962c4 321 wait_us(3);
Kruskal 8:089b778962c4 322
Kruskal 8:089b778962c4 323
Kruskal 8:089b778962c4 324 line_imageL[i]=line_CamL->read_u16();
Kruskal 8:089b778962c4 325
Kruskal 8:089b778962c4 326 // big small
backman 2:c51647d3c14d 327
backman 2:c51647d3c14d 328
backman 2:c51647d3c14d 329 if(line_imageL[i] > w_f_vL)
backman 2:c51647d3c14d 330 w_f_vL=line_imageL[i];
backman 2:c51647d3c14d 331 else if(line_imageL[i] < b_f_vL )
backman 2:c51647d3c14d 332 b_f_vL = line_imageL[i];
backman 1:82bc25a7b68b 333
backman 1:82bc25a7b68b 334
backman 1:82bc25a7b68b 335
backman 1:82bc25a7b68b 336
backman 1:82bc25a7b68b 337 *cam_clk=0;
backman 1:82bc25a7b68b 338 wait_us(5);
backman 1:82bc25a7b68b 339
backman 1:82bc25a7b68b 340
backman 1:82bc25a7b68b 341 }
Kruskal 8:089b778962c4 342 line_CamL->disable();
backman 1:82bc25a7b68b 343
backman 1:82bc25a7b68b 344
backman 2:c51647d3c14d 345 //filter L R //may change
backman 1:82bc25a7b68b 346
backman 1:82bc25a7b68b 347 for(int i=0;i<128;i++){
backman 1:82bc25a7b68b 348
backman 1:82bc25a7b68b 349
backman 2:c51647d3c14d 350 if( (line_imageR[i]-b_f_vR) < (w_f_vR - line_imageR[i] ) )
backman 2:c51647d3c14d 351 sign_line_imageR[i]=' ';
backman 2:c51647d3c14d 352 else
backman 2:c51647d3c14d 353 sign_line_imageR[i]='O';
backman 2:c51647d3c14d 354
backman 2:c51647d3c14d 355
backman 2:c51647d3c14d 356 if( (line_imageL[i]-b_f_vL) < (w_f_vL - line_imageL[i] ) )
backman 2:c51647d3c14d 357 sign_line_imageL[i]=' ';
backman 1:82bc25a7b68b 358 else
backman 2:c51647d3c14d 359 sign_line_imageL[i]='O';
backman 2:c51647d3c14d 360
backman 1:82bc25a7b68b 361
backman 2:c51647d3c14d 362
backman 2:c51647d3c14d 363
backman 2:c51647d3c14d 364
backman 2:c51647d3c14d 365 if(i==0){
backman 2:c51647d3c14d 366 sign_line_imageR[i]='X';
backman 2:c51647d3c14d 367 sign_line_imageL[i]='X';
backman 2:c51647d3c14d 368 }
backman 2:c51647d3c14d 369
backman 1:82bc25a7b68b 370
backman 1:82bc25a7b68b 371 }
backman 1:82bc25a7b68b 372
backman 1:82bc25a7b68b 373
backman 1:82bc25a7b68b 374
backman 1:82bc25a7b68b 375 }