SSI OpComms 3CM Board RX
Dependencies: mbed
Fork of Optical3cmRX by
main.cpp
- Committer:
- bhz
- Date:
- 2015-11-08
- Revision:
- 3:1235cbe67ec9
- Parent:
- 2:eb447fcd8284
- Child:
- 4:037345932888
File content as of revision 3:1235cbe67ec9:
#include "mbed.h" AnalogIn rx(p20); Serial pc(USBTX, USBRX); // tx, rx static const int N_PPM = 4; static const int PULSE_TIME = 10000; 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; 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; 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; } void sample() { if(sample_lock) pc.printf("\r\nWarning: Sampling too fast\r\n"); sample_lock = true; float v = rx.read(); if(v < THRESH){ 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) { buf[idx] = offPulses; idx++; if(idx == BUF_SIZE) idx = 0; if(idx == start) pc.printf("\r\nWarning: Buffer too small\r\n"); } offCount = 0; } onCount++; } sample_lock = false; } int main() { pc.printf("3 CM Link Board - Recieve\r\n"); bool calib = true; if(calib){ while(true){ pc.printf("%f\r\r\n", rx.read()); wait_ms(100); if(pc.readable()) break; } } 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; } } }