SSI OpComms 3CM Board RX

Dependencies:   mbed

Fork of Optical3cmRX by Thomas Teisberg

Committer:
bhz
Date:
Sat Nov 07 22:21:17 2015 +0000
Revision:
2:eb447fcd8284
Parent:
1:b446f401a861
Child:
3:1235cbe67ec9
Not working.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:879aa9d0247b 1 #include "mbed.h"
tteisberg 1:b446f401a861 2
tteisberg 1:b446f401a861 3 AnalogIn rx(p20);
tteisberg 1:b446f401a861 4
mbed_official 0:879aa9d0247b 5 Serial pc(USBTX, USBRX); // tx, rx
tteisberg 1:b446f401a861 6
bhz 2:eb447fcd8284 7 static const int N_PPM = 4;
bhz 2:eb447fcd8284 8 static const int PULSE_TIME = 1000;
bhz 2:eb447fcd8284 9 static const int SAMPLE_RATE = 10; // must be an even divisor of PULSE_TIME
bhz 2:eb447fcd8284 10 static const int SAMPLE_GAP = PULSE_TIME / SAMPLE_RATE;
bhz 2:eb447fcd8284 11 static const int HALF_SAMPLE = SAMPLE_RATE / 2;
bhz 2:eb447fcd8284 12 static const int BUF_SIZE = 1024;
bhz 2:eb447fcd8284 13 static const float THRESH = 0.005;
bhz 2:eb447fcd8284 14
bhz 2:eb447fcd8284 15 int buf[BUF_SIZE];
tteisberg 1:b446f401a861 16 int idx = 0;
tteisberg 1:b446f401a861 17 int nzeros = 0;
bhz 2:eb447fcd8284 18 int start = 0;
bhz 2:eb447fcd8284 19 bool sample_lock = false;
bhz 2:eb447fcd8284 20 int offCount, onCount;
tteisberg 1:b446f401a861 21
bhz 2:eb447fcd8284 22 char* decodechar(int stopIdx){
bhz 2:eb447fcd8284 23 int len = (stopIdx - start + BUF_SIZE) % BUF_SIZE;
bhz 2:eb447fcd8284 24 bool* data = new bool[len * N_PPM];
bhz 2:eb447fcd8284 25 for(int i = 0; i < len; i++)
bhz 2:eb447fcd8284 26 {
bhz 2:eb447fcd8284 27 int c = buf[start++];
bhz 2:eb447fcd8284 28 if(start == BUF_SIZE) start = 0;
bhz 2:eb447fcd8284 29 for(int j = 0; j < N_PPM; j++)
bhz 2:eb447fcd8284 30 {
bhz 2:eb447fcd8284 31 data[i * N_PPM + j] = (c >> (N_PPM - j - 1));
bhz 2:eb447fcd8284 32 }
bhz 2:eb447fcd8284 33 }
bhz 2:eb447fcd8284 34 return (char*) data;
bhz 2:eb447fcd8284 35 }
tteisberg 1:b446f401a861 36
bhz 2:eb447fcd8284 37 void sample()
bhz 2:eb447fcd8284 38 {
bhz 2:eb447fcd8284 39 if(sample_lock) pc.printf("\r\nWarning: Sampling too fast\r\n");
bhz 2:eb447fcd8284 40 sample_lock = true;
bhz 2:eb447fcd8284 41 float v = rx.read();
bhz 2:eb447fcd8284 42 if(v < 0.001){
bhz 2:eb447fcd8284 43 offCount++;
bhz 2:eb447fcd8284 44 if(offCount > HALF_SAMPLE) onCount = 0;
bhz 2:eb447fcd8284 45 }else{ // Pulse
bhz 2:eb447fcd8284 46 if(onCount > HALF_SAMPLE && offCount > HALF_SAMPLE)
bhz 2:eb447fcd8284 47 {
bhz 2:eb447fcd8284 48 int offPulses = (offCount + HALF_SAMPLE) / SAMPLE_RATE;
bhz 2:eb447fcd8284 49 pc.printf("%d:%d ", offCount, offPulses);
bhz 2:eb447fcd8284 50 if(offPulses <= N_PPM)
bhz 2:eb447fcd8284 51 {
bhz 2:eb447fcd8284 52 buf[idx] = offPulses - 1;
bhz 2:eb447fcd8284 53 idx++;
bhz 2:eb447fcd8284 54 if(idx == BUF_SIZE) idx = 0;
bhz 2:eb447fcd8284 55 if(idx == start) pc.printf("\r\nWarning: Buffer too small\r\n");
bhz 2:eb447fcd8284 56 }
bhz 2:eb447fcd8284 57 offCount = 0;
bhz 2:eb447fcd8284 58 }
bhz 2:eb447fcd8284 59 onCount++;
tteisberg 1:b446f401a861 60 }
bhz 2:eb447fcd8284 61 sample_lock = false;
tteisberg 1:b446f401a861 62 }
tteisberg 1:b446f401a861 63
tteisberg 1:b446f401a861 64 int main()
tteisberg 1:b446f401a861 65 {
bhz 2:eb447fcd8284 66 pc.printf("3 CM Link Board - Recieve\r\n");
tteisberg 1:b446f401a861 67 bool calib = true;
tteisberg 1:b446f401a861 68 if(calib){
tteisberg 1:b446f401a861 69 while(true){
bhz 2:eb447fcd8284 70 pc.printf("%f\r\r\n", rx.read());
tteisberg 1:b446f401a861 71 wait_ms(100);
tteisberg 1:b446f401a861 72 if(pc.readable()) break;
tteisberg 1:b446f401a861 73 }
tteisberg 1:b446f401a861 74 }
tteisberg 1:b446f401a861 75 idx = 0;
bhz 2:eb447fcd8284 76 offCount = 0;
tteisberg 1:b446f401a861 77 offCount = 0;
bhz 2:eb447fcd8284 78 pc.printf("Ready.\r\n");
bhz 2:eb447fcd8284 79 float v = rx.read();
bhz 2:eb447fcd8284 80 while(v < THRESH)
bhz 2:eb447fcd8284 81 {
bhz 2:eb447fcd8284 82 v = rx.read();
bhz 2:eb447fcd8284 83 wait_us(SAMPLE_GAP);
bhz 2:eb447fcd8284 84 }
bhz 2:eb447fcd8284 85 pc.printf("Starting...\r\n");
bhz 2:eb447fcd8284 86 Ticker sampler;
bhz 2:eb447fcd8284 87 sampler.attach_us(&sample, SAMPLE_GAP);
bhz 2:eb447fcd8284 88
tteisberg 1:b446f401a861 89 while(true){
bhz 2:eb447fcd8284 90 if((offCount + HALF_SAMPLE) / SAMPLE_RATE > N_PPM){ // synchronize
bhz 2:eb447fcd8284 91 char* data = decodechar(idx);
bhz 2:eb447fcd8284 92 pc.printf(data);
bhz 2:eb447fcd8284 93 delete[] data;
tteisberg 1:b446f401a861 94 offCount = 0;
tteisberg 1:b446f401a861 95 }
mbed_official 0:879aa9d0247b 96 }
mbed_official 0:879aa9d0247b 97 }