SSI OpComms 3CM Board RX
Dependencies: mbed
Fork of Optical3cmRX by
Diff: main.cpp
- Revision:
- 4:037345932888
- Parent:
- 3:1235cbe67ec9
- Child:
- 5:fc3ce1c5db88
--- a/main.cpp Sun Nov 08 00:45:49 2015 +0000 +++ b/main.cpp Sun Nov 08 02:36:11 2015 +0000 @@ -4,8 +4,9 @@ Serial pc(USBTX, USBRX); // tx, rx -static const int N_PPM = 4; -static const int PULSE_TIME = 10000; +static const int PPM_BITS = 2; +static const int N_PPM = 1 << PPM_BITS; +static const int PULSE_TIME = 1000; static const int SAMPLE_RATE = 10; // must be a divisor of PULSE_TIME static const int SAMPLE_GAP = PULSE_TIME / SAMPLE_RATE; static const int HALF_SAMPLE = SAMPLE_RATE / 2; @@ -19,19 +20,34 @@ bool sample_lock = false; int offCount, onCount; -char* decodechar(int stopIdx){ +void printDecode(int stopIdx){ + int len = (stopIdx - start + BUF_SIZE) % BUF_SIZE; - bool* data = new bool[len * N_PPM]; + 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 < N_PPM; j++) + for(int j = 0; j < PPM_BITS; j++) { - data[i * N_PPM + j] = (c >> (N_PPM - j - 1)); + 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("%d", bit); } } - return (char*) data; + pc.printf("Received: \""); + pc.printf(data); + pc.printf("\"\r\n"); + if(start != stopIdx) + { + pc.printf("\r\nWarning: Incomplete byte\r\n"); + } } void sample() @@ -43,16 +59,15 @@ offCount++; if(onCount > HALF_SAMPLE) { - pc.printf("0:%d ", onCount); onCount = 0; } }else{ // Pulse if(offCount > HALF_SAMPLE) { int offPulses = (offCount + HALF_SAMPLE) / SAMPLE_RATE - 1; - pc.printf("1:%d:%d ", offCount, offPulses); if(offPulses <= N_PPM) { + //pc.printf("%d ", offPulses); buf[idx] = offPulses; idx++; if(idx == BUF_SIZE) idx = 0; @@ -68,6 +83,7 @@ int main() { pc.printf("3 CM Link Board - Recieve\r\n"); + /* bool calib = true; if(calib){ while(true){ @@ -79,23 +95,14 @@ idx = 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){ - if((offCount + HALF_SAMPLE) / SAMPLE_RATE > N_PPM){ // synchronize - char* data = decodechar(idx); - pc.printf(data); - delete[] data; - offCount = 0; + if((offCount + HALF_SAMPLE) / SAMPLE_RATE > N_PPM && start != idx){ // synchronize + printDecode(idx); } + wait_us(PULSE_TIME); } } \ No newline at end of file