SSI OpComms 3CM Board RX

Dependencies:   mbed

Fork of Optical3cmRX by Thomas Teisberg

Revision:
2:eb447fcd8284
Parent:
1:b446f401a861
Child:
3:1235cbe67ec9
--- a/main.cpp	Fri Oct 30 05:30:21 2015 +0000
+++ b/main.cpp	Sat Nov 07 22:21:17 2015 +0000
@@ -4,59 +4,94 @@
 
 Serial pc(USBTX, USBRX); // tx, rx
 
-int buf[20];
+static const int N_PPM = 4;
+static const int PULSE_TIME = 1000;
+static const int SAMPLE_RATE = 10; // must be an even divisor of PULSE_TIME
+static const int SAMPLE_GAP = PULSE_TIME / SAMPLE_RATE;
+static const int HALF_SAMPLE = SAMPLE_RATE / 2;
+static const int BUF_SIZE = 1024;
+static const float THRESH = 0.005;
+
+int buf[BUF_SIZE];
 int idx = 0;
 int nzeros = 0;
+int start = 0;
+bool sample_lock = false;
+int offCount, onCount;
 
-int onCount, offCount;
+char* decodechar(int stopIdx){
+    int len = (stopIdx - start + BUF_SIZE) % BUF_SIZE;
+    bool* data = new bool[len * N_PPM];
+    for(int i = 0; i < len; i++)
+    {
+        int c = buf[start++];
+        if(start == BUF_SIZE) start = 0;
+        for(int j = 0; j < N_PPM; j++)
+        {
+            data[i * N_PPM + j] = (c >> (N_PPM - j - 1));
+        }
+    }
+    return (char*) data;
+}
 
-char decodechar(int start){
-    int val = 0;
-    for(int i=7;i>=0;i--){
-        val += ((int)buf[start+i]) << (7-i);
+void sample()
+{
+    if(sample_lock) pc.printf("\r\nWarning: Sampling too fast\r\n");
+    sample_lock = true;
+    float v = rx.read();
+    if(v < 0.001){
+        offCount++;
+        if(offCount > HALF_SAMPLE) onCount = 0;
+    }else{ // Pulse
+        if(onCount > HALF_SAMPLE && offCount > HALF_SAMPLE)
+        {
+            int offPulses = (offCount + HALF_SAMPLE) / SAMPLE_RATE;
+            pc.printf("%d:%d ", offCount, offPulses);
+            if(offPulses <= N_PPM)
+            {
+                buf[idx] = offPulses - 1;
+                idx++;
+                if(idx == BUF_SIZE) idx = 0;
+                if(idx == start) pc.printf("\r\nWarning: Buffer too small\r\n");
+            }
+            offCount = 0;
+        }
+        onCount++;
     }
-    return (char) val;
+    sample_lock = false;
 }
 
 int main()
 {
-    pc.printf("3 CM Link Board - Recieve\n");
+    pc.printf("3 CM Link Board - Recieve\r\n");
     bool calib = true;
     if(calib){
         while(true){
-            pc.printf("%f\r\n", rx.read());
+            pc.printf("%f\r\r\n", rx.read());
             wait_ms(100);
             if(pc.readable()) break;
         }
     }
     idx = 0;
-    onCount = 0;
+    offCount = 0;
     offCount = 0;
+    pc.printf("Ready.\r\n");
+    float v = rx.read();
+    while(v < THRESH)
+    {
+        v = rx.read();
+        wait_us(SAMPLE_GAP);
+    }
+    pc.printf("Starting...\r\n");
+    Ticker sampler;
+    sampler.attach_us(&sample, SAMPLE_GAP);
+    
     while(true){
-        float v = rx.read();
-        if(v > 0.001){
-            //pc.printf("1 ");
-            onCount++;
+        if((offCount + HALF_SAMPLE) / SAMPLE_RATE > N_PPM){ // synchronize
+            char* data = decodechar(idx);
+            pc.printf(data);
+            delete[] data;
             offCount = 0;
-        }else{
-            if(onCount >= 5 && onCount <= 15){
-                buf[idx] = false;
-                idx++;
-            }else if(onCount >= 15 && onCount <= 25){
-                buf[idx] = true;
-                idx++;
-            }
-            onCount = 0;
-            offCount++;
-            if(offCount >= 25 || idx >=8){
-                int c = decodechar(0);
-                pc.printf("%c",c);
-                for(int i=0;i<8;i++) buf[i] = 0;
-                idx = 0;
-                offCount = 0;
-            }
         }
-        
-        wait_ms(1);
     }
 }
\ No newline at end of file