Jelle Raes / Mbed 2 deprecated VLC_decoder_finished

Dependencies:   mbed

Fork of VLC_decoder_copy_copy_omdatetkan by Jelle Raes

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers manchester.cpp Source File

manchester.cpp

00001 #include "mbed.h"
00002 #include "manchester.h"
00003 
00004 DigitalIn signal(p7);
00005 Serial pc(USBTX, USBRX);
00006 Timer timer;
00007 Timeout timeout;
00008 bool notTimedOut;
00009 
00010 manchester::manchester()
00011 {
00012 }
00013 
00014 int manchester::decode()
00015 {
00016 
00017     int previous,current,begin,end=0;
00018     int time=0;
00019     int i = 0;
00020     int datas[] = {1,1,1,1,0,0,0,0,0,0};
00021     notTimedOut = true;
00022     timer.start();
00023     while(i< DELAY) {
00024         previous = current;
00025         current = signal.read();
00026         if(current != previous) {
00027             i=0;
00028         }
00029         i++;
00030         wait_us(100);
00031     }
00032     i = 0;
00033     timeout.attach(callback(this,&manchester::isTimedOut), 3);
00034     while(i<4 && notTimedOut) {
00035         end = begin;
00036         previous = current;
00037         current = signal.read();
00038         if(current<previous) {
00039             begin = timer.read_us();
00040             if(end != 0) {
00041                 time +=(begin - end);
00042             }
00043             i++;
00044         }
00045     }
00046     if(notTimedOut == true) {
00047         float atime=((float)time/3000000);
00048         i=4;
00049         wait(atime/2);
00050         while(i<10) {
00051             int x = signal.read();
00052             wait(atime/2);
00053             int y = signal.read();
00054             wait(atime/2);
00055 
00056             if(x<y) {
00057                 datas[i]=0;
00058             } else if(x>y) {
00059                 datas[i]=1;
00060             }
00061             i++;
00062         }
00063         wait(1);
00064         pc.printf("\n\raverage time is = %f s\n\r",atime);
00065         pc.printf("with frequency %f\n\r",1/atime);
00066         pc.printf("data is:");
00067         int direction = getDirection(datas);
00068         bool stopcode =getStopCode(datas);
00069         for(int j = 0; j<10; j++) {
00070             printf("%d",datas[j]);
00071         }
00072         printf("with direction:%d\r\n",direction);
00073         timer.stop();
00074         if(stopcode==true){
00075             return direction;
00076         }
00077         else{
00078             return 5;
00079         }
00080         //return direction;
00081     } else {
00082         printf("timed out\r\n");
00083         return 4;
00084     }
00085 //}
00086 }
00087 
00088 int manchester::getDirection(int* data)
00089 {
00090     int direction;
00091     if(data[4]==0) {
00092         if(data[5]==0) {
00093             direction = 0;
00094         } else {
00095             direction = 1;
00096         }
00097     } else {
00098         if(data[5]==0) {
00099             direction = 2;
00100         } else {
00101             direction = 3;
00102         }
00103     }
00104     return direction;
00105 }
00106 
00107 void manchester::isTimedOut()
00108 {
00109     notTimedOut = false;
00110 }
00111 
00112 bool manchester::getStopCode(int* data)
00113 {
00114     bool code= true;
00115     int test[] = {0,0,0,0,0,0,0,0,0,1};
00116     for(int i = 6; i < 10; i++){
00117         if(data[i]==test[i]){
00118             code = true;
00119         }
00120         else{
00121             code = false;
00122             break;
00123         }
00124     }    
00125     return code;
00126 }