SSI OpComms 3CM Board RX

Dependencies:   mbed

Fork of Optical3cmRX by Thomas Teisberg

Revision:
6:6c996fb7a742
Parent:
5:fc3ce1c5db88
Child:
7:41b22e5a1d8d
--- a/main.cpp	Sun Nov 15 00:21:59 2015 +0000
+++ b/main.cpp	Sun Nov 15 23:20:38 2015 +0000
@@ -1,6 +1,7 @@
 #include "mbed.h"
+#include <sstream>
 #include <string>
-#include <sstream>
+#include <queue>
 using namespace std;
 
 AnalogIn rx(p20);
@@ -10,15 +11,17 @@
 // e.g. PPM_BITS = 2 --> 4-PPM; PPM_BITS = 3 --> 8-PPM, etc.
 static const int PPM_BITS = 2;
 static const int N_PPM = 1 << PPM_BITS;
-static int PULSE_TIME = 1000; // microseconds
-static int SAMPLE_RATE = 10; // must be a divisor of PULSE_TIME; 
-static int SAMPLE_GAP = PULSE_TIME / SAMPLE_RATE;
-static int HALF_SAMPLE = SAMPLE_RATE / 2;
+static const int EOM = N_PPM;
+static int PULSE_TIME; // microseconds
+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 = 0.005; // threshold for 1 vs 0. Must adjust in the field by testing.
+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;
@@ -45,7 +48,6 @@
     istringstream ints(s);
     double out;
     if(!(ints >> out)) out = def;
-    pc.printf("Value set: %f\r\n", out);
     return out;
 }
 
@@ -54,10 +56,45 @@
     PULSE_TIME = (int) getNumber("Input pulse time (us; blank for 1000): ", 1000);
     SAMPLE_RATE = (int) getNumber("Input sample rate (blank for 10): ", 10);
     THRESH = getNumber("Input threshold (blank for 0.005): ", 0.005);
+    pc.printf("Parameters: PULSE_TIME=%d, SAMPLE_RATE=%d, THRESH=%f\r\n", PULSE_TIME, SAMPLE_RATE, THRESH);
     SAMPLE_GAP = PULSE_TIME / SAMPLE_RATE;
     HALF_SAMPLE = SAMPLE_RATE / 2;
 }
 
+void printDecodeBuf()
+{
+    char cur = 0;
+    int k = 0;
+    string data;
+    pc.printf("\r\nsbuf: ");
+    while(!sbuf.empty())
+    {
+        int c = sbuf.front();
+        sbuf.pop();
+        if(c == EOM) break;
+        pc.printf("%d ", c);
+        if(sbuf.empty())
+        {
+            pc.printf("\r\nWarning: reached end of buffer\r\n");
+        }
+        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;
+            cur |= bit << (7 - k%8);
+            k++;
+            if(k%8 == 0)
+            {
+                data += cur;
+                cur = 0;
+            }
+        }
+    }
+    pc.printf("\r\nReceived (sbuf): \"");
+    pc.printf(data.c_str());
+    pc.printf("\"\r\n");
+}
+
 // 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){
@@ -89,7 +126,7 @@
         pc.printf("\r\nWarning: Incomplete byte\r\n");
     }
     start = stopIdx;
-}
+}        
 
 // Samples once, and writes to the buffer if necessary.
 void sample()
@@ -107,9 +144,10 @@
         if(offCount > HALF_SAMPLE)
         {
             int offPulses = (offCount + HALF_SAMPLE) / SAMPLE_RATE - 1;
-            if(offPulses <= N_PPM)
+            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;
@@ -148,6 +186,9 @@
     
     while(true){
         if((offCount + HALF_SAMPLE) / SAMPLE_RATE > N_PPM && start != idx){ // synchronize
+            pc.printf("\r\n");
+            sbuf.push(EOM);
+            printDecodeBuf();
             printDecode(idx);
         }
         wait_us(PULSE_TIME);