Serial Level Shifter - pc 38400baud, device 9600baud.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
TimFrost
Date:
Tue Feb 07 12:19:45 2017 +0000
Parent:
0:c75bf1e12ea5
Commit message:
Beefed up so it now works properly with differing input and output speeds.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Feb 06 14:28:13 2017 +0000
+++ b/main.cpp	Tue Feb 07 12:19:45 2017 +0000
@@ -9,33 +9,33 @@
 //Serial dev(PB_6, PB_7);
 Serial dev(PA_9, PA_10);
 
+CircularBuffer<char, 2000> pcbuf;
+CircularBuffer<char, 2000> devbuf;
+
 DigitalOut myled(LED1);
 
 void pcchar()
 {
-    char ch;
-    //printf("x ");
     while (pc.readable()) {
-        ch = pc.getc();
-        dev.putc(ch);
+        pcbuf.push(pc.getc());
+        //ch = pc.getc();
+        //dev.putc(ch);
     }
 }
 
 void devchar()
 {
-    char ch;
-    //printf("x ");
     while (dev.readable()) {
-        ch = dev.getc();
-        pc.putc(ch);
+        devbuf.push(dev.getc());
+        //ch = dev.getc();
+        //pc.putc(ch);
     }
 }
 
 int main()
 {
-    //char c = 0;
-    //int iWait = 1000;
-    //int iGran = 50;
+    char ch;
+    int ipc, idev;
 
     pc.baud(38400);
     dev.baud(9600);
@@ -45,24 +45,22 @@
     
     pc.printf("SerialConv\n");
     while(1) {
-        /*for (int i = 0; i < iWait; i += iGran) {
-            if (pc.readable()) {
-                c = pc.getc();
-                //c &= ~0x20;
-                dev.putc(c);
-            }
-            
-            if (dev.readable()) {
-                c = dev.getc();
-                //c &= ~0x20;
-                pc.putc(c);
-            }
-            wait_ms(iGran);
-            //iCount += iGran;
-        }*/
+        ipc = 0;
+        idev = 0;
+        
+        while (!pcbuf.empty() && (ipc++ < 10)) {
+            pcbuf.pop(ch);
+            dev.putc(ch);
+            myled = 1;
+        }
 
-        //pc.printf("This program has run for %d ms.\n", iCount);
-        wait(0.5);
-        myled = !myled;
+        while (!devbuf.empty() && (idev++ < 10)) {
+            devbuf.pop(ch);
+            pc.putc(ch);
+            myled = 1;
+        }
+
+        //wait(0.5);
+        myled = 0;
     }
 }