SSI OpComms 3CM Board RX

Dependencies:   mbed

Fork of Optical3cmRX by Thomas Teisberg

Revision:
7:41b22e5a1d8d
Parent:
6:6c996fb7a742
Child:
8:20d405a03cb9
--- a/main.cpp	Sun Nov 15 23:20:38 2015 +0000
+++ b/main.cpp	Mon Nov 16 00:08:19 2015 +0000
@@ -16,15 +16,9 @@
 static int SAMPLE_RATE; // must be a divisor of PULSE_TIME; 
 static int SAMPLE_GAP;
 static int HALF_SAMPLE;
-static const int BUF_SIZE = 1024; // Size of buffer. Each element of buffer represents PPM_BITS bits of information, so 
-                                  // e.g. BUF_SIZE = 1024 and PPM_BITS = 2 gives 2 Kbit buffer.
 static double THRESH; // threshold for 1 vs 0. Must adjust in the field by testing.
 
-int buf[BUF_SIZE];
 queue<int> sbuf;
-int idx = 0;
-int nzeros = 0;
-int start = 0;
 bool sample_lock = false;
 int offCount, onCount;
 
@@ -66,13 +60,13 @@
     char cur = 0;
     int k = 0;
     string data;
-    pc.printf("\r\nsbuf: ");
+    //pc.printf("\r\nsbuf: ");
     while(!sbuf.empty())
     {
         int c = sbuf.front();
         sbuf.pop();
         if(c == EOM) break;
-        pc.printf("%d ", c);
+        //pc.printf("%d ", c);
         if(sbuf.empty())
         {
             pc.printf("\r\nWarning: reached end of buffer\r\n");
@@ -90,43 +84,18 @@
             }
         }
     }
-    pc.printf("\r\nReceived (sbuf): \"");
+    int rcv_checksum = data[data.size()-1];
+    data.erase(data.size()-1, data.size());
+    pc.printf("Received: \"");
     pc.printf(data.c_str());
     pc.printf("\"\r\n");
+    
+    int checksum = 0;
+    for(int i = 0; i < data.size(); i++) checksum ^= data[i];
+    
+    pc.printf("Received: %d | Computed: %d\r\n", rcv_checksum, checksum);
 }
 
-// Decodes from the buffer, from start (above) to the provided stop index. Wraps around the end of the buffer.
-// Prints the decoded string.
-void printDecode(int stopIdx){
-
-    int len = (stopIdx - start + BUF_SIZE) % BUF_SIZE;
-    int size = PPM_BITS * len / 8 + 1;
-    char data[size];
-    for(int i = 0; i < size; i++) data[i] = 0;
-    //pc.printf("\r\nDecode: ");
-    int k = 0;
-    for(int i = 0; i < len; i++)
-    {
-        int c = buf[start++];
-        if(start == BUF_SIZE) start = 0;
-        for(int j = 0; j < PPM_BITS; j++)
-        {
-            // do some bit hacking to put the bit into the proper character in the output array
-            bool bit = (c >> (PPM_BITS - j - 1)) & 1;
-            if(k%8 == 0) data[k/8] = 0;
-            data[k/8] |= bit << (7 - k%8);
-            k++;
-        }
-    }
-    pc.printf("\r\nReceived: \"");
-    pc.printf(data);
-    pc.printf("\"\r\n");
-    if(start != stopIdx)
-    {
-        pc.printf("\r\nWarning: Incomplete byte\r\n");
-    }
-    start = stopIdx;
-}        
 
 // Samples once, and writes to the buffer if necessary.
 void sample()
@@ -146,12 +115,8 @@
             int offPulses = (offCount + HALF_SAMPLE) / SAMPLE_RATE - 1;
             if(offPulses < N_PPM)
             {
-                //pc.printf("%d ", offPulses);
+                pc.printf("%d ", offPulses);
                 sbuf.push(offPulses);
-                buf[idx] = offPulses;
-                idx++;
-                if(idx == BUF_SIZE) idx = 0;
-                if(idx == start) pc.printf("\r\nWarning: Buffer too small\r\n");
             }
             offCount = 0;
         }
@@ -177,7 +142,6 @@
     }
     set_constants();
     pc.printf("Ready! \r\n");
-    idx = 0;
     offCount = 0;
     offCount = 0;
     //*/
@@ -185,11 +149,10 @@
     sampler.attach_us(&sample, SAMPLE_GAP);
     
     while(true){
-        if((offCount + HALF_SAMPLE) / SAMPLE_RATE > N_PPM && start != idx){ // synchronize
+        if((offCount + HALF_SAMPLE) / SAMPLE_RATE > N_PPM && !sbuf.empty()){ // synchronize
             pc.printf("\r\n");
             sbuf.push(EOM);
             printDecodeBuf();
-            printDecode(idx);
         }
         wait_us(PULSE_TIME);
     }