A USB to UART bridge

Dependencies:   USBDevice BufferedSerial mbed

Revision:
3:2b4d2284bab0
Parent:
2:427b69ad737c
Child:
4:9c038f12d460
--- a/main.cpp	Wed Dec 25 03:04:09 2013 +0000
+++ b/main.cpp	Wed Jul 23 09:40:47 2014 +0000
@@ -4,15 +4,46 @@
  
 #include "mbed.h"
 #include "USBSerial.h"
+#include "Buffer.h"
 
 Serial uart(USBTX, USBRX);
 USBSerial pc;
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
 DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+Buffer <char> rxbuf;
+Buffer <char> txbuf;
+
+Ticker ticker;
+volatile bool rxflag = false;
+volatile bool txflag = false;
+
+
+void indicate()
+{
+    if (rxflag) {
+        led3 = !led3;
+        rxflag = false;
+    } else {
+        if (led3) {
+            led3 = 0;
+        }
+    }
+    
+    if (txflag) {
+        led4 = !led4;
+        txflag = false;
+    } else {
+        if (led4) {
+            led4 = 0;
+        }
+    }   
+}
 
 // Called by ISR
-void settingsChanged(int baud, int bits, int parity, int stop)
+void settings_changed(int baud, int bits, int parity, int stop)
 {
     const Serial::Parity parityTable[] = {Serial::None, Serial::Odd, Serial::Even, Serial::Forced0, Serial::Forced1};
     
@@ -28,19 +59,28 @@
 
 int main()
 {
-    pc.attach(settingsChanged);
+    pc.attach(settings_changed);
+    ticker.attach_us(indicate, 10000);
     
     while (1) {
         while (uart.readable()) {
-            led2 = 1;
-            pc.putc(uart.getc());
-            led2 = 0;
+            rxflag = true;
+            char r = uart.getc();
+            rxbuf.put(r);
         }
         
-        while (pc.readable()) {
-            led3 = 1;
-            uart.putc(pc.getc());
-            led3 = 0;
+        while (txbuf.available() && uart.writeable()) {
+            txflag = true;
+            uart.putc(txbuf.get());
+        }
+        
+        if (pc.readable()) {
+            char r = pc.getc();
+            txbuf.put(r);
+        }
+        
+        if (rxbuf.available() && pc.writeable()) {
+            pc.putc(rxbuf.get());
         }
     }
 }