Alexandre Lemay / Mbed 2 deprecated APP4_FunTimes

Dependencies:   mbed mbed-rtos

Revision:
7:332766fb3114
Parent:
6:ac7c0ccf9b5d
Child:
8:7c56fb1ed8c0
--- a/Receiver.cpp	Tue Oct 24 02:33:50 2017 +0000
+++ b/Receiver.cpp	Tue Oct 24 07:04:28 2017 +0000
@@ -5,20 +5,17 @@
 
 extern Serial pc;
 
-
-Receiver::Receiver():currentData(0),endData(0)
+Receiver::Receiver():currentData(0),endData(0),lastTime(0),timedOut(true)
 {   
     time.start();
-    pc.printf("receive");
     DO.rise(this,&Receiver::rise);
     DO.fall(this,&Receiver::fall);
-    pc.printf("contructor\n");
 }
 
-Receiver::EdgeData Receiver::getNext()
+char Receiver::getNext()
 {
     dataReady.wait();
-    EdgeData val = data[currentData];
+    char val = data[currentData];
     currentData++;
     if(currentData==size)
         currentData = 0 ;
@@ -26,177 +23,149 @@
 }
 
 
-void Receiver::pushState(Edge edge)
+void Receiver::pushData(char bitdata)
 {
-    EdgeData point(time.read_us(),edge);
-    data[endData] = point;
+    data[endData] = bitdata;
     endData++;
     if(endData==size)
         endData = 0;    
     dataReady.release();
 }
 
+void Receiver::edgeFunction(char mode)
+{
+    int curTime = time.read_us();
+    int dif = curTime - lastTime;
+    // timeout
+    if( dif > us_timeout)
+    {
+        pushData(BitData::timeout);
+        if(mode == BitData::zero)
+        {
+            lastTime = curTime;
+            pushData(BitData::zero);
+            return;
+        }
+    }
+    // data
+    if(dif > us_prepare)
+    {
+        lastTime = curTime;
+        pushData(mode);
+    }
+    // else prepare for data
+    return;
+}
 
 void Receiver::rise()
 {
-    pushState(rising);
+    if(timedOut)
+    {
+        timedOut = false;
+        lastTime = time.read_us();
+        pushData(BitData::zero);
+        return;
+    }
+    edgeFunction(BitData::zero);    
 }
 void Receiver::fall()
 {
-    pushState(falling);
+    if(timedOut)
+    {// ignore it, preparation for start
+        return;
+    }
+    edgeFunction(BitData::one);
 }
 
 
-void ManchesterReceiver::getState(Edge &edge, int &timeStamp)
+
+bool ManchesterReceiver::getByte(unsigned char &val)
 {
-    EdgeData data = r.getNext();
-    edge = data.value;
-    timeStamp = data.us_timeStamp;    
+    val =0 ;
+    for(int i = 0; i <8; i++)
+    {
+        char bitData = r.getNext();
+        switch(bitData)
+        {
+        case BitData::zero:
+            break;
+        case BitData::one:
+            val = val|(1 << 7-i);
+            break;
+        case BitData::timeout:
+            return false;       
+        }
+    }
+    pc.printf(" byte:%i\n",val);
+    return true;
 }
 
-unsigned char ManchesterReceiver::getFirstByte(int &lastTime)
+bool ManchesterReceiver::getMessage(vector<unsigned char> &message, int& crc )
 {
-    while(true)
+    unsigned char byte = 0;
+    // for crc;
+    vector<unsigned char> frame;
+    
+    // wait till a valid byte start the message
+    while(byte!=preamble_byte)
     {
-        Edge edge;
-        int us;
-        int lastData;
-        getState(edge,lastData);
-        if(edge == rising)
-        {   
-            // start first byte
-            unsigned char val = 0;
-            for(int i =0; i < 7; i++ )
-            {
-                getState(edge,us);
-                pc.printf("timeout : %i\n", us-lastData);
-                // timed out
-                if(us - lastData > us_period*5/4)
-                {
-                //a potential new preamble byte
-                    if( edge == rising ){
-                        i = 0; continue;
-                    }
-                    else break;
-                }
-                // Closer to data transition than 
-                else if(us - lastData > us_period*3/4)
-                {
-                    lastData = us;
-                    if(edge == falling)
-                    {
-                        val = val | 1 << 6-i;
-                    }
-                    if(i==6)
-                    {
-                        lastTime = lastData;
-                        return val;
-                    }
-                }
-                // skip it, because it is just before a meaningful transition
-                else if(us - lastData < us_period*3/4)
-                {
-                    i--; 
-                    continue;
-                }
-            }               
-        }        
+        while(!getByte(byte))
+        {    }        
+    }
+    frame.push_back(byte);
+    
+    if(!getByte(byte)){// timed out 
+        return false;
     }
-}
-
-bool ManchesterReceiver::getByte(int& lastTime, unsigned char &val)
-{
-    val = 0;
-    int us = 0;
-    for(int i = 0 ; i <8; i++) 
+    if(byte!=STARTBYTE)
+        return false;
+    frame.push_back(byte);
+    
+    //flags
+    if(!getByte( byte))
+        return false;
+    frame.push_back(byte);
+    
+    // payload length
+    if(!getByte(byte))
+        return false;
+    
+    frame.push_back(byte);
+    
+    int length = byte;
+    //data
+    for(int i = 0; i < length; i++)
     {
-        Edge edge;
-        getState(edge,us);
-        
-                pc.printf("\ntimeout : %i\n", us-lastTime);
-        // timed out
-        if(us - lastTime > us_period*5/4)
-        {   pc.printf ("\n\n\n TIMEOUT GETBYTE");
-            return false;
-        }
-        // Close to data transition time
-        else if(us - lastTime > us_period*3/4) 
-        {
-            lastTime = us;
-            if(edge == falling) 
-            {
-                val = val | 1 << 7-i;
-            }
-            if(i==7)
-                return true;
-        }// between meaningful transitions, skip it
-        else if(us - lastTime < us_period*3/4)
-        {
-            i--; 
-            continue;
-        }
+        if(!getByte(byte))
+        return false;
+        message.push_back(byte);
+        frame.push_back(byte);
     }
-    return false;
-}
-
-bool ManchesterReceiver::getMessage(int lastTime,vector<unsigned char> &message )
-{
-    message.push_back(preamble_byte);
+    
+    int calccrc = crc16Remainder(frame);
+    
+    // crc byte1
+    if(!getByte(byte))
+        return false;
     
-    unsigned char byte;
-    if(!getByte(lastTime, byte))
-    {   pc.printf("1");// timed out 
+    crc = byte << 8;
+    // crc byte2
+    if(!getByte(byte))
+        return false;
+    crc = crc | byte;
+    
+    if(calccrc!=crc)
+    {
+        pc.printf("\ncrc error\n");
         return false;
     }
     
-    
-    pc.printf("%i\n",byte);
-    
-    if(byte!=STARTBYTE)
-        return false;
-    message.push_back(byte);
-    //flags
-    if(!getByte(lastTime, byte))
-        return false;
-    message.push_back(byte);
-    
-    if(!getByte(lastTime, byte))
-        return false;
-    message.push_back(byte);
-    
-    int length = byte;
-    //data
-    for(int i = 0; i < length-3; i++)
-    {
-        if(!getByte(lastTime, byte))
+    // endbyte
+    if(!getByte(byte))
         return false;
-        message.push_back(byte);
-        
-        
-        pc.printf("%i",message.size());
-    }
-    
-    int crc = crc16Remainder(message);
-    
-    // crc byte1
-    if(!getByte(lastTime, byte))
-        return false;
-    message.push_back(byte);
-    
-    int readCrc = byte << 8;
-    // crc byte2
-    if(!getByte(lastTime, byte))
-        return false;
-    message.push_back(byte);
-    readCrc = readCrc | byte;
-    
-    // endbyte
-    if(!getByte(lastTime, byte))
-        return false;
-    message.push_back(byte);
-    
     if( byte != ENDBYTE )
         return false;
+    
     return true;
 }
 
@@ -204,21 +173,21 @@
 {
     while(true)
     {
-        int lastTime;
-        unsigned char byte = getFirstByte(lastTime);
-        pc.printf("%i",byte);
-        if(byte == preamble_byte)
+        vector<unsigned char> message;
+        int crc;
+        if(getMessage(message,crc))
         {
-            vector<unsigned char> message;
-            if(getMessage(lastTime, message)==true)
+            for (int i = 0; i < message.size();i++)
             {
-                for(int i = 0 ; i < message.size(); i++)
-                {
-                    pc.printf("%c", message[i]);
-                }
+                pc.printf("%c", message[i] );
             }
+            pc.printf("\nCrc16 : %i\n\n",crc);
         }
+        else
+        {
+            printf("\n\n **********************************\n Echec de reception du message\n\n\n");
+        }
+        
     }
-
 }