Mavlink bridge for Mbed devices

Dependents:   AIT_UWB_Range

Revision:
0:28183cc7963f
Child:
6:48b46bcdd5cb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uart_interface.h	Tue Nov 24 16:41:11 2015 +0000
@@ -0,0 +1,52 @@
+#pragma once
+
+#ifdef __MBED__
+#include "mbed.h"
+#endif
+
+namespace ait {
+
+class UART_Interface {
+public:
+    virtual bool writeChar(uint8_t c) = 0;
+    virtual bool isCharAvailable() = 0;
+    virtual uint8_t readChar(bool* err_flag = NULL) = 0;
+};
+
+#ifdef __MBED__
+class UART_Mbed : public UART_Interface {
+    Serial _serial;
+
+public:
+    UART_Mbed(PinName tx, PinName rx, int baudrate = 115200)
+            : _serial(tx, rx) {
+        _serial.baud(baudrate);
+    }
+
+    virtual bool writeChar(uint8_t c) {
+        int ret = _serial.putc(c);
+        if (ret == -1) {
+            return false;
+            //throw std::exception("Unable to write on serial port");
+        }
+        return true;
+    }
+
+    virtual bool isCharAvailable() {
+        return _serial.readable();
+    }
+
+    virtual uint8_t readChar(bool* err_flag = NULL) {
+        int c = _serial.getc();
+        if (err_flag != NULL) {
+            if (c == -1)
+                *err_flag = true;
+            else
+                *err_flag = false;
+        }
+        return static_cast<uint8_t>(c);
+    }
+};
+#endif // __MBED__
+
+}