SSI OpComms 3CM Board RX
Dependencies: mbed
Fork of Optical3cmRX by
Diff: main.cpp
- Revision:
- 1:b446f401a861
- Parent:
- 0:879aa9d0247b
- Child:
- 2:eb447fcd8284
diff -r 879aa9d0247b -r b446f401a861 main.cpp --- a/main.cpp Tue Feb 12 17:39:05 2013 +0000 +++ b/main.cpp Fri Oct 30 05:30:21 2015 +0000 @@ -1,10 +1,62 @@ #include "mbed.h" - + +AnalogIn rx(p20); + Serial pc(USBTX, USBRX); // tx, rx - -int main() { - pc.printf("Hello World!\n"); - while(1) { - pc.putc(pc.getc() + 1); + +int buf[20]; +int idx = 0; +int nzeros = 0; + +int onCount, offCount; + +char decodechar(int start){ + int val = 0; + for(int i=7;i>=0;i--){ + val += ((int)buf[start+i]) << (7-i); + } + return (char) val; +} + +int main() +{ + pc.printf("3 CM Link Board - Recieve\n"); + bool calib = true; + if(calib){ + while(true){ + pc.printf("%f\r\n", rx.read()); + wait_ms(100); + if(pc.readable()) break; + } + } + idx = 0; + onCount = 0; + offCount = 0; + while(true){ + float v = rx.read(); + if(v > 0.001){ + //pc.printf("1 "); + onCount++; + 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